This repository has been archived by the owner on Aug 25, 2019. It is now read-only.
forked from claremacrae/Pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PipelineTests.cs
294 lines (241 loc) · 9.26 KB
/
PipelineTests.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApprovalTests.Reporters;
using ApprovalTests.Reporters.TestFrameworks;
using ApprovalTests.Reporters.Windows;
using ApprovalUtilities.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Refactoring.Pipelines.Approvals;
using Refactoring.Pipelines.DotGraph;
namespace Refactoring.Pipelines.Test
{
[UseReporter(typeof(DotReporter))]
[TestClass]
public class PipelineTests
{
[TestMethod]
public void BasicPipelineFunctionalTest()
{
Func<string, long> normal = age =>
{
// begin-snippet: basic_code_line
var result = long.Parse(age);
// end-snippet
return result;
};
Func<string, long> piped = age =>
{
// begin-snippet: basic_pipeline
var inputPipe = new InputPipe<string>("age");
var parsePipe = inputPipe.ProcessFunction(long.Parse);
var collector = parsePipe.Collect();
inputPipe.Send("42");
var result = collector.SingleResult;
// end-snippet
return result;
};
Assert.AreEqual(normal("42"), piped("42"));
}
[TestMethod]
public void BasicPipelineTest()
{
var input = new InputPipe<string>("age");
var parse = input.ProcessFunction(long.Parse);
var collector = parse.Collect();
PipelineApprovals.Verify(input);
input.Send("42");
Assert.AreEqual(42, collector.SingleResult);
}
[TestMethod]
public void ProcessIsNotCovariant()
{
var input = new InputPipe<int>("i");
var collector = input.ProcessFunction(RangeArray)
.ProcessFunction(_ => (IEnumerable<int>) _) // Would be nice not to need this
.ProcessFunction(SumEnumerable)
.Collect();
input.Send(4);
Assert.AreEqual(10, collector.SingleResult);
}
private int SumEnumerable(IEnumerable<int> _) { return _.Sum(); }
private int[] RangeArray(int count) { return Enumerable.Range(1, count).ToArray(); }
[TestMethod]
public void ConnectedPipelinesTest()
{
var input = new InputPipe<string>("age");
var parsePipe = input.ProcessFunction(long.Parse);
var collector = parsePipe.Collect();
parsePipe.ProcessFunction(LongToString)
.WithCollector()
.ProcessFunction(long.Parse)
.WithCollector()
.ProcessFunction(LongToString)
.Collect();
PipelineApprovals.Verify(input);
}
[TestMethod]
public void SplitAndJoin()
{
var input = new InputPipe<string>("age");
var parse = input.ProcessFunction(long.Parse);
var longToString = parse.ProcessFunction(LongToString);
var incrementLong = parse.ProcessFunction(IncrementLong);
var joinedPipes = longToString.JoinTo(incrementLong).Collect();
PipelineApprovals.Verify(input);
}
[TestMethod]
public void SplitInput()
{
var input = new InputPipe<long>("value");
input.ProcessFunction(LongToString);
input.ProcessFunction(IncrementLong);
PipelineApprovals.Verify(input);
}
[TestMethod]
public void JoinInputs()
{
var input1 = new InputPipe<long>("value1");
var input2 = new InputPipe<long>("value2");
var join = input1.JoinTo(input2);
var collector = join.ProcessFunction(Echo).Collect();
input1.Send(42);
Assert.IsTrue(collector.IsEmpty);
input2.Send(99);
Assert.AreEqual("(42, 99)", collector.SingleResult.ToString());
PipelineApprovals.Verify(join);
}
[TestMethod]
public void MultipleParameters()
{
var input1 = new InputPipe<long>("value1");
var input2 = new InputPipe<long>("value2");
var join = input1.JoinTo(input2);
var collector = join.Process((a, b) => a + b).Collect();
input1.Send(3);
Assert.IsTrue(collector.IsEmpty);
input2.Send(4);
Assert.AreEqual(7, collector.SingleResult);
PipelineApprovals.Verify(join);
}
[TestMethod]
public void Flatten()
{
var input1 = new InputPipe<long>("value1");
var input2 = new InputPipe<long>("value2");
var join = input1.JoinTo(input2);
var input3 = new InputPipe<long>("value3");
Sender<Tuple<long, long, long>> all = join.JoinTo(input3).Flatten();
var collector = all.Collect();
input1.Send(1);
input2.Send(2);
input3.Send(3);
Assert.AreEqual("(1, 2, 3)", collector.SingleResult.ToString());
PipelineApprovals.Verify(collector);
}
[TestMethod]
public void JoinInputsSample()
{
// begin-snippet: joined_pipeline
var input1 = new InputPipe<long>("value1");
var input2 = new InputPipe<long>("value2");
var join = input1.JoinTo(input2);
// end-snippet
PipelineApprovals.Verify(join);
}
[TestMethod]
public void CannotFindNodeExceptionHelpMessage()
{
var input1 = new InputPipe<long>("value1");
var subject = new NodeMetadata.CannotFindNodeException(input1);
ApprovalTests.Approvals.Verify(subject);
}
[TestMethod]
public void ApplyTo()
{
var prefix = new InputPipe<string>("prefix");
var values = new InputPipe<int[]>("values");
var applyToPipeline = prefix.ApplyTo(values);
var collector = applyToPipeline.Collect();
PipelineApprovals.Verify(applyToPipeline);
// begin-snippet: ApplyTo_inputs
var apply = "#";
var to = new[] {1, 2};
// end-snippet
// begin-snippet: ApplyTo_outputs
var result = "[(#, 1), (#, 2)]";
// end-snippet
var manualApplyTo =
// begin-snippet: ApplyTo_manual
prefix.JoinTo(values).ProcessFunction(t => t.Item2.Select(i => Tuple.Create(t.Item1, i)));
// end-snippet
var manualApplyToResult = manualApplyTo.Collect();
prefix.Send(apply);
values.Send(to);
Assert.AreEqual(result, collector.SingleResult.ToReadableString());
Assert.AreEqual(
manualApplyToResult.SingleResult.ToReadableString(),
collector.SingleResult.ToReadableString());
}
[TestMethod]
public void Concat()
{
var part1 = new InputPipe<List<int>>("part1");
var part2 = new InputPipe<int[]>("part2");
var concatWithPipeline = part1.ConcatWith(part2);
var collector = concatWithPipeline.Collect();
PipelineApprovals.Verify(concatWithPipeline);
// begin-snippet: ConcatWith_inputs
var concat = new List<int> {1, 2};
var with = new[] {3, 4};
// end-snippet
// begin-snippet: ConcatWith_outputs
var result = "[1, 2, 3, 4]";
// end-snippet
var manualConcatWith =
// begin-snippet: ConcatWith_manual
part1.JoinTo(part2).ProcessFunction(t => t.Item1.Concat(t.Item2).ToList());
// end-snippet
var manualConcatWithResult = manualConcatWith.Collect();
part1.Send(concat);
part2.Send(with);
Assert.AreEqual(result, collector.SingleResult.ToReadableString());
Assert.AreEqual(
manualConcatWithResult.SingleResult.ToReadableString(),
collector.SingleResult.ToReadableString());
}
[TestMethod]
public void ForEach()
{
var part1 = new InputPipe<List<long>>("part1");
var collector = part1.ProcessForEach(IncrementLong).Collect();
part1.Send(new List<long> {1, 2});
Assert.AreEqual("[2, 3]", collector.SingleResult.ToReadableString());
PipelineApprovals.Verify(part1);
}
[TestMethod]
public void Lambda()
{
// begin-snippet: process_lambda
var input = new InputPipe<int>("input");
input.Process(p => p.ToString());
// end-snippet
PipelineApprovals.Verify(input);
}
private string LongToString(long value) { return value.ToString(); }
private long IncrementLong(long value) { return value + 1; }
private T Echo<T>(T t) =>
t;
}
internal static class _
{
public static FunctionPipe<TInput, TOutput> WithCollector<TInput, TOutput>(
this FunctionPipe<TInput, TOutput> @this)
{
@this.Collect();
return @this;
}
}
}