-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNullableTests.cs
174 lines (161 loc) · 6.9 KB
/
NullableTests.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
using System.Text.RegularExpressions;
using Cecilifier.Core.Tests.Tests.Unit.Framework;
using NUnit.Framework;
namespace Cecilifier.Core.Tests.Tests.Unit;
[TestFixture]
public class NullableTests : CecilifierUnitTestBase
{
[TestCase("object", TestName = "object")]
[TestCase("Foo", TestName = "Foo")]
[TestCase("IFoo", TestName = "IFoo")]
[TestCase("int", TestName = "int")]
public void MethodsWithNullableParameters_AreRegistered_Once(string parameterType)
{
var result = RunCecilifier(
$$"""
interface IFoo {}
class Foo : IFoo
{
void M({{parameterType}}? o) {}
void M2({{parameterType}} p) => M(p);
}
""");
var actual = result.GeneratedCode.ReadToEnd();
Assert.That(
Regex.Count(actual, """var .+ = new MethodDefinition\("M", MethodAttributes.Private \| MethodAttributes.HideBySig, assembly.MainModule.TypeSystem.Void\);\n"""),
Is.EqualTo(1),
"Only only declaration for method M() is expected.");
}
[TestCase("object", @".+TypeSystem\.Object", TestName = "object")]
[TestCase("Foo", "cls_foo_1", TestName = "Foo")]
[TestCase("IFoo", "itf_iFoo_0", TestName = "IFoo")]
[TestCase("int", @".+ImportReference\(typeof\(System.Nullable<>\)\).MakeGenericInstanceType\(.+Int32\)", TestName = "int")]
public void MethodsWithNullableReturnTypes_AreRegistered_Once(string returnType, string expectedReturnTypeInDeclaration)
{
var result = RunCecilifier(
$$"""
interface IFoo {}
class Foo : IFoo
{
{{returnType}}? M() => default({{returnType}});
void M2() => M();
}
""");
var actual = result.GeneratedCode.ReadToEnd();
Assert.That(
Regex.Count(actual, $$"""var m_M_\d+ = new MethodDefinition\("M", MethodAttributes.+, {{expectedReturnTypeInDeclaration}}\);\n"""),
Is.EqualTo(1),
actual);
}
[TestCase(
"""
class Foo
{
int Bar(int? i) => i.Value;
int? Test(int i1) { return Bar(i1); } // i1 should be converted to Nullable<int> and Bar() return also.
}
""",
"""
//return Bar\(i1\);
(\s+il_test_\d+\.Emit\(OpCodes\.)Ldarg_0\);
\1Ldarg_1\);
(?<implicit_nullable_conversion>\1Newobj, .+ImportReference\(typeof\(System.Nullable<>\).MakeGenericType\(typeof\(System.Int32\)\).GetConstructors\(\).+;)
\1Call, m_bar_1\);
\k<implicit_nullable_conversion>
\1Ret\);
""",
TestName = "Method parameter and return value"
)]
[TestCase(
"""
class Foo
{
void Bar(int? p)
{
p = 41;
int ?lp;
lp = 42;
}
}
""",
"""
//p = 41;
(\s+il_bar_\d+\.Emit\(OpCodes\.)Ldc_I4, 41\);
\1Newobj, .+ImportReference\(typeof\(System.Nullable<>\).MakeGenericType\(typeof\(System.Int32\)\).GetConstructors\(\).+;
\1Starg_S, p_p_3\);
""",
TestName = "Variable assignment"
)]
public void ImplicitNullableConversions_AreApplied(string code, string expectedSnippet)
{
//https://github.com/adrianoc/cecilifier/issues/251
var result = RunCecilifier(code);
Assert.That(result.GeneratedCode.ReadToEnd(), Does.Match(expectedSnippet));
}
[Test]
public void ConstructorIsInvokedAfterCast()
{
var result = RunCecilifier("""int? M(object o) => (int) o;""");
Assert.That(result.GeneratedCode.ReadToEnd(), Does.Match("""
\s+//\(int\) o
(?<emit>\s+il_M_\d+\.Emit\(OpCodes\.)Ldarg_0\);
\k<emit>Unbox_Any, assembly.MainModule.TypeSystem.Int32\);
\k<emit>Newobj,.+ImportReference\(typeof\(System.Nullable<>\)\.MakeGenericType\(typeof\(System.Int32\)\)\.GetConstructors\(\).+\);
\k<emit>Ret\);
"""));
}
[TestCase(/*language=C#*/
//"class C { void M(System.DateTime p) => p = default; }",
"class C { void M(int? p) => p = null; }",
"""
//p = null
\s+il_M_2.Emit\(OpCodes.Ldarga, p_p_\d+\);
\s+il_M_2.Emit\(OpCodes.Initobj, .+ImportReference\(typeof\(System.Nullable<>\)\).MakeGenericInstanceType\(.+Int32\)\);
""",
TestName = "Parameter")]
[TestCase(/*language=C#*/
"class C { void M() { int? i; i = null; } }",
"""
//i = null;
\s+il_M_2.Emit\(OpCodes.Ldloca, l_i_\d+\);
\s+il_M_2.Emit\(OpCodes.Initobj, .+ImportReference\(typeof\(System.Nullable<>\)\).MakeGenericInstanceType\(.+Int32\)\);
""",
TestName = "Local variable assignment")]
[TestCase(/*language=C#*/
"class C { void M() { int? i = null; } }",
"""
//int\? i = null;
\s+var (?<var>l_i_\d+) = new VariableDefinition\((?<nullable>.+ImportReference\(typeof\(System.Nullable<>\)\)\.MakeGenericInstanceType\(.+Int32\))\);
\s+m_M_1.Body.Variables.Add\(\k<var>\);
(?<emit>\s+il_M_\d+\.Emit\(OpCodes\.)Ldloca, \k<var>\);
\k<emit>Initobj, \k<nullable>\);
""",
TestName = "Local variable initializer")]
[TestCase(/*language=C#*/
"class C { void M(int? p) => M(null); }",
"""
//M\(null\)
(\s+il_M_\d+\.Emit\(OpCodes\.)Ldarg_0\);
\s+var (?<var>l_tmpNull_\d+) = new VariableDefinition\(.+ImportReference\(.+System.Nullable<>\)\).MakeGenericInstanceType\(.+Int32\)\);
\s+m_M_\d+.Body.Variables.Add\(\k<var>\);
\1Ldloca_S, \k<var>\);
\1Initobj, .+ImportReference\(typeof\(System.Nullable<>\)\).MakeGenericInstanceType\(.+Int32\)\);
(\s+il_M_\d+\.Emit\(OpCodes\.)Ldloc_S, \k<var>\);
\1Call, m_M_\d+\);
""",
TestName = "Argument")]
[TestCase(/*language=C#*/
"class C { void M(int? p) => f = null; int ?f; }",
"""
//f = null
(\s+il_M_\d+\.Emit\(OpCodes\.)Ldarg_0\);
\1Ldflda, fld_f_\d+\);
\1Initobj, .+ImportReference\(typeof\(System.Nullable<>\)\).MakeGenericInstanceType\(.+Int32\)\);
""",
TestName = "Field")]
public void InitializingWithNull_EmitsCorrectCode(string code, string expectedSnippet)
{
var result = RunCecilifier(code);
Assert.That(result.GeneratedCode.ReadToEnd(), Does.Match(expectedSnippet));
}
}