Skip to content

Commit bc0ce17

Browse files
committed
Refactor TestParse_error tests
1 parent e750878 commit bc0ce17

File tree

1 file changed

+57
-101
lines changed

1 file changed

+57
-101
lines changed

parser/parser_test.go

Lines changed: 57 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -717,131 +717,87 @@ world`},
717717
}
718718
}
719719

720-
const errorTests = `
721-
foo.
722-
unexpected end of expression (1:4)
720+
func TestParse_error(t *testing.T) {
721+
var tests = []struct {
722+
input string
723+
err string
724+
}{
725+
{`foo.`, `unexpected end of expression (1:4)
723726
| foo.
724-
| ...^
725-
726-
a+
727-
unexpected token EOF (1:2)
727+
| ...^`},
728+
{`a+`, `unexpected token EOF (1:2)
728729
| a+
729-
| .^
730-
731-
a ? (1+2) c
732-
unexpected token Identifier("c") (1:11)
730+
| .^`},
731+
{`a ? (1+2) c`, `unexpected token Identifier("c") (1:11)
733732
| a ? (1+2) c
734-
| ..........^
735-
736-
[a b]
737-
unexpected token Identifier("b") (1:4)
733+
| ..........^`},
734+
{`[a b]`, `unexpected token Identifier("b") (1:4)
738735
| [a b]
739-
| ...^
740-
741-
foo.bar(a b)
742-
unexpected token Identifier("b") (1:11)
736+
| ...^`},
737+
{`foo.bar(a b)`, `unexpected token Identifier("b") (1:11)
743738
| foo.bar(a b)
744-
| ..........^
745-
746-
{-}
747-
a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator("-")) (1:2)
739+
| ..........^`},
740+
{`{-}`, `a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator("-")) (1:2)
748741
| {-}
749-
| .^
750-
751-
foo({.bar})
752-
a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator(".")) (1:6)
742+
| .^`},
743+
{`foo({.bar})`, `a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator(".")) (1:6)
753744
| foo({.bar})
754-
| .....^
755-
756-
.foo
757-
cannot use pointer accessor outside predicate (1:1)
745+
| .....^`},
746+
{`.foo`, `cannot use pointer accessor outside predicate (1:1)
758747
| .foo
759-
| ^
760-
761-
[1, 2, 3,,]
762-
unexpected token Operator(",") (1:10)
748+
| ^`},
749+
{`[1, 2, 3,,]`, `unexpected token Operator(",") (1:10)
763750
| [1, 2, 3,,]
764-
| .........^
765-
766-
[,]
767-
unexpected token Operator(",") (1:2)
751+
| .........^`},
752+
{`[,]`, `unexpected token Operator(",") (1:2)
768753
| [,]
769-
| .^
770-
771-
{,}
772-
a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator(",")) (1:2)
754+
| .^`},
755+
{`{,}`, `a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator(",")) (1:2)
773756
| {,}
774-
| .^
775-
776-
{foo:1, bar:2, ,}
777-
unexpected token Operator(",") (1:16)
757+
| .^`},
758+
{`{foo:1, bar:2, ,}`, `unexpected token Operator(",") (1:16)
778759
| {foo:1, bar:2, ,}
779-
| ...............^
780-
781-
foo ?? bar || baz
782-
Operator (||) and coalesce expressions (??) cannot be mixed. Wrap either by parentheses. (1:12)
760+
| ...............^`},
761+
{`foo ?? bar || baz`, `Operator (||) and coalesce expressions (??) cannot be mixed. Wrap either by parentheses. (1:12)
783762
| foo ?? bar || baz
784-
| ...........^
785-
786-
0b15
787-
bad number syntax: "0b15" (1:4)
763+
| ...........^`},
764+
{`0b15`, `bad number syntax: "0b15" (1:4)
788765
| 0b15
789-
| ...^
790-
791-
0X10G
792-
bad number syntax: "0X10G" (1:5)
766+
| ...^`},
767+
{`0X10G`, `bad number syntax: "0X10G" (1:5)
793768
| 0X10G
794-
| ....^
795-
796-
0o1E
797-
invalid float literal: strconv.ParseFloat: parsing "0o1E": invalid syntax (1:4)
769+
| ....^`},
770+
{`0o1E`, `invalid float literal: strconv.ParseFloat: parsing "0o1E": invalid syntax (1:4)
798771
| 0o1E
799-
| ...^
800-
801-
0b1E
802-
invalid float literal: strconv.ParseFloat: parsing "0b1E": invalid syntax (1:4)
772+
| ...^`},
773+
{`0b1E`, `invalid float literal: strconv.ParseFloat: parsing "0b1E": invalid syntax (1:4)
803774
| 0b1E
804-
| ...^
805-
806-
0b1E+6
807-
bad number syntax: "0b1E+6" (1:6)
775+
| ...^`},
776+
{`0b1E+6`, `bad number syntax: "0b1E+6" (1:6)
808777
| 0b1E+6
809-
| .....^
810-
811-
0b1E+1
812-
invalid float literal: strconv.ParseFloat: parsing "0b1E+1": invalid syntax (1:6)
778+
| .....^`},
779+
{`0b1E+1`, `invalid float literal: strconv.ParseFloat: parsing "0b1E+1": invalid syntax (1:6)
813780
| 0b1E+1
814-
| .....^
815-
816-
0o1E+1
817-
invalid float literal: strconv.ParseFloat: parsing "0o1E+1": invalid syntax (1:6)
781+
| .....^`},
782+
{`0o1E+1`, `invalid float literal: strconv.ParseFloat: parsing "0o1E+1": invalid syntax (1:6)
818783
| 0o1E+1
819-
| .....^
820-
821-
1E
822-
invalid float literal: strconv.ParseFloat: parsing "1E": invalid syntax (1:2)
784+
| .....^`},
785+
{`1E`, `invalid float literal: strconv.ParseFloat: parsing "1E": invalid syntax (1:2)
823786
| 1E
824-
| .^
825-
826-
1 not == [1, 2, 5]
827-
unexpected token Operator("==") (1:7)
787+
| .^`},
788+
{`1 not == [1, 2, 5]`, `unexpected token Operator("==") (1:7)
828789
| 1 not == [1, 2, 5]
829-
| ......^
830-
`
790+
| ......^`},
791+
}
831792

832-
func TestParse_error(t *testing.T) {
833-
tests := strings.Split(strings.Trim(errorTests, "\n"), "\n\n")
834793
for _, test := range tests {
835-
input := strings.SplitN(test, "\n", 2)
836-
if len(input) != 2 {
837-
t.Errorf("syntax error in test: %q", test)
838-
break
839-
}
840-
_, err := parser.Parse(input[0])
841-
if err == nil {
842-
err = fmt.Errorf("<nil>")
843-
}
844-
assert.Equal(t, input[1], err.Error(), input[0])
794+
t.Run(test.input, func(t *testing.T) {
795+
_, err := parser.Parse(test.input)
796+
if err == nil {
797+
err = fmt.Errorf("<nil>")
798+
}
799+
assert.Equal(t, test.err, err.Error(), test.input)
800+
})
845801
}
846802
}
847803

0 commit comments

Comments
 (0)