-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCodeTests.cs
More file actions
388 lines (321 loc) · 13.6 KB
/
CodeTests.cs
File metadata and controls
388 lines (321 loc) · 13.6 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
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
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace SharpCode.Test;
[TestFixture]
public class CodeTests
{
[Test]
public void CreateNamespace_Works()
{
var expected = @"
namespace TestNamespace
{
}
".Trim().WithUnixEOL();
// CreateNamespace()
Assert.AreEqual(expected, Code.CreateNamespace().WithName("TestNamespace").ToSourceCode().WithUnixEOL());
// CreateNamespace(string)
Assert.AreEqual(expected, Code.CreateNamespace("TestNamespace").ToSourceCode().WithUnixEOL());
}
[Test]
public void CreateNamespace_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateNamespace(name: null),
"Generating the source code for a namespace with null as a name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateNamespace(name: string.Empty),
"Generating the source code for a namespace with an empty name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateNamespace(name: " "),
"Generating the source code for a namespace with a whitespace name should throw an exception.");
}
[Test]
public void CreateEnum_Works()
{
var expected = @"
public enum TestEnum
{
}
".Trim().WithUnixEOL();
// CreateEnum()
Assert.AreEqual(expected, Code.CreateEnum().WithName("TestEnum").ToSourceCode().WithUnixEOL());
// CreateEnum(string, AccessModifier)
Assert.AreEqual(expected, Code.CreateEnum("TestEnum").ToSourceCode().WithUnixEOL());
}
[Test]
public void CreateEnum_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateEnum(name: null),
"Generating the source for an enum with null as a name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateEnum(name: string.Empty),
"Generating the source for an enum with an empty name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateEnum(name: " "),
"Generating the source for an enum with a whitespace name should throw an exception.");
}
[Test]
public void CreateEnumMember_Works()
{
var expected = @"
public enum TestEnum
{
OptionOne = 0,
OptionTwo = 1
}
".Trim().WithUnixEOL();
Assert.AreEqual(
expected,
Code.CreateEnum("TestEnum")
.WithMembers(
Code.CreateEnumMember().WithName("OptionOne").WithValue(0),
Code.CreateEnumMember("OptionTwo", 1))
.ToSourceCode()
.WithUnixEOL());
}
[Test]
public void CreateEnumMember_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateEnumMember(null),
"Generating the source code for an enum member with null as name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateEnumMember(string.Empty),
"Generating the source code for an enum member with an empty string as name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateEnumMember(" "),
"Generating the source code for an enum member with whitespace name should throw an exception.");
}
[Test]
public void CreateInterface_Works()
{
var expected = @"
public interface ITest
{
}
".Trim().WithUnixEOL();
// CreateInterface()
Assert.AreEqual(expected, Code.CreateInterface().WithName("ITest").ToSourceCode().WithUnixEOL());
// CreateInterface(string, AccessModifier)
Assert.AreEqual(expected, Code.CreateInterface("ITest").ToSourceCode().WithUnixEOL());
}
[Test]
public void CreateInterface_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateInterface(name: null),
"Generating the source for an interface with null as a name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateInterface(name: string.Empty),
"Generating the source for an interface with an empty name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateInterface(name: " "),
"Generating the source for an interface with a whitespace name should throw an exception.");
}
[Test]
public void CreateClass_Works()
{
var expected = @"
public class Test
{
}
".Trim().WithUnixEOL();
// CreateClass()
Assert.AreEqual(expected, Code.CreateClass().WithName("Test").ToSourceCode().WithUnixEOL());
// CreateClass(string, AccessModifier)
Assert.AreEqual(expected, Code.CreateClass("Test").ToSourceCode().WithUnixEOL());
}
[Test]
public void CreateClass_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateClass(name: null),
"Generating the source for a class with null as a name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateClass(name: string.Empty),
"Generating the source for a class with an empty name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateClass(name: " "),
"Generating the source for a class with a whitespace name should throw an exception.");
}
[Test]
public void CreateStruct_Works()
{
var expected = @"
public struct Test
{
}
".Trim().WithUnixEOL();
// CreateStruct()
Assert.AreEqual(expected, Code.CreateStruct().WithName("Test").ToSourceCode().WithUnixEOL());
// CreateStruct(string, AccessModifier)
Assert.AreEqual(expected, Code.CreateStruct("Test").ToSourceCode().WithUnixEOL());
}
[Test]
public void CreateStruct_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateStruct(name: null),
"Generating the source for a struct with null as a name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateStruct(name: string.Empty),
"Generating the source for a struct with an empty name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateStruct(name: " "),
"Generating the source for a struct with a whitespace name should throw an exception.");
}
[Test]
public void CreateField_Works()
{
var expected = "private Action _field;";
// CreateField()
Assert.AreEqual(
expected,
Code.CreateField()
.WithType(typeof(Action))
.WithName("_field")
.ToSourceCode()
.WithUnixEOL());
// CreateField(string, string, AccessModifier)
Assert.AreEqual(
expected,
Code.CreateField("Action", "_field")
.ToSourceCode()
.WithUnixEOL());
// CreateField(Type, string, AccessModifier)
Assert.AreEqual(
expected,
Code.CreateField(typeof(Action), "_field")
.ToSourceCode()
.WithUnixEOL());
}
[Test]
public void CreateField_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateField(name: null, type: "string"),
"Generating the source code for a field with 'null' as name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateField(name: string.Empty, type: "string"),
"Generating the source code for a field with an empty string as name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateField(name: " ", type: "string"),
"Generating the source code for a field with a whitespace string as name should throw an exception.");
}
[Test]
public void CreateField_WithInvalidType_Throws()
{
// CreateField(string, string)
Assert.Throws<ArgumentNullException>(
() => Code.CreateField(name: "test", type: null as string),
"Generating the source code for a field with 'null' as type should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateField(name: "test", type: string.Empty),
"Generating the source code for a field with an empty string as type should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateField(name: "test", type: " "),
"Generating the source code for a field with a whitespace string as type should throw an exception.");
// CreateField(string, Type)
Assert.Throws<ArgumentNullException>(
() => Code.CreateField(name: "test", type: null as Type),
"Generating the source code for a field with 'null' as type should throw an exception.");
}
[Test]
public void CreateProperty_Works()
{
var expected = "public Action DoWork { get; set; }";
// CreateProperty()
Assert.AreEqual(
expected,
Code.CreateProperty()
.WithType(typeof(Action))
.WithName("DoWork")
.ToSourceCode()
.WithUnixEOL());
// CreateProperty(string, string, AccessModifier)
Assert.AreEqual(
expected,
Code.CreateProperty("Action", "DoWork")
.ToSourceCode()
.WithUnixEOL());
// CreateProperty(string, string, AccessModifier, AccessModifier)
Assert.AreEqual(
expected,
Code.CreateProperty("Action", "DoWork", AccessModifier.Public, AccessModifier.Public)
.ToSourceCode()
.WithUnixEOL());
// CreateProperty(Type, string, AccessModifier)
Assert.AreEqual(
expected,
Code.CreateProperty(typeof(Action), "DoWork")
.ToSourceCode()
.WithUnixEOL());
// CreateProperty(Type, string, AccessModifier, AccessModifier)
Assert.AreEqual(
expected,
Code.CreateProperty(typeof(Action), "DoWork", AccessModifier.Public, AccessModifier.Public)
.ToSourceCode()
.WithUnixEOL());
}
[Test]
public void CreateProperty_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateProperty(name: null, type: "string"),
"Generating the source code for a property with 'null' as name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateProperty(name: string.Empty, type: "string"),
"Generating the source code for a property with an empty string as name should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateProperty(name: " ", type: "string"),
"Generating the source code for a property with a whitespace string as name should throw an exception.");
}
[Test]
public void CreateProperty_WithInvalidType_Throws()
{
// CreateProperty(string, string)
Assert.Throws<ArgumentNullException>(
() => Code.CreateProperty(name: "test", type: null as string),
"Generating the source code for a property with 'null' as type should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateProperty(name: "test", type: string.Empty),
"Generating the source code for a property with an empty string as type should throw an exception.");
Assert.Throws<ArgumentException>(
() => Code.CreateProperty(name: "test", type: " "),
"Generating the source code for a property with a whitespace string as type should throw an exception.");
// CreateProperty(string, Type)
Assert.Throws<ArgumentNullException>(
() => Code.CreateProperty(name: "test", type: null as Type),
"Generating the source code for a property with 'null' as type should throw an exception.");
}
[Test]
public void CreateTypeParameter_WithInvalidName_Throws()
{
Assert.Throws<ArgumentNullException>(
() => Code.CreateTypeParameter(name: null),
"Creating type parameter with name 'null' should throw.");
Assert.Throws<ArgumentException>(
() => Code.CreateTypeParameter(name: string.Empty),
"Creating a type parameter with an empty string as name should throw.");
Assert.Throws<ArgumentException>(
() => Code.CreateTypeParameter(name: " "),
"Creating a type parameter with a whitespace string as name should throw.");
}
[Test]
public void CreateTypeParameter_WithInvalidConstraints_Throws()
{
// CreateTypeParameter(string, params)
Assert.Throws<ArgumentNullException>(
() => Code.CreateTypeParameter("T", null as string[]));
Assert.Throws<ArgumentException>(
() => Code.CreateTypeParameter("T", new string[] { null }));
// CreateTypeParameter(string, IEnumerable)
Assert.Throws<ArgumentNullException>(
() => Code.CreateTypeParameter("T", null as IEnumerable<string>));
Assert.Throws<ArgumentException>(
() => Code.CreateTypeParameter("T", new List<string> { null }));
}
}