107 lines
4.5 KiB
Go
107 lines
4.5 KiB
Go
package toml
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit/parse"
|
|
)
|
|
|
|
func TestStartNumber(t *testing.T) {
|
|
parser := newParser()
|
|
wrapper := func(p *parse.API) { parser.parseNumber(p) }
|
|
testParse(t, parser, wrapper, parseTest{"INVALID", "{}", "unexpected input (expected a number) at start of file"})
|
|
}
|
|
|
|
func TestInteger(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
// Decimal
|
|
{`x=0`, `{"x": 0}`, ``},
|
|
{`x=+0`, `{"x": 0}`, ``},
|
|
{`x=-0`, `{"x": 0}`, ``},
|
|
{`x=1`, `{"x": 1}`, ``},
|
|
{`x=42`, `{"x": 42}`, ``},
|
|
{`x=+99`, `{"x": 99}`, ``},
|
|
{`x=-17`, `{"x": -17}`, ``},
|
|
{`x=1234`, `{"x": 1234}`, ``},
|
|
{`x=_`, `{}`, `unexpected input (expected a value) at line 1, column 3`},
|
|
{`x=1_`, `{"x": 1}`, `unexpected end of file (expected a value assignment) at line 1, column 5`},
|
|
{`x=1_000`, `{"x": 1000}`, ``},
|
|
{`x=5_349_221`, `{"x": 5349221}`, ``},
|
|
{`x=1_2_3_4_5`, `{"x": 12345}`, ``},
|
|
{`x=9_223_372_036_854_775_807`, `{"x": 9223372036854775807}`, ``},
|
|
{`x=9_223_372_036_854_775_808`, `{}`,
|
|
`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)`},
|
|
{`x=-9_223_372_036_854_775_808`, `{"x": -9223372036854775808}`, ``},
|
|
// TODO make the use of the same kind of handling for panics and for errors between parsekit and TOML.
|
|
{`x=-9_223_372_036_854_775_809`, `{}`,
|
|
`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
|
|
{`x=0x0`, `{"x": 0}`, ``},
|
|
{`x=0x1`, `{"x": 1}`, ``},
|
|
{`x=0x01`, `{"x": 1}`, ``},
|
|
{`x=0x00fF`, `{"x": 255}`, ``},
|
|
{`x=0xf_f`, `{"x": 255}`, ``},
|
|
{`x=0x0_0_f_f`, `{"x": 255}`, ``},
|
|
{`x=0xdead_beef`, `{"x": 3735928559}`, ``},
|
|
{`x=0xgood_beef`, `{"x": 0}`, `unexpected end of file (expected a value assignment) at line 1, column 14`},
|
|
{`x=0x7FFFFFFFFFFFFFFF`, `{"x": 9223372036854775807}`, ``},
|
|
{`x=0x8000000000000000`, `{}`, `invalid integer value 0x8000000000000000: strconv.ParseInt: parsing "8000000000000000": value out of range at line 1, column 21`},
|
|
//Octal
|
|
{`x=0o0`, `{"x": 0}`, ``},
|
|
{`x=0o1`, `{"x": 1}`, ``},
|
|
{`x=0o01`, `{"x": 1}`, ``},
|
|
{`x=0o10`, `{"x": 8}`, ``},
|
|
{`x=0o1_6`, `{"x": 14}`, ``},
|
|
{`x=0o0_0_1_1_1`, `{"x": 73}`, ``},
|
|
{`x=0o9`, `{"x": 0}`, `unexpected end of file (expected a value assignment) at line 1, column 6`},
|
|
{`x=0o777777777777777777777`, `{"x": 9223372036854775807}`, ``},
|
|
{`x=0o1000000000000000000000`, `{}`, `invalid integer value 0o1000000000000000000000: strconv.ParseInt: parsing "1000000000000000000000": value out of range at line 1, column 27`},
|
|
// Binary
|
|
{`x=0b0`, `{"x": 0}`, ``},
|
|
{`x=0b1`, `{"x": 1}`, ``},
|
|
{`x=0b01`, `{"x": 1}`, ``},
|
|
{`x=0b10`, `{"x": 2}`, ``},
|
|
{`x=0b0100`, `{"x": 4}`, ``},
|
|
{`x=0b00001000`, `{"x": 8}`, ``},
|
|
{`x=0b0001_0000`, `{"x": 16}`, ``},
|
|
{`x=0b9`, `{"x": 0}`, `unexpected end of file (expected a value assignment) at line 1, column 6`},
|
|
{`x=0b1_1_0_1_1`, `{"x": 27}`, ``},
|
|
{`x=0b11111111_11111111`, `{"x": 65535}`, ``},
|
|
{`x=0b01111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111`, `{"x": 9223372036854775807}`, ``},
|
|
{`x=0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000`, `{}`, `invalid integer value 0b1000000000000000000000000000000000000000000000000000000000000000: strconv.ParseInt: parsing "1000000000000000000000000000000000000000000000000000000000000000": value out of range at line 1, column 76`},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startKeyValuePair, test)
|
|
}
|
|
}
|
|
|
|
func TestFloat(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{`x=0.0`, `{"x": 0}`, ``},
|
|
{`x=+0.0`, `{"x": 0}`, ``},
|
|
{`x=-0.0`, `{"x": -0}`, ``},
|
|
{`x=+1.0`, `{"x": 1}`, ``},
|
|
{`x=3.1415`, `{"x": 3.1415}`, ``},
|
|
{`x=-0.01`, `{"x": -0.01}`, ``},
|
|
{`x=5e+22`, `{"x": 5e+22}`, ``},
|
|
{`x=1E6`, `{"x": 1e+06}`, ``},
|
|
{`x=-2E-2`, `{"x": -0.02}`, ``},
|
|
{`x=6.626e-34`, `{"x": 6.626e-34}`, ``},
|
|
{`x=224_617.445_991_228`, `{"x": 224617.445991228}`, ``},
|
|
{`x=12_345.111_222e+1_2_3`, `{"x": 1.2345111222e+127}`, ``},
|
|
{`x=+nan`, `{"x": NaN}`, ``},
|
|
{`x=-nan`, `{"x": NaN}`, ``},
|
|
{`x=nan`, `{"x": NaN}`, ``},
|
|
{`x=inf`, `{"x": +Inf}`, ``},
|
|
{`x=+inf`, `{"x": +Inf}`, ``},
|
|
{`x=-inf`, `{"x": -Inf}`, ``},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startKeyValuePair, test)
|
|
}
|
|
}
|