108 lines
4.3 KiB
Go
108 lines
4.3 KiB
Go
package toml
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestInteger(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{``, []string{`Error: unexpected end of file (expected a number) at start of file`}},
|
|
// Decimal
|
|
{`0`, []string{`0`}},
|
|
{`+0`, []string{`0`}},
|
|
{`-0`, []string{`0`}},
|
|
{`1`, []string{`1`}},
|
|
{`42`, []string{`42`}},
|
|
{`+99`, []string{`99`}},
|
|
{`-17`, []string{`-17`}},
|
|
{`1234`, []string{`1234`}},
|
|
{`_`, []string{`Error: unexpected input (expected a number) at start of file`}},
|
|
{`1_`, []string{`1`, `Error: unexpected input (expected end of file) at line 1, column 2`}},
|
|
{`1_000`, []string{`1000`}},
|
|
{`5_349_221`, []string{`5349221`}},
|
|
{`1_2_3_4_5`, []string{`12345`}},
|
|
{`9_223_372_036_854_775_807`, []string{`9223372036854775807`}},
|
|
{`9_223_372_036_854_775_808`, []string{
|
|
`Panic: Handler error: MakeInt64Token cannot handle input "9223372036854775808": ` +
|
|
`strconv.ParseInt: parsing "9223372036854775808": value out of range (only use a ` +
|
|
`type conversion token maker, when the input has been validated on beforehand)`}},
|
|
{`-9_223_372_036_854_775_808`, []string{`-9223372036854775808`}},
|
|
// TODO make the use of the same kind of handling for panics and for errors between parsekit and TOML.
|
|
{`-9_223_372_036_854_775_809`, []string{
|
|
`Panic: Handler error: MakeInt64Token cannot handle input "-9223372036854775809": ` +
|
|
`strconv.ParseInt: parsing "-9223372036854775809": value out of range (only use a ` +
|
|
`type conversion token maker, when the input has been validated on beforehand)`}},
|
|
// Hexadecimal
|
|
{`0x0`, []string{`0`}},
|
|
{`0x1`, []string{`1`}},
|
|
{`0x01`, []string{`1`}},
|
|
{`0x00fF`, []string{`255`}},
|
|
{`0xf_f`, []string{`255`}},
|
|
{`0x0_0_f_f`, []string{`255`}},
|
|
{`0xdead_beef`, []string{`3735928559`}},
|
|
{`0xgood_beef`, []string{`0`, `Error: unexpected input (expected end of file) at line 1, column 2`}},
|
|
{`0x7FFFFFFFFFFFFFFF`, []string{`9223372036854775807`}},
|
|
{`0x8000000000000000`, []string{
|
|
`Error: Cannot parse value 0x8000000000000000: strconv.ParseInt: parsing "8000000000000000": ` +
|
|
`value out of range at line 1, column 19`}},
|
|
//Octal
|
|
{`0o0`, []string{`0`}},
|
|
{`0o1`, []string{`1`}},
|
|
{`0o01`, []string{`1`}},
|
|
{`0o10`, []string{`8`}},
|
|
{`0o1_6`, []string{`14`}},
|
|
{`0o0_0_1_1_1`, []string{`73`}},
|
|
{`0o9`, []string{`0`, `Error: unexpected input (expected end of file) at line 1, column 2`}},
|
|
{`0o777777777777777777777`, []string{`9223372036854775807`}},
|
|
{`0o1000000000000000000000`, []string{
|
|
`Error: Cannot parse value 0o1000000000000000000000: strconv.ParseInt: parsing "1000000000000000000000": ` +
|
|
`value out of range at line 1, column 25`}},
|
|
// Binary
|
|
{`0b0`, []string{`0`}},
|
|
{`0b1`, []string{`1`}},
|
|
{`0b01`, []string{`1`}},
|
|
{`0b10`, []string{`2`}},
|
|
{`0b0100`, []string{`4`}},
|
|
{`0b00001000`, []string{`8`}},
|
|
{`0b0001_0000`, []string{`16`}},
|
|
{`0b9`, []string{`0`, `Error: unexpected input (expected end of file) at line 1, column 2`}},
|
|
{`0b1_1_0_1_1`, []string{`27`}},
|
|
{`0b11111111_11111111`, []string{`65535`}},
|
|
{`0b01111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111`, []string{`9223372036854775807`}},
|
|
{`0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000`, []string{
|
|
`Error: Cannot parse value 0b1000000000000000000000000000000000000000000000000000000000000000: ` +
|
|
`strconv.ParseInt: parsing "1000000000000000000000000000000000000000000000000000000000000000": ` +
|
|
`value out of range at line 1, column 74`}},
|
|
} {
|
|
p := newParser()
|
|
testParseHandler(t, p, p.startNumber, test)
|
|
}
|
|
}
|
|
|
|
func TestFloat(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{``, []string{`Error: unexpected end of file (expected a number) at start of file`}},
|
|
{`0.0`, []string{`0`}},
|
|
{`+0.0`, []string{`0`}},
|
|
{`-0.0`, []string{`-0`}},
|
|
{`+1.0`, []string{`1`}},
|
|
{`3.1415`, []string{`3.1415`}},
|
|
{`-0.01`, []string{`-0.01`}},
|
|
{`5e+22`, []string{`5e+22`}},
|
|
{`1E6`, []string{`1e+06`}},
|
|
{`-2E-2`, []string{`-0.02`}},
|
|
{`6.626e-34`, []string{`6.626e-34`}},
|
|
{`224_617.445_991_228`, []string{`224617.445991228`}},
|
|
{`12_345.111_222e+1_2_3`, []string{`1.2345111222e+127`}},
|
|
{`+nan`, []string{`NaN`}},
|
|
{`-nan`, []string{`NaN`}},
|
|
{`nan`, []string{`NaN`}},
|
|
{`inf`, []string{`+Inf`}},
|
|
{`+inf`, []string{`+Inf`}},
|
|
{`-inf`, []string{`-Inf`}},
|
|
} {
|
|
p := newParser()
|
|
testParseHandler(t, p, p.startNumber, test)
|
|
}
|
|
}
|