|
| 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 | +} |
0 commit comments