Skip to content

Commit 6d7c39c

Browse files
committed
tests for autoprops
1 parent 820e255 commit 6d7c39c

File tree

4 files changed

+235
-11
lines changed

4 files changed

+235
-11
lines changed

test/coverlet.core.coverage.tests/Coverage/CoverageTests.AutoProps.cs

Lines changed: 182 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@ public partial class CoverageTests
1616
[Theory]
1717
[InlineData(true)]
1818
[InlineData(false)]
19-
public void SkipAutoProps(bool skipAutoProps)
19+
public void SkipClassWithAutoProps(bool skipAutoProps)
2020
{
2121
string path = Path.GetTempFileName();
2222
try
2323
{
2424
FunctionExecutor.Run(async (string[] parameters) =>
2525
{
26-
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AutoProps>(instance =>
26+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<ClassWithAutoProps>(instance =>
2727
{
2828
instance.AutoPropsNonInit = 10;
2929
instance.AutoPropsInit = 20;
3030
int readValue = instance.AutoPropsNonInit;
3131
readValue = instance.AutoPropsInit;
32+
readValue = instance.AutoPropsInitKeyword;
3233
return Task.CompletedTask;
3334
},
3435
persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1]));
@@ -40,8 +41,8 @@ public void SkipAutoProps(bool skipAutoProps)
4041
{
4142
TestInstrumentationHelper.GetCoverageResult(path)
4243
.Document("Instrumentation.AutoProps.cs")
43-
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 12, 13)
44-
.AssertNonInstrumentedLines(BuildConfiguration.Release, 12, 13)
44+
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 12, 14)
45+
.AssertNonInstrumentedLines(BuildConfiguration.Release, 12, 14)
4546
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 9, 11)
4647
.AssertLinesCovered(BuildConfiguration.Debug, (7, 1))
4748
.AssertLinesCovered(BuildConfiguration.Release, (10, 1));
@@ -50,9 +51,184 @@ public void SkipAutoProps(bool skipAutoProps)
5051
{
5152
TestInstrumentationHelper.GetCoverageResult(path)
5253
.Document("Instrumentation.AutoProps.cs")
53-
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 7, 13)
54+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 7, 14)
5455
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 10, 10)
55-
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 12, 13);
56+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 12, 14);
57+
}
58+
}
59+
finally
60+
{
61+
File.Delete(path);
62+
}
63+
}
64+
65+
[Theory]
66+
[InlineData(true)]
67+
[InlineData(false)]
68+
public void SkipClassWithAutoPropsPrimaryConstructor(bool skipAutoProps)
69+
{
70+
string path = Path.GetTempFileName();
71+
try
72+
{
73+
FunctionExecutor.Run(async (string[] parameters) =>
74+
{
75+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<ClassWithAutoPropsPrimaryConstructor>(instance =>
76+
{
77+
return Task.CompletedTask;
78+
},
79+
persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1]));
80+
81+
return 0;
82+
}, [path, skipAutoProps.ToString()]);
83+
84+
if (skipAutoProps)
85+
{
86+
TestInstrumentationHelper.GetCoverageResult(path)
87+
.Document("Instrumentation.AutoProps.cs")
88+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 28, 28)
89+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 28, 28)
90+
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 30, 31, 32)
91+
.AssertNonInstrumentedLines(BuildConfiguration.Release, 30, 31, 32);
92+
}
93+
else
94+
{
95+
TestInstrumentationHelper.GetCoverageResult(path)
96+
.Document("Instrumentation.AutoProps.cs")
97+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 28, 28)
98+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 30, 32)
99+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 28, 28)
100+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 30, 32);
101+
}
102+
}
103+
finally
104+
{
105+
File.Delete(path);
106+
}
107+
}
108+
109+
[Theory]
110+
[InlineData(true)]
111+
[InlineData(false)]
112+
public void SkipRecordWithAutoProps(bool skipAutoProps)
113+
{
114+
string path = Path.GetTempFileName();
115+
try
116+
{
117+
FunctionExecutor.Run(async (string[] parameters) =>
118+
{
119+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<RecordWithAutoProps>(instance =>
120+
{
121+
instance.AutoPropsNonInit = 10;
122+
instance.AutoPropsInit = 20;
123+
int readValue = instance.AutoPropsNonInit;
124+
readValue = instance.AutoPropsInit;
125+
readValue = instance.AutoPropsInitKeyword;
126+
return Task.CompletedTask;
127+
},
128+
persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1]));
129+
130+
return 0;
131+
}, [path, skipAutoProps.ToString()]);
132+
133+
if (skipAutoProps)
134+
{
135+
TestInstrumentationHelper.GetCoverageResult(path)
136+
.Document("Instrumentation.AutoProps.cs")
137+
.AssertNonInstrumentedLines(BuildConfiguration.Debug, 43, 45)
138+
.AssertNonInstrumentedLines(BuildConfiguration.Release, 43, 45)
139+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 40, 42)
140+
.AssertLinesCovered(BuildConfiguration.Debug, (39, 1))
141+
.AssertLinesCovered(BuildConfiguration.Release, (39, 1));
142+
}
143+
else
144+
{
145+
TestInstrumentationHelper.GetCoverageResult(path)
146+
.Document("Instrumentation.AutoProps.cs")
147+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 38, 45) // go on here
148+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 39, 39)
149+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 41, 45);
150+
}
151+
}
152+
finally
153+
{
154+
File.Delete(path);
155+
}
156+
}
157+
158+
[Theory]
159+
[InlineData(true)]
160+
[InlineData(false)]
161+
public void SkipRecordWithAutoPropsPrimaryConstructor(bool skipAutoProps)
162+
{
163+
string path = Path.GetTempFileName();
164+
try
165+
{
166+
FunctionExecutor.Run(async (string[] parameters) =>
167+
{
168+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<RecordsWithPrimaryConstructor>(instance =>
169+
{
170+
return Task.CompletedTask;
171+
},
172+
persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1]));
173+
174+
return 0;
175+
}, [path, skipAutoProps.ToString()]);
176+
177+
if (skipAutoProps)
178+
{
179+
TestInstrumentationHelper.GetCoverageResult(path)
180+
.Document("Instrumentation.AutoProps.cs")
181+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 50, 50)
182+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 36, 36);
183+
}
184+
else
185+
{
186+
TestInstrumentationHelper.GetCoverageResult(path)
187+
.Document("Instrumentation.AutoProps.cs")
188+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 50, 50)
189+
.AssertLinesCoveredFromTo(BuildConfiguration.Release, 36, 36);
190+
}
191+
}
192+
finally
193+
{
194+
File.Delete(path);
195+
}
196+
}
197+
198+
[Theory]
199+
[InlineData(true)]
200+
[InlineData(false)]
201+
public void SkipRecordWithAutoPropsPrimaryConstructorMultiline(bool skipAutoProps)
202+
{
203+
string path = Path.GetTempFileName();
204+
try
205+
{
206+
FunctionExecutor.Run(async (string[] parameters) =>
207+
{
208+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<RecordsWithPrimaryConstructor>(instance =>
209+
{
210+
return Task.CompletedTask;
211+
},
212+
persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1]));
213+
214+
return 0;
215+
}, [path, skipAutoProps.ToString()]);
216+
217+
if (skipAutoProps)
218+
{
219+
TestInstrumentationHelper.GetCoverageResult(path)
220+
.Document("Instrumentation.AutoProps.cs")
221+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 52, 55)
222+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 52, 55);
223+
}
224+
else
225+
{
226+
TestInstrumentationHelper.GetCoverageResult(path)
227+
.Document("Instrumentation.AutoProps.cs")
228+
.AssertLinesCovered(BuildConfiguration.Debug, 52, 55)
229+
.AssertLinesNotCovered(BuildConfiguration.Debug, 53, 54)
230+
.AssertLinesCovered(BuildConfiguration.Release, 52, 55)
231+
.AssertLinesNotCovered(BuildConfiguration.Release, 53, 54);
56232
}
57233
}
58234
finally

