github.com/hairyhenderson/templater@v3.5.0+incompatible/coll/coll_test.go (about)

     1  package coll
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestSlice(t *testing.T) {
    11  	expected := []string{"foo", "bar"}
    12  	actual := Slice("foo", "bar")
    13  	assert.Equal(t, expected[0], actual[0])
    14  	assert.Equal(t, expected[1], actual[1])
    15  }
    16  
    17  func TestHas(t *testing.T) {
    18  
    19  	in := map[string]interface{}{
    20  		"foo": "bar",
    21  		"baz": map[string]interface{}{
    22  			"qux": "quux",
    23  		},
    24  	}
    25  
    26  	testdata := []struct {
    27  		in  interface{}
    28  		key interface{}
    29  		out bool
    30  	}{
    31  		{in, "foo", true},
    32  		{in, "bar", false},
    33  		{in["baz"], "qux", true},
    34  		{[]string{"foo", "bar", "baz"}, "bar", true},
    35  		{[]interface{}{"foo", "bar", "baz"}, "bar", true},
    36  		{[]interface{}{"foo", "bar", "baz"}, 42, false},
    37  		{[]int{1, 2, 42}, 42, true},
    38  	}
    39  
    40  	for _, d := range testdata {
    41  		assert.Equal(t, d.out, Has(d.in, d.key))
    42  	}
    43  }
    44  
    45  func TestDict(t *testing.T) {
    46  	testdata := []struct {
    47  		args     []interface{}
    48  		expected map[string]interface{}
    49  	}{
    50  		{nil, map[string]interface{}{}},
    51  		{[]interface{}{}, map[string]interface{}{}},
    52  		{[]interface{}{"foo"}, map[string]interface{}{"foo": ""}},
    53  		{[]interface{}{42}, map[string]interface{}{"42": ""}},
    54  		{[]interface{}{"foo", nil}, map[string]interface{}{"foo": nil}},
    55  		{[]interface{}{"foo", "bar"}, map[string]interface{}{"foo": "bar"}},
    56  		{[]interface{}{"foo", "bar", "baz", true}, map[string]interface{}{
    57  			"foo": "bar",
    58  			"baz": true,
    59  		}},
    60  	}
    61  
    62  	for _, d := range testdata {
    63  		actual, _ := Dict(d.args...)
    64  		assert.Equal(t, d.expected, actual)
    65  	}
    66  }
    67  
    68  func TestKeys(t *testing.T) {
    69  	_, err := Keys()
    70  	assert.Error(t, err)
    71  
    72  	in := map[string]interface{}{
    73  		"foo": 1,
    74  		"bar": 2,
    75  	}
    76  	expected := []string{"bar", "foo"}
    77  	keys, err := Keys(in)
    78  	assert.NoError(t, err)
    79  	assert.EqualValues(t, expected, keys)
    80  
    81  	in2 := map[string]interface{}{
    82  		"baz": 3,
    83  		"qux": 4,
    84  	}
    85  	expected = []string{"bar", "foo", "baz", "qux"}
    86  	keys, err = Keys(in, in2)
    87  	assert.NoError(t, err)
    88  	assert.EqualValues(t, expected, keys)
    89  
    90  	in3 := map[string]interface{}{
    91  		"Foo": 5,
    92  		"Bar": 6,
    93  		"foo": 7,
    94  		"bar": 8,
    95  	}
    96  	expected = []string{"bar", "foo", "baz", "qux", "Bar", "Foo", "bar", "foo"}
    97  	keys, err = Keys(in, in2, in3)
    98  	assert.NoError(t, err)
    99  	assert.EqualValues(t, expected, keys)
   100  }
   101  
   102  func TestValues(t *testing.T) {
   103  	_, err := Values()
   104  	assert.Error(t, err)
   105  
   106  	in := map[string]interface{}{
   107  		"foo": 1,
   108  		"bar": 2,
   109  	}
   110  	expected := []interface{}{2, 1}
   111  	values, err := Values(in)
   112  	assert.NoError(t, err)
   113  	assert.EqualValues(t, expected, values)
   114  
   115  	in2 := map[string]interface{}{
   116  		"baz": 3,
   117  		"qux": 4,
   118  	}
   119  	expected = []interface{}{2, 1, 3, 4}
   120  	values, err = Values(in, in2)
   121  	assert.NoError(t, err)
   122  	assert.EqualValues(t, expected, values)
   123  
   124  	in3 := map[string]interface{}{
   125  		"Foo": 5,
   126  		"Bar": 6,
   127  		"foo": 7,
   128  		"bar": 8,
   129  	}
   130  	expected = []interface{}{2, 1, 3, 4, 6, 5, 8, 7}
   131  	values, err = Values(in, in2, in3)
   132  	assert.NoError(t, err)
   133  	assert.EqualValues(t, expected, values)
   134  }
   135  
   136  func TestAppend(t *testing.T) {
   137  	out, err := Append(42, []interface{}{})
   138  	assert.NoError(t, err)
   139  	assert.EqualValues(t, out, []interface{}{42})
   140  
   141  	out, err = Append(42, []interface{}{4.9, false, "foo"})
   142  	assert.NoError(t, err)
   143  	assert.EqualValues(t, out, []interface{}{4.9, false, "foo", 42})
   144  
   145  	// a strange but valid use-cases, since we're converting to an []interface{}
   146  	out, err = Append(42, []string{"foo"})
   147  	assert.NoError(t, err)
   148  	assert.EqualValues(t, []interface{}{"foo", 42}, out)
   149  
   150  	out, err = Append("baz", []string{"foo", "bar"})
   151  	assert.NoError(t, err)
   152  	assert.EqualValues(t, out, []interface{}{"foo", "bar", "baz"})
   153  }
   154  
   155  func TestPrepend(t *testing.T) {
   156  	out, err := Prepend(42, []interface{}{})
   157  	assert.NoError(t, err)
   158  	assert.EqualValues(t, out, []interface{}{42})
   159  
   160  	out, err = Prepend(42, []interface{}{4.9, false, "foo"})
   161  	assert.NoError(t, err)
   162  	assert.EqualValues(t, out, []interface{}{42, 4.9, false, "foo"})
   163  
   164  	// a strange but valid use-cases, since we're converting to an []interface{}
   165  	out, err = Prepend(42, []string{"foo"})
   166  	assert.NoError(t, err)
   167  	assert.EqualValues(t, []interface{}{42, "foo"}, out)
   168  
   169  	out, err = Prepend("foo", []string{"bar", "baz"})
   170  	assert.NoError(t, err)
   171  	assert.EqualValues(t, out, []interface{}{"foo", "bar", "baz"})
   172  }
   173  
   174  func TestUniq(t *testing.T) {
   175  	out, err := Uniq([]interface{}{1, 2, 3, 1, true, false, true, "1", 2})
   176  	assert.NoError(t, err)
   177  	assert.EqualValues(t, []interface{}{1, 2, 3, true, false, "1"}, out)
   178  
   179  	out, err = Uniq([]string{"one", "two", "one", "three"})
   180  	assert.NoError(t, err)
   181  	assert.EqualValues(t, []interface{}{"one", "two", "three"}, out)
   182  }
   183  
   184  func TestReverse(t *testing.T) {
   185  	out, err := Reverse([]interface{}{})
   186  	assert.NoError(t, err)
   187  	assert.EqualValues(t, []interface{}{}, out)
   188  
   189  	out, err = Reverse([]interface{}{8})
   190  	assert.NoError(t, err)
   191  	assert.EqualValues(t, []interface{}{8}, out)
   192  
   193  	out, err = Reverse([]interface{}{1, 2, 3, 4})
   194  	assert.NoError(t, err)
   195  	assert.EqualValues(t, []interface{}{4, 3, 2, 1}, out)
   196  
   197  	out, err = Reverse([]int{1, 2, 3, 4})
   198  	assert.NoError(t, err)
   199  	assert.EqualValues(t, []interface{}{4, 3, 2, 1}, out)
   200  }
   201  
   202  func TestMerge(t *testing.T) {
   203  	dst := map[string]interface{}{}
   204  	src := map[string]interface{}{}
   205  	expected := map[string]interface{}{}
   206  
   207  	out, err := Merge(dst, src)
   208  	assert.NoError(t, err)
   209  	assert.EqualValues(t, expected, out)
   210  
   211  	dst = map[string]interface{}{"a": 4, "c": 5}
   212  	src = map[string]interface{}{"a": 1, "b": 2, "c": 3}
   213  	expected = map[string]interface{}{
   214  		"a": dst["a"], "b": src["b"], "c": dst["c"],
   215  	}
   216  
   217  	out, err = Merge(dst, src)
   218  	assert.NoError(t, err)
   219  	assert.EqualValues(t, expected, out)
   220  
   221  	dst = map[string]interface{}{"a": 4, "c": 5}
   222  	src = map[string]interface{}{"a": 1, "b": 2, "c": 3}
   223  	src2 := map[string]interface{}{"a": 1, "b": 2, "c": 3, "d": 4}
   224  	expected = map[string]interface{}{
   225  		"a": dst["a"], "b": src["b"], "c": dst["c"], "d": src2["d"],
   226  	}
   227  
   228  	out, err = Merge(dst, src, src2)
   229  	assert.NoError(t, err)
   230  	assert.EqualValues(t, expected, out)
   231  
   232  	dst = map[string]interface{}{"a": false, "c": 5}
   233  	src = map[string]interface{}{"a": true, "b": 2, "c": 3}
   234  	src2 = map[string]interface{}{"a": true, "b": 2, "c": 3, "d": 4}
   235  	expected = map[string]interface{}{
   236  		"a": dst["a"], "b": src["b"], "c": dst["c"], "d": src2["d"],
   237  	}
   238  
   239  	out, err = Merge(dst, src, src2)
   240  	assert.NoError(t, err)
   241  	assert.EqualValues(t, expected, out)
   242  
   243  	dst = map[string]interface{}{"a": true, "c": 5}
   244  	src = map[string]interface{}{"a": false,
   245  		"b": map[string]interface{}{
   246  			"ca": "foo",
   247  		},
   248  	}
   249  	src2 = map[string]interface{}{"a": false, "b": 2, "c": 3, "d": 4}
   250  	expected = map[string]interface{}{
   251  		"a": dst["a"], "b": src["b"], "c": dst["c"], "d": src2["d"],
   252  	}
   253  
   254  	out, err = Merge(dst, src, src2)
   255  	assert.NoError(t, err)
   256  	assert.EqualValues(t, expected, out)
   257  
   258  	dst = map[string]interface{}{
   259  		"a": true,
   260  		"b": map[string]interface{}{
   261  			"ca": "foo",
   262  			"cb": "bar",
   263  		},
   264  		"c": 5}
   265  	src = map[string]interface{}{
   266  		"a": false,
   267  		"b": map[string]interface{}{
   268  			"ca": 8,
   269  		},
   270  	}
   271  	expected = map[string]interface{}{
   272  		"a": dst["a"], "b": map[string]interface{}{
   273  			"ca": "foo",
   274  			"cb": "bar",
   275  		}, "c": dst["c"],
   276  	}
   277  
   278  	out, err = Merge(dst, src)
   279  	assert.NoError(t, err)
   280  	assert.EqualValues(t, expected, out)
   281  }
   282  
   283  type coords struct {
   284  	X, Y int
   285  }
   286  
   287  func TestSameTypes(t *testing.T) {
   288  	data := []struct {
   289  		in  []interface{}
   290  		out bool
   291  	}{
   292  		{[]interface{}{}, true},
   293  		{[]interface{}{"a", "b"}, true},
   294  		{[]interface{}{1.0, 3.14}, true},
   295  		{[]interface{}{1, 3}, true},
   296  		{[]interface{}{true, false}, true},
   297  		{[]interface{}{1, 3.0}, false},
   298  		{[]interface{}{"a", nil}, false},
   299  		{[]interface{}{"a", true}, false},
   300  		{[]interface{}{coords{2, 3}, coords{3, 4}}, true},
   301  		{[]interface{}{coords{2, 3}, &coords{3, 4}}, false},
   302  	}
   303  
   304  	for _, d := range data {
   305  		assert.Equal(t, d.out, sameTypes(d.in))
   306  	}
   307  }
   308  
   309  func TestLessThan(t *testing.T) {
   310  	data := []struct {
   311  		key         string
   312  		left, right interface{}
   313  		out         bool
   314  	}{
   315  		{"", nil, nil, false},
   316  		{"", "a", "b", true},
   317  		{"", "a", "a", false},
   318  		{"", "b", "a", false},
   319  		{"", 1.00, 3.14, true},
   320  		{"", 'a', 'A', false},
   321  		{"", 'a', 'b', true},
   322  		{"", uint(0xff), uint(0x32), false},
   323  		{"", 1, 3, true},
   324  		{"", true, false, false},
   325  		{"", map[string]interface{}{"foo": 1}, map[string]interface{}{"foo": 2}, false},
   326  		{"foo", map[string]interface{}{"foo": 1}, map[string]interface{}{"foo": 2}, true},
   327  		{"bar", map[string]interface{}{"foo": 1}, map[string]interface{}{"foo": 2}, false},
   328  		{"X", coords{}, coords{-1, 2}, false},
   329  		{"Y", &coords{1, 1}, &coords{-1, 2}, true},
   330  		{"", &coords{1, 1}, &coords{-1, 2}, false},
   331  		{"foo", &coords{1, 1}, &coords{-1, 2}, false},
   332  	}
   333  
   334  	for _, d := range data {
   335  		t.Run(fmt.Sprintf(`LessThan("%s")(<%T>%#v,%#v)==%v`, d.key, d.left, d.left, d.right, d.out), func(t *testing.T) {
   336  			assert.Equal(t, d.out, lessThan(d.key)(d.left, d.right))
   337  		})
   338  	}
   339  }
   340  
   341  func TestSort(t *testing.T) {
   342  	out, err := Sort("", 42)
   343  	assert.Error(t, err)
   344  	assert.Nil(t, out)
   345  
   346  	data := []struct {
   347  		key string
   348  		in  interface{}
   349  		out []interface{}
   350  	}{
   351  		{
   352  			key: "",
   353  			in:  []string{"b", "c", "a", "d"},
   354  			out: []interface{}{"a", "b", "c", "d"},
   355  		},
   356  		{
   357  			key: "",
   358  			in:  []interface{}{"b", "c", "a", "d"},
   359  			out: []interface{}{"a", "b", "c", "d"},
   360  		},
   361  		{
   362  			key: "",
   363  			in:  []interface{}{"c", "a", "b", 3, 1, 2},
   364  			out: []interface{}{"c", "a", "b", 3, 1, 2},
   365  		},
   366  		{
   367  			key: "",
   368  			in:  nil,
   369  			out: nil,
   370  		},
   371  
   372  		{
   373  			key: "",
   374  			in: []map[string]interface{}{
   375  				{"name": "Bart", "age": 12},
   376  				{"age": 1, "name": "Maggie"},
   377  				{"name": "Lisa", "age": 6},
   378  			},
   379  			out: []interface{}{
   380  				map[string]interface{}{"name": "Bart", "age": 12},
   381  				map[string]interface{}{"age": 1, "name": "Maggie"},
   382  				map[string]interface{}{"name": "Lisa", "age": 6},
   383  			},
   384  		},
   385  		{
   386  			key: "name",
   387  			in: []map[string]interface{}{
   388  				{"name": "Bart", "age": 12},
   389  				{"age": 1, "name": "Maggie"},
   390  				{"name": "Lisa", "age": 6},
   391  			},
   392  			out: []interface{}{
   393  				map[string]interface{}{"name": "Bart", "age": 12},
   394  				map[string]interface{}{"name": "Lisa", "age": 6},
   395  				map[string]interface{}{"age": 1, "name": "Maggie"},
   396  			},
   397  		},
   398  		{
   399  			key: "age",
   400  			in: []map[string]interface{}{
   401  				{"name": "Bart", "age": 12},
   402  				{"age": 1, "name": "Maggie"},
   403  				{"name": "Lisa", "age": 6},
   404  			},
   405  			out: []interface{}{
   406  				map[string]interface{}{"age": 1, "name": "Maggie"},
   407  				map[string]interface{}{"name": "Lisa", "age": 6},
   408  				map[string]interface{}{"name": "Bart", "age": 12},
   409  			},
   410  		},
   411  		{
   412  			key: "y",
   413  			in: []map[string]int{
   414  				{"x": 54, "y": 6},
   415  				{"x": 13, "y": -8},
   416  				{"x": 1, "y": 0},
   417  			},
   418  			out: []interface{}{
   419  				map[string]int{"x": 13, "y": -8},
   420  				map[string]int{"x": 1, "y": 0},
   421  				map[string]int{"x": 54, "y": 6},
   422  			},
   423  		},
   424  		{
   425  			key: "X",
   426  			in: []coords{
   427  				{2, 4},
   428  				{3, 3},
   429  				{1, 5},
   430  			},
   431  			out: []interface{}{
   432  				coords{1, 5},
   433  				coords{2, 4},
   434  				coords{3, 3},
   435  			},
   436  		},
   437  		{
   438  			key: "X",
   439  			in: []*coords{
   440  				{2, 4},
   441  				{3, 3},
   442  				{1, 5},
   443  			},
   444  			out: []interface{}{
   445  				&coords{1, 5},
   446  				&coords{2, 4},
   447  				&coords{3, 3},
   448  			},
   449  		},
   450  	}
   451  
   452  	for _, d := range data {
   453  		t.Run(fmt.Sprintf(`Sort("%s",<%T>)==%#v`, d.key, d.in, d.out), func(t *testing.T) {
   454  			out, err := Sort(d.key, d.in)
   455  			assert.NoError(t, err)
   456  			assert.EqualValues(t, d.out, out)
   457  		})
   458  	}
   459  }