-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathfrom_test.go
More file actions
180 lines (148 loc) · 3.67 KB
/
from_test.go
File metadata and controls
180 lines (148 loc) · 3.67 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package linq
import (
"context"
"testing"
"time"
)
func TestFromSlice(t *testing.T) {
s := [3]int{1, 2, 3}
w := []any{1, 2, 3}
if q := FromSlice(s[:]); !testQueryIteration(q, w) {
t.Errorf("FromSlice(%v)!=%v", s, w)
}
}
func TestFromMap(t *testing.T) {
s := map[string]bool{"foo": true}
w := []any{KeyValue{"foo", true}}
if q := FromMap(s); !testQueryIteration(q, w) {
t.Errorf("FromMap(%v)!=%v", s, w)
}
}
func TestFromChannel(t *testing.T) {
c := make(chan int, 3)
c <- 10
c <- 15
c <- -3
close(c)
w := []any{10, 15, -3}
if q := FromChannel(c); !assertQueryOutput(q, w) {
t.Errorf("FromChannel() failed expected %v", w)
}
}
func TestFromChannel_DryRun(t *testing.T) {
c := make(chan int, 3)
c <- 10
c <- 15
c <- -3
close(c)
q := FromChannel(c)
runDryIteration(q)
}
func TestFromChannelWithContext_Cancel(t *testing.T) {
c := make(chan int, 3)
defer close(c)
c <- 10
c <- 15
c <- -3
w := []any{10, 15, -3}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if q := FromChannelWithContext(ctx, c); !assertQueryOutput(q, w) {
t.Errorf("FromChannelWithContext() failed expected %v", w)
}
}
func TestFromChannelWithContext_Closed(t *testing.T) {
c := make(chan int, 3)
c <- 10
c <- 15
c <- -3
close(c)
w := []any{10, 15, -3}
ctx := context.Background()
if q := FromChannelWithContext(ctx, c); !assertQueryOutput(q, w) {
t.Errorf("FromChannelWithContext() failed expected %v", w)
}
}
func TestFromString(t *testing.T) {
s := "string"
w := []any{'s', 't', 'r', 'i', 'n', 'g'}
if q := FromString(s); !testQueryIteration(q, w) {
t.Errorf("FromString(%v)!=%v", s, w)
}
}
func TestFromIterable(t *testing.T) {
s := foo{f1: 1, f2: true, f3: "string"}
w := []any{1, true, "string"}
if q := FromIterable(s); !testQueryIteration(q, w) {
t.Errorf("FromIterable(%v)!=%v", s, w)
}
}
func TestFrom(t *testing.T) {
tests := []struct {
input any
output []any
want bool
}{
{[]int{1, 2, 3}, []any{1, 2, 3}, true},
{[]int{1, 2, 4}, []any{1, 2, 3}, false},
{[3]int{1, 2, 3}, []any{1, 2, 3}, true},
{[3]int{1, 2, 4}, []any{1, 2, 3}, false},
{"str", []any{'s', 't', 'r'}, true},
{"str", []any{'s', 't', 'g'}, false},
{map[string]bool{"foo": true}, []any{KeyValue{"foo", true}}, true},
{map[string]bool{"foo": true}, []any{KeyValue{"foo", false}}, false},
{foo{f1: 1, f2: true, f3: "string"}, []any{1, true, "string"}, true},
{nil, nil, true},
}
for _, test := range tests {
if q := From(test.input); testQueryIteration(q, test.output) != test.want {
if test.want {
t.Errorf("From(%v)=%v expected %v", test.input, toSlice(q), test.output)
} else {
t.Errorf("From(%v)=%v expected not equal", test.input, test.output)
}
}
}
}
func TestFrom_Channel(t *testing.T) {
c := make(chan any, 3)
c <- -1
c <- 0
c <- 1
close(c)
ct := make(chan int, 3)
ct <- -10
ct <- 0
ct <- 10
close(ct)
tests := []struct {
input any
output []any
}{
{c, []any{-1, 0, 1}},
{ct, []any{-10, 0, 10}},
}
for _, test := range tests {
if q := From(test.input); !assertQueryOutput(q, test.output) {
t.Errorf("From(%v) failed, expected %v", test.input, test.output)
}
}
}
func TestFrom_UnsupportedTypePanics(t *testing.T) {
mustPanicWithError(t, "unsupported type for From: int", func() {
// int is not supported by From, should panic
From(123)
})
}
func TestRange(t *testing.T) {
w := []any{-2, -1, 0, 1, 2}
if q := Range(-2, 5); !testQueryIteration(q, w) {
t.Errorf("Range(-2, 5)=%v expected %v", toSlice(q), w)
}
}
func TestRepeat(t *testing.T) {
w := []any{1, 1, 1, 1, 1}
if q := Repeat(1, 5); !testQueryIteration(q, w) {
t.Errorf("Repeat(1, 5)=%v expected %v", toSlice(q), w)
}
}