Skip to content

Commit

Permalink
go: Fix up Recovery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clarete committed May 7, 2024
1 parent 3776c05 commit b14e34d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions go/tests/import_gr_expr.peg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import Value from "./import_gr_value.peg"

Expr <- Term EOF^eof
Term <- Multi (( '+' / '-' ) Multi^term_right_operand )*
Multi <- Primary (( '*' / '/' ) Primary^multi_right_operand)*
Term <- Multi (( '+' / '-' ) Multi^TermRightOperand )*
Multi <- Primary (( '*' / '/' ) Primary^MultiRightOperand)*
Primary <- Value / '(' Expr ')'
2 changes: 1 addition & 1 deletion go/tests/import_gr_value.peg
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Hexadecimal <- '0x' [a-fA-F0-9]+^LabelHex

// recovery expression for failure on the last subexpression of the
// Hexadecimal rule
LabelHex <- (!Value .)*
LabelHex <- (!' ' .)*
26 changes: 21 additions & 5 deletions go/tests/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ func TestImport(t *testing.T) {
Error string
}{
{
Name: " accept overriden",
Input: "0xLOL",
Error: "LabelHex @ 2",
}, {
Name: "Dont accept overriden",
Input: "3+#xC0FFEE",
Error: "term_right_operand @ 2",
Error: "TermRightOperand @ 2",
},
} {
t.Run(test.Name, func(t *testing.T) {
Expand All @@ -67,4 +63,24 @@ func TestImport(t *testing.T) {
assert.Equal(t, test.Error, err.Error())
})
}

for _, test := range []struct {
Name string
Input string
Expected string
}{
{
Name: "label dependency",
Input: "0xG + 2",
Expected: "0xerror[LabelHex: G] + 2",
},
} {
t.Run(test.Name, func(t *testing.T) {
p := NewImportParser()
p.SetInput(test.Input)
v, err := p.ParseExpr()
require.NoError(t, err)
assert.Equal(t, test.Expected, v.Text())
})
}
}
12 changes: 6 additions & 6 deletions go/tests/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ func TestRecovery(t *testing.T) {
{
Name: "Basic Check Assignment",
Input: "var = 1;",
Expected: `P(Stm(AssignStm(Sequence(Identifier(Sequence("v" @ 0..1, Sequence("a" @ 1..2, "r" @ 2..3) @ 1..3) @ 0..3) @ 0..3, "=" @ 4..5, Expr(Number("1" @ 6..7) @ 6..7) @ 6..7, ";" @ 7..8) @ 7..8) @ 0..8) @ 0..8) @ 0..8`,
Expected: `P(Stm(AssignStm(Sequence(Identifier(Sequence("v" @ 0..1, Sequence("a" @ 1..2, "r" @ 2..3) @ 1..3) @ 0..3) @ 0..3, "=" @ 4..5, Expr(Number("1" @ 6..7) @ 6..7) @ 6..7, ";" @ 7..8) @ 0..8) @ 0..8) @ 0..8) @ 0..8`,
},
{
Name: "Basic Check If Statement",
Input: "if (false) {}",
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, "(" @ 3..4, Expr(Bool("false" @ 4..9) @ 4..9) @ 4..9, ")" @ 9..10, Body(Sequence("{" @ 11..12, "}" @ 12..13) @ 11..13) @ 11..13) @ 11..13) @ 0..13) @ 0..13) @ 0..13`,
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, "(" @ 3..4, Expr(Bool("false" @ 4..9) @ 4..9) @ 4..9, ")" @ 9..10, Body(Sequence("{" @ 11..12, "}" @ 12..13) @ 11..13) @ 11..13) @ 0..13) @ 0..13) @ 0..13) @ 0..13`,
},
{
Name: "Missing Semi Colon in Assignment",
Input: "var = 1",
Expected: `P(Stm(AssignStm(Sequence(Identifier(Sequence("v" @ 0..1, Sequence("a" @ 1..2, "r" @ 2..3) @ 1..3) @ 0..3) @ 0..3, "=" @ 4..5, Expr(Number("1" @ 6..7) @ 6..7) @ 6..7, Error("assignsemi") @ 7) @ 7) @ 0..7) @ 0..7) @ 0..7`,
Expected: `P(Stm(AssignStm(Sequence(Identifier(Sequence("v" @ 0..1, Sequence("a" @ 1..2, "r" @ 2..3) @ 1..3) @ 0..3) @ 0..3, "=" @ 4..5, Expr(Number("1" @ 6..7) @ 6..7) @ 6..7, Error("assignsemi") @ 7) @ 0..7) @ 0..7) @ 0..7) @ 0..7`,
},
{
Name: "Missing Left Parenthesis in If Expression",
Input: "if false) {}",
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, Error("iflpar") @ 3, Expr(Bool("false" @ 3..8) @ 3..8) @ 3..8, ")" @ 8..9, Body(Sequence("{" @ 10..11, "}" @ 11..12) @ 10..12) @ 10..12) @ 10..12) @ 0..12) @ 0..12) @ 0..12`,
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, Error("iflpar") @ 3, Expr(Bool("false" @ 3..8) @ 3..8) @ 3..8, ")" @ 8..9, Body(Sequence("{" @ 10..11, "}" @ 11..12) @ 10..12) @ 10..12) @ 0..12) @ 0..12) @ 0..12) @ 0..12`,
},
{
Name: "Missing If Expression",
Input: "if () {}",
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, "(" @ 3..4, Error("ifexpr") @ 4, ")" @ 4..5, Body(Sequence("{" @ 6..7, "}" @ 7..8) @ 6..8) @ 6..8) @ 6..8) @ 0..8) @ 0..8) @ 0..8`,
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, "(" @ 3..4, Error("ifexpr") @ 4, ")" @ 4..5, Body(Sequence("{" @ 6..7, "}" @ 7..8) @ 6..8) @ 6..8) @ 0..8) @ 0..8) @ 0..8) @ 0..8`,
},
{
Name: "Garbage in If Expression",
Input: "if ($%^) {}",
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, "(" @ 3..4, Error("ifexpr", ifexpr(Sequence("$" @ 4..5, "%" @ 5..6, "^" @ 6..7) @ 4..7) @ 4..7) @ 4..7, ")" @ 7..8, Body(Sequence("{" @ 9..10, "}" @ 10..11) @ 9..11) @ 9..11) @ 9..11) @ 0..11) @ 0..11) @ 0..11`,
Expected: `P(Stm(IfStm(Sequence("if" @ 0..2, "(" @ 3..4, Error("ifexpr", ifexpr(Sequence("$" @ 4..5, "%" @ 5..6, "^" @ 6..7) @ 4..7) @ 4..7) @ 4..7, ")" @ 7..8, Body(Sequence("{" @ 9..10, "}" @ 10..11) @ 9..11) @ 9..11) @ 0..11) @ 0..11) @ 0..11) @ 0..11`,
},
} {
t.Run(test.Name, func(t *testing.T) {
Expand Down

0 comments on commit b14e34d

Please sign in to comment.