go-parsekit/tokenhandler_test.go

81 lines
2.4 KiB
Go

package parsekit_test
import (
"testing"
"git.makaay.nl/mauricem/go-parsekit"
)
func TestWithinTokenHandler_AcceptIncludesAndSkipIgnoresRuneInOutput(t *testing.T) {
parser := parsekit.NewMatcher(func(t *parsekit.TokenAPI) bool {
for i := 0; i < 33; i++ {
t.NextRune()
t.Accept()
t.NextRune()
t.Skip()
}
return true
}, "test")
output, _ := parser.Execute("Txhxixsx xsxhxoxuxlxdx xbxexcxoxmxex xqxuxixtxex xrxexaxdxaxbxlxex")
if output != "This should become quite readable" {
t.Fatalf("Got unexpected output from TokenHandler: %s", output)
}
}
func TestGivenNextRuneCalled_WithoutAcceptOrSkip_NextCallToNextRunePanics(t *testing.T) {
parser := parsekit.NewMatcher(func(t *parsekit.TokenAPI) bool {
t.NextRune()
t.NextRune()
return false
}, "test")
RunPanicTest(t, PanicTest{
func() { parser.Execute("input string") },
`TokenHandler bug: NextRune\(\) was called from .*NextCallToNextRunePanics.* ` +
`at .*/tokenhandler_test\.go:\d+ without accepting or skipping the previously read rune`})
}
func TestGivenNextRuneNotCalled_CallToAcceptPanics(t *testing.T) {
parser := parsekit.NewMatcher(func(t *parsekit.TokenAPI) bool {
t.Accept()
return false
}, "test")
RunPanicTest(t, PanicTest{
func() { parser.Execute("input string") },
`TokenHandler bug: Accept\(\) was called from .*CallToAcceptPanics.* ` +
`at .*/tokenhandler_test\.go:\d+ without a prior call to NextRune\(\)`})
}
func TestGivenNextRuneNotCalled_CallToSkipPanics(t *testing.T) {
parser := parsekit.NewMatcher(func(t *parsekit.TokenAPI) bool {
t.Skip()
return false
}, "test")
RunPanicTest(t, PanicTest{
func() { parser.Execute("input string") },
`TokenHandler bug: Skip\(\) was called from .*CallToSkipPanics.* ` +
`at .*tokenhandler_test\.go:\d+ without a prior call to NextRune\(\)`})
}
func TestGivenNextRuneReturningNotOk_CallToAcceptPanics(t *testing.T) {
parser := parsekit.NewMatcher(func(t *parsekit.TokenAPI) bool {
t.NextRune()
t.Accept()
return false
}, "test")
RunPanicTest(t, PanicTest{
func() { parser.Execute("\xcd") },
`TokenHandler bug: Accept\(\) was called from .*CallToAcceptPanics.* ` +
`at .*tokenhandler_test\.go:\d+, but prior call to NextRune\(\) did not ` +
`return OK \(EOF or invalid rune\)`})
}
func TestGivenRootTokenAPI_CallingMergePanics(t *testing.T) {
RunPanicTest(t, PanicTest{
func() {
a := parsekit.TokenAPI{}
a.Merge()
},
`TokenHandler bug: Cannot call Merge a a non-forked MatchDialog`,
})
}