diff options
Diffstat (limited to 'spec/outputs/test/string_spec.lua')
| -rw-r--r-- | spec/outputs/test/string_spec.lua | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/spec/outputs/test/string_spec.lua b/spec/outputs/test/string_spec.lua new file mode 100644 index 0000000..76d4ae6 --- /dev/null +++ b/spec/outputs/test/string_spec.lua | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | return describe("string", function() | ||
| 2 | it("should support single quote strings", function() | ||
| 3 | local s = 'hello' | ||
| 4 | return assert.same(s, "hello") | ||
| 5 | end) | ||
| 6 | it("should support double quote strings", function() | ||
| 7 | local s = "world" | ||
| 8 | return assert.same(s, "world") | ||
| 9 | end) | ||
| 10 | it("should support escape sequences", function() | ||
| 11 | local s = "hello\nworld" | ||
| 12 | return assert.is_true(s:match("\n") ~= nil) | ||
| 13 | end) | ||
| 14 | it("should support escaped quotes", function() | ||
| 15 | local s = "he said \"hello\"" | ||
| 16 | return assert.same(s, 'he said "hello"') | ||
| 17 | end) | ||
| 18 | it("should support backslash escape", function() | ||
| 19 | local s = "\\" | ||
| 20 | return assert.same(s, "\\") | ||
| 21 | end) | ||
| 22 | it("should support multi-line strings with [[ ]]", function() | ||
| 23 | local s = [[ hello | ||
| 24 | world | ||
| 25 | ]] | ||
| 26 | assert.is_true(s:match("hello") ~= nil) | ||
| 27 | return assert.is_true(s:match("world") ~= nil) | ||
| 28 | end) | ||
| 29 | it("should support multi-line strings with [=[ ]=]", function() | ||
| 30 | local s = [==[ hello | ||
| 31 | world | ||
| 32 | ]==] | ||
| 33 | assert.is_true(s:match("hello") ~= nil) | ||
| 34 | return assert.is_true(s:match("world") ~= nil) | ||
| 35 | end) | ||
| 36 | it("should support string interpolation with double quotes", function() | ||
| 37 | local name = "world" | ||
| 38 | local s = "hello " .. tostring(name) | ||
| 39 | return assert.same(s, "hello world") | ||
| 40 | end) | ||
| 41 | it("should support expression interpolation", function() | ||
| 42 | local a, b = 1, 2 | ||
| 43 | local s = tostring(a) .. " + " .. tostring(b) .. " = " .. tostring(a + b) | ||
| 44 | return assert.same(s, "1 + 2 = 3") | ||
| 45 | end) | ||
| 46 | it("should not interpolate in single quotes", function() | ||
| 47 | local name = "world" | ||
| 48 | local s = 'hello #{name}' | ||
| 49 | return assert.same(s, "hello #{name}") | ||
| 50 | end) | ||
| 51 | it("should escape interpolation with \\#", function() | ||
| 52 | local name = "world" | ||
| 53 | local s = "hello #{name}" | ||
| 54 | return assert.same(s, 'hello #{name}') | ||
| 55 | end) | ||
| 56 | it("should support method calls on string literals", function() | ||
| 57 | local result = ("hello"):upper() | ||
| 58 | return assert.same(result, "HELLO") | ||
| 59 | end) | ||
| 60 | it("should support chained method calls", function() | ||
| 61 | local result = ("hello world"):upper():match("HELLO") | ||
| 62 | return assert.same(result, "HELLO") | ||
| 63 | end) | ||
| 64 | it("should support YAML style strings", function() | ||
| 65 | local s = "hello\nworld" | ||
| 66 | assert.is_true(s:match("hello") ~= nil) | ||
| 67 | return assert.is_true(s:match("world") ~= nil) | ||
| 68 | end) | ||
| 69 | it("should support YAML style with interpolation", function() | ||
| 70 | local name = "test" | ||
| 71 | local s = "hello " .. tostring(name) | ||
| 72 | return assert.same(s, "hello test") | ||
| 73 | end) | ||
| 74 | it("should support string concatenation", function() | ||
| 75 | local s = "hello" .. " " .. "world" | ||
| 76 | return assert.same(s, "hello world") | ||
| 77 | end) | ||
| 78 | it("should handle empty strings", function() | ||
| 79 | local s = "" | ||
| 80 | return assert.same(s, "") | ||
| 81 | end) | ||
| 82 | it("should support Unicode characters", function() | ||
| 83 | local s = "hello 世界" | ||
| 84 | return assert.is_true(s:match("世界") ~= nil) | ||
| 85 | end) | ||
| 86 | it("should support string length", function() | ||
| 87 | local s = "hello" | ||
| 88 | return assert.same(#s, 5) | ||
| 89 | end) | ||
| 90 | it("should support multi-line YAML with complex content", function() | ||
| 91 | local config = "key1: value1\nkey2: value2\nkey3: value3" | ||
| 92 | return assert.is_true(config:match("key1") ~= nil) | ||
| 93 | end) | ||
| 94 | it("should support interpolation in YAML strings", function() | ||
| 95 | local x, y = 10, 20 | ||
| 96 | local s = "point:\n\tx: " .. tostring(x) .. "\n\ty: " .. tostring(y) | ||
| 97 | assert.is_true(s:match("x: 10") ~= nil) | ||
| 98 | return assert.is_true(s:match("y: 20") ~= nil) | ||
| 99 | end) | ||
| 100 | it("should support function call in interpolation", function() | ||
| 101 | local s = "result: " .. tostring((function() | ||
| 102 | return 42 | ||
| 103 | end)()) | ||
| 104 | return assert.same(s, "result: 42") | ||
| 105 | end) | ||
| 106 | it("should support table indexing in interpolation", function() | ||
| 107 | local t = { | ||
| 108 | value = 100 | ||
| 109 | } | ||
| 110 | local s = "value: " .. tostring(t.value) | ||
| 111 | return assert.same(s, "value: 100") | ||
| 112 | end) | ||
| 113 | it("should handle escaped characters correctly", function() | ||
| 114 | local s = "tab:\t, newline:\n, return:\r" | ||
| 115 | assert.is_true(s:match("\t") ~= nil) | ||
| 116 | return assert.is_true(s:match("\n") ~= nil) | ||
| 117 | end) | ||
| 118 | it("should support string methods with colon syntax", function() | ||
| 119 | local s = "hello" | ||
| 120 | return assert.same(s:sub(1, 2), "he") | ||
| 121 | end) | ||
| 122 | it("should work in expressions", function() | ||
| 123 | local result = "hello" .. " world" | ||
| 124 | return assert.same(result, "hello world") | ||
| 125 | end) | ||
| 126 | it("should support octal escape", function() | ||
| 127 | local s = "\65" | ||
| 128 | return assert.same(s, "A") | ||
| 129 | end) | ||
| 130 | it("should support hex escape", function() | ||
| 131 | local s = "\x41" | ||
| 132 | return assert.same(s, "A") | ||
| 133 | end) | ||
| 134 | return it("should support unicode escape", function() | ||
| 135 | local s = "\u{4e16}" | ||
| 136 | return assert.same(s, "世") | ||
| 137 | end) | ||
| 138 | end) | ||
