-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI8_test.go
150 lines (122 loc) · 3.21 KB
/
I8_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
// 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 getI8TestData(is *testy.T, v interface{}) {
err := json.Unmarshal(testdata["ints.json"], v)
if err != nil {
is.Fatalf("Error unmarshaling ints.json: %s", err)
}
}
func TestListI8Box(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Box struct {
Concat []int8
Elem3 int8
Head int8
Init []int8
Input []int8
Last int8
Len int
Less23 bool
Less32 bool
Reverse []int8
Swap []int8
Tail []int8
Unbox []int8
}
}
getI8TestData(is, &data)
xs := listy.I8(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.ConcatI8(listy.I8(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.I8(data.Box.Input)
xs.Swap(0, 1)
is.Equal(xs.Unbox(), data.Box.Swap)
}
func TestListI8Contains(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Contains struct {
Input []int8
ContainsTrue []int8
ContainsFalse []int8
}
}
getI8TestData(is, &data)
xs := listy.I8(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 TestListI8Filter(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Filter struct {
Input []int8
Lessthan int8
Filtered []int8
}
}
getI8TestData(is, &data)
xs := listy.I8(data.Filter.Input)
ys := xs.Filter(func(v int8) bool { return v < data.Filter.Lessthan })
is.Equal(ys.Unbox(), data.Filter.Filtered)
}
func TestListI8Map(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Map struct {
Input []int8
Add int8
Mapped []int8
}
}
getI8TestData(is, &data)
xs := listy.I8(data.Map.Input)
ys := xs.Map(func(v int8) int8 { return v + data.Map.Add })
is.Equal(ys.Unbox(), data.Map.Mapped)
}
func TestListI8Uniq(t *testing.T) {
is := testy.New(t)
defer func() { t.Logf(is.Done()) }()
var data struct {
Uniq struct {
Cases map[string][2][]int8
}
}
getI8TestData(is, &data)
for k, v := range data.Uniq.Cases {
is.Label(k).Equal(listy.I8(v[0]).Uniq().Unbox(), v[1])
}
}