Skip to content

Commit c4da65e

Browse files
committed
chore: add unit test for type alias conversions
--- FAIL: TestConvert (0.00s) --- FAIL: TestConvert/aliases (0.00s) --- FAIL: TestConvert/aliases/string_type_alias (0.00s) conversion_test.go:1522: expected no error, got conversion issue: unsupported type from safecast_test.StringTypeAlias --- FAIL: TestConvert/aliases/bool_type_alias (0.00s) conversion_test.go:1522: expected no error, got conversion issue: unsupported type from safecast_test.BoolTypeAlias --- FAIL: TestConvert/aliases/type_alias_overflows_for_int8 (0.00s) conversion_test.go:1513: unexpected error got conversion issue: unsupported type from safecast_test.UintTypeAlias, expected maximum value for this type exceeded --- FAIL: TestConvert/aliases/integer_type_alias (0.00s) conversion_test.go:1522: expected no error, got conversion issue: unsupported type from safecast_test.UintTypeAlias
1 parent 52a0277 commit c4da65e

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Diff for: conversion_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,7 @@ type MapTest[TypeInput safecast.Input, TypeOutput safecast.Number] struct {
14931493
Input TypeInput
14941494
ExpectedOutput TypeOutput
14951495
ExpectedError error
1496+
ErrorContains string
14961497
}
14971498

14981499
func (mt MapTest[I, O]) TestConvert(t *testing.T) {
@@ -1510,6 +1511,11 @@ func (mt MapTest[I, O]) TestConvert(t *testing.T) {
15101511
if mt.ExpectedError != nil {
15111512
requireErrorIs(t, err, safecast.ErrConversionIssue)
15121513
requireErrorIs(t, err, mt.ExpectedError)
1514+
1515+
if mt.ErrorContains != "" {
1516+
requireErrorContains(t, err, mt.ErrorContains)
1517+
}
1518+
15131519
return
15141520
}
15151521

@@ -1924,6 +1930,78 @@ func TestConvert(t *testing.T) {
19241930
c.TestConvert(t)
19251931
})
19261932
}
1933+
1934+
t.Run("aliases", func(t *testing.T) {
1935+
// Type aliases are handled separately
1936+
1937+
type (
1938+
// UintSimpleAlias is an alias
1939+
UintSimpleAlias = uint
1940+
1941+
// UintTypeAlias is a type alias
1942+
UintTypeAlias uint
1943+
1944+
// StringAlias is an alias
1945+
StringAlias = string
1946+
1947+
// StringTypeAlias is a type alias
1948+
StringTypeAlias string
1949+
1950+
// BoolAlias is an alias
1951+
BoolAlias = bool
1952+
1953+
// BoolTypeAlias is a type alias
1954+
BoolTypeAlias bool
1955+
)
1956+
1957+
for name, c := range map[string]TestableConvert{
1958+
"integer simple alias": MapTest[UintSimpleAlias, int8]{
1959+
Input: UintSimpleAlias(42),
1960+
ExpectedOutput: int8(42),
1961+
},
1962+
1963+
"integer type alias": MapTest[UintTypeAlias, int8]{
1964+
Input: UintTypeAlias(42),
1965+
ExpectedOutput: int8(42),
1966+
},
1967+
1968+
"string simple alias": MapTest[StringAlias, int8]{
1969+
Input: StringAlias("42"),
1970+
ExpectedOutput: int8(42),
1971+
},
1972+
1973+
"string type alias": MapTest[StringTypeAlias, int8]{
1974+
Input: StringTypeAlias("42"),
1975+
ExpectedOutput: int8(42),
1976+
},
1977+
1978+
"bool simple alias": MapTest[BoolAlias, int8]{
1979+
Input: BoolAlias(true),
1980+
ExpectedOutput: int8(1),
1981+
},
1982+
1983+
"bool type alias": MapTest[BoolTypeAlias, int8]{
1984+
Input: BoolTypeAlias(true),
1985+
ExpectedOutput: int8(1),
1986+
},
1987+
1988+
"simple alias overflows for int8": MapTest[UintSimpleAlias, int8]{
1989+
Input: UintSimpleAlias(255),
1990+
ExpectedError: safecast.ErrExceedMaximumValue,
1991+
ErrorContains: "255 (uint) is greater than 127 (int8)",
1992+
},
1993+
1994+
"type alias overflows for int8": MapTest[UintTypeAlias, int8]{
1995+
Input: UintTypeAlias(255),
1996+
ExpectedError: safecast.ErrExceedMaximumValue,
1997+
ErrorContains: "255 (uint) is greater than 127 (int8)",
1998+
},
1999+
} {
2000+
t.Run(name, func(t *testing.T) {
2001+
c.TestConvert(t)
2002+
})
2003+
}
2004+
})
19272005
}
19282006

19292007
type MapMustConvertTest[TypeInput safecast.Input, TypeOutput safecast.Number] struct {

0 commit comments

Comments
 (0)