|
| 1 | +import { Expect, Test, TestCase, IgnoreTest } from "alsatian"; |
| 2 | +import * as util from "../../src/util" |
| 3 | + |
| 4 | +export class StringTests { |
| 5 | + |
| 6 | + @TestCase([]) |
| 7 | + @TestCase([65]) |
| 8 | + @TestCase([65, 66]) |
| 9 | + @TestCase([65, 66, 67]) |
| 10 | + @Test("String.fromCharCode") |
| 11 | + public stringFromCharcode(inp: number[], expected: string) { |
| 12 | + // Transpile |
| 13 | + let lua = util.transpileString( |
| 14 | + `return String.fromCharCode(${inp.toString()})`, |
| 15 | + util.dummyTypes.String |
| 16 | + ); |
| 17 | + |
| 18 | + // Execute |
| 19 | + let result = util.executeLua(lua); |
| 20 | + |
| 21 | + // Assert |
| 22 | + Expect(result).toBe(String.fromCharCode(...inp)); |
| 23 | + } |
| 24 | + |
| 25 | + @TestCase("hello test", "", "") |
| 26 | + @TestCase("hello test", " ", "") |
| 27 | + @TestCase("hello test", "hello", "") |
| 28 | + @TestCase("hello test", "test", "") |
| 29 | + @TestCase("hello test", "test", "world") |
| 30 | + @Test("string.replace") |
| 31 | + public replace<T>(inp: string, searchValue: string, replaceValue: string) { |
| 32 | + // Transpile |
| 33 | + let lua = util.transpileString( |
| 34 | + `return "${inp}".replace("${searchValue}", "${replaceValue}")`, |
| 35 | + util.dummyTypes.String |
| 36 | + ); |
| 37 | + |
| 38 | + // Execute |
| 39 | + let result = util.executeLua(lua); |
| 40 | + |
| 41 | + // Assert |
| 42 | + Expect(result).toBe(inp.replace(searchValue, replaceValue)); |
| 43 | + } |
| 44 | + |
| 45 | + @TestCase(["", ""], "") |
| 46 | + @TestCase(["hello", "test"], "hellotest") |
| 47 | + @TestCase(["hello", "test", "bye"], "hellotestbye") |
| 48 | + @TestCase(["hello", 42], "hello42") |
| 49 | + @TestCase([42, "hello"], "42hello") |
| 50 | + @Test("string.concat[+]") |
| 51 | + public concat(inp: any[], expected: string) { |
| 52 | + // Transpile |
| 53 | + let lua = util.transpileString( |
| 54 | + `return '${inp.join("' + '")}'`, |
| 55 | + util.dummyTypes.String |
| 56 | + ); |
| 57 | + |
| 58 | + // Execute |
| 59 | + let result = util.executeLua(lua); |
| 60 | + |
| 61 | + // Assert |
| 62 | + Expect(result).toBe(expected); |
| 63 | + } |
| 64 | + |
| 65 | + @TestCase("hello test", new RegExp("123", "g"), "") |
| 66 | + @IgnoreTest() |
| 67 | + @Test("string.replace[Regex]") |
| 68 | + public replaceRegex(inp: string, searchValue: string, replaceValue: string) { |
| 69 | + // Transpile |
| 70 | + let lua = util.transpileString( |
| 71 | + `return "${inp}".replace("${searchValue}", "${replaceValue}")`, |
| 72 | + util.dummyTypes.String |
| 73 | + ); |
| 74 | + |
| 75 | + // Execute |
| 76 | + let result = util.executeLua(lua); |
| 77 | + |
| 78 | + // Assert |
| 79 | + Expect(result).toBe(inp.replace(searchValue, replaceValue)); |
| 80 | + } |
| 81 | + |
| 82 | + @TestCase("hello test", "") |
| 83 | + @TestCase("hello test", "t") |
| 84 | + @TestCase("hello test", "h") |
| 85 | + @Test("string.indexOf") |
| 86 | + public indexOf(inp: string, searchValue: string) { |
| 87 | + // Transpile |
| 88 | + let lua = util.transpileString( |
| 89 | + `return "${inp}".indexOf("${searchValue}")`, |
| 90 | + util.dummyTypes.String |
| 91 | + ); |
| 92 | + |
| 93 | + // Execute |
| 94 | + let result = util.executeLua(lua); |
| 95 | + |
| 96 | + // Assert |
| 97 | + Expect(result).toBe(inp.indexOf(searchValue)); |
| 98 | + } |
| 99 | + |
| 100 | + @TestCase("hello test", 0) |
| 101 | + @TestCase("hello test", 1) |
| 102 | + @TestCase("hello test", 1, 2) |
| 103 | + @TestCase("hello test", 1, 5) |
| 104 | + @Test("string.substring") |
| 105 | + public substring(inp: string, start: number, end?: number) { |
| 106 | + // Transpile |
| 107 | + let paramStr = end ? `${start}, ${end}` : `${start}`; |
| 108 | + let lua = util.transpileString( |
| 109 | + `return "${inp}".substring(${paramStr})`, |
| 110 | + util.dummyTypes.String |
| 111 | + ); |
| 112 | + |
| 113 | + // Execute |
| 114 | + let result = util.executeLua(lua); |
| 115 | + |
| 116 | + // Assert |
| 117 | + Expect(result).toBe(inp.substring(start, end)); |
| 118 | + } |
| 119 | +} |
0 commit comments