Skip to content

Commit 7da2864

Browse files
committed
[ADDED] Test for sample JSON files
1 parent 987ad9f commit 7da2864

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package io.github.json5.kotlin
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import java.nio.file.Files
6+
import java.nio.file.Paths
7+
8+
class JSON5ParserTextExampleFiles {
9+
@Test
10+
fun testParseSimpleObjectJson5() {
11+
val path = Paths.get("src/test/resources/simple-object.json5")
12+
val json5Text = Files.readString(path)
13+
val result = JSON5Parser.parse(json5Text)
14+
15+
val expected = mapOf(
16+
"name" to "John Doe",
17+
"age" to 30.0,
18+
"isEmployed" to true,
19+
"salary" to 75000.50,
20+
"address" to mapOf(
21+
"street" to "123 Main St",
22+
"city" to "New York",
23+
"zipCode" to 10001.0
24+
)
25+
)
26+
27+
assertEquals(expected, result)
28+
}
29+
30+
@Test
31+
fun testParseArrayExampleJson5() {
32+
val path = Paths.get("src/test/resources/array-example.json5")
33+
val json5Text = Files.readString(path)
34+
val result = JSON5Parser.parse(json5Text)
35+
36+
val expected = listOf(
37+
42.0,
38+
"Hello World",
39+
true,
40+
null,
41+
mapOf(
42+
"name" to "Nested object",
43+
"active" to true
44+
),
45+
listOf(1.0, 2.0, 3.0),
46+
Double.POSITIVE_INFINITY,
47+
Double.NEGATIVE_INFINITY,
48+
Double.NaN
49+
)
50+
51+
assertEquals(expected.size, (result as List<*>).size)
52+
for (i in expected.indices) {
53+
val exp = expected[i]
54+
val act = result[i]
55+
if (exp is Double && exp.isNaN()) {
56+
assert((act as Double).isNaN())
57+
} else {
58+
assertEquals(exp, act)
59+
}
60+
}
61+
}
62+
63+
@Test
64+
fun testParseEmptyJson5() {
65+
val path = Paths.get("src/test/resources/empty-json.json5")
66+
val json5Text = Files.readString(path)
67+
val result = JSON5Parser.parse(json5Text)
68+
val expected = emptyMap<String, Any?>()
69+
assertEquals(expected, result)
70+
}
71+
72+
@Test
73+
fun testParseNumericFormatsJson5() {
74+
val path = Paths.get("src/test/resources/numeric-formats.json5")
75+
val json5Text = Files.readString(path)
76+
val result = JSON5Parser.parse(json5Text)
77+
val expected = mapOf(
78+
"integer" to 42.0,
79+
"negative" to -17.0,
80+
"float" to 3.14159,
81+
"leadingDecimal" to 0.25,
82+
"trailingDecimal" to 5.0,
83+
"positiveSign" to 42.0,
84+
"hex" to 12648430.0,
85+
"smallHex" to 255.0,
86+
"negativeHex" to -123.0,
87+
"scientific" to 6.02e23,
88+
"negativeExp" to 1e-10,
89+
"positiveExp" to 1.5e4,
90+
"infinite" to Double.POSITIVE_INFINITY,
91+
"negativeInfinite" to Double.NEGATIVE_INFINITY,
92+
"notANumber" to Double.NaN
93+
)
94+
assertEquals(expected.size, (result as Map<*, *>).size)
95+
for ((k, v) in expected) {
96+
val actual = result[k]
97+
if (v is Double && v.isNaN()) {
98+
assert((actual as Double).isNaN())
99+
} else {
100+
assertEquals(v, actual)
101+
}
102+
}
103+
}
104+
105+
@Test
106+
fun testParseStringAndIdentifiersJson5() {
107+
val path = Paths.get("src/test/resources/string-and-identifiers.json5")
108+
val json5Text = Files.readString(path)
109+
val result = JSON5Parser.parse(json5Text)
110+
val expected = mapOf(
111+
"escapes" to """Escaped characters: \b \f \n \r \t \u000B \u0000 \u000F \u00A9""",
112+
"quotes" to "He said, \"Hello!\"",
113+
"singleQuotes" to "It's working",
114+
"multiline" to "This string spans multiple lines",
115+
"lineSeparators" to "Line\u2028and\u2029paragraph separators",
116+
"\$special_key" to "Dollar sign prefix",
117+
"_under_score" to "Underscore prefix",
118+
"ùñîçødë" to "Unicode property name",
119+
"abc" to "abc via Unicode escapes",
120+
"emptyObject" to emptyMap<String, Any?>(),
121+
"emptyArray" to emptyList<Any?>(),
122+
"indentedProperty" to 42.0
123+
)
124+
val resultMap = result as Map<*, *>
125+
assertEquals(expected.size, resultMap.size)
126+
for ((k, v) in expected) {
127+
val actual = resultMap[k]
128+
if (v is Double && v.isNaN()) {
129+
assert((actual as Double).isNaN())
130+
} else {
131+
assertEquals(v, actual)
132+
}
133+
}
134+
}
135+
136+
@Test
137+
fun testParseRootStringJson5() {
138+
val path = Paths.get("src/test/resources/root-string.json5")
139+
val json5Text = Files.readString(path)
140+
val result = JSON5Parser.parse(json5Text)
141+
val expected = "This is a valid root-level string in JSON5"
142+
assertEquals(expected, result)
143+
}
144+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
// This is an array with different data types
3+
42,
4+
"Hello World",
5+
true,
6+
null,
7+
{
8+
name: 'Nested object',
9+
active: true,
10+
},
11+
[1, 2, 3], // Nested array
12+
Infinity, // Special numeric values
13+
-Infinity,
14+
NaN,
15+
// Trailing comma is valid in JSON5
16+
]
17+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
/* Multi-line comment example
3+
showing various numeric formats
4+
supported by JSON5 */
5+
6+
// Regular numbers
7+
integer: 42,
8+
negative: -17,
9+
float: 3.14159,
10+
11+
// Numbers with different formats
12+
leadingDecimal: .25, // Leading decimal point
13+
trailingDecimal: 5., // Trailing decimal point
14+
positiveSign: +42, // Explicit positive sign
15+
16+
// Hexadecimal numbers
17+
hex: 0xC0FFEE, // Uppercase hex
18+
smallHex: 0xff, // Lowercase hex
19+
negativeHex: -0x7B, // Negative hex
20+
21+
// Scientific notation
22+
scientific: 6.02e23, // Regular scientific notation
23+
negativeExp: 1e-10, // Negative exponent
24+
positiveExp: 1.5e+4, // Explicit positive exponent
25+
26+
// Special numeric values
27+
infinite: Infinity,
28+
negativeInfinite: -Infinity,
29+
notANumber: NaN,
30+
}
31+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Root-level values can be strings, numbers, booleans, or null in JSON5
2+
"This is a valid root-level string in JSON5"
3+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// This is a simple object with various types of values
3+
name: "John Doe", // Unquoted property name
4+
age: 30,
5+
isEmployed: true,
6+
salary: 75000.50,
7+
address: {
8+
street: '123 Main St', // Single-quoted string
9+
city: "New York",
10+
zipCode: 10001
11+
}
12+
}
13+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// String escape sequences
3+
escapes: 'Escaped characters: \b \f \n \r \t \v \0 \x0F \u00A9',
4+
quotes: "He said, \"Hello!\"",
5+
singleQuotes: 'It\'s working',
6+
multiline: 'This string \
7+
spans multiple \
8+
lines',
9+
lineSeparators: 'Line\u2028and\u2029paragraph separators',
10+
11+
// Special identifiers as keys
12+
$special_key: "Dollar sign prefix",
13+
_under_score: "Underscore prefix",
14+
ùñîçødë: "Unicode property name",
15+
16+
// Escaped property names
17+
\u0061\u0062\u0063: "abc via Unicode escapes",
18+
19+
// Empty object and arrays
20+
emptyObject: {},
21+
emptyArray: [],
22+
23+
// Indentation and whitespace
24+
indentedProperty: 42,
25+
26+
// Root value example shown in another file
27+
}

0 commit comments

Comments
 (0)