| 1 |
// Copyright (c) 2021, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/test" |
| 15 |
) |
| 16 |
|
| 17 |
func TestCanonStructField(t *testing.T) { |
| 18 |
for _, tst := range []struct{ got, expected string }{ |
| 19 |
{"", ""}, |
| 20 |
{"pipo", "pipo"}, |
| 21 |
{">pipo", ">pipo"}, |
| 22 |
{"> pipo ", ">pipo"}, |
| 23 |
{"123=~.*", "123=~.*"}, |
| 24 |
{" 123 =~ .* ", "123=~.*"}, |
| 25 |
{"&badField", "&badField"}, |
| 26 |
} { |
| 27 |
test.EqualStr(t, canonStructField(tst.got), tst.expected) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestMergeStructFields(t *testing.T) { |
| 32 |
sfs := mergeStructFields() |
| 33 |
if sfs != nil { |
| 34 |
t.Errorf("not nil") |
| 35 |
} |
| 36 |
|
| 37 |
x := StructFields{} |
| 38 |
sfs = mergeStructFields(x) |
| 39 |
if reflect.ValueOf(sfs).Pointer() != reflect.ValueOf(x).Pointer() { |
| 40 |
t.Errorf("not x") |
| 41 |
} |
| 42 |
|
| 43 |
a := StructFields{"pipo": 1} |
| 44 |
b := StructFields{"pipo": 2} |
| 45 |
c := StructFields{"pipo": 3} |
| 46 |
sfs = mergeStructFields(a, b, c) |
| 47 |
if reflect.ValueOf(sfs).Pointer() == reflect.ValueOf(c).Pointer() { |
| 48 |
t.Errorf("is c") |
| 49 |
} |
| 50 |
test.EqualInt(t, len(sfs), 1) |
| 51 |
test.EqualInt(t, sfs["pipo"].(int), 3) |
| 52 |
|
| 53 |
a = StructFields{">pipo": 1} |
| 54 |
b = StructFields{"> pipo": 2} |
| 55 |
c = StructFields{">pipo ": 3} |
| 56 |
sfs = mergeStructFields(a, b, c) |
| 57 |
if reflect.ValueOf(sfs).Pointer() == reflect.ValueOf(c).Pointer() { |
| 58 |
t.Errorf("is c") |
| 59 |
} |
| 60 |
test.EqualInt(t, len(sfs), 1) |
| 61 |
test.EqualInt(t, sfs[">pipo "].(int), 3) |
| 62 |
|
| 63 |
a = StructFields{"1=~pipo": 1} |
| 64 |
b = StructFields{" 1 =~ pipo ": 2} |
| 65 |
c = StructFields{"1\t=~\tpipo": 3} |
| 66 |
sfs = mergeStructFields(a, b, c) |
| 67 |
if reflect.ValueOf(sfs).Pointer() == reflect.ValueOf(c).Pointer() { |
| 68 |
t.Errorf("is c") |
| 69 |
} |
| 70 |
test.EqualInt(t, len(sfs), 1) |
| 71 |
test.EqualInt(t, sfs["1\t=~\tpipo"].(int), 3) |
| 72 |
} |
| 73 |
|
| 74 |
func TestFieldMatcher(t *testing.T) { |
| 75 |
_, err := newFieldMatcher("pipo", 123) |
| 76 |
if test.Error(t, err) { |
| 77 |
if err != errNotAMatcher { |
| 78 |
t.Errorf("got %q, but %q was expected", err, errNotAMatcher) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
for _, tst := range []struct { |
| 83 |
name string |
| 84 |
order int |
| 85 |
match bool |
| 86 |
}{ |
| 87 |
// Regexp |
| 88 |
{name: "=~.*", match: true}, |
| 89 |
{name: "=~bc", match: true}, |
| 90 |
{name: "=~3$", match: true}, |
| 91 |
{name: "!~^b", match: false}, |
| 92 |
{name: "134=~bc", match: true, order: 134}, |
| 93 |
{name: "134 =~ bc", match: true, order: 134}, |
| 94 |
{name: " 134 =~ bc", match: true, order: 134}, |
| 95 |
// Shell pattern |
| 96 |
{name: "=*", match: true}, |
| 97 |
{name: "=*bc*", match: true}, |
| 98 |
{name: "=*3", match: true}, |
| 99 |
{name: "!b*", match: false}, |
| 100 |
{name: "134=*", match: true, order: 134}, |
| 101 |
{name: "134 = *", match: true, order: 134}, |
| 102 |
{name: " 134 = *", match: true, order: 134}, |
| 103 |
} { |
| 104 |
fm, err := newFieldMatcher(tst.name, 123) |
| 105 |
test.NoError(t, err, tst.name) |
| 106 |
test.EqualStr(t, fm.name, tst.name, tst.name) |
| 107 |
test.EqualInt(t, fm.expected.(int), 123, tst.name) |
| 108 |
test.EqualInt(t, fm.order, tst.order, tst.name) |
| 109 |
test.EqualBool(t, fm.ok, strings.ContainsRune(tst.name, '='), tst.name) |
| 110 |
if test.IsTrue(t, fm.match != nil, tst.name) { |
| 111 |
ok, err := fm.match("abc123") |
| 112 |
test.NoError(t, err, tst.name) |
| 113 |
test.EqualBool(t, ok, tst.match) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
_, err = newFieldMatcher("=~bad(*", 123) |
| 118 |
if test.Error(t, err) { |
| 119 |
test.IsTrue(t, strings.HasPrefix(err.Error(), "bad regexp field `=~bad(*`: ")) |
| 120 |
} |
| 121 |
} |