From 2ac7a6c6677e513288558ded4dd87145a032388d Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Thu, 26 Jul 2018 19:26:14 +0530 Subject: [PATCH] Refactor string slice helper to common/test Signed-off-by: Anant Prakash --- .../dendrite/common/test/strings.go | 32 +++++++++++++++++++ .../dendrite/roomserver/query/query_test.go | 25 ++------------- 2 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/common/test/strings.go diff --git a/src/github.com/matrix-org/dendrite/common/test/strings.go b/src/github.com/matrix-org/dendrite/common/test/strings.go new file mode 100644 index 000000000..d72e2b5e1 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/common/test/strings.go @@ -0,0 +1,32 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test + +import "sort" + +// StringSliceEqual returns true if the slices have same length & elements. +func StringSliceEqual(a []string, b []string) bool { + if len(a) != len(b) { + return false + } + + sort.Strings(a) + sort.Strings(b) + for i := range a { + if a[i] != b[i] { + return false + } + } + + return true +} diff --git a/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go b/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go index 45e46f67f..b01e00e0e 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go +++ b/src/github.com/matrix-org/dendrite/roomserver/query/query_test.go @@ -19,8 +19,7 @@ import ( "encoding/json" "testing" - "sort" - + "github.com/matrix-org/dendrite/common/test" "github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/gomatrixserverlib" ) @@ -90,24 +89,6 @@ func (db *getEventDB) EventsFromIDs(ctx context.Context, eventIDs []string) (res return } -// Returns if the slices are equal after sorting them. -func compareUnsortedStringSlices(a []string, b []string) bool { - if len(a) != len(b) { - return false - } - - sort.Strings(a) - sort.Strings(b) - - for i := range a { - if a[i] != b[i] { - return false - } - } - - return true -} - func TestGetAuthChainSingle(t *testing.T) { db := createEventDB() @@ -135,7 +116,7 @@ func TestGetAuthChainSingle(t *testing.T) { expectedIDs := []string{"a", "b", "c", "d", "e"} - if !compareUnsortedStringSlices(expectedIDs, returnedIDs) { + if !test.StringSliceEqual(expectedIDs, returnedIDs) { t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs) } } @@ -168,7 +149,7 @@ func TestGetAuthChainMultiple(t *testing.T) { expectedIDs := []string{"a", "b", "c", "d", "e", "f"} - if !compareUnsortedStringSlices(expectedIDs, returnedIDs) { + if !test.StringSliceEqual(expectedIDs, returnedIDs) { t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs) } }