-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS_test.go
152 lines (123 loc) · 3.3 KB
/
S_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// DO NOT EDIT! This file was generated via `go generate`
// Copyright 2017 by David A. Golden. All rights reserved.
//
// 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
package listy_test
import (
"encoding/json"
"testing"
"github.com/xdg/listy"
"github.com/xdg/testy"
)
func getSTestData(is *testy.T, v interface{}) {
err := json.Unmarshal(testdata["strings.json"], v)
if err != nil {
is.Fatalf("Error unmarshaling strings.json: %s", err)
}
}
func TestListSBox(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Box struct {
Concat []string
Elem3 string
Head string
Init []string
Input []string
Last string
Len int
Less23 bool
Less32 bool
Reverse []string
Swap []string
Tail []string
Unbox []string
}
}
getSTestData(is, &data)
xs := listy.S(data.Box.Input)
// Tests that return ints, bools, etc. irrespective of value type
is.Equal(xs.Len(), data.Box.Len)
is.Equal(xs.Less(2, 3), data.Box.Less23)
is.Equal(xs.Less(3, 2), data.Box.Less32)
// Tests that return the value type
is.Equal(xs.Head(), data.Box.Head)
is.Equal(xs.Elem(3), data.Box.Elem3)
is.Equal(xs.Last(), data.Box.Last)
// Tests that return a slice of the value type
is.Equal(xs.Unbox(), data.Box.Unbox)
is.Equal(xs.Tail().Unbox(), data.Box.Tail)
is.Equal(xs.Init().Unbox(), data.Box.Init)
is.Equal(xs.Concat(data.Box.Input).Unbox(), data.Box.Concat)
is.Equal(xs.ConcatS(listy.S(data.Box.Input)).Unbox(), data.Box.Concat)
is.Equal(xs.Reverse().Unbox(), data.Box.Reverse)
// For Sort.Interface, Swap is in place and returns nothing
xs = listy.S(data.Box.Input)
xs.Swap(0, 1)
is.Equal(xs.Unbox(), data.Box.Swap)
}
func TestListSContains(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Contains struct {
Input []string
ContainsTrue []string
ContainsFalse []string
}
}
getSTestData(is, &data)
xs := listy.S(data.Contains.Input)
for _, x := range data.Contains.ContainsTrue {
is.True(xs.Contains(x))
}
for _, x := range data.Contains.ContainsFalse {
is.False(xs.Contains(x))
}
}
func TestListSFilter(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Filter struct {
Input []string
Lessthan string
Filtered []string
}
}
getSTestData(is, &data)
xs := listy.S(data.Filter.Input)
ys := xs.Filter(func(v string) bool { return v < data.Filter.Lessthan })
is.Equal(ys.Unbox(), data.Filter.Filtered)
}
func TestListSMap(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Map struct {
Input []string
Add string
Mapped []string
}
}
getSTestData(is, &data)
xs := listy.S(data.Map.Input)
ys := xs.Map(func(v string) string { return v + data.Map.Add })
is.Equal(ys.Unbox(), data.Map.Mapped)
}
func TestListSUniq(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Uniq struct {
Cases map[string][2][]string
}
}
getSTestData(is, &data)
for k, v := range data.Uniq.Cases {
is.Label(k).Equal(listy.S(v[0]).Uniq().Unbox(), v[1])
}
}