Repository navigation
Expand file tree
/
Copy pathtest.asp
More file actions
399 lines (337 loc) · 14.9 KB
/
Copy pathtest.asp
File metadata and controls
399 lines (337 loc) · 14.9 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<%
Option Explicit
Response.LCID = 1046 ' Brazilian LCID (use your locale code here).
' Could also be the LCID property of the page declaration or Session.LCID to set it to the entire session.
Response.Buffer = true
%>
<!--#include file="jsonObject.class.asp" -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ASPJSON Tests</title>
<style type="text/css">
body { font-family: Arial, Helvetica, sans-serif; }
pre {
border: solid 1px #CCCCCC;
background-color: #EEE;
padding: 5px;
text-indent: 0;
width: 90%;
white-space: pre-wrap;
word-wrap: break-word;
}
.pass { color: #0a0; }
.fail { color: #c00; font-weight: bold; }
</style>
</head>
<body>
<h1>JSON Object and Array Tests</h1>
<%
Server.ScriptTimeout = 10
dim passCount, failCount
passCount = 0
failCount = 0
sub checkResult(cond, testName)
if cond then
passCount = passCount + 1
response.write "<span class=""pass"">PASS</span>: " & testName & "<br>"
else
failCount = failCount + 1
response.write "<span class=""fail"">FAIL</span>: " & testName & "<br>"
end if
end sub
dim jsonLiteral
jsonLiteral = "{ ""strings"" : ""valorTexto"", ""numbers"": 123.456, ""bools"": true, ""arrays"": [1, ""2"", 3.4, [5, -6, [7, 8, [[[""9"", ""10""]]]]]], ""emptyArray"": [], ""emptyObject"": {}, ""objects"": { ""prop1"": ""outroTexto"", ""prop2"": [ { ""id"": 1, ""name"": ""item1"" }, { ""id"": 2, ""name"": ""item2"", ""teste"": { ""maisum"": [1, 2, 3] } } ] }, ""multiline"": ""Texto com\r\nMais de\r\numa linha e escapado com \\."" }"
' ============================================================
' Test: parse a JSON object
' ============================================================
%><h3>Test: parse a JSON object</h3><%
dim parseObj, parseStart, serObj
set parseObj = new JSONobject
parseObj.debug = false
parseStart = timer()
parseObj.parse jsonLiteral
response.write "Parse time: " & round(timer() - parseStart, 6) & " s<br>"
serObj = parseObj.Serialize()
Call checkResult(parseObj.value("strings") = "valorTexto", "parse object: string value")
Call checkResult(parseObj.value("numbers") = 123.456, "parse object: number value")
Call checkResult(parseObj.value("bools") = true, "parse object: boolean value")
Call checkResult(InStr(serObj, """arrays"":[1,""2"",3.4") > 0, "parse object: array value")
Call checkResult(InStr(serObj, """emptyArray"":[]") > 0, "parse object: empty array")
Call checkResult(InStr(serObj, """emptyObject"":{}") > 0, "parse object: empty object")
Call checkResult(InStr(serObj, """prop1"":""outroTexto""") > 0, "parse object: nested object")
Call checkResult(InStr(serObj, "Texto com") > 0, "parse object: multiline value")
' ============================================================
' Test: parse a JSON array
' ============================================================
%><h3>Test: parse a JSON array</h3><%
dim arrayLiteral, arrayParser, parsedArray, serArr
arrayLiteral = "[" & jsonLiteral & "]"
set arrayParser = new JSONobject
set parsedArray = arrayParser.parse(arrayLiteral)
serArr = parsedArray.Serialize()
Call checkResult(TypeName(parsedArray) = "JSONarray", "parse array: returns a JSONarray")
Call checkResult(parsedArray.length = 1, "parse array: has one item")
Call checkResult(InStr(serArr, "valorTexto") > 0, "parse array: item serialized")
' ============================================================
' Test: add properties
' ============================================================
%><h3>Test: add properties</h3><%
dim addObj, simpleArr, multArr, nested, serAdd
set addObj = new JSONobject
addObj.debug = false
addObj.add "nome", "José"
addObj.add "ficticio", true
addObj.add "idade", 25
addObj.add "saldo", -52
addObj.add "bio", "Nascido em São Paulo\Brasil" & vbcrlf & "Sem filhos" & vbcrlf & vbtab & "Jogador de WoW"
addObj.add "data-atual", now
simpleArr = Array(1, "teste", 234.56, "mais teste", "234", now)
addObj.add "lista", simpleArr
redim multArr(2, 3)
multArr(0, 0) = "0,0"
multArr(0, 1) = "0,1"
multArr(0, 2) = "0,2"
multArr(0, 3) = "0,3"
multArr(1, 0) = "1,0"
multArr(1, 1) = "1,1"
multArr(1, 2) = "1,2"
multArr(1, 3) = "1,3"
multArr(2, 0) = "2,0"
multArr(2, 1) = "2,1"
multArr(2, 2) = "2,2"
multArr(2, 3) = "2,3"
addObj.add "lista2", multArr
set nested = new JSONobject
nested.add "sub1", "value of sub1"
nested.add "sub2", "value of ""sub2"""
addObj.add "nested", nested
serAdd = addObj.Serialize()
Call checkResult(addObj.value("nome") = "José", "add: string property")
Call checkResult(addObj.value("ficticio") = true, "add: boolean property")
Call checkResult(addObj.value("idade") = 25, "add: integer property")
Call checkResult(addObj.value("saldo") = -52, "add: negative integer property")
Call checkResult(InStr(serAdd, "Nascido em São Paulo") > 0, "add: multiline string property")
Call checkResult(InStr(serAdd, """lista"":[1,""teste"",234.56") > 0, "add: simple array property")
Call checkResult(InStr(serAdd, """lista2"":[[""0,0""") > 0, "add: multidimensional array property")
Call checkResult(InStr(serAdd, """sub1"":""value of sub1""") > 0, "add: nested object property")
Call checkResult(InStr(serAdd, """sub2"":") > 0, "add: escaped quotes in nested object")
' ============================================================
' Test: remove properties
' ============================================================
%><h3>Test: remove properties</h3><%
dim removeObj, serRemove
set removeObj = new JSONobject
removeObj.add "removeMe", "x"
removeObj.add "keepMe", "y"
removeObj.remove "removeMe"
removeObj.remove "doesNotExist"
serRemove = removeObj.Serialize()
Call checkResult(InStr(serRemove, "removeMe") = 0, "remove: existing property removed")
Call checkResult(removeObj.value("keepMe") = "y", "remove: other property untouched")
Call checkResult(IsNull(removeObj.value("doesNotExist")), "remove: non-existent property is silent")
' ============================================================
' Test: get values
' ============================================================
%><h3>Test: get values</h3><%
dim getObj
set getObj = new JSONobject
getObj.add "nome", "José"
getObj.add "idade", 25
Call checkResult(getObj.value("nome") = "José", "value: value() method")
Call checkResult(getObj("idade") = 25, "value: default property syntax")
Call checkResult(IsNull(getObj("aNonExistantPropertyName")), "value: non-existent returns Null")
' ============================================================
' Test: change values
' ============================================================
%><h3>Test: change values</h3><%
dim changeObj
set changeObj = new JSONobject
changeObj.add "nome", "José"
changeObj.change "nome", "Mario"
changeObj.change "created", -1
Call checkResult(changeObj.value("nome") = "Mario", "change: existing property")
Call checkResult(changeObj.value("created") = -1, "change: creates missing property")
' ============================================================
' Test: array push
' ============================================================
%><h3>Test: array push</h3><%
dim pushArr, pushedObj, firstItem
set pushArr = new JSONarray
set pushedObj = new JSONobject
pushedObj.add "newJson", "property"
pushedObj.add "version", pushedObj.version
pushArr.Push pushedObj
pushArr.Push 1
pushArr.Push "strings too"
set firstItem = pushArr(0)
Call checkResult(pushArr.length = 3, "push: length after three pushes")
Call checkResult(firstItem.value("newJson") = "property", "push: object item")
Call checkResult(pushArr(1) = 1, "push: integer item")
Call checkResult(pushArr(2) = "strings too", "push: string item")
' ============================================================
' Test: load recordsets
' ============================================================
%><h3>Test: load recordsets</h3>
<!--
METADATA
TYPE="TypeLib"
NAME="Microsoft ActiveX Data Objects 2.5 Library"
UUID="{00000205-0000-0010-8000-00AA006D2EA4}"
VERSION="2.5"
-->
<%
dim rs, firstObj, rsArr, serRs
set rs = createObject("ADODB.Recordset")
rs.CursorType = adOpenKeyset
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Fields.Append "ID", adInteger, , adFldKeyColumn
rs.Fields.Append "NomeCompleto", adVarChar, 50, adFldMayBeNull
rs.Fields.Append "Valor", adDecimal, 14, adFldMayBeNull
rs.Fields("Valor").NumericScale = 2
rs.Open
rs.AddNew
rs("ID") = 1
rs("NomeCompleto") = "Nome 1"
rs("Valor") = 10.99
rs.Update
rs.AddNew
rs("ID") = 2
rs("NomeCompleto") = "Nome 2"
rs("Valor") = 29.90
rs.Update
set firstObj = new JSONobject
rs.moveFirst
firstObj.LoadFirstRecord rs
Call checkResult(firstObj.value("ID") = 1, "LoadFirstRecord: integer field")
Call checkResult(firstObj.value("NomeCompleto") = "Nome 1", "LoadFirstRecord: string field")
Call checkResult(Abs(CDbl(firstObj.value("Valor")) - 10.99) < 0.001, "LoadFirstRecord: decimal field")
set rsArr = new JSONarray
rs.moveFirst
rsArr.LoadRecordSet rs
dim rsFirst
set rsFirst = rsArr(0)
Call checkResult(rsArr.length = 2, "JSONarray.LoadRecordSet: row count")
Call checkResult(rsFirst.value("NomeCompleto") = "Nome 1", "JSONarray.LoadRecordSet: first row")
set firstObj = new JSONobject
rs.moveFirst
firstObj.LoadRecordSet rs
serRs = firstObj.Serialize()
Call checkResult(InStr(serRs, """NomeCompleto"":""Nome 1""") > 0, "JSONobject.LoadRecordSet: first row")
Call checkResult(InStr(serRs, """NomeCompleto"":""Nome 2""") > 0, "JSONobject.LoadRecordSet: second row")
rs.Close
set rs = nothing
' ============================================================
' Test: default property name
' ============================================================
%><h3>Test: default property name</h3><%
dim defObj, defArr
set defObj = new JSONobject
Call checkResult(defObj.defaultPropertyName = "data", "defaultPropertyName: initial value")
defObj.defaultPropertyName = "CustomName"
Call checkResult(defObj.defaultPropertyName = "CustomName", "defaultPropertyName: setter")
set defArr = new JSONarray
defArr.defaultPropertyName = "CustomArrName"
Call checkResult(defArr.defaultPropertyName = "CustomArrName", "JSONarray.defaultPropertyName: setter")
' ============================================================
' Test: lowerCaseKeys
' ============================================================
%><h3>Test: lowerCaseKeys</h3><%
dim lcObj, lcArr, lcInner, lcJson
set lcObj = new JSONobject
lcObj.add "FULLNAME", "John"
lcObj.lowerCaseKeys = true
lcJson = lcObj.Serialize()
Call checkResult(InStr(lcJson, """fullname""") > 0, "lowerCaseKeys: JSONobject key lowercased")
Call checkResult(lcObj.value("FULLNAME") = "John", "lowerCaseKeys: original lookup still works")
set lcArr = new JSONarray
set lcInner = new JSONobject
lcInner.add "ID", 123
lcArr.Push lcInner
lcArr.lowerCaseKeys = true
lcJson = lcArr.Serialize()
Call checkResult(InStr(lcJson, """id""") > 0, "lowerCaseKeys: JSONarray item key lowercased")
set lcObj = new JSONobject
lcObj.add JSON_ROOT_KEY, "v"
lcObj.defaultPropertyName = "RootKey"
lcObj.lowerCaseKeys = true
lcJson = lcObj.Serialize()
Call checkResult(InStr(lcJson, """rootkey""") > 0, "lowerCaseKeys: wrapper key lowercased")
' ============================================================
' Test: JSONscript serialization
' ============================================================
%><h3>Test: JSONscript serialization</h3><%
dim jsCode, jsKey, jsObj, jsScr, expectedJs, actualJs
jsCode = "function() { alert('test'); }"
jsKey = "script"
expectedJs = "{""" & jsKey & """:" & jsCode & "}"
set jsScr = new JSONscript
jsScr.value = jsCode
set jsObj = new JSONobject
jsObj.Add jsKey, jsScr
actualJs = jsObj.Serialize()
Call checkResult(actualJs = expectedJs, "JSONscript: serialized without quotes")
' ============================================================
' Test: Issue regressions
' ============================================================
%><h3>Test: Issue regressions</h3><%
' Issue #103: 2D VBScript arrays serialize without commas between rows
dim arr2d(1, 1), issue103Obj, issue103Json
arr2d(0, 0) = 1
arr2d(0, 1) = 2
arr2d(1, 0) = 3
arr2d(1, 1) = 4
set issue103Obj = new JSONobject
issue103Obj.add "matrix", arr2d
issue103Json = issue103Obj.Serialize()
Call checkResult(InStr(issue103Json, "[[1,2],[3,4]]") > 0, "Issue #103: 2D array serialization has inter-row commas")
' Issue #104: Parse() function call syntax - verify correct usage works
dim issue104Obj, issue104Result
set issue104Obj = new JSONobject
set issue104Result = issue104Obj.parse("{""a"":1}")
Call checkResult(issue104Result.value("a") = 1, "Issue #104: Parse() with parentheses works")
' Issue #94: Can't access array after parse (root-level array)
dim issue94Parser, issue94Arr
set issue94Parser = new JSONobject
set issue94Arr = issue94Parser.parse("[{""id"":1},{""id"":2}]")
Call checkResult(TypeName(issue94Arr) = "JSONarray", "Issue #94: Root array parses as JSONarray")
Call checkResult(issue94Arr.length = 2, "Issue #94: Root array has correct length")
Call checkResult(issue94Arr(0).value("id") = 1, "Issue #94: Root array item access")
' Issue #68: Array not closed correctly in parse (nested arrays)
dim issue68Parser, issue68Obj, issue68Json
set issue68Parser = new JSONobject
set issue68Obj = issue68Parser.parse("{""a"":[[1,2],[3,4]]}")
issue68Json = issue68Obj.Serialize()
Call checkResult(InStr(issue68Json, """a"":[[1,2],[3,4]]") > 0, "Issue #68: Nested arrays parse correctly")
' Issue #66: Date serialization produces valid JSON
dim issue66Obj, issue66Json
set issue66Obj = new JSONobject
issue66Obj.add "dt", #1/2/2020 3:04:05 AM#
issue66Json = issue66Obj.Serialize()
Call checkResult(InStr(issue66Json, "2020-01-02T03:04:05") > 0, "Issue #66: Date serializes to ISO format")
Call checkResult(Right(issue66Json, 2) = """" & "}", "Issue #66: Date JSON is well-formed (ends with quoted string)")
' Issue #45: LCID handling with arrays
dim issue45Obj, issue45Json, oldLCID
oldLCID = Response.LCID
Response.LCID = 1033 ' en-US
set issue45Obj = new JSONobject
issue45Obj.add "num", 1234.56
issue45Json = issue45Obj.Serialize()
Response.LCID = oldLCID
Call checkResult(InStr(issue45Json, "1234.56") > 0, "Issue #45: Numbers use dot decimal separator with en-US LCID")
' ============================================================
' Summary
' ============================================================
%><h3>Summary</h3><%
response.write "Passed: " & passCount & "<br>"
response.write "Failed: " & failCount & "<br>"
if failCount = 0 then
response.write "<b>All " & passCount & " assertions passed.</b><br>"
else
response.write "<b>" & failCount & " assertion(s) failed.</b><br>"
end if
%>
</body>
</html>