-
Notifications
You must be signed in to change notification settings - Fork 15
/
TestJSON.cls
112 lines (96 loc) · 4.86 KB
/
TestJSON.cls
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
Class JSON.TestJSON Extends %UnitTest.TestCase
{
Method TestJSON()
{
#Dim tException As %Exception.AbstractException
Try {
Set tEscapeChars=##class(RSA.Data.DT.Simple.JSON).GetEscapeChars()
Set tEscapeChars=tEscapeChars_$LB($LB("[","["),$LB("]","]"),$LB("{","{"),$LB("}","}"),$LB(":",":"))
for tI=1:1:$LL(tEscapeChars){
For tJ=1:1:$LL(tEscapeChars){
for tK=1:1:$LL(tEscapeChars){
Set tChar1=$LG(tEscapeChars,tI)
Set tChar2=$LG(tEscapeChars,tJ)
Set tChar3=$LG(tEscapeChars,tK)
Set tJSON="{""name"":"""_$LG(tChar1,2)_$LG(tChar2,2)_$LG(tChar3,2)_" is char""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,$LG(tChar1,1)_$LG(tChar2,1)_$LG(tChar3,1)_" is char","Escaping for "_$LG(tChar1,2)_$LG(tChar2,2)_$LG(tChar3,2))
Set tJSON="{""name"":""the "_$LG(tChar1,2)_$LG(tChar2,2)_$LG(tChar3,2)_" is char""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,"the "_$LG(tChar1,1)_$LG(tChar2,1)_$LG(tChar3,1)_" is char","Escaping middle for "_$LG(tChar1,2)_$LG(tChar2,2)_$LG(tChar3,2))
Set tJSON="{""name"":""the "_$LG(tChar1,2)_$LG(tChar2,2)_$LG(tChar3,2)_"""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,"the "_$LG(tChar1,1)_$LG(tChar2,1)_$LG(tChar3,1),"Escaping end for "_$LG(tChar1,2)_$LG(tChar2,2)_$LG(tChar3,2))
if (tK=1){
if (tI=1){
Set tJSON="{""name"":"""_$LG(tChar1,2)_" is char""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,$LG(tChar1,1)_" is char","Escaping for "_$LG(tChar1,2))
Set tJSON="{""name"":""the "_$LG(tChar1,2)_" is char""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,"the "_$LG(tChar1,1)_" is char","Escaping middle for "_$LG(tChar1,2))
Set tJSON="{""name"":""the "_$LG(tChar1,2)_"""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,"the "_$LG(tChar1,1),"Escaping end for "_$LG(tChar1,2))
for tC=97:1:122{
Set tJSON="{""name"":"""_$LG(tChar1,2)_$C(tC)_"""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,$LG(tChar1,1)_$C(tC),"Escaping for "_$LG(tChar1,2)_$C(tC))
}
}
Set tJSON="{""name"":"""_$LG(tChar1,2)_$LG(tChar2,2)_" is char""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,$LG(tChar1,1)_$LG(tChar2,1)_" is char","Escaping for "_$LG(tChar1,2)_$LG(tChar2,2))
Set tJSON="{""name"":""the "_$LG(tChar1,2)_$LG(tChar2,2)_" is char""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,"the "_$LG(tChar1,1)_$LG(tChar2,1)_" is char","Escaping middle for "_$LG(tChar1,2)_$LG(tChar2,2))
Set tJSON="{""name"":""the "_$LG(tChar1,2)_$LG(tChar2,2)_"""}"
Set tObj=##class(RSA.Data.DT.Simple.JSON).Parse(tJSON)
Set tName=tObj.GetAt("name")
Do $$$AssertEquals(tName,"the "_$LG(tChar1,1)_$LG(tChar2,1),"Escaping end for "_$LG(tChar1,2)_$LG(tChar2,2))
}
}
}
}
} catch tException {
Do $$$AssertEquals(1,0,"Exception thrown - " _ tException.Code_ ": " _ tException.Name _ " " _ tException.Data _ " " _ tException.Location)
}
}
Method TestLeadingZerosValuesAreEscaped()
{
Set input = "000123"
set output = ##class(App.API.Helper.CacheJSON).Encode(input)
set expected = """"_input_""""
Do $$$AssertEquals(output, expected, "Checking that a number with leading zeros is escaped")
}
Method TestZeroValueIsNotEscaped()
{
Set input = "0"
set output = ##class(App.API.Helper.CacheJSON).Encode(input)
set expected = input
Do $$$AssertEquals(output, expected, "Checking that a zero value is not escaped")
}
Method TestZeroFollowedByDecimalIsNotQuoted()
{
Set input = "0.1"
set output = ##class(App.API.Helper.CacheJSON).Encode(input)
set expected = """"_input_""""
Do $$$AssertEquals(output, expected, "Checking that a zero value followed by a decimal is not escaped")
}
Method TestMultipleZerosFollowedByDecimalIsQuoted()
{
Set input = "00.1"
set output = ##class(App.API.Helper.CacheJSON).Encode(input)
set expected = """"_input_""""
Do $$$AssertEquals(output, expected, "Checking that a zero value followed by a decimal is not escaped")
}
}