100 lines
4.2 KiB
Go
100 lines
4.2 KiB
Go
package stringy
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
type tostringTestcase struct {
|
|
input interface{}
|
|
expected string
|
|
}
|
|
|
|
var toStringTests = []tostringTestcase{
|
|
// strings
|
|
{input: "", expected: ""},
|
|
{input: "Hello world", expected: "Hello world"},
|
|
// int
|
|
{input: int(-1), expected: "-0000000000000000001"},
|
|
{input: int8(-1), expected: "-0000000000000000001"},
|
|
{input: int16(-1), expected: "-0000000000000000001"},
|
|
{input: int32(-1), expected: "-0000000000000000001"},
|
|
{input: int64(-1), expected: "-0000000000000000001"},
|
|
{input: int(0), expected: "+0000000000000000000"},
|
|
{input: int8(0), expected: "+0000000000000000000"},
|
|
{input: int16(0), expected: "+0000000000000000000"},
|
|
{input: int32(0), expected: "+0000000000000000000"},
|
|
{input: int64(0), expected: "+0000000000000000000"},
|
|
{input: int(1), expected: "+0000000000000000001"},
|
|
{input: int8(1), expected: "+0000000000000000001"},
|
|
{input: int16(1), expected: "+0000000000000000001"},
|
|
{input: int32(1), expected: "+0000000000000000001"},
|
|
{input: int64(1), expected: "+0000000000000000001"},
|
|
|
|
{input: int8(math.MinInt8), expected: "-0000000000000000128"},
|
|
{input: int16(math.MinInt8), expected: "-0000000000000000128"},
|
|
{input: int32(math.MinInt8), expected: "-0000000000000000128"},
|
|
{input: int64(math.MinInt8), expected: "-0000000000000000128"},
|
|
{input: int16(math.MinInt16), expected: "-0000000000000032768"},
|
|
{input: int32(math.MinInt16), expected: "-0000000000000032768"},
|
|
{input: int64(math.MinInt16), expected: "-0000000000000032768"},
|
|
{input: int32(math.MinInt32), expected: "-0000000002147483648"},
|
|
{input: int64(math.MinInt32), expected: "-0000000002147483648"},
|
|
{input: int64(math.MinInt64), expected: "-9223372036854775808"},
|
|
|
|
{input: int8(math.MaxInt8), expected: "+0000000000000000127"},
|
|
{input: int16(math.MaxInt8), expected: "+0000000000000000127"},
|
|
{input: int32(math.MaxInt8), expected: "+0000000000000000127"},
|
|
{input: int64(math.MaxInt8), expected: "+0000000000000000127"},
|
|
{input: int16(math.MaxInt16), expected: "+0000000000000032767"},
|
|
{input: int32(math.MaxInt16), expected: "+0000000000000032767"},
|
|
{input: int64(math.MaxInt16), expected: "+0000000000000032767"},
|
|
{input: int32(math.MaxInt32), expected: "+0000000002147483647"},
|
|
{input: int64(math.MaxInt32), expected: "+0000000002147483647"},
|
|
{input: int64(math.MaxInt64), expected: "+9223372036854775807"},
|
|
|
|
// uint
|
|
{input: uint(0), expected: "00000000000000000000"},
|
|
{input: uint8(0), expected: "00000000000000000000"},
|
|
{input: uint16(0), expected: "00000000000000000000"},
|
|
{input: uint32(0), expected: "00000000000000000000"},
|
|
{input: uint64(0), expected: "00000000000000000000"},
|
|
{input: uint(1), expected: "00000000000000000001"},
|
|
{input: uint8(1), expected: "00000000000000000001"},
|
|
{input: uint16(1), expected: "00000000000000000001"},
|
|
{input: uint32(1), expected: "00000000000000000001"},
|
|
{input: uint64(1), expected: "00000000000000000001"},
|
|
|
|
{input: uint8(math.MaxUint8), expected: "00000000000000000255"},
|
|
{input: uint16(math.MaxUint8), expected: "00000000000000000255"},
|
|
{input: uint32(math.MaxUint8), expected: "00000000000000000255"},
|
|
{input: uint64(math.MaxUint8), expected: "00000000000000000255"},
|
|
{input: uint16(math.MaxUint16), expected: "00000000000000065535"},
|
|
{input: uint32(math.MaxUint16), expected: "00000000000000065535"},
|
|
{input: uint64(math.MaxUint16), expected: "00000000000000065535"},
|
|
{input: uint32(math.MaxUint32), expected: "00000000004294967295"},
|
|
{input: uint64(math.MaxUint32), expected: "00000000004294967295"},
|
|
{input: uint64(math.MaxUint64), expected: "18446744073709551615"},
|
|
|
|
// slices
|
|
{input: []int{}, expected: "[]"},
|
|
{input: []int{0, 1}, expected: "[+0000000000000000000,+0000000000000000001]"},
|
|
{input: []string{"once", "upon", "a", "midnight", "dreary"}, expected: "[once,upon,a,midnight,dreary]"},
|
|
|
|
// bytes (get base64 encoded)
|
|
{input: []byte("hi"), expected: "aGk"},
|
|
{input: [][]byte{[]byte("hi")}, expected: "[aGk]"},
|
|
}
|
|
|
|
func TestToString(t *testing.T) {
|
|
for _, tc := range toStringTests {
|
|
actual, err := ToString(tc.input)
|
|
if err != nil {
|
|
t.Errorf("Did not expect string conversion of %#v to give error, got %s", tc.input, err)
|
|
}
|
|
if actual != tc.expected {
|
|
t.Errorf("Expected %#v to convert to '%s', got '%s'", tc.input, tc.expected, actual)
|
|
}
|
|
}
|
|
}
|