-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_test.go
152 lines (123 loc) · 3.25 KB
/
B_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 getBTestData(is *testy.T, v interface{}) {
err := json.Unmarshal(testdata["ints.json"], v)
if err != nil {
is.Fatalf("Error unmarshaling ints.json: %s", err)
}
}
func TestListBBox(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Box struct {
Concat []byte
Elem3 byte
Head byte
Init []byte
Input []byte
Last byte
Len int
Less23 bool
Less32 bool
Reverse []byte
Swap []byte
Tail []byte
Unbox []byte
}
}
getBTestData(is, &data)
xs := listy.B(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.ConcatB(listy.B(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.B(data.Box.Input)
xs.Swap(0, 1)
is.Equal(xs.Unbox(), data.Box.Swap)
}
func TestListBContains(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Contains struct {
Input []byte
ContainsTrue []byte
ContainsFalse []byte
}
}
getBTestData(is, &data)
xs := listy.B(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 TestListBFilter(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Filter struct {
Input []byte
Lessthan byte
Filtered []byte
}
}
getBTestData(is, &data)
xs := listy.B(data.Filter.Input)
ys := xs.Filter(func(v byte) bool { return v < data.Filter.Lessthan })
is.Equal(ys.Unbox(), data.Filter.Filtered)
}
func TestListBMap(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Map struct {
Input []byte
Add byte
Mapped []byte
}
}
getBTestData(is, &data)
xs := listy.B(data.Map.Input)
ys := xs.Map(func(v byte) byte { return v + data.Map.Add })
is.Equal(ys.Unbox(), data.Map.Mapped)
}
func TestListBUniq(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Uniq struct {
Cases map[string][2][]byte
}
}
getBTestData(is, &data)
for k, v := range data.Uniq.Cases {
is.Label(k).Equal(listy.B(v[0]).Uniq().Unbox(), v[1])
}
}