-
Notifications
You must be signed in to change notification settings - Fork 31
/
Lenses.fs
133 lines (131 loc) · 5.42 KB
/
Lenses.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module Tests.Lenses
open System
open System.Collections.Generic
open System.Linq
open Fuchu
open Fleece
open FSharpPlus
open FSharpPlus.Lens
#if FSHARPDATA
open FSharp.Data
open Fleece.FSharpData
open Fleece.FSharpData.Operators
open Fleece.FSharpData.Lens
#endif
#if SYSTEMJSON
open System.Json
open Fleece.SystemJson
open Fleece.SystemJson.Operators
open Fleece.SystemJson.Lens
#endif
#if SYSTEMTEXTJSON
open Fleece.SystemTextJson.Helpers
open Fleece.SystemTextJson
open Fleece.SystemTextJson.Operators
open System.Text.Json
open Fleece.SystemTextJson.Lens
#endif
#if NEWTONSOFT
open Fleece.Newtonsoft
open Fleece.Newtonsoft.Operators
open Fleece.Newtonsoft.Lens
open Newtonsoft.Json
open Newtonsoft.Json.Linq
#endif
let tests = [
testList "key" [
test "example 1: read first key" {
let actual = JsonValue.Parse( "{\"a\": true, \"b\": 200}" ) ^? (_jkey "a" << _JBool)
let expected = true
Assert.Equal("item", Some expected, actual)
}
test "example 2: read second key" {
let actual = JsonValue.Parse( "{\"a\": true, \"b\": 200}" ) ^? (_jkey "b" << _JNumber)
let expected = 200m
Assert.Equal("item", Some expected, actual)
}
test "example 3: read with missing key" {
let actual = JsonValue.Parse( "{\"a\": true, \"b\": 200}" ) ^? (_jkey "c" << _JNumber)
Assert.Equal("item", None, actual)
}
test "example 4.1: write with missing key" {
let actual = JsonValue.Parse( "{\"a\": true, \"b\": 200}" )|> (_jkey "c" ) .-> JString "a"
let expected = JsonValue.Parse ("{\"a\": true, \"b\": 200, \"c\":\"a\"}")
Assert.Equal("item", string expected, string actual)
}
test "example 4.2: write with missing key" { //TODO: Fix
let actual = JsonValue.Parse( "{\"a\": true, \"b\": 200}" )|> (_jkey "c" << _JString) .-> "a"
let expected = JsonValue.Parse ("{\"a\": true, \"b\": 200, \"c\":\"a\"}")
//Assert.Equal("item", string expected, string actual)
printfn "todo: %A ~ %A" expected actual
}
test "example 5: write existing key" {
let actual = JsonValue.Parse( "{\"a\": true, \"b\": 200}" )|> (_jkey "a" << _JBool) .-> false
let expected = JsonValue.Parse ("{\"a\": false, \"b\": 200}")
Assert.Equal("item", string expected, string actual)
}
test "example 6: read key from a different type" {
let actual = JsonValue.Parse( "[1,2,3]" ) ^? _jkey "a"
Assert.Equal("item", true, actual.IsNone)
}
]
testList "_String" [
test "example 1" {
let actual = JsonValue.Parse ("{\"a\": \"xyz\", \"b\": true}") ^? (_jkey "a" << _JString)
let expected = "xyz"
Assert.Equal("item", Some expected, actual)
}
test "example 2" {
let actual = JsonValue.Parse ("{\"a\": \"xyz\", \"b\": true}") ^? (_jkey "b" << _JString)
Assert.Equal("item", None, actual)
}
test "example 3" {
let actual = JString "a" |> _JString .-> "b"
let expected = JString "b"
Assert.Equal("item", string expected, string actual)
}
]
testList "_Number" [
test "example 1" {
let actual = JsonValue.Parse ("{\"a\": 100, \"b\": true}") ^? (_jkey "a" << _JNumber)
let expected = 100m
Assert.Equal("item", Some expected, actual)
}
test "example 2: write" {
let actual = JsonValue.Parse ("{\"a\": 100, \"b\": true}") |> (_jkey "a" << _JNumber) .-> 200m
let expected =
#if NEWTONSOFT
"{\"a\": 200.0, \"b\": true}"
#else
"{\"a\": 200, \"b\": true}"
#endif
Assert.Equal("item", (string (JsonValue.Parse expected)), string actual)
}
]
testList "array" [
test "example 1" {
let actual = JsonValue.Parse ("[\"a\"]") ^? (_jnth 0 << _JString)
let expected = "a"
Assert.Equal("item", Some expected, actual)
}
test "example 2" {
let actual = JsonValue.Parse ("[123]") ^? (_jnth 0 << _JNumber)
let expected = 123m
Assert.Equal("item", Some expected, actual)
}
test "example 3: read for missing index" {
let actual = JsonValue.Parse ("[1,2,3]") ^? (_jnth 4 << _JNumber)
Assert.Equal("item", None, actual)
}
test "example 4: write" {
let actual = JsonValue.Parse ("[1,2,3]") |> (_jnth 1 << _JNumber) .-> 2.5m
let expected = JsonValue.Parse ("[1,2.5,3]")
Assert.Equal("item", string expected, string actual)
}
test "example 5: write for missing index" {
let actual = JsonValue.Parse ("[1]") |> (_jnth 1 << _JString) .-> "a"
let expected = JsonValue.Parse ("[1]")
Assert.Equal("item", string expected, string actual)
}
]
]