Files
cell/tests/toml.cm
2025-12-05 23:21:07 -06:00

116 lines
2.7 KiB
Plaintext

var toml = use('toml')
function deep_equal(a, b) {
if (a == b) return true
if (a == null || b == null) return false
if (typeof a != typeof b) return false
if (typeof a == 'object') {
var keys_a = Object.keys(a)
var keys_b = Object.keys(b)
if (keys_a.length != keys_b.length) return false
for (var i = 0; i < keys_a.length; i++) {
if (!deep_equal(a[keys_a[i]], b[keys_a[i]])) return false
}
return true
}
return false
}
function test_roundtrip(obj, name) {
var encoded = toml.encode(obj)
var decoded = toml.decode(encoded)
if (!deep_equal(obj, decoded)) {
log.console(name + " - Original:", json.encode(obj))
log.console(name + " - Round-trip:", json.encode(decoded))
throw name + " round-trip failed"
}
}
return {
test_basic_string: function() {
var obj = { string_value: "hello world" }
test_roundtrip(obj, "basic_string")
},
test_basic_number: function() {
var obj = { number_value: 42 }
test_roundtrip(obj, "basic_number")
},
test_basic_float: function() {
var obj = { float_value: 3.14 }
test_roundtrip(obj, "basic_float")
},
test_basic_bool: function() {
var obj = { bool_true: true, bool_false: false }
test_roundtrip(obj, "basic_bool")
},
test_basic_array: function() {
var obj = { array_mixed: ["string", 123, true, false] }
test_roundtrip(obj, "basic_array")
},
test_nested_objects: function() {
var obj = {
name: "Test Config",
version: 1.0,
database: {
host: "localhost",
port: 5432,
credentials: {
username: "admin",
password: "secret123"
}
}
}
test_roundtrip(obj, "nested_objects")
},
test_multiple_nested_sections: function() {
var obj = {
servers: {
alpha: {
ip: "192.168.1.1",
port: 8080
},
beta: {
ip: "192.168.1.2",
port: 8081
}
},
features: ["auth", "api", "websocket"]
}
test_roundtrip(obj, "multiple_nested_sections")
},
test_empty_string: function() {
var obj = { empty_string: "" }
test_roundtrip(obj, "empty_string")
},
test_string_with_quotes: function() {
var obj = { string_with_quotes: 'She said "Hello"' }
test_roundtrip(obj, "string_with_quotes")
},
test_empty_array: function() {
var obj = { empty_array: [] }
test_roundtrip(obj, "empty_array")
},
test_single_element_array: function() {
var obj = { single_array: [42] }
test_roundtrip(obj, "single_element_array")
},
test_nested_empty_section: function() {
var obj = { nested_empty: { section: {} } }
test_roundtrip(obj, "nested_empty_section")
}
}