-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathExampleInfo.cs
More file actions
313 lines (271 loc) · 9.67 KB
/
ExampleInfo.cs
File metadata and controls
313 lines (271 loc) · 9.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
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExampleInfo.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace ExampleLibrary
{
using System;
using System.Collections.Generic;
using System.Reflection;
using ExampleLibrary.Utilities;
using OxyPlot;
/// <summary>
/// Provides information about an example.
/// </summary>
public class ExampleInfo
{
/// <summary>
/// The method to invoke.
/// </summary>
private readonly MethodInfo method;
/// <summary>
/// Indicates whether this instance was already initialized.
/// </summary>
private bool initialized;
/// <summary>
/// The un-modified example.
/// </summary>
private Example Example;
/// <summary>
/// The examples for this example info.
/// </summary>
private Dictionary<ExampleFlags, Example> examples;
/// <summary>
/// The options supported by this example.
/// </summary>
private ExampleFlags exampleSupport;
/// <summary>
/// Initializes a new instance of the <see cref="ExampleInfo"/> class.
/// </summary>
/// <param name="category">The category.</param>
/// <param name="title">The title.</param>
/// <param name="tags">The tags.</param>
/// <param name="method">The method.</param>
/// <param name="excludeFromAutomatedTests">A value indiciating whether the example should be excluded from automated tests.</param>
public ExampleInfo(string category, string title, string[] tags, MethodInfo method, bool excludeFromAutomatedTests)
{
this.Category = category;
this.Title = title;
this.Tags = tags;
this.method = method;
this.ExcludeFromAutomatedTests = excludeFromAutomatedTests;
}
/// <summary>
/// Gets the category.
/// </summary>
/// <value>
/// The category.
/// </value>
public string Category { get; }
/// <summary>
/// Gets the code.
/// </summary>
/// <value>
/// The code.
/// </value>
public string Code => this.PlotModel?.ToCode();
/// <summary>
/// Gets a value indicating whether the plot model is reversible.
/// </summary>
public bool IsReversible
{
get
{
this.EnsureInitialized();
return exampleSupport.HasFlag(ExampleFlags.Reverse);
}
}
/// <summary>
/// Gets a value indicating whether the plot model is transposable.
/// </summary>
public bool IsTransposable
{
get
{
this.EnsureInitialized();
return exampleSupport.HasFlag(ExampleFlags.Transpose);
}
}
/// <summary>
/// Gets the code for the example with the given flags.
/// </summary>
/// <param name="flags">The flags for the example.</param>
/// <returns>Code that produces the example.</returns>
/// <remarks>Ignores unsupported flags.</remarks>
public string GetCode(ExampleFlags flags)
{
return this.GetModel(flags)?.ToCode();
}
/// <summary>
/// Gets the <see cref="PlotModel"/> for the example with the given flags.
/// </summary>
/// <param name="flags">The flags for the example.</param>
/// <returns>The <see cref="PlotModel"/>.</returns>
/// <remarks>Ignores unsupported flags.</remarks>
public PlotModel GetModel(ExampleFlags flags)
{
return this.GetExample(flags).Model;
}
/// <summary>
/// Gets the <see cref="IPlotController"/> for the example with the given flags.
/// </summary>
/// <param name="flags">The flags for the example.</param>
/// <returns>The <see cref="IPlotController"/>.</returns>
/// <remarks>Ignores unsupported flags.</remarks>
public IPlotController GetController(ExampleFlags flags)
{
return this.GetExample(flags).Controller;
}
/// <summary>
/// Gets the <see cref="Example"/> with the given flags.
/// </summary>
/// <param name="flags">The flags for the example.</param>
/// <returns>The <see cref="Example"/>.</returns>
/// <remarks>Ignores unsupported flags.</remarks>
private Example GetExample(ExampleFlags flags)
{
this.EnsureInitialized();
// ignore flags we don't support
flags = FilterFlags(flags);
if (!examples.TryGetValue(flags, out var example))
{
example = GetDefaultExample();
if (flags.HasFlag(ExampleFlags.Transpose))
{
example.Model.Transpose();
}
if (flags.HasFlag(ExampleFlags.Reverse))
{
example.Model.ReverseAllAxes();
}
examples[flags] = example;
}
return example;
}
/// <summary>
/// Gets the plot controller for the default example.
/// </summary>
/// <value>
/// The plot controller.
/// </value>
public IPlotController PlotController
{
get
{
this.EnsureInitialized();
return this.Example.Controller;
}
}
/// <summary>
/// Gets the plot model for the default example.
/// </summary>
/// <value>
/// The plot model.
/// </value>
public PlotModel PlotModel
{
get
{
this.EnsureInitialized();
return this.Example.Model;
}
}
/// <summary>
/// Gets the tags.
/// </summary>
/// <value>
/// The tags.
/// </value>
public string[] Tags { get; }
/// <summary>
/// Gets a value indiciating whether this example should be excluded from automated tests.
/// </summary>
/// <value>
/// <c>true</c> if the example should be excluded from automated tests, otherwise <c>false</c>.
/// </value>
public bool ExcludeFromAutomatedTests { get; }
/// <summary>
/// Gets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title { get; }
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return this.Title;
}
/// <summary>
/// Prepares <see cref="ExampleFlags"/> from the given parameters.
/// </summary>
/// <param name="transpose">Whether to set the <see cref="ExampleFlags.Transpose"/> flag.</param>
/// <param name="reverse">Whether to set the <see cref="ExampleFlags.Reverse"/> flag.</param>
/// <returns>The <see cref="ExampleFlags"/>.</returns>
public static ExampleFlags PrepareFlags(bool transpose, bool reverse)
{
var flags = (ExampleFlags)0;
if (transpose)
{
flags |= ExampleFlags.Transpose;
}
if (reverse)
{
flags |= ExampleFlags.Reverse;
}
return flags;
}
/// <summary>
/// Initializes this instance if it is not already initialized.
/// </summary>
private void EnsureInitialized()
{
if (this.initialized)
{
return;
}
this.initialized = true;
this.examples = new Dictionary<ExampleFlags, Example>();
this.Example = GetDefaultExample();
this.exampleSupport = PrepareFlags(this.Example.Model?.IsTransposable() == true, this.Example.Model?.IsReversible() == true);
// remember the 'default' model: loads the transposed/reversed ones as we need them
this.examples.Add(PrepareFlags(false, false), this.Example);
}
/// <summary>
/// Gets a new instance of the default example.
/// </summary>
/// <returns>An <see cref="Example"/>.</returns>
private Example GetDefaultExample()
{
var result = this.method.Invoke(null, null);
if (result is null)
{
return new Example(null, null);
}
if (result is PlotModel plotModel)
{
return new Example(plotModel, null);
}
else if (result is Example example)
{
return example;
}
throw new Exception($"Unsupport type returned by example method for example {Category} > {Title}: {result.GetType().FullName}.");
}
/// <summary>
/// Filters unsupported flags from the given flags.
/// </summary>
/// <param name="flags">The original set of flags.</param>
/// <returns>The filtered flags.</returns>
private ExampleFlags FilterFlags(ExampleFlags flags)
{
return flags & exampleSupport;
}
}
}