Skip to content

Commit

Permalink
#234 Fixed code conflicts, use new test API AssertCodeErrors.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhang6222 committed May 25, 2020
1 parent a2b1aa3 commit c2bf772
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 30 deletions.
6 changes: 3 additions & 3 deletions syntax/std_seq_contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestStrContains(t *testing.T) {
AssertCodesEvalToSameValue(t, `false`, `//seq.contains("A", "")`)
AssertCodesEvalToSameValue(t, `true`, `//seq.contains("", "")`)

assertExprPanics(t, `//seq.contains(1, "ABC")`)
AssertCodeErrors(t, `//seq.contains(1, "ABC")`, "")
}

func TestArrayContains(t *testing.T) { //nolint:dupl
Expand Down Expand Up @@ -45,8 +45,8 @@ func TestArrayContains(t *testing.T) { //nolint:dupl
AssertCodesEvalToSameValue(t, `true`, `//seq.contains([], [1])`)
AssertCodesEvalToSameValue(t, `true`, `//seq.contains([], [])`)

assertExprPanics(t, `//seq.contains(1, [1,2,3,4,5])`)
assertExprPanics(t, `//seq.contains('A',['A','B','C','D','E'])`)
AssertCodeErrors(t, `//seq.contains(1, [1,2,3,4,5])`, "")
AssertCodeErrors(t, `//seq.contains('A',['A','B','C','D','E'])`, "")
}

func TestBytesContains(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions syntax/std_seq_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestStrJoin(t *testing.T) {
// It is not supported
// AssertCodesEvalToSameValue(t, `""`, `//seq.join("",[])`)

assertExprPanics(t, `//seq.join("this", 2)`)
AssertCodeErrors(t, `//seq.join("this", 2)`, "")
}

func TestArrayJoin(t *testing.T) {
Expand Down Expand Up @@ -51,12 +51,12 @@ func TestArrayJoin(t *testing.T) {
AssertCodesEvalToSameValue(t, `[[1, 2], 3, 4]`, `//seq.join([], [[[1, 2]], [3, 4]])`)
AssertCodesEvalToSameValue(t, `[[1, 2], [3, 4], 5]`, `//seq.join([], [[[1, 2]], [[3,4], 5]])`)

assertExprPanics(t, `//seq.join(1, [1,2,3,4,5])`)
assertExprPanics(t, `//seq.join('A', [1,2])`)
assertExprPanics(t, `//seq.join([],[1,2])`)
assertExprPanics(t, `//seq.join([1],[1,2])`)
assertExprPanics(t, `//seq.join([0], [1,2,3,4,5])`)
assertExprPanics(t, `//seq.join(['A'], ['AT','BB', 'CD'])`)
AssertCodeErrors(t, `//seq.join(1, [1,2,3,4,5])`, "")
AssertCodeErrors(t, `//seq.join('A', [1,2])`, "")
AssertCodeErrors(t, `//seq.join([],[1,2])`, "")
AssertCodeErrors(t, `//seq.join([1],[1,2])`, "")
AssertCodeErrors(t, `//seq.join([0], [1,2,3,4,5])`, "")
AssertCodeErrors(t, `//seq.join(['A'], ['AT','BB', 'CD'])`, "")
}

func TestBytesJoin(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions syntax/std_seq_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestStrPrefix(t *testing.T) {
AssertCodesEvalToSameValue(t, `false`, `//seq.has_prefix("A","")`)
AssertCodesEvalToSameValue(t, `true`, `//seq.has_prefix("","A")`)

assertExprPanics(t, `//seq.has_prefix(1,"ABC")`)
AssertCodeErrors(t, `//seq.has_prefix(1,"ABC")`, "")
}

func TestArrayPrefix(t *testing.T) {
Expand All @@ -41,8 +41,8 @@ func TestArrayPrefix(t *testing.T) {
AssertCodesEvalToSameValue(t, `false`, `//seq.has_prefix(['A','B','C','D','E','F'],[])`)
AssertCodesEvalToSameValue(t, `true`, `//seq.has_prefix([],['A','B','C','D','E'])`)

assertExprPanics(t, `//seq.has_prefix(1,[1,2,3])`)
assertExprPanics(t, `//seq.has_prefix('A',['A','B','C'])`)
AssertCodeErrors(t, `//seq.has_prefix(1,[1,2,3])`, "")
AssertCodeErrors(t, `//seq.has_prefix('A',['A','B','C'])`, "")
}

func TestBytesPrefix(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions syntax/std_seq_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestStrSplit(t *testing.T) {
AssertCodesEvalToSameValue(t, `["this", "is", "a", "test"]`, `//seq.split(" ","this is a test") `)
AssertCodesEvalToSameValue(t, `["this is a test"] `, `//seq.split(",","this is a test") `)
AssertCodesEvalToSameValue(t, `["th", " ", " a test"] `, `//seq.split("is","this is a test")`)
assertExprPanics(t, `//seq.split(1, "this is a test")`)
AssertCodeErrors(t, `//seq.split(1, "this is a test")`, "")

AssertCodesEvalToSameValue(t,
`["t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t"]`,
Expand All @@ -24,7 +24,7 @@ func TestStrSplit(t *testing.T) {

AssertCodesEvalToSameValue(t, `[""]`, `//seq.split(",","") `)

assertExprPanics(t, `//seq.split(1,"ABC")`)
AssertCodeErrors(t, `//seq.split(1,"ABC")`, "")
}

func TestArraySplit(t *testing.T) { //nolint:dupl
Expand Down Expand Up @@ -63,8 +63,8 @@ func TestArraySplit(t *testing.T) { //nolint:dupl
AssertCodesEvalToSameValue(t, `[]`, `//seq.split([],[])`)
AssertCodesEvalToSameValue(t, `[[]]`, `//seq.split(['A'],[])`)

assertExprPanics(t, `//seq.split(1,[1,2,3])`)
assertExprPanics(t, `//seq.split('A',['A','B'])`)
AssertCodeErrors(t, `//seq.split(1,[1,2,3])`, "")
AssertCodeErrors(t, `//seq.split('A',['A','B'])`, "")
}

func TestBytesSplit(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions syntax/std_seq_sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestStrSub(t *testing.T) {
AssertCodesEvalToSameValue(t, `"t1his is not1 a t1est1"`, `//seq.sub("t", "t1","this is not a test")`)
AssertCodesEvalToSameValue(t, `"this is still a test"`,
`//seq.sub( "doesn't matter", "hello there","this is still a test")`)
assertExprPanics(t, `//seq.sub("hello there", "test", 1)`)
AssertCodeErrors(t, `//seq.sub("hello there", "test", 1)`, "")
/////////////////
AssertCodesEvalToSameValue(t, `""`, `//seq.sub( "","", "")`)
AssertCodesEvalToSameValue(t, `"A"`, `//seq.sub( "","A", "")`)
Expand All @@ -20,7 +20,7 @@ func TestStrSub(t *testing.T) {
AssertCodesEvalToSameValue(t, `"EAEBECE"`, `//seq.sub( "", "E","ABC")`)
AssertCodesEvalToSameValue(t, `"BC"`, `//seq.sub( "A", "","ABC")`)

assertExprPanics(t, `//seq.sub(1,'B','BCD')`)
AssertCodeErrors(t, `//seq.sub(1,'B','BCD')`, "")
}

func TestArraySub(t *testing.T) {
Expand All @@ -32,8 +32,8 @@ func TestArraySub(t *testing.T) {
AssertCodesEvalToSameValue(t, `[2, 2, 3]`, `//seq.sub([1], [2], [1, 2, 3])`)
AssertCodesEvalToSameValue(t, `[[1,1], [4,4], [3,3]]`, `//seq.sub([[2,2]], [[4,4]], [[1,1], [2,2], [3,3]])`)

assertExprPanics(t, `//seq.sub(1,'B',[1,2,3])`)
assertExprPanics(t, `//seq.sub(1,'B',['A','B','C'])`)
AssertCodeErrors(t, `//seq.sub(1,'B',[1,2,3])`, "")
AssertCodeErrors(t, `//seq.sub(1,'B',['A','B','C'])`, "")
}

func TestArraySubEdgeCases(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions syntax/std_seq_suffix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestStrSuffix(t *testing.T) {
AssertCodesEvalToSameValue(t, `true`, `//seq.has_suffix("","")`)
AssertCodesEvalToSameValue(t, `true`, `//seq.has_suffix("","ABCDE")`)

assertExprPanics(t, `//seq.has_suffix(1,"ABC")`)
AssertCodeErrors(t, `//seq.has_suffix(1,"ABC")`, "")
}

func TestArraySuffix(t *testing.T) {
Expand All @@ -39,8 +39,8 @@ func TestArraySuffix(t *testing.T) {
AssertCodesEvalToSameValue(t, `true`, `//seq.has_suffix([], [])`)
AssertCodesEvalToSameValue(t, `false`, `//seq.has_suffix(['D','E'],[])`)

assertExprPanics(t, `//seq.has_suffix(1,[1,2])`)
assertExprPanics(t, `//seq.has_suffix('A',['A','B'])`)
AssertCodeErrors(t, `//seq.has_suffix(1,[1,2])`, "")
AssertCodeErrors(t, `//seq.has_suffix('A',['A','B'])`, "")
}

func TestBytesSuffix(t *testing.T) {
Expand Down
6 changes: 0 additions & 6 deletions syntax/std_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package syntax

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStrLower(t *testing.T) {
Expand Down Expand Up @@ -32,7 +30,3 @@ func TestStrTitle(t *testing.T) {
AssertCodesEvalToSameValue(t, `"This Is A Test"`, `//str.title("this is a test")`)
AssertCodeErrors(t, `//str.title(123)`, "")
}

func assertExprPanics(t *testing.T, code string) {
assert.Panics(t, func() { AssertCodesEvalToSameValue(t, `"doesn't matter"`, code) })
}
4 changes: 4 additions & 0 deletions syntax/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package syntax

import (
"errors"
"fmt"
"testing"

"github.com/arr-ai/arrai/rel"
Expand Down Expand Up @@ -84,6 +85,9 @@ func AssertCodeErrors(t *testing.T, code, errString string) bool {
if assert.NoError(t, err, "parsing code: %s", code) {
codeExpr := pc.CompileExpr(ast)
_, err := codeExpr.Eval(rel.EmptyScope)
if err == nil {
panic(fmt.Sprintf("the code `%s` didn't generate any error", code))
}
assert.EqualError(t, errors.New(err.Error()[:len(errString)]), errString)
}
return false
Expand Down

0 comments on commit c2bf772

Please sign in to comment.