1+ module FSharp.Data.Tests.JsonDocument
2+
3+ open NUnit.Framework
4+ open FsUnit
5+ open FSharp.Data
6+ open FSharp.Data .Runtime .BaseTypes
7+ open System.IO
8+ open System.Reflection
9+
10+ // Use reflection to access the "generated code only" methods for testing
11+ let private getCreateMethod () =
12+ typeof< JsonDocument>. GetMethod( " Create" , [| typeof< JsonValue>; typeof< string> |])
13+
14+ let private getCreateFromReaderMethod () =
15+ typeof< JsonDocument>. GetMethod( " Create" , [| typeof< System.IO.TextReader> |])
16+
17+ let private getCreateListMethod () =
18+ typeof< JsonDocument>. GetMethod( " CreateList" , [| typeof< System.IO.TextReader> |])
19+
20+ [<Test>]
21+ let ``JsonDocument.Create with JsonValue should return IJsonDocument using reflection`` () =
22+ let createMethod = getCreateMethod()
23+ let jsonValue = JsonValue.Number 42 M
24+ let doc = createMethod.Invoke( null , [| jsonValue; " /path" |]) :?> IJsonDocument
25+
26+ doc |> should not' ( be null )
27+ doc.JsonValue |> should equal jsonValue
28+
29+ [<Test>]
30+ let ``JsonDocument.Create with TextReader should parse JSON using reflection`` () =
31+ let createMethod = getCreateFromReaderMethod()
32+ let json = """ {"name": "test", "value": 123}"""
33+ use reader = new StringReader( json)
34+ let doc = createMethod.Invoke( null , [| reader |]) :?> IJsonDocument
35+
36+ doc |> should not' ( be null )
37+ doc.JsonValue |> should not' ( be null )
38+
39+ [<Test>]
40+ let ``JsonDocument.CreateList with single array should return array elements using reflection`` () =
41+ let createListMethod = getCreateListMethod()
42+ let json = """ [{"id": 1}, {"id": 2}]"""
43+ use reader = new StringReader( json)
44+ let docs = createListMethod.Invoke( null , [| reader |]) :?> IJsonDocument[]
45+
46+ docs |> should haveLength 2
47+ docs.[ 0 ]. JsonValue.[ " id" ]. AsInteger() |> should equal 1
48+ docs.[ 1 ]. JsonValue.[ " id" ]. AsInteger() |> should equal 2
49+
50+ [<Test>]
51+ let ``JsonDocument.CreateList with multiple JSON objects should return separate documents using reflection`` () =
52+ let createListMethod = getCreateListMethod()
53+ let json = """ {"id": 1}{"id": 2}"""
54+ use reader = new StringReader( json)
55+ let docs = createListMethod.Invoke( null , [| reader |]) :?> IJsonDocument[]
56+
57+ docs |> should haveLength 2
58+ docs.[ 0 ]. JsonValue.[ " id" ]. AsInteger() |> should equal 1
59+ docs.[ 1 ]. JsonValue.[ " id" ]. AsInteger() |> should equal 2
60+
61+ [<Test>]
62+ let ``JsonDocument ToString should return JsonValue string representation using reflection`` () =
63+ let createMethod = getCreateMethod()
64+ let jsonValue = JsonValue.Number 42 M
65+ let docObj = createMethod.Invoke( null , [| jsonValue; " /test" |])
66+
67+ docObj.ToString() |> should equal " 42"
68+
69+ [<Test>]
70+ let ``JsonDocument JsonValue property should return original JsonValue using reflection`` () =
71+ let createMethod = getCreateMethod()
72+ let jsonValue = JsonValue.String " test"
73+ let doc = createMethod.Invoke( null , [| jsonValue; " /test" |]) :?> IJsonDocument
74+
75+ doc.JsonValue |> should equal jsonValue
76+
77+ [<Test>]
78+ let ``IJsonDocument Path method should return path using reflection`` () =
79+ let createMethod = getCreateMethod()
80+ let jsonValue = JsonValue.Boolean true
81+ let doc = createMethod.Invoke( null , [| jsonValue; " /root/item" |]) :?> IJsonDocument
82+
83+ // Use reflection to call the Path method to avoid the compiler message
84+ let pathMethod = typeof< IJsonDocument>. GetMethod( " Path" )
85+ let path = pathMethod.Invoke( doc, [||]) :?> string
86+
87+ path |> should equal " /root/item"
88+
89+ [<Test>]
90+ let ``IJsonDocument CreateNew should create new document with incremented path using reflection`` () =
91+ let createMethod = getCreateMethod()
92+ let jsonValue = JsonValue.Array [| JsonValue.Number 1 M; JsonValue.Number 2 M |]
93+ let originalDoc = createMethod.Invoke( null , [| jsonValue; " /root" |]) :?> IJsonDocument
94+ let newValue = JsonValue.Number 42 M
95+
96+ // Use reflection to call CreateNew to avoid the compiler message
97+ let createNewMethod = typeof< IJsonDocument>. GetMethod( " CreateNew" )
98+ let newDoc = createNewMethod.Invoke( originalDoc, [| newValue; " /item[0]" |]) :?> IJsonDocument
99+
100+ newDoc.JsonValue |> should equal newValue
101+ let pathMethod = typeof< IJsonDocument>. GetMethod( " Path" )
102+ let path = pathMethod.Invoke( newDoc, [||]) :?> string
103+ path |> should equal " /root/item[0]"
104+
105+ [<Test>]
106+ let ``JsonDocument.Create with empty JSON should work using reflection`` () =
107+ let createMethod = getCreateFromReaderMethod()
108+ let json = " {}"
109+ use reader = new StringReader( json)
110+ let doc = createMethod.Invoke( null , [| reader |]) :?> IJsonDocument
111+
112+ doc |> should not' ( be null )
113+ doc.JsonValue |> should not' ( be null )
114+
115+ [<Test>]
116+ let ``JsonDocument.CreateList with empty array should return empty array using reflection`` () =
117+ let createListMethod = getCreateListMethod()
118+ let json = " []"
119+ use reader = new StringReader( json)
120+ let docs = createListMethod.Invoke( null , [| reader |]) :?> IJsonDocument[]
121+
122+ docs |> should haveLength 0
0 commit comments