59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package tokenize_test
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit/tokenize"
|
|
)
|
|
|
|
func ExampleToken() {
|
|
t0 := tokenize.Token{}
|
|
|
|
t1 := tokenize.Token{
|
|
Type: "Number",
|
|
Value: 224,
|
|
}
|
|
|
|
const TName = 1
|
|
|
|
t2 := tokenize.Token{
|
|
Type: TName,
|
|
Value: "John",
|
|
}
|
|
|
|
t3 := tokenize.Token{
|
|
Value: 42,
|
|
}
|
|
|
|
fmt.Printf("%s\n%s\n%s\n%s\n", t0, t1, t2, t3)
|
|
|
|
// Result: [ip("0.0.0.0") mask((int8)0)]
|
|
// Result: [ip("192.168.0.1") mask((int8)24)]
|
|
// Result: [ip("255.255.255.255") mask((int8)32)]
|
|
// Error: mismatch at start of file
|
|
// Error: mismatch at start of file
|
|
}
|
|
|
|
func TestSetResult_AcceptsVariousTypesAsInput(t *testing.T) {
|
|
i := tokenize.NewAPI(strings.NewReader("Testing"))
|
|
i.Result().SetRunes("string")
|
|
AssertEqual(t, "string", string(i.Result().String()), "i.Result() with string input")
|
|
i.Result().SetRunes([]rune("rune slice"))
|
|
AssertEqual(t, "rune slice", string(i.Result().String()), "i.Result() with rune slice input")
|
|
i.Result().SetRunes('X')
|
|
AssertEqual(t, "X", string(i.Result().String()), "i.Result() with rune input")
|
|
}
|
|
|
|
func TestSetResult_PanicsOnUnhandledInput(t *testing.T) {
|
|
AssertPanic(t, PanicT{
|
|
Function: func() {
|
|
i := tokenize.NewAPI(strings.NewReader("Testing"))
|
|
i.Result().SetRunes(1234567)
|
|
},
|
|
Regexp: true,
|
|
Expect: `tokenize\.Result\.SetRunes\(\): unsupported type 'int' used at /.*/result_test.go:\d+`,
|
|
})
|
|
}
|