|
| 1 | +// |
| 2 | +// JSONValueCoercionTests.swift |
| 3 | +// CodableKitTests |
| 4 | +// |
| 5 | +// Created by Wendell Wang on 2026/4/2. |
| 6 | +// |
| 7 | + |
| 8 | +import CodableKit |
| 9 | +import Testing |
| 10 | + |
| 11 | +@Suite("JSONValue coercion tests") |
| 12 | +struct JSONValueCoercionTests { |
| 13 | + |
| 14 | + // MARK: - coercedBoolValue |
| 15 | + |
| 16 | + @Suite("coercedBoolValue") |
| 17 | + struct CoercedBool { |
| 18 | + @Test func from_bool() { |
| 19 | + #expect(JSONValue.bool(true).coercedBoolValue == true) |
| 20 | + #expect(JSONValue.bool(false).coercedBoolValue == false) |
| 21 | + } |
| 22 | + |
| 23 | + @Test func from_int() { |
| 24 | + #expect(JSONValue.int(1).coercedBoolValue == true) |
| 25 | + #expect(JSONValue.int(0).coercedBoolValue == false) |
| 26 | + #expect(JSONValue.int(-1).coercedBoolValue == true) |
| 27 | + #expect(JSONValue.int(42).coercedBoolValue == true) |
| 28 | + } |
| 29 | + |
| 30 | + @Test func from_double() { |
| 31 | + #expect(JSONValue.double(1.0).coercedBoolValue == true) |
| 32 | + #expect(JSONValue.double(0.0).coercedBoolValue == false) |
| 33 | + #expect(JSONValue.double(0.5).coercedBoolValue == true) |
| 34 | + } |
| 35 | + |
| 36 | + @Test func from_truthy_strings() { |
| 37 | + for s in ["true", "True", "TRUE", "t", "T", "yes", "Yes", "YES", "y", "Y", "1"] { |
| 38 | + #expect(JSONValue.string(s).coercedBoolValue == true, "Expected true for \"\(s)\"") |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test func from_falsy_strings() { |
| 43 | + for s in ["false", "False", "FALSE", "f", "F", "no", "No", "NO", "n", "N", "0"] { |
| 44 | + #expect(JSONValue.string(s).coercedBoolValue == false, "Expected false for \"\(s)\"") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test func from_unrecognized_string() { |
| 49 | + #expect(JSONValue.string("maybe").coercedBoolValue == nil) |
| 50 | + #expect(JSONValue.string("2").coercedBoolValue == nil) |
| 51 | + #expect(JSONValue.string("").coercedBoolValue == nil) |
| 52 | + } |
| 53 | + |
| 54 | + @Test func from_null_array_object() { |
| 55 | + #expect(JSONValue.null.coercedBoolValue == nil) |
| 56 | + #expect(JSONValue.array([]).coercedBoolValue == nil) |
| 57 | + #expect(JSONValue.object([:]).coercedBoolValue == nil) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // MARK: - coercedIntValue |
| 62 | + |
| 63 | + @Suite("coercedIntValue") |
| 64 | + struct CoercedInt { |
| 65 | + @Test func from_int() { |
| 66 | + #expect(JSONValue.int(42).coercedIntValue == 42) |
| 67 | + #expect(JSONValue.int(-1).coercedIntValue == -1) |
| 68 | + #expect(JSONValue.int(0).coercedIntValue == 0) |
| 69 | + } |
| 70 | + |
| 71 | + @Test func from_double_exact() { |
| 72 | + #expect(JSONValue.double(3.0).coercedIntValue == 3) |
| 73 | + #expect(JSONValue.double(-5.0).coercedIntValue == -5) |
| 74 | + } |
| 75 | + |
| 76 | + @Test func from_double_fractional() { |
| 77 | + #expect(JSONValue.double(3.5).coercedIntValue == nil) |
| 78 | + #expect(JSONValue.double(0.1).coercedIntValue == nil) |
| 79 | + } |
| 80 | + |
| 81 | + @Test func from_bool() { |
| 82 | + #expect(JSONValue.bool(true).coercedIntValue == 1) |
| 83 | + #expect(JSONValue.bool(false).coercedIntValue == 0) |
| 84 | + } |
| 85 | + |
| 86 | + @Test func from_numeric_string() { |
| 87 | + #expect(JSONValue.string("123").coercedIntValue == 123) |
| 88 | + #expect(JSONValue.string("-7").coercedIntValue == -7) |
| 89 | + #expect(JSONValue.string("0").coercedIntValue == 0) |
| 90 | + } |
| 91 | + |
| 92 | + @Test func from_float_string_exact() { |
| 93 | + #expect(JSONValue.string("3.0").coercedIntValue == 3) |
| 94 | + } |
| 95 | + |
| 96 | + @Test func from_float_string_fractional() { |
| 97 | + #expect(JSONValue.string("3.5").coercedIntValue == nil) |
| 98 | + } |
| 99 | + |
| 100 | + @Test func from_non_numeric_string() { |
| 101 | + #expect(JSONValue.string("abc").coercedIntValue == nil) |
| 102 | + #expect(JSONValue.string("").coercedIntValue == nil) |
| 103 | + } |
| 104 | + |
| 105 | + @Test func from_null_array_object() { |
| 106 | + #expect(JSONValue.null.coercedIntValue == nil) |
| 107 | + #expect(JSONValue.array([]).coercedIntValue == nil) |
| 108 | + #expect(JSONValue.object([:]).coercedIntValue == nil) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // MARK: - coercedDoubleValue |
| 113 | + |
| 114 | + @Suite("coercedDoubleValue") |
| 115 | + struct CoercedDouble { |
| 116 | + @Test func from_double() { |
| 117 | + #expect(JSONValue.double(3.14).coercedDoubleValue == 3.14) |
| 118 | + } |
| 119 | + |
| 120 | + @Test func from_int() { |
| 121 | + #expect(JSONValue.int(42).coercedDoubleValue == 42.0) |
| 122 | + } |
| 123 | + |
| 124 | + @Test func from_bool() { |
| 125 | + #expect(JSONValue.bool(true).coercedDoubleValue == 1.0) |
| 126 | + #expect(JSONValue.bool(false).coercedDoubleValue == 0.0) |
| 127 | + } |
| 128 | + |
| 129 | + @Test func from_numeric_string() { |
| 130 | + #expect(JSONValue.string("3.14").coercedDoubleValue == 3.14) |
| 131 | + #expect(JSONValue.string("42").coercedDoubleValue == 42.0) |
| 132 | + #expect(JSONValue.string("-1.5").coercedDoubleValue == -1.5) |
| 133 | + } |
| 134 | + |
| 135 | + @Test func from_non_numeric_string() { |
| 136 | + #expect(JSONValue.string("abc").coercedDoubleValue == nil) |
| 137 | + #expect(JSONValue.string("").coercedDoubleValue == nil) |
| 138 | + } |
| 139 | + |
| 140 | + @Test func from_null_array_object() { |
| 141 | + #expect(JSONValue.null.coercedDoubleValue == nil) |
| 142 | + #expect(JSONValue.array([]).coercedDoubleValue == nil) |
| 143 | + #expect(JSONValue.object([:]).coercedDoubleValue == nil) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // MARK: - coercedStringValue |
| 148 | + |
| 149 | + @Suite("coercedStringValue") |
| 150 | + struct CoercedString { |
| 151 | + @Test func from_string() { |
| 152 | + #expect(JSONValue.string("hello").coercedStringValue == "hello") |
| 153 | + } |
| 154 | + |
| 155 | + @Test func from_bool() { |
| 156 | + #expect(JSONValue.bool(true).coercedStringValue == "true") |
| 157 | + #expect(JSONValue.bool(false).coercedStringValue == "false") |
| 158 | + } |
| 159 | + |
| 160 | + @Test func from_int() { |
| 161 | + #expect(JSONValue.int(42).coercedStringValue == "42") |
| 162 | + #expect(JSONValue.int(-1).coercedStringValue == "-1") |
| 163 | + } |
| 164 | + |
| 165 | + @Test func from_double() { |
| 166 | + #expect(JSONValue.double(3.14).coercedStringValue == "3.14") |
| 167 | + } |
| 168 | + |
| 169 | + @Test func from_null() { |
| 170 | + #expect(JSONValue.null.coercedStringValue == "null") |
| 171 | + } |
| 172 | + |
| 173 | + @Test func from_array_object() { |
| 174 | + #expect(JSONValue.array([]).coercedStringValue == nil) |
| 175 | + #expect(JSONValue.object([:]).coercedStringValue == nil) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // MARK: - Compatibility numeric accessors with coercion |
| 180 | + |
| 181 | + @Suite("Compatibility numeric coercion") |
| 182 | + struct CompatibilityNumeric { |
| 183 | + @Test func int64Value_from_string() { |
| 184 | + #expect(JSONValue.string("123").int64Value == 123) |
| 185 | + #expect(JSONValue.string("3.0").int64Value == 3) |
| 186 | + #expect(JSONValue.string("abc").int64Value == nil) |
| 187 | + } |
| 188 | + |
| 189 | + @Test func int64Value_from_bool() { |
| 190 | + #expect(JSONValue.bool(true).int64Value == 1) |
| 191 | + #expect(JSONValue.bool(false).int64Value == 0) |
| 192 | + } |
| 193 | + |
| 194 | + @Test func int8Value_from_string() { |
| 195 | + #expect(JSONValue.string("42").int8Value == 42) |
| 196 | + #expect(JSONValue.string("200").int8Value == nil) // overflow |
| 197 | + #expect(JSONValue.string("abc").int8Value == nil) |
| 198 | + } |
| 199 | + |
| 200 | + @Test func int8Value_from_bool() { |
| 201 | + #expect(JSONValue.bool(true).int8Value == 1) |
| 202 | + #expect(JSONValue.bool(false).int8Value == 0) |
| 203 | + } |
| 204 | + |
| 205 | + @Test func numberValue_from_string() { |
| 206 | + #expect(JSONValue.string("3.14").numberValue == 3.14) |
| 207 | + #expect(JSONValue.string("42").numberValue == 42.0) |
| 208 | + #expect(JSONValue.string("abc").numberValue == nil) |
| 209 | + } |
| 210 | + |
| 211 | + @Test func numberValue_from_bool() { |
| 212 | + #expect(JSONValue.bool(true).numberValue == 1.0) |
| 213 | + #expect(JSONValue.bool(false).numberValue == 0.0) |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments