...

Source file src/github.com/go-kivik/kivik/v4/x/collate/collate_test.go

Documentation: github.com/go-kivik/kivik/v4/x/collate

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  // GopherJS does not work with go-cmp diffs
    14  //go:build !js
    15  
    16  package collate
    17  
    18  import (
    19  	"math/rand"
    20  	"sort"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestCompareString(t *testing.T) {
    27  	want := []string{
    28  		// "\"`\"", `"^"`, // TODO: These don't sort according to CouchDB rules
    29  		`"_"`, `"-"`, `","`, `";"`, `":"`, `"!"`, `"?"`,
    30  		`"."`, `"'"`, `"""`, `"("`, `")"`, `"["`, `"]"`, `"{"`, `"}"`,
    31  		`"@"`, `"*"`, `"/"`, `"\"`, `"&"`, `"#"`, `"%"`, `"+"`, `"<"`,
    32  		`"="`, `">"`, `"|"`, `"~"`, `"$"`, `"0"`, `"1"`, `"2"`, `"3"`,
    33  		`"4"`, `"5"`, `"6"`, `"7"`, `"8"`, `"9"`,
    34  		`"a"`, `"A"`, `"b"`, `"B"`, `"c"`, `"C"`, `"d"`, `"D"`, `"e"`,
    35  		`"E"`, `"f"`, `"F"`, `"g"`, `"G"`, `"h"`, `"H"`, `"i"`, `"I"`,
    36  		`"j"`, `"J"`, `"k"`, `"K"`, `"l"`, `"L"`, `"m"`, `"M"`, `"n"`,
    37  		`"N"`, `"o"`, `"O"`, `"p"`, `"P"`, `"q"`, `"Q"`, `"r"`, `"R"`,
    38  		`"s"`, `"S"`, `"t"`, `"T"`, `"u"`, `"U"`, `"v"`, `"V"`, `"w"`,
    39  		`"W"`, `"x"`, `"X"`, `"y"`, `"Y"`, `"z"`, `"Z"`,
    40  	}
    41  	input := make([]string, len(want))
    42  	copy(input, want)
    43  	// Shuffle the input
    44  	rand.Shuffle(len(input), func(i, j int) { input[i], input[j] = input[j], input[i] })
    45  
    46  	sort.Slice(input, func(i, j int) bool {
    47  		return CompareString(input[i], input[j]) < 0
    48  	})
    49  
    50  	if d := cmp.Diff(want, input); d != "" {
    51  		t.Errorf("Unexpected result:\n%s", d)
    52  	}
    53  }
    54  
    55  func TestCompareObject(t *testing.T) {
    56  	want := []interface{}{
    57  		nil,
    58  		false,
    59  		true,
    60  
    61  		// then numbers
    62  		float64(1),
    63  		float64(2),
    64  		float64(3.0),
    65  		float64(4),
    66  
    67  		// then text, case sensitive
    68  		"a",
    69  		"A",
    70  		"aa",
    71  		"b",
    72  		"B",
    73  		"ba",
    74  		"bb",
    75  
    76  		// then arrays. compared element by element until different.
    77  		// Longer arrays sort after their prefixes
    78  		[]interface{}{"a"},
    79  		[]interface{}{"b"},
    80  		[]interface{}{"b", "c"},
    81  		[]interface{}{"b", "c", "a"},
    82  		[]interface{}{"b", "d"},
    83  		[]interface{}{"b", "d", "e"},
    84  
    85  		// then object, compares each key value in the list until different.
    86  		// larger objects sort after their subset objects.
    87  		map[string]interface{}{"a": float64(1)},
    88  		map[string]interface{}{"a": float64(2)},
    89  		map[string]interface{}{"b": float64(1)},
    90  		map[string]interface{}{"b": float64(2)},
    91  		// TODO: See #952
    92  		// map[string]interface{}{"b": float64(2), "a": float64(1)}, // Member order does matter for collation. CouchDB preserves member order but doesn't require that clients will. this test might fail if used with a js engine that doesn't preserve order.
    93  		map[string]interface{}{"b": float64(2), "c": float64(2)},
    94  	}
    95  
    96  	input := make([]interface{}, len(want))
    97  	copy(input, want)
    98  	// Shuffle the input
    99  	rand.Shuffle(len(input), func(i, j int) { input[i], input[j] = input[j], input[i] })
   100  
   101  	sort.Slice(input, func(i, j int) bool {
   102  		return CompareObject(input[i], input[j]) < 0
   103  	})
   104  
   105  	if d := cmp.Diff(want, input); d != "" {
   106  		t.Errorf("Unexpected result:\n%s", d)
   107  	}
   108  }
   109  

View as plain text