|
1 | 1 | local yaml = require("yaml") |
2 | | -local text = [[ |
| 2 | +local test = {} |
| 3 | + |
| 4 | +-- test decode |
| 5 | +function test:decode() |
| 6 | + local text = [[ |
| 7 | +a: |
| 8 | + b: 1 |
| 9 | +]] |
| 10 | + local result, err = yaml.decode(text) |
| 11 | + assert(not err, tostring(err)) |
| 12 | + assert(result["a"]["b"] == 1, tostring(result["a"]["b"])) |
| 13 | + print("done: yaml.decode()") |
| 14 | +end |
| 15 | + |
| 16 | +-- test decode with no args throws exception |
| 17 | +function test:decode_no_args() |
| 18 | + local ok, errMsg = pcall(yaml.decode) |
| 19 | + assert(not ok) |
| 20 | + assert(errMsg) |
| 21 | + assert(errMsg:find("(string expected, got nil)"), tostring(errMsg)) |
| 22 | +end |
| 23 | + |
| 24 | +-- test encode of decoded(text) == text |
| 25 | +function test:decoded_text_equals_text() |
| 26 | + local text = [[ |
3 | 27 | a: |
4 | 28 | b: 1 |
5 | 29 | ]] |
6 | | -local result, err = yaml.decode(text) |
7 | | -if err then error(err) end |
8 | | -if not(result["a"]["b"] == 1) then error("decode error") end |
| 30 | + local result = { a = { b = 1 } } |
| 31 | + local encodedResult, err = yaml.encode(result) |
| 32 | + assert(not err, tostring(err)) |
| 33 | + assert(encodedResult == text, tostring(encodedResult) |
| 34 | + ) |
| 35 | +end |
| 36 | + |
| 37 | +-- test encode(slice) works |
| 38 | +function test:encode_slice_works() |
| 39 | + local encodedSlice = yaml.encode({ "foo", "bar", "baz" }) |
| 40 | + assert(encodedSlice == [[ |
| 41 | +- foo |
| 42 | +- bar |
| 43 | +- baz |
| 44 | +]], tostring(encodedSlice)) |
| 45 | +end |
| 46 | + |
| 47 | +-- test encode(sparse slice) works |
| 48 | +function test:encode_sparse_slice_returns_map() |
| 49 | + local slice = { [0] = "foo", [1] = "bar", [2] = "baz" } |
| 50 | + local encodedSlice = yaml.encode(slice) |
| 51 | + assert(encodedSlice == [[ |
| 52 | +0: foo |
| 53 | +1: bar |
| 54 | +2: baz |
| 55 | +]], tostring(encodedSlice)) |
| 56 | +end |
| 57 | + |
| 58 | +-- test encode(map) works |
| 59 | +function test:encode_map_returns_map() |
| 60 | + local map = { foo = "bar", bar = { 1, 2, 3.45 } } |
| 61 | + local encodedMap = yaml.encode(map) |
| 62 | + assert(encodedMap == [[ |
| 63 | +bar: |
| 64 | +- 1 |
| 65 | +- 2 |
| 66 | +- 3.45 |
| 67 | +foo: bar |
| 68 | +]], tostring(encodedMap)) |
| 69 | +end |
| 70 | + |
| 71 | +-- test encode(function) fails |
| 72 | +function test:encode_function_fails() |
| 73 | + local _, errMsg = yaml.encode(function() |
| 74 | + return "" |
| 75 | + end) |
| 76 | + assert(errMsg) |
| 77 | + assert(errMsg:find("cannot encode values with function in them"), errMsg) |
| 78 | + |
| 79 | + -- test encode with no args throws exception |
| 80 | + local ok, errMsg = pcall(yaml.encode) |
| 81 | + assert(not ok) |
| 82 | + assert(errMsg:find("(value expected)"), tostring(errMsg)) |
| 83 | +end |
| 84 | + |
| 85 | +-- test cycles |
| 86 | +function test:cycles_return_error() |
| 87 | + local t1 = {} |
| 88 | + local t2 = { t1 = t1 } |
| 89 | + t1[t2] = t2 |
| 90 | + local _, errMsg = yaml.encode(t1) |
| 91 | + assert(errMsg) |
| 92 | + assert(errMsg:find("nested table"), tostring(errMsg)) |
| 93 | +end |
9 | 94 |
|
10 | | -print("done: yaml.decode()") |
| 95 | +return test |
0 commit comments