/
LoggingPipelines.cs
389 lines (325 loc) · 13.9 KB
/
LoggingPipelines.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//---------------------------------------------------------------------
// <copyright file="LoggingPipelines.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Microsoft.Spatial.Tests
{
/// <summary>
/// Spatial pipeline implementation that tracks all of the method calls made to it and their arguments.
///
/// Can also be configured to always throw an exception on all method calls, which is useful in a scenario where you need
/// a pipeline for a test scenario, but don't actually expect it to be used by the code under test.
///
/// This class is intended to be very general and just do logging of the calls it receives, so that's why it implements
/// both geometry and geography, and doesn't try to do any coordinate argument reversal.
/// </summary>
public class CallSequenceLoggingPipeline : SpatialPipeline
{
private readonly bool alwaysThrow;
private readonly List<KeyValuePair<PipelineMethod, object>> methodCalls;
public CallSequenceLoggingPipeline()
: this(false)
{
}
public CallSequenceLoggingPipeline(bool alwaysThrow)
{
this.alwaysThrow = alwaysThrow;
this.methodCalls = new List<KeyValuePair<PipelineMethod, object>>();
}
public void Verify(CallSequenceLoggingPipeline expectedPipeline)
{
List<KeyValuePair<PipelineMethod, object>> expectedCalls = expectedPipeline.methodCalls;
List<KeyValuePair<PipelineMethod, object>> actualCalls = this.methodCalls;
SpatialTestUtils.AssertEqualContents(expectedCalls, actualCalls);
}
public void Merge(CallSequenceLoggingPipeline target, bool keepAllSetCrsCalls = false)
{
// when merging two pipeline calls, the inner SetCoordinateSystem shouldn't be
// merged. This is primarily used for constructing expected pipeline calls for collection types
// from individual member types. Add a flag to this if another usage requires the full pipeline to be merged.
this.methodCalls.AddRange(target.methodCalls.Where(kvp => keepAllSetCrsCalls || (kvp.Key != PipelineMethod.GeographySetCoordinateSystem && kvp.Key != PipelineMethod.GeometrySetCoordinateSystem)));
}
public override GeometryPipeline GeometryPipeline
{
get { return new GeometryPipe(this); }
}
public override GeographyPipeline GeographyPipeline
{
get { return new GeographyPipe(this); }
}
private class GeographyPipe : GeographyPipeline
{
private CallSequenceLoggingPipeline logger;
public GeographyPipe(CallSequenceLoggingPipeline logger)
{
this.logger = logger;
}
public override void BeginGeography(SpatialType type)
{
logger.BeginShape(PipelineMethod.BeginGeography, type);
}
public override void BeginFigure(GeographyPosition position)
{
logger.AddCoordinates(PipelineMethod.GeographyBeginFigure, position.Latitude, position.Longitude, position.Z, position.M);
}
public override void LineTo(GeographyPosition position)
{
logger.AddCoordinates(PipelineMethod.GeographyAddLineTo, position.Latitude, position.Longitude, position.Z, position.M);
}
public override void EndFigure()
{
logger.EndFigure(PipelineMethod.GeographyEndFigure);
}
public override void EndGeography()
{
logger.EndShape(PipelineMethod.EndGeography);
}
public override void SetCoordinateSystem(CoordinateSystem coordinateSystem)
{
logger.SetCoordinateSystem(PipelineMethod.GeographySetCoordinateSystem, coordinateSystem);
}
public override void Reset()
{
logger.Reset(PipelineMethod.GeographyReset);
}
}
private class GeometryPipe : GeometryPipeline
{
private CallSequenceLoggingPipeline logger;
public GeometryPipe(CallSequenceLoggingPipeline logger)
{
this.logger = logger;
}
public override void BeginGeometry(SpatialType type)
{
logger.BeginShape(PipelineMethod.BeginGeometry, type);
}
public override void BeginFigure(GeometryPosition position)
{
logger.AddCoordinates(PipelineMethod.GeometryBeginFigure, position.X, position.Y, position.Z, position.M);
}
public override void LineTo(GeometryPosition position)
{
logger.AddCoordinates(PipelineMethod.GeometryAddLineTo, position.X, position.Y, position.Z, position.M);
}
public override void EndFigure()
{
logger.EndFigure(PipelineMethod.GeometryEndFigure);
}
public override void EndGeometry()
{
logger.EndShape(PipelineMethod.EndGeometry);
}
public override void SetCoordinateSystem(CoordinateSystem coordinateSystem)
{
logger.SetCoordinateSystem(PipelineMethod.GeometrySetCoordinateSystem, coordinateSystem);
}
public override void Reset()
{
logger.Reset(PipelineMethod.GeometryReset);
}
}
#region Helper Methods for both pipeline types
private void BeginShape(PipelineMethod pipelineMethod, SpatialType type)
{
CheckAlwaysThrow();
LogMethodCall(pipelineMethod, type);
}
private void AddCoordinates(PipelineMethod pipelineMethod, double x, double y, double? z, double? m)
{
CheckAlwaysThrow();
LogMethodCall(pipelineMethod, new double?[] { x, y, z, m });
}
private void EndFigure(PipelineMethod pipelineMethod)
{
CheckAlwaysThrow();
LogMethodCall(pipelineMethod, null);
}
private void EndShape(PipelineMethod pipelineMethod)
{
CheckAlwaysThrow();
LogMethodCall(pipelineMethod, null);
}
private void SetCoordinateSystem(PipelineMethod pipelineMethod, CoordinateSystem coordinateSystem)
{
CheckAlwaysThrow();
LogMethodCall(pipelineMethod, coordinateSystem);
}
private void Reset(PipelineMethod pipelineMethod)
{
CheckAlwaysThrow();
LogMethodCall(pipelineMethod, null);
}
private void CheckAlwaysThrow()
{
if (this.alwaysThrow)
{
throw new NotImplementedException();
}
}
private void LogMethodCall(PipelineMethod pipelineMethod, object methodArgs)
{
methodCalls.Add(new KeyValuePair<PipelineMethod, object>(pipelineMethod, methodArgs));
}
#endregion
private enum PipelineMethod
{
// IGeographyPipeline
BeginGeography,
GeographyBeginFigure,
GeographyAddLineTo,
GeographyEndFigure,
EndGeography,
GeographySetCoordinateSystem,
GeographyReset,
// IGeometryPipeline
BeginGeometry,
GeometryBeginFigure,
GeometryAddLineTo,
GeometryEndFigure,
EndGeometry,
GeometrySetCoordinateSystem,
GeometryReset,
}
}
/// <summary>
/// Defines an interface that allows configuration of expected call sequences for either geometry or geography, so
/// that test code can just use the interface and not have to specifically make geometry or geography calls against the
/// CallSequenceLoggingPipeline itself.
/// </summary>
public interface ICommonLoggingPipeline
{
// Pipeline methods
void BeginShape(SpatialType spatialType);
void BeginFigure(double coordinate1, double coordinate2, double? coordinate3, double? coordinate4);
void AddLineTo(double coordinate1, double coordinate2, double? coordinate3, double? coordinate4);
void EndFigure();
void EndShape();
void SetCoordinateSystem(CoordinateSystem coordinateSystem);
// Verification method
void VerifyPipeline(CallSequenceLoggingPipeline actualPipeline);
}
/// <summary>
/// Logging pipeline that can be used to set up a sequence of expected calls that are all using the geography pipeline interface.
/// </summary>
public class GeographyLoggingPipeline : ICommonLoggingPipeline
{
private readonly bool reverseCoordinates;
private GeographyPipeline drawGeography;
private CallSequenceLoggingPipeline pipeline;
public GeographyLoggingPipeline()
: this(true)
{
}
/// <summary>
/// Creates a new instance of the GeographyLoggingPipeline.
/// </summary>
/// <param name="reverseCoordinates">
/// True if calls to BeginFigure and AddLine should reverse the first two coordinates before logging the call.
/// </param>
public GeographyLoggingPipeline(bool reverseCoordinates)
{
this.reverseCoordinates = reverseCoordinates;
this.pipeline = new CallSequenceLoggingPipeline();
this.drawGeography = pipeline;
}
public void BeginShape(SpatialType spatialType)
{
this.drawGeography.BeginGeography(spatialType);
}
public void BeginFigure(double coordinate1, double coordinate2, double? coordinate3, double? coordinate4)
{
if (this.reverseCoordinates)
{
this.drawGeography.BeginFigure(new GeographyPosition(coordinate2, coordinate1, coordinate3, coordinate4));
}
else
{
this.drawGeography.BeginFigure(new GeographyPosition(coordinate1, coordinate2, coordinate3, coordinate4));
}
}
public void AddLineTo(double coordinate1, double coordinate2, double? coordinate3, double? coordinate4)
{
if (this.reverseCoordinates)
{
this.drawGeography.LineTo(new GeographyPosition(coordinate2, coordinate1, coordinate3, coordinate4));
}
else
{
this.drawGeography.LineTo(new GeographyPosition(coordinate1, coordinate2, coordinate3, coordinate4));
}
}
public void EndFigure()
{
this.drawGeography.EndFigure();
}
public void EndShape()
{
this.drawGeography.EndGeography();
}
public void SetCoordinateSystem(CoordinateSystem coordinateSystem)
{
Assert.True(coordinateSystem.TopologyIs(CoordinateSystem.Topology.Geography));
this.drawGeography.SetCoordinateSystem(coordinateSystem);
}
public void VerifyPipeline(CallSequenceLoggingPipeline actualPipeline)
{
actualPipeline.Verify(pipeline);
}
public void MergeCalls(GeographyLoggingPipeline target, bool keepAllSetCrsCalls = false)
{
this.pipeline.Merge(target.pipeline, keepAllSetCrsCalls);
}
}
/// <summary>
/// Logging pipeline that can be used to set up a sequence of expected calls that are all using the geometry pipeline interface.
/// </summary>
public class GeometryLoggingPipeline : ICommonLoggingPipeline
{
private GeometryPipeline drawGeometry;
private CallSequenceLoggingPipeline pipeline;
public GeometryLoggingPipeline()
{
this.pipeline = new CallSequenceLoggingPipeline();
this.drawGeometry = pipeline;
}
public void BeginShape(SpatialType spatialType)
{
this.drawGeometry.BeginGeometry(spatialType);
}
public void BeginFigure(double coordinate1, double coordinate2, double? coordinate3, double? coordinate4)
{
this.drawGeometry.BeginFigure(new GeometryPosition(coordinate1, coordinate2, coordinate3, coordinate4));
}
public void AddLineTo(double coordinate1, double coordinate2, double? coordinate3, double? coordinate4)
{
this.drawGeometry.LineTo(new GeometryPosition(coordinate1, coordinate2, coordinate3, coordinate4));
}
public void EndFigure()
{
this.drawGeometry.EndFigure();
}
public void EndShape()
{
this.drawGeometry.EndGeometry();
}
public void SetCoordinateSystem(CoordinateSystem coordinateSystem)
{
Assert.True(coordinateSystem.TopologyIs(CoordinateSystem.Topology.Geometry));
this.drawGeometry.SetCoordinateSystem(coordinateSystem);
}
public void VerifyPipeline(CallSequenceLoggingPipeline actualPipeline)
{
actualPipeline.Verify(pipeline);
}
public void MergeCalls(GeometryLoggingPipeline target, bool keepAllSetCrsCalls = false)
{
this.pipeline.Merge(target.pipeline, keepAllSetCrsCalls);
}
}
}