-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
package charm: improve error reporting
removes "EndpointError" and "Parse" in favor of a Parser object that can be interrogated for status and errors. the package level ParseEof method still exists, but its error reporting is improved.
- Loading branch information
Showing
7 changed files
with
155 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
package charm | ||
package charm_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ionous/tell/charm" | ||
) | ||
|
||
func TestRequires(t *testing.T) { | ||
isSpace := func(r rune) bool { return r == ' ' } | ||
|
||
// index of the fail point, or -1 if success is expected | ||
count := func(failPoint int, str string, style State) (err error) { | ||
var ep EndpointError | ||
if e := ParseEof(str, style); e == nil && failPoint != -1 { | ||
count := func(failPoint int, str string, style charm.State) (err error) { | ||
p := charm.MakeParser(strings.NewReader(str)) | ||
if e := p.ParseEof(style); e == nil && failPoint != -1 { | ||
err = errors.New("unexpected success") | ||
} else if !errors.As(e, &ep) { | ||
err = e | ||
} else if at := ep.End(); at != failPoint { | ||
} else if failPoint == -1 { | ||
err = e // expected success; if err is not nil caller will fail. | ||
} else if at := p.Offset(); at != failPoint { | ||
// 0 means okay, -1 incomplete, >0 the one-index of the failure point. | ||
err = fmt.Errorf("%s len: %d", str, at) | ||
} | ||
return | ||
} | ||
if e := count(0, "a", AtleastOne(isSpace)); e != nil { | ||
if e := count(0, "a", charm.AtleastOne(isSpace)); e != nil { | ||
t.Fatal(e) | ||
} | ||
if e := count(0, "a", Optional(isSpace)); e != nil { | ||
if e := count(0, "a", charm.Optional(isSpace)); e != nil { | ||
t.Fatal(e) | ||
} | ||
if e := count(-1, strings.Repeat(" ", 5), Optional(isSpace)); e != nil { | ||
if e := count(-1, strings.Repeat(" ", 5), charm.Optional(isSpace)); e != nil { | ||
t.Fatal(e) | ||
} | ||
if e := count(3, strings.Repeat(" ", 3)+"x", Optional(isSpace)); e != nil { | ||
if e := count(3, strings.Repeat(" ", 3)+"x", charm.Optional(isSpace)); e != nil { | ||
t.Fatal(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.