From ba370bedd3081d269bd15d64dd4cc852e269a7c4 Mon Sep 17 00:00:00 2001 From: Zhang Eric Date: Tue, 12 May 2020 23:13:15 +0800 Subject: [PATCH] #234 Made operator join work. --- syntax/std_seq.go | 9 +++++++-- syntax/std_seq_test.go | 10 +++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/syntax/std_seq.go b/syntax/std_seq.go index 7741e826..eff99ba7 100644 --- a/syntax/std_seq.go +++ b/syntax/std_seq.go @@ -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(); { @@ -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 diff --git a/syntax/std_seq_test.go b/syntax/std_seq_test.go index 0d0a26e9..2a618ecd 100644 --- a/syntax/std_seq_test.go +++ b/syntax/std_seq_test.go @@ -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)`) }