-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSharpValueOptionSerializationTests.fs
84 lines (68 loc) · 3.14 KB
/
FSharpValueOptionSerializationTests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
namespace FSharp.MongoDB.Bson.Tests.Serialization
open MongoDB.Bson
open FsUnit
open NUnit.Framework
module FSharpValueOptionSerialization =
type Primitive =
{ Bool : bool voption
Int : int voption
String : string voption
Float : float voption }
[<Test>]
let ``test serialize value optional primitives (valuenone) in a record type``() =
let value = { Bool = ValueNone
Int = ValueNone
String = ValueNone
Float = ValueNone }
let result = serialize value
let expected = BsonDocument([ BsonElement("Bool", BsonNull.Value)
BsonElement("Int", BsonNull.Value)
BsonElement("String", BsonNull.Value)
BsonElement("Float", BsonNull.Value) ])
result |> should equal expected
[<Test>]
let ``test deserialize value optional primitives (valuenone) in a record type)``() =
let doc = BsonDocument()
let result = deserialize<Primitive> doc
let expected = { Bool = ValueNone
Int = ValueNone
String = ValueNone
Float = ValueNone }
result |> should equal expected
[<Test>]
let ``test serialize value optional primitives (valuesome) in a record type``() =
let value = { Bool = ValueSome false
Int = ValueSome 0
String = ValueSome "0.0"
Float = ValueSome 0.0 }
let result = serialize value
let expected = BsonDocument([ BsonElement("Bool", BsonBoolean false)
BsonElement("Int", BsonInt32 0)
BsonElement("String", BsonString "0.0")
BsonElement("Float", BsonDouble 0.0) ])
result |> should equal expected
[<Test>]
let ``test deserialize value optional primitives (value some) in a record type``() =
let doc = BsonDocument([ BsonElement("Bool", BsonBoolean true)
BsonElement("Int", BsonInt32 1)
BsonElement("String", BsonString "1.0")
BsonElement("Float", BsonDouble 1.0) ])
let result = deserialize<Primitive> doc
let expected = { Bool = ValueSome true
Int = ValueSome 1
String = ValueSome "1.0"
Float = ValueSome 1.0 }
result |> should equal expected