github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/parse/read/int_test.go (about) 1 package read_test 2 3 import ( 4 "testing" 5 "github.com/v2pro/plz/test" 6 "github.com/v2pro/plz/countlog" 7 "github.com/v2pro/plz/test/must" 8 "io" 9 "strings" 10 "github.com/v2pro/plz/parse" 11 "github.com/v2pro/plz/parse/read" 12 ) 13 14 func Test_ConsumeUint64_from_string(t *testing.T) { 15 type TestCase struct { 16 Input string 17 Output uint64 18 } 19 testCases := []TestCase{{ 20 "1", 1, 21 }, { 22 "12", 12, 23 }, { 24 "123", 123, 25 }} 26 for _, tmp := range testCases { 27 testCase := tmp 28 t.Run(testCase.Input, test.Case(func(ctx *countlog.Context) { 29 src := parse.NewSourceString(testCase.Input) 30 must.Equal(testCase.Output, read.Uint64(src)) 31 })) 32 } 33 t.Run("Overflow", test.Case(func(ctx *countlog.Context) { 34 src := parse.NewSourceString("18446744073709551615") 35 must.Equal(uint64(18446744073709551615), read.Uint64(src)) 36 must.Equal(io.EOF, src.Error()) 37 src = parse.NewSourceString("18446744073709551616") 38 must.Equal(uint64(0), read.Uint64(src)) 39 must.NotNil(src.Error()) 40 must.Pass(io.EOF != src.Error()) 41 })) 42 } 43 44 func Test_ConsumeUint64_from_reader(t *testing.T) { 45 type TestCase struct { 46 Input string 47 Output uint64 48 Selected bool 49 } 50 testCases := []TestCase{{ 51 Input: "1", Output: 1, 52 }, { 53 Input: "12", Output: 12, 54 }, { 55 Input: "123", Output: 123, 56 }, { 57 Input: "1234", Output: 1234, Selected: true, 58 }, { 59 Input: "12345", Output: 12345, 60 }} 61 for _, testCase := range testCases { 62 if testCase.Selected { 63 testCases = []TestCase{testCase} 64 break 65 } 66 } 67 for _, tmp := range testCases { 68 testCase := tmp 69 t.Run(testCase.Input, test.Case(func(ctx *countlog.Context) { 70 src := must.Call(parse.NewSource, 71 strings.NewReader(testCase.Input), make([]byte, 2))[0].(*parse.Source) 72 must.Equal(testCase.Output, read.Uint64(src)) 73 })) 74 } 75 }