-
Notifications
You must be signed in to change notification settings - Fork 146
/
JSON.Writer.Tests.fs
84 lines (59 loc) · 2.39 KB
/
JSON.Writer.Tests.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
module FSharpx.TypeProviders.Tests.JSON.WriterTests
open NUnit.Framework
open FSharpx
open FsUnit
type InlinedJSON = StructuredJSON<Schema="""{ "firstName": "Max" "lastName": "Mustermann" "age": 26 "isCool": true, "size":42.42 }""">
[<Test>]
let ``Can set properties in inlined properties``() =
let inlined = new InlinedJSON()
let person = inlined.Root
person.FirstName <- "John"
person.FirstName |> should equal "John"
person.LastName <- "Doe"
person.LastName |> should equal "Doe"
person.Age <- 30
person.Age |> should equal 30
person.IsCool <- false
person.IsCool |> should equal false
person.Size <- 43.43
person.Size |> should equal 43.43
type AuthorsJSON = StructuredJSON<Schema="""{ "authors": [{ "name": "Steffen" }, { "name": "Tomas", "age": 29, "isCool": true, "size":42.42 }]}""">
[<Test>]
let ``Can set optional properties in inlined JSON``() =
let inlined = new AuthorsJSON()
let author = inlined.Root.GetAuthors() |> Seq.head
author.Age <- None
author.Age |> should equal None
author.Age <- Some 42
author.Age |> should equal (Some 42)
author.IsCool <- None
author.IsCool |> should equal None
author.IsCool <- Some true
author.IsCool |> should equal (Some true)
author.Size <- None
author.Size |> should equal None
author.Size <- Some 42.45
author.Size |> should equal (Some 42.45)
[<Test>]
let ``Can add author in inlined JSON``() =
let inlined = new AuthorsJSON()
let author = inlined.Root.NewAuthor()
author.Name <- "John"
inlined.Root.AddAuthor author
let authors = inlined.Root.GetAuthors() |> Seq.toList
authors.Length |> should equal 3
authors.[0].Name |> should equal "Steffen"
authors.[1].Name |> should equal "Tomas"
authors.[2].Name |> should equal "John"
[<Test>]
let ``Can serialize the json``() =
let inlined = new AuthorsJSON()
let json = inlined.ToString()
json |> should equal """{"authors":[{"name":"Steffen"},{"size":42.42,"isCool":true,"age":29,"name":"Tomas"}]}"""
open System.Xml.Linq
[<Test>]
let ``Can convert the json to xml``() =
let inlined = new AuthorsJSON()
let xml = inlined.ToXml() |> Seq.head
let expectedXml = XDocument.Parse("<authors><item name=\"Steffen\" /><item size=\"42.42\" isCool=\"true\" age=\"29\" name=\"Tomas\" /></authors>")
xml.ToString() |> should equal (expectedXml.ToString())