|
| 1 | +// Copyright © 2014 Steve Francia <spf@spf13.com>. |
| 2 | +// |
| 3 | +// Use of this source code is governed by an MIT-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | +package cast |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + qt "github.com/frankban/quicktest" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAlias(t *testing.T) { |
| 14 | + type MyStruct struct{} |
| 15 | + |
| 16 | + type MyString string |
| 17 | + type MyOtherString MyString |
| 18 | + type MyAliasString = MyOtherString |
| 19 | + |
| 20 | + type MyBool bool |
| 21 | + type MyOtherBool MyBool |
| 22 | + type MyAliasBool = MyOtherBool |
| 23 | + |
| 24 | + type MyInt int |
| 25 | + type MyInt8 int8 |
| 26 | + type MyInt16 int16 |
| 27 | + type MyInt32 int32 |
| 28 | + type MyInt64 int64 |
| 29 | + type MyUint uint |
| 30 | + type MyUint8 uint8 |
| 31 | + type MyUint16 uint16 |
| 32 | + type MyUint32 uint32 |
| 33 | + type MyUint64 uint64 |
| 34 | + type MyFloat32 float32 |
| 35 | + type MyFloat64 float64 |
| 36 | + |
| 37 | + testCases := []struct { |
| 38 | + input any |
| 39 | + expectedValue any |
| 40 | + expectedOk bool |
| 41 | + }{ |
| 42 | + {"string", "string", false}, // Already resolved |
| 43 | + {MyStruct{}, MyStruct{}, false}, // Non-resolvable |
| 44 | + |
| 45 | + {MyString("string"), "string", true}, |
| 46 | + {MyOtherString("string"), "string", true}, |
| 47 | + {MyAliasString("string"), "string", true}, |
| 48 | + |
| 49 | + {MyBool(true), true, true}, |
| 50 | + {MyOtherBool(true), true, true}, |
| 51 | + {MyAliasBool(true), true, true}, |
| 52 | + |
| 53 | + {MyInt(1234), int(1234), true}, |
| 54 | + {MyInt8(123), int8(123), true}, |
| 55 | + {MyInt16(1234), int16(1234), true}, |
| 56 | + {MyInt32(1234), int32(1234), true}, |
| 57 | + {MyInt64(1234), int64(1234), true}, |
| 58 | + |
| 59 | + {MyUint(1234), uint(1234), true}, |
| 60 | + {MyUint8(123), uint8(123), true}, |
| 61 | + {MyUint16(1234), uint16(1234), true}, |
| 62 | + {MyUint32(1234), uint32(1234), true}, |
| 63 | + {MyUint64(1234), uint64(1234), true}, |
| 64 | + |
| 65 | + {MyFloat32(1.0), float32(1.0), true}, |
| 66 | + {MyFloat64(1.0), float64(1.0), true}, |
| 67 | + } |
| 68 | + |
| 69 | + for _, testCase := range testCases { |
| 70 | + // TODO: remove after minimum Go version is >=1.22 |
| 71 | + testCase := testCase |
| 72 | + |
| 73 | + t.Run("", func(t *testing.T) { |
| 74 | + c := qt.New(t) |
| 75 | + |
| 76 | + actualValue, ok := resolveAlias(testCase.input) |
| 77 | + |
| 78 | + c.Assert(actualValue, qt.Equals, testCase.expectedValue) |
| 79 | + c.Assert(ok, qt.Equals, testCase.expectedOk) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments