Skip to content

Commit

Permalink
#234 Made operator join work.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhang6222 committed May 12, 2020
1 parent 605ad83 commit ba370be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions syntax/std_seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ var (
return nil
},
// API join
typeMethod{reflect.TypeOf(rel.String{}), "join"}: func(args ...rel.Value) rel.Value {
typeMethod{reflect.TypeOf(rel.GenericSet{}), "join"}: func(args ...rel.Value) rel.Value {
strs := args[0].(rel.Set)
toJoin := make([]string, 0, strs.Count())
for i, ok := strs.(rel.Set).ArrayEnumerator(); ok && i.MoveNext(); {
Expand All @@ -162,7 +162,12 @@ var (
return rel.NewString([]rune(strings.Join(toJoin, mustAsString(args[1]))))
},
typeMethod{reflect.TypeOf(rel.Array{}), "join"}: func(args ...rel.Value) rel.Value {
return nil
strs := args[0].(rel.Set)
toJoin := make([]string, 0, strs.Count())
for i, ok := strs.(rel.Set).ArrayEnumerator(); ok && i.MoveNext(); {
toJoin = append(toJoin, mustAsString(i.Current()))
}
return rel.NewString([]rune(strings.Join(toJoin, mustAsString(args[1]))))
},
typeMethod{reflect.TypeOf(rel.Bytes{}), "join"}: func(args ...rel.Value) rel.Value {
return nil
Expand Down
10 changes: 5 additions & 5 deletions syntax/std_seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func TestStrSplit(t *testing.T) {

func TestStrJoin(t *testing.T) {
t.Parallel()
// AssertCodesEvalToSameValue(t, `"" `, `//seq.join([], ",") `)
// AssertCodesEvalToSameValue(t, `",," `, `//seq.join(["", "", ""], ",") `)
// AssertCodesEvalToSameValue(t, `"this is a test" `, `//seq.join(["this", "is", "a", "test"], " ")`)
// AssertCodesEvalToSameValue(t, `"this" `, `//seq.join(["this"], ",") `)
// assertExprPanics(t, `//seq.join("this", 2)`)
AssertCodesEvalToSameValue(t, `"" `, `//seq.join([], ",") `)
AssertCodesEvalToSameValue(t, `",," `, `//seq.join(["", "", ""], ",") `)
AssertCodesEvalToSameValue(t, `"this is a test" `, `//seq.join(["this", "is", "a", "test"], " ")`)
AssertCodesEvalToSameValue(t, `"this" `, `//seq.join(["this"], ",") `)
assertExprPanics(t, `//seq.join("this", 2)`)
}

0 comments on commit ba370be

Please sign in to comment.