|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func testTypeDeclarationAndDefinitionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 10 | + transpilerFunc(t, ` |
| 11 | + type testType int |
| 12 | +
|
| 13 | + var a testType |
| 14 | + a = testType(1) |
| 15 | +
|
| 16 | + print(a) |
| 17 | + `, func(output string, err error) { |
| 18 | + require.Nil(t, err) |
| 19 | + require.Equal(t, "1", output) |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +func testTypeDeclarationAndAssignmentFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 24 | + transpilerFunc(t, ` |
| 25 | + type testType int |
| 26 | +
|
| 27 | + var a testType |
| 28 | + a = 1 |
| 29 | +
|
| 30 | + print(a) |
| 31 | + `, func(output string, err error) { |
| 32 | + require.EqualError(t, shortenError(err), "expected testType but got int") |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +func testTypeAliasAndAssignmentSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 37 | + transpilerFunc(t, ` |
| 38 | + type testType = int |
| 39 | +
|
| 40 | + var a testType |
| 41 | + a = 1 |
| 42 | +
|
| 43 | + print(a) |
| 44 | + `, func(output string, err error) { |
| 45 | + require.Nil(t, err) |
| 46 | + require.Equal(t, "1", output) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func testTypeDeclarationAndDefinitionInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 51 | + transpilerFunc(t, ` |
| 52 | + func main() { |
| 53 | + type testType int |
| 54 | +
|
| 55 | + var a testType |
| 56 | + a = testType(1) |
| 57 | +
|
| 58 | + print(a) |
| 59 | + } |
| 60 | + main() |
| 61 | + `, func(output string, err error) { |
| 62 | + require.Nil(t, err) |
| 63 | + require.Equal(t, "1", output) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func testTypeDeclarationAndAssignmentInFunctionFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 68 | + transpilerFunc(t, ` |
| 69 | + func main() { |
| 70 | + type testType int |
| 71 | +
|
| 72 | + var a testType |
| 73 | + a = 1 |
| 74 | +
|
| 75 | + print(a) |
| 76 | + } |
| 77 | + main() |
| 78 | + `, func(output string, err error) { |
| 79 | + require.EqualError(t, shortenError(err), "expected testType but got int") |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func testTypeAliasAndAssignmentInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 84 | + transpilerFunc(t, ` |
| 85 | + func main() { |
| 86 | + type testType = int |
| 87 | +
|
| 88 | + var a testType |
| 89 | + a = 1 |
| 90 | +
|
| 91 | + print(a) |
| 92 | + } |
| 93 | + main() |
| 94 | + `, func(output string, err error) { |
| 95 | + require.Nil(t, err) |
| 96 | + require.Equal(t, "1", output) |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func testTypeDeclaredInFunctionUsedOutsideFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 101 | + transpilerFunc(t, ` |
| 102 | + func main() { |
| 103 | + type testType int |
| 104 | + } |
| 105 | + var a testType |
| 106 | + `, func(output string, err error) { |
| 107 | + require.EqualError(t, shortenError(err), "expected valid data type") |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +func testTypeDeclaredInIfUsedOutsideFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 112 | + transpilerFunc(t, ` |
| 113 | + if true { |
| 114 | + type testType int |
| 115 | + } |
| 116 | + var a testType |
| 117 | + `, func(output string, err error) { |
| 118 | + require.EqualError(t, shortenError(err), "expected valid data type") |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func testDeclareTypeTwiceFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 123 | + transpilerFunc(t, ` |
| 124 | + type myType int |
| 125 | + type myType string |
| 126 | + `, func(output string, err error) { |
| 127 | + require.EqualError(t, shortenError(err), "myType has already been defined") |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +func testPassDeclaredTypeToFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 132 | + transpilerFunc(t, ` |
| 133 | + type myType string |
| 134 | + |
| 135 | + func test(param myType) { |
| 136 | + print(param) |
| 137 | + } |
| 138 | + test(myType("test")) |
| 139 | + `, func(output string, err error) { |
| 140 | + require.Nil(t, err) |
| 141 | + require.Equal(t, "test", output) |
| 142 | + }) |
| 143 | +} |
| 144 | + |
| 145 | +func testPassBaseTypeToFunctionFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 146 | + transpilerFunc(t, ` |
| 147 | + type myType string |
| 148 | + |
| 149 | + func test(param myType) { |
| 150 | + print(param) |
| 151 | + } |
| 152 | + test("test") |
| 153 | + `, func(output string, err error) { |
| 154 | + require.EqualError(t, shortenError(err), "expected parameter of type myType (param) but got string") |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func testPassValueWithSameBaseTypeToFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) { |
| 159 | + transpilerFunc(t, ` |
| 160 | + type myType1 = string |
| 161 | + type myType2 = string |
| 162 | + type myType3 = myType2 |
| 163 | + type myType4 = myType3 |
| 164 | + |
| 165 | + func test(param myType2) { |
| 166 | + print(param) |
| 167 | + } |
| 168 | + test(myType4("test")) |
| 169 | + `, func(output string, err error) { |
| 170 | + require.Nil(t, err) |
| 171 | + require.Equal(t, "test", output) |
| 172 | + }) |
| 173 | +} |
| 174 | + |
| 175 | +func testAssignDifferentDefinedTypeFail(t *testing.T, transpilerFunc transpilerFunc) { |
| 176 | + transpilerFunc(t, ` |
| 177 | + type myType1 string |
| 178 | + type myType2 string |
| 179 | + |
| 180 | + var a myType1 |
| 181 | + a = myType2("test") |
| 182 | + `, func(output string, err error) { |
| 183 | + require.EqualError(t, shortenError(err), "expected myType1 but got myType2") |
| 184 | + }) |
| 185 | +} |
0 commit comments