test/coverlet.core.coverage.tests/Coverage/CoverageTests.Records.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void SkipInheritingRecordsWithProperties(bool skipAutoProps)
109109
{
110110
FunctionExecutor.Run(async (string[] parameters) =>
111111
{
112-
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<ClassWithInheritingRecordsAndAutoProperties>(instance =>
112+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<ClassWithInheritingRecordsAndPrimaryConstructor>(instance =>
113113
{
114114
return Task.CompletedTask;
115115
},

test/coverlet.core.coverage.tests/Samples/Instrumentation.AutoProps.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,62 @@
22

33
namespace Coverlet.Core.CoverageSamples.Tests
44
{
5-
public class AutoProps
5+
public class ClassWithAutoProps
66
{
77
private int _myVal = 0;
8-
public AutoProps()
8+
public ClassWithAutoProps()
99
{
1010
_myVal = new Random().Next();
1111
}
1212
public int AutoPropsNonInit { get; set; }
1313
public int AutoPropsInit { get; set; } = 10;
14+
public int AutoPropsInitKeyword { get; init; }
1415
}
16+
17+
public class ClassWithAutoPropsPrimaryConstructor
18+
{
19+
public ClassWithAutoPropsPrimaryConstructor()
20+
{
21+
var instance = new InnerClassWithAutoProps(1)
22+
{
23+
AutoPropsNonInit = 20,
24+
AutoPropsInit = 30,
25+
AutoPropsInitKeyword = 33
26+
};
27+
}
28+
private class InnerClassWithAutoProps(int myVal = 10)
29+
{
30+
public int AutoPropsNonInit { get; set; }
31+
public int AutoPropsInit { get; set; } = myVal;
32+
public int AutoPropsInitKeyword { get; init; }
33+
}
34+
}
35+
36+
public record RecordWithAutoProps
37+
{
38+
private int _myVal = 0;
39+
public RecordWithAutoProps()
40+
{
41+
_myVal = new Random().Next();
42+
}
43+
public int AutoPropsNonInit { get; set; }
44+
public int AutoPropsInit { get; set; } = 10;
45+
public int AutoPropsInitKeyword { get; init; }
46+
}
47+
48+
public class RecordsWithPrimaryConstructor
49+
{
50+
public record RecordWithAutoPropsPrimaryConstructor(int AutoPropsNonInit, int AutoPropsInit = 10);
51+
52+
public record RecordWithAutoPropsPrimaryConstructorMultiline(
53+
int AutoPropsNonInit,
54+
int AutoPropsInit = 10
55+
);
56+
57+
public RecordsWithPrimaryConstructor()
58+
{
59+
var primaryConstructor = new RecordWithAutoPropsPrimaryConstructor(20, 30);
60+
var primaryConstructorMultiline = new RecordWithAutoPropsPrimaryConstructorMultiline(20);
61+
}
62+
}
1563
}

test/coverlet.core.coverage.tests/Samples/Instrumentation.Records.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public ClassWithRecordsAutoProperties()
2323
}
2424
}
2525

26-
public class ClassWithInheritingRecordsAndAutoProperties
26+
public class ClassWithInheritingRecordsAndPrimaryConstructor
2727
{
2828
record BaseRecord(int A);
2929

3030
record InheritedRecord(int A) : BaseRecord(A);
3131

32-
public ClassWithInheritingRecordsAndAutoProperties()
32+
public ClassWithInheritingRecordsAndPrimaryConstructor()
3333
{
3434
var record = new InheritedRecord(1);
3535
}

0 commit comments

Comments
 (0)