| 1 | |
|---|
| 2 | require "yaml" |
|---|
| 3 | require "lunit" |
|---|
| 4 | |
|---|
| 5 | lunit.import "all" |
|---|
| 6 | |
|---|
| 7 | local testcase = lunit.TestCase("LuaYAML Testcases") |
|---|
| 8 | |
|---|
| 9 | function testcase.test_load() |
|---|
| 10 | assert_error(function() yaml.load() end) |
|---|
| 11 | assert_nil(yaml.load("--- ")) |
|---|
| 12 | assert_true(yaml.load("--- true")) |
|---|
| 13 | assert_false(yaml.load("--- false")) |
|---|
| 14 | assert_equal(10, yaml.load("--- 10")) |
|---|
| 15 | assert_equal(10.3321, yaml.load("--- 10.3321")) |
|---|
| 16 | local t = yaml.load("--- \n- 5\n- 10\n- 15") |
|---|
| 17 | assert_equal(5, t[1]) |
|---|
| 18 | assert_equal(10, t[2]) |
|---|
| 19 | assert_equal(15, t[3]) |
|---|
| 20 | local t = yaml.load("--- \n- one\n- two\n- three") |
|---|
| 21 | assert_equal("one", t[1]) |
|---|
| 22 | assert_equal("two", t[2]) |
|---|
| 23 | assert_equal("three", t[3]) |
|---|
| 24 | local t = yaml.load("--- \nthree: 15\ntwo: 10\none: 5") |
|---|
| 25 | assert_equal(5, t.one) |
|---|
| 26 | assert_equal(10, t.two) |
|---|
| 27 | assert_equal(15, t.three) |
|---|
| 28 | local t = yaml.load("--- \nints: \n - 1\n - 2\n - 3\nmaps: \n three: 3\n two: 2\n one: 1\nstrings: \n - one\n - two\n - three") |
|---|
| 29 | assert_equal(1, t.ints[1]) |
|---|
| 30 | assert_equal(2, t.ints[2]) |
|---|
| 31 | assert_equal(3, t.ints[3]) |
|---|
| 32 | assert_equal(1, t.maps.one) |
|---|
| 33 | assert_equal(2, t.maps.two) |
|---|
| 34 | assert_equal(3, t.maps.three) |
|---|
| 35 | assert_equal("one", t.strings[1]) |
|---|
| 36 | assert_equal("two", t.strings[2]) |
|---|
| 37 | assert_equal("three", t.strings[3]) |
|---|
| 38 | end |
|---|
| 39 | |
|---|
| 40 | function testcase.test_dump() |
|---|
| 41 | assert_equal("--- \n", yaml.dump(nil)) |
|---|
| 42 | assert_equal("--- hey\n", yaml.dump("hey")) |
|---|
| 43 | assert_equal("--- 5\n", yaml.dump(5)) |
|---|
| 44 | assert_equal("--- 5.9991\n", yaml.dump(5.9991)) |
|---|
| 45 | assert_equal("--- true\n", yaml.dump(true)) |
|---|
| 46 | assert_equal("--- false\n", yaml.dump(false)) |
|---|
| 47 | assert_equal("--- \n- 5\n- 6\n- 7\n", yaml.dump({5, 6, 7})) |
|---|
| 48 | |
|---|
| 49 | local str = "--- \n- \n - 1\n - 2\n - 3\n- \n - 6\n - 7\n - 8\n" |
|---|
| 50 | assert_equal(str, yaml.dump({{1, 2, 3}, {6, 7, 8}})) |
|---|
| 51 | local str = "--- \n- \n - 1\n - 2\n - 3\n- \n - one\n - two\n - three\n" |
|---|
| 52 | assert_equal(str, yaml.dump({{1, 2, 3}, {"one", "two", "three"}})) |
|---|
| 53 | |
|---|
| 54 | local str = "--- \none: 1\nthree: 3\ntwo: 2\n" |
|---|
| 55 | assert_equal(str, yaml.dump({one=1, two=2, three=3})) |
|---|
| 56 | end |
|---|
| 57 | |
|---|
| 58 | function testcase.test_file() |
|---|
| 59 | local file = "test.dump" |
|---|
| 60 | |
|---|
| 61 | local f = assert(io.open(file, "w")) |
|---|
| 62 | local obj = {5, 6, "hello"} |
|---|
| 63 | |
|---|
| 64 | yaml.dump(obj, f) |
|---|
| 65 | f:close() |
|---|
| 66 | |
|---|
| 67 | local obj2, err = yaml.load_file(file) |
|---|
| 68 | assert_nil(err) |
|---|
| 69 | assert_equal(5, obj2[1]) |
|---|
| 70 | assert_equal(6, obj2[2]) |
|---|
| 71 | assert_equal("hello", obj2[3]) |
|---|
| 72 | |
|---|
| 73 | yaml.dump_file(obj, file) |
|---|
| 74 | local obj2, err = yaml.load_file(file) |
|---|
| 75 | assert_nil(err) |
|---|
| 76 | assert_equal(5, obj2[1]) |
|---|
| 77 | assert_equal(6, obj2[2]) |
|---|
| 78 | assert_equal("hello", obj2[3]) |
|---|
| 79 | end |
|---|
| 80 | |
|---|
| 81 | os.exit(lunit.run()) |
|---|