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") }, "internal Matcher error: NextRune() was called 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") }, "internal Matcher error: Accept() was called 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") }, "internal Matcher error: Skip() was called 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") }, "internal Matcher error: Accept() was called, 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() }, "internal parser error: Cannot call Merge a a non-forked MatchDialog", }) }