|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func testLenSliceSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 12 | + transpilerFunc(t, ` |
| 13 | + a := []int{1, 2, 3} |
| 14 | +
|
| 15 | + print(len(a)) |
| 16 | + `, func(output string, err error) { |
| 17 | + require.Nil(t, err) |
| 18 | + require.Equal(t, "3", output) |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +func testLenStringSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 23 | + transpilerFunc(t, ` |
| 24 | + print(len("abc")) |
| 25 | + `, func(output string, err error) { |
| 26 | + require.Nil(t, err) |
| 27 | + require.Equal(t, "3", output) |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func testCopySuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 32 | + transpilerFunc(t, ` |
| 33 | + s1 := []int{} |
| 34 | + s2 := []int{1, 2, 3} |
| 35 | +
|
| 36 | + a := copy(s1, s2) |
| 37 | +
|
| 38 | + print(a) |
| 39 | +
|
| 40 | + for i := 0; i < len(s1); i++ { |
| 41 | + print(s1[i]) |
| 42 | + } |
| 43 | + `, func(output string, err error) { |
| 44 | + require.Nil(t, err) |
| 45 | + require.Equal(t, "3\n1\n2\n3", output) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func testReadSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 50 | + file := "read-test.txt" |
| 51 | + content := "Hello World" |
| 52 | + os.WriteFile(file, []byte(content), 0x777) |
| 53 | + defer os.Remove(file) |
| 54 | + |
| 55 | + transpilerFunc(t, ` |
| 56 | + `+fmt.Sprintf(`a := read("%s")`, file)+` |
| 57 | + print(a) |
| 58 | + `, func(output string, err error) { |
| 59 | + require.Nil(t, err) |
| 60 | + require.Equal(t, content, output) |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +func testWriteSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 65 | + file := "read-test.txt" |
| 66 | + content := "Hello Moon" |
| 67 | + defer os.Remove(file) |
| 68 | + |
| 69 | + transpilerFunc(t, ` |
| 70 | + `+fmt.Sprintf(`write("%s", "%s")`, file, content)+` |
| 71 | + `+fmt.Sprintf(`a := read("%s")`, file)+` |
| 72 | + print(a) |
| 73 | + `, func(output string, err error) { |
| 74 | + require.Nil(t, err) |
| 75 | + require.Equal(t, content, output) |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func testPanicSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 80 | + transpilerFunc(t, ` |
| 81 | + var a = 1 |
| 82 | +
|
| 83 | + if a == 1 { |
| 84 | + panic("panic") |
| 85 | + } |
| 86 | + print("got through") |
| 87 | + `, func(output string, err error) { |
| 88 | + require.Error(t, err) |
| 89 | + require.Equal(t, "panic: panic", output) |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +func testLenSliceInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 94 | + transpilerFunc(t, ` |
| 95 | + func test() { |
| 96 | + a := []int{1, 2, 3} |
| 97 | +
|
| 98 | + print(len(a)) |
| 99 | + } |
| 100 | + test() |
| 101 | + `, func(output string, err error) { |
| 102 | + require.Nil(t, err) |
| 103 | + require.Equal(t, "3", output) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func testLenStringInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 108 | + transpilerFunc(t, ` |
| 109 | + func test() { |
| 110 | + print(len("abc")) |
| 111 | + } |
| 112 | + test() |
| 113 | + `, func(output string, err error) { |
| 114 | + require.Nil(t, err) |
| 115 | + require.Equal(t, "3", output) |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +func testCopyInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 120 | + transpilerFunc(t, ` |
| 121 | + func test() { |
| 122 | + s1 := []int{} |
| 123 | + s2 := []int{1, 2, 3} |
| 124 | +
|
| 125 | + a := copy(s1, s2) |
| 126 | +
|
| 127 | + print(a) |
| 128 | +
|
| 129 | + for i := 0; i < len(s1); i++ { |
| 130 | + print(s1[i]) |
| 131 | + } |
| 132 | + } |
| 133 | + test() |
| 134 | + `, func(output string, err error) { |
| 135 | + require.Nil(t, err) |
| 136 | + require.Equal(t, "3\n1\n2\n3", output) |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +func testReadInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 141 | + file := "read-test.txt" |
| 142 | + content := "Hello World" |
| 143 | + os.WriteFile(file, []byte(content), 0x777) |
| 144 | + defer os.Remove(file) |
| 145 | + |
| 146 | + transpilerFunc(t, ` |
| 147 | + func test() { |
| 148 | + `+fmt.Sprintf(`a := read("%s")`, file)+` |
| 149 | + print(a) |
| 150 | + } |
| 151 | + test() |
| 152 | + `, func(output string, err error) { |
| 153 | + require.Nil(t, err) |
| 154 | + require.Equal(t, content, output) |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func testWriteInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 159 | + file := "read-test.txt" |
| 160 | + content := "Hello Moon" |
| 161 | + defer os.Remove(file) |
| 162 | + |
| 163 | + transpilerFunc(t, ` |
| 164 | + func test() { |
| 165 | + `+fmt.Sprintf(`write("%s", "%s")`, file, content)+` |
| 166 | + `+fmt.Sprintf(`a := read("%s")`, file)+` |
| 167 | + print(a) |
| 168 | + } |
| 169 | + test() |
| 170 | + `, func(output string, err error) { |
| 171 | + require.Nil(t, err) |
| 172 | + require.Equal(t, content, output) |
| 173 | + }) |
| 174 | +} |
| 175 | + |
| 176 | +func testPanicInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 177 | + transpilerFunc(t, ` |
| 178 | + func test() { |
| 179 | + var a = 1 |
| 180 | +
|
| 181 | + if a == 1 { |
| 182 | + panic("panic") |
| 183 | + } |
| 184 | + print("got through") |
| 185 | + } |
| 186 | + test() |
| 187 | + `, func(output string, err error) { |
| 188 | + require.Error(t, err) |
| 189 | + require.Equal(t, "panic: panic", output) |
| 190 | + }) |
| 191 | +} |
0 commit comments