forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpHelperTest.cs
More file actions
780 lines (654 loc) · 27.2 KB
/
CSharpHelperTest.cs
File metadata and controls
780 lines (654 loc) · 27.2 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Numerics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;
namespace Microsoft.EntityFrameworkCore.Design.Internal;
public class CSharpHelperTest
{
private static readonly string EOL = Environment.NewLine;
[ConditionalTheory]
[InlineData(
"single-line string with \"",
"\"single-line string with \\\"\"")]
[InlineData(
true,
"true")]
[InlineData(
false,
"false")]
[InlineData(
(byte)42,
"(byte)42")]
[InlineData(
'A',
"'A'")]
[InlineData(
'\'',
@"'\''")]
[InlineData(
4.2,
"4.2000000000000002")]
[InlineData(
double.NegativeInfinity,
"double.NegativeInfinity")]
[InlineData(
double.PositiveInfinity,
"double.PositiveInfinity")]
[InlineData(
double.NaN,
"double.NaN")]
[InlineData(
0.84551240822557006,
"0.84551240822557006")]
[InlineData(
6E-14,
"5.9999999999999997E-14")]
[InlineData(
-1.7976931348623157E+308, // Double MinValue
"-1.7976931348623157E+308")]
[InlineData(
1.7976931348623157E+308, // Double MaxValue
"1.7976931348623157E+308")]
[InlineData(
4.2f,
"4.2f")]
[InlineData(
-3.402823E+38f, // Single MinValue
"-3.402823E+38f")]
[InlineData(
3.402823E+38f, // Single MaxValue
"3.402823E+38f")]
[InlineData(
42,
"42")]
[InlineData(
42L,
"42L")]
[InlineData(
9000000000000000000L, // Ensure not printed as exponent
"9000000000000000000L")]
[InlineData(
(sbyte)42,
"(sbyte)42")]
[InlineData(
(short)42,
"(short)42")]
[InlineData(
42u,
"42u")]
[InlineData(
42ul,
"42ul")]
[InlineData(
18000000000000000000ul, // Ensure not printed as exponent
"18000000000000000000ul")]
[InlineData(
(ushort)42,
"(ushort)42")]
[InlineData(
"",
"\"\"")]
[InlineData(
SomeEnum.Default,
"CSharpHelperTest.SomeEnum.Default")]
public void Literal_works(object value, string expected)
{
var literal = new CSharpHelper(TypeMappingSource).UnknownLiteral(value);
Assert.Equal(expected, literal);
}
[ConditionalFact]
public void Literal_works_when_empty_ByteArray()
=> Literal_works(
Array.Empty<byte>(),
"new byte[0]");
[ConditionalFact]
public void Literal_works_when_single_ByteArray()
=> Literal_works(
new byte[] { 1 },
"new byte[] { 1 }");
[ConditionalFact]
public void Literal_works_when_many_ByteArray()
=> Literal_works(
new byte[] { 1, 2 },
"new byte[] { 1, 2 }");
[ConditionalFact]
public void Literal_works_when_empty_list()
=> Literal_works(
new List<string>(),
@"new List<string>()");
[ConditionalFact]
public void Literal_works_when_list_with_single_element()
=> Literal_works(
new List<string> { "one" },
@"new List<string> { ""one"" }");
[ConditionalFact]
public void Literal_works_when_list_of_mixed_objects()
=> Literal_works(
new List<object> { 1, "two" },
@"new List<object> { 1, ""two"" }");
[ConditionalFact]
public void Literal_works_when_list_with_ctor_arguments()
=> Literal_works(
new List<string>(new [] { "one" }) { "two", "three" },
@"new List<string> { ""one"", ""two"", ""three"" }");
[ConditionalFact]
public void Literal_works_when_multiline_string()
=> Literal_works(
"multi-line\r\nstring\nwith\r\"",
"\"multi-line\\r\\nstring\\nwith\\r\\\"\"");
[ConditionalFact]
[UseCulture("de-DE")]
public void Literal_works_when_DateTime()
=> Literal_works(
new DateTime(2015, 3, 15, 20, 45, 17, 300, DateTimeKind.Local),
"new DateTime(2015, 3, 15, 20, 45, 17, 300, DateTimeKind.Local)");
[ConditionalFact]
[UseCulture("de-DE")]
public void Literal_works_when_DateTimeOffset()
=> Literal_works(
new DateTimeOffset(new DateTime(2015, 3, 15, 19, 43, 47, 500), new TimeSpan(-7, 0, 0)),
"new DateTimeOffset(new DateTime(2015, 3, 15, 19, 43, 47, 500, DateTimeKind.Unspecified), new TimeSpan(0, -7, 0, 0, 0))");
[ConditionalFact]
public void Literal_works_when_decimal()
=> Literal_works(
4.2m,
"4.2m");
[ConditionalFact]
public void Literal_works_when_decimal_max_value()
=> Literal_works(
79228162514264337593543950335m, // Decimal MaxValue
"79228162514264337593543950335m");
[ConditionalFact]
public void Literal_works_when_decimal_min_value()
=> Literal_works(
-79228162514264337593543950335m, // Decimal MinValue
"-79228162514264337593543950335m");
[ConditionalFact]
public void Literal_works_when_Guid()
=> Literal_works(
new Guid("fad4f3c3-9501-4b3a-af99-afeb496f7664"),
"new Guid(\"fad4f3c3-9501-4b3a-af99-afeb496f7664\")");
[ConditionalFact]
public void Literal_works_when_TimeSpan()
=> Literal_works(
new TimeSpan(17, 21, 42, 37, 250),
"new TimeSpan(17, 21, 42, 37, 250)");
[ConditionalFact]
public void Literal_works_when_NullableInt()
=> Literal_works(
(int?)42,
"42");
[ConditionalFact]
public void Literal_works_when_StringArray()
{
var literal = new CSharpHelper(TypeMappingSource).Literal(new[] { "A", "B" });
Assert.Equal("new[] { \"A\", \"B\" }", literal);
}
[ConditionalFact]
public void Literal_works_when_empty_StringArray()
{
var literal = new CSharpHelper(TypeMappingSource).Literal(new string[] { });
Assert.Equal("new string[0]", literal);
}
[ConditionalFact]
public void Literal_works_when_ObjectArray()
{
var literal = new CSharpHelper(TypeMappingSource).Literal(new object[] { 'A', 1 });
Assert.Equal("new object[] { 'A', 1 }", literal);
}
[ConditionalFact]
public void Literal_works_when_MultidimensionalArray()
{
var value = new object[,] { { 'A', 1 }, { 'B', 2 } };
var result = new CSharpHelper(TypeMappingSource).Literal(value);
Assert.Equal(
"new object[,]" + EOL + "{" + EOL + " { 'A', 1 }," + EOL + " { 'B', 2 }" + EOL + "}",
result);
}
[ConditionalFact]
public void Literal_works_when_BigInteger()
=> Literal_works(
new BigInteger(42),
"BigInteger.Parse(\"42\", NumberFormatInfo.InvariantInfo)");
[ConditionalFact]
public void UnknownLiteral_throws_when_unknown()
{
var ex = Assert.Throws<InvalidOperationException>(
() => new CSharpHelper(TypeMappingSource).UnknownLiteral(new object()));
Assert.Equal(DesignStrings.UnknownLiteral(typeof(object)), ex.Message);
}
[ConditionalTheory]
[InlineData(typeof(int), "int")]
[InlineData(typeof(int?), "int?")]
[InlineData(typeof(int[]), "int[]")]
[InlineData(typeof(int[,]), "int[,]")]
[InlineData(typeof(int[][]), "int[][]")]
[InlineData(typeof(Generic<int>), "Generic<int>")]
[InlineData(typeof(Nested), "CSharpHelperTest.Nested")]
[InlineData(typeof(Generic<Generic<int>>), "Generic<Generic<int>>")]
[InlineData(typeof(MultiGeneric<int, int>), "MultiGeneric<int, int>")]
[InlineData(typeof(NestedGeneric<int>), "CSharpHelperTest.NestedGeneric<int>")]
[InlineData(typeof(Nested.DoubleNested), "CSharpHelperTest.Nested.DoubleNested")]
[InlineData(typeof(NestedGeneric<Nested.DoubleNested>), "CSharpHelperTest.NestedGeneric<CSharpHelperTest.Nested.DoubleNested>")]
public void Reference_works(Type type, string expected)
=> Assert.Equal(expected, new CSharpHelper(TypeMappingSource).Reference(type));
private static class Nested
{
public class DoubleNested
{
}
}
internal class NestedGeneric<T>
{
}
private enum SomeEnum
{
Default
}
[ConditionalTheory]
[InlineData("dash-er", "dasher")]
[InlineData("params", "@params")]
[InlineData("true", "@true")]
[InlineData("yield", "yield")]
[InlineData("spac ed", "spaced")]
[InlineData("1nders", "_1nders")]
[InlineData("name.space", "@namespace")]
[InlineData("$", "_")]
public void Identifier_works(string input, string expected)
=> Assert.Equal(expected, new CSharpHelper(TypeMappingSource).Identifier(input));
[ConditionalTheory]
[InlineData(new[] { "WebApplication1", "Migration" }, "WebApplication1.Migration")]
[InlineData(new[] { "WebApplication1.Migration" }, "WebApplication1.Migration")]
[InlineData(new[] { "ef-xplat.namespace" }, "efxplat.@namespace")]
[InlineData(new[] { "#", "$" }, "_._")]
[InlineData(new[] { "" }, "_")]
[InlineData(new string[] { }, "_")]
[InlineData(new string[] { null }, "_")]
public void Namespace_works(string[] input, string excepted)
=> Assert.Equal(excepted, new CSharpHelper(TypeMappingSource).Namespace(input));
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, true, 42);
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal(".TestFunc(true, 42)", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_with_arrays()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { "foo", "bar" });
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal(".TestFunc(new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { \"foo\", \"bar\" })", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_when_niladic()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo);
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal(".TestFunc()", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_when_chaining()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo)
.Chain(_testFuncMethodInfo);
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal($".TestFunc(){EOL}.TestFunc()", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_when_chaining_on_chain()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, "One")
.Chain(new MethodCallCodeFragment(_testFuncMethodInfo, "Two"))
.Chain(_testFuncMethodInfo, "Three");
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal(@$".TestFunc(""One""){EOL}.TestFunc(""Two""){EOL}.TestFunc(""Three"")", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_when_chaining_on_chain_with_call()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, "One")
.Chain(new MethodCallCodeFragment(_testFuncMethodInfo, "Two"))
.Chain(
new MethodCallCodeFragment(_testFuncMethodInfo, "Three").Chain(
new MethodCallCodeFragment(_testFuncMethodInfo, "Four")));
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal(@$".TestFunc(""One""){EOL}.TestFunc(""Two""){EOL}.TestFunc(""Three""){EOL}.TestFunc(""Four"")", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_when_nested_closure()
{
var method = new MethodCallCodeFragment(
_testFuncMethodInfo,
new NestedClosureCodeFragment("x", new MethodCallCodeFragment(_testFuncMethodInfo)));
var result = new CSharpHelper(TypeMappingSource).Fragment(method);
Assert.Equal(".TestFunc(x => x.TestFunc())", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_with_identifier()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, true, 42);
var result = new CSharpHelper(TypeMappingSource).Fragment(method, instanceIdentifier: "builder");
Assert.Equal("builder.TestFunc(true, 42)", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_with_identifier_chained()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, "One").Chain(new MethodCallCodeFragment(_testFuncMethodInfo));
var result = new CSharpHelper(TypeMappingSource).Fragment(method, instanceIdentifier: "builder");
Assert.Equal($@"builder{EOL} .TestFunc(""One""){EOL} .TestFunc()", result);
}
[ConditionalFact]
public void Fragment_MethodCallCodeFragment_works_with_type_qualified()
{
var method = new MethodCallCodeFragment(_testFuncMethodInfo, true, 42);
var result = new CSharpHelper(TypeMappingSource).Fragment(method, instanceIdentifier: "builder");
Assert.Equal("builder.TestFunc(true, 42)", result);
}
[ConditionalFact]
public void Really_unknown_literal_with_no_mapping_support()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(null);
Assert.Equal(
CoreStrings.LiteralGenerationNotSupported(nameof(SimpleTestType)),
Assert.Throws<NotSupportedException>(
() => new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType())).Message);
}
[ConditionalFact]
public void Literal_with_parameterless_constructor()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.New(typeof(SimpleTestType)));
Assert.Equal(
"new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType()",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_one_parameter_constructor()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.New(
typeof(SimpleTestType).GetConstructor(new[] { typeof(string) }),
Expression.Constant(v.Arg1, typeof(string))));
Assert.Equal(
"new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType(\"Jerry\")",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry")));
}
[ConditionalFact]
public void Literal_with_two_parameter_constructor()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.New(
typeof(SimpleTestType).GetConstructor(new[] { typeof(string), typeof(int?) }),
Expression.Constant(v.Arg1, typeof(string)),
Expression.Constant(v.Arg2, typeof(int?))));
Assert.Equal(
"new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType(\"Jerry\", 77)",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry", 77)));
}
[ConditionalFact]
public void Literal_with_parameterless_static_factory()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Call(
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.StaticCreate),
new Type[0])));
Assert.Equal(
"Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory.StaticCreate()",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_one_parameter_static_factory()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Call(
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.StaticCreate),
new[] { typeof(string) }),
Expression.Constant(v.Arg1, typeof(string))));
Assert.Equal(
"Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory.StaticCreate(\"Jerry\")",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry")));
}
[ConditionalFact]
public void Literal_with_two_parameter_static_factory()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Call(
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.StaticCreate),
new[] { typeof(string), typeof(int?) }),
Expression.Constant(v.Arg1, typeof(string)),
Expression.Constant(v.Arg2, typeof(int?))));
Assert.Equal(
"Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory.StaticCreate(\"Jerry\", 77)",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry", 77)));
}
[ConditionalFact]
public void Literal_with_parameterless_instance_factory()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Call(
Expression.New(typeof(SimpleTestTypeFactory)),
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.Create),
new Type[0])));
Assert.Equal(
"new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory().Create()",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_one_parameter_instance_factory()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Convert(
Expression.Call(
Expression.New(typeof(SimpleTestTypeFactory)),
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.Create),
new[] { typeof(string) }),
Expression.Constant(v.Arg1, typeof(string))),
typeof(SimpleTestType)));
Assert.Equal(
"(Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType)new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory().Create(\"Jerry\")",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry", 77)));
}
[ConditionalFact]
public void Literal_with_two_parameter_instance_factory()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Convert(
Expression.Call(
Expression.New(
typeof(SimpleTestTypeFactory).GetConstructor(new[] { typeof(string) }),
Expression.Constant("4096", typeof(string))),
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.Create),
new[] { typeof(string), typeof(int?) }),
Expression.Constant(v.Arg1, typeof(string)),
Expression.Constant(v.Arg2, typeof(int?))),
typeof(SimpleTestType)));
Assert.Equal(
"(Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType)new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory(\"4096\").Create(\"Jerry\", 77)",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry", 77)));
}
[ConditionalFact]
public void Literal_with_two_parameter_instance_factory_and_internal_cast()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Convert(
Expression.Call(
Expression.New(
typeof(SimpleTestTypeFactory).GetConstructor(new[] { typeof(string) }),
Expression.Constant("4096", typeof(string))),
typeof(SimpleTestTypeFactory).GetMethod(
nameof(SimpleTestTypeFactory.Create),
new[] { typeof(string), typeof(int?) }),
Expression.Constant(v.Arg1, typeof(string)),
Expression.Convert(
Expression.Constant(v.Arg2, typeof(int)),
typeof(int?))),
typeof(SimpleTestType)));
Assert.Equal(
"(Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType)new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestTypeFactory(\"4096\").Create(\"Jerry\", (int?)77)",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType("Jerry", 77)));
}
[ConditionalFact]
public void Literal_with_static_field()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Field(null, typeof(SimpleTestType).GetField(nameof(SimpleTestType.SomeStaticField))));
Assert.Equal(
"Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType.SomeStaticField",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_static_property()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Property(null, typeof(SimpleTestType).GetProperty(nameof(SimpleTestType.SomeStaticProperty))));
Assert.Equal(
"Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType.SomeStaticProperty",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_instance_property()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Property(
Expression.New(typeof(SimpleTestType)),
typeof(SimpleTestType).GetProperty(nameof(SimpleTestType.SomeInstanceProperty))));
Assert.Equal(
"new Microsoft.EntityFrameworkCore.Design.Internal.SimpleTestType().SomeInstanceProperty",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_add()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Add(
Expression.Constant(10),
Expression.Constant(10)));
Assert.Equal(
"10 + 10",
new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType()));
}
[ConditionalFact]
public void Literal_with_unsupported_node_throws()
{
var typeMapping = CreateTypeMappingSource<SimpleTestType>(
v => Expression.Multiply(
Expression.Constant(10),
Expression.Constant(10)));
Assert.Equal(
DesignStrings.LiteralExpressionNotSupported(
"(10 * 10)",
nameof(SimpleTestType)),
Assert.Throws<NotSupportedException>(
() => new CSharpHelper(typeMapping).UnknownLiteral(new SimpleTestType())).Message);
}
private IRelationalTypeMappingSource TypeMappingSource { get; } = CreateTypeMappingSource();
private static SqlServerTypeMappingSource CreateTypeMappingSource<T>(
Func<T, Expression> literalExpressionFunc)
=> CreateTypeMappingSource(new TestTypeMappingPlugin<T>(literalExpressionFunc));
private static SqlServerTypeMappingSource CreateTypeMappingSource(
params IRelationalTypeMappingSourcePlugin[] plugins)
=> new(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
new RelationalTypeMappingSourceDependencies(
plugins));
private class TestTypeMappingPlugin<T> : IRelationalTypeMappingSourcePlugin
{
private readonly Func<T, Expression> _literalExpressionFunc;
public TestTypeMappingPlugin(Func<T, Expression> literalExpressionFunc)
{
_literalExpressionFunc = literalExpressionFunc;
}
public RelationalTypeMapping FindMapping(in RelationalTypeMappingInfo mappingInfo)
=> _literalExpressionFunc == null
? new SimpleTestNonImplementedTypeMapping()
: new SimpleTestTypeMapping<T>(_literalExpressionFunc);
}
private class SimpleTestTypeMapping<T> : RelationalTypeMapping
{
private readonly Func<T, Expression> _literalExpressionFunc;
public SimpleTestTypeMapping(
Func<T, Expression> literalExpressionFunc)
: base("storeType", typeof(SimpleTestType))
{
_literalExpressionFunc = literalExpressionFunc;
}
public override Expression GenerateCodeLiteral(object value)
=> _literalExpressionFunc((T)value);
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> throw new NotSupportedException();
}
private class SimpleTestNonImplementedTypeMapping : RelationalTypeMapping
{
public SimpleTestNonImplementedTypeMapping()
: base("storeType", typeof(SimpleTestType))
{
}
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> throw new NotSupportedException();
}
private static readonly MethodInfo _testFuncMethodInfo
= typeof(CSharpHelperTest).GetRuntimeMethod(
nameof(TestFunc),
new[] { typeof(object), typeof(object), typeof(object), typeof(object) });
public static void TestFunc(object builder, object o1, object o2, object o3)
=> throw new NotSupportedException();
}
internal class SimpleTestType
{
public static readonly int SomeStaticField = 8;
public readonly int SomeField = 8;
public static int SomeStaticProperty { get; } = 8;
public int SomeInstanceProperty { get; } = 8;
public SimpleTestType()
{
}
public SimpleTestType(string arg1)
: this(arg1, null)
{
}
public SimpleTestType(string arg1, int? arg2)
{
Arg1 = arg1;
Arg2 = arg2;
}
public string Arg1 { get; }
public int? Arg2 { get; }
}
internal class SimpleTestTypeFactory
{
public SimpleTestTypeFactory()
{
}
public SimpleTestTypeFactory(string factoryArg)
{
FactoryArg = factoryArg;
}
public string FactoryArg { get; }
public SimpleTestType Create()
=> new();
public object Create(string arg1)
=> new SimpleTestType(arg1);
public object Create(string arg1, int? arg2)
=> new SimpleTestType(arg1, arg2);
public static SimpleTestType StaticCreate()
=> new();
public static object StaticCreate(string arg1)
=> new SimpleTestType(arg1);
public static object StaticCreate(string arg1, int? arg2)
=> new SimpleTestType(arg1, arg2);
}
internal class Generic<T>
{
}
internal class MultiGeneric<T1, T2>
{
}