Skip to content

Commit 19bc2cb

Browse files
committed
Improve collection assertions
Asserting with Assert.Empty / Assert.NotEmpty gives much better error messages when the assertion fails. Before: ``` Assert.True() Failure Expected: True Actual: False at XmlSchemaClassGenerator.Tests.Compiler.Compile(String name, String[] contents) in XmlSchemaClassGenerator/XmlSchemaClassGenerator.Tests/Compiler.cs:line 152 ``` After: ``` Assert.Empty() Failure Collection: [(11144,6): error CS0579: Duplicate 'System.CodeDom.Compiler.GeneratedCodeAttribute' attribute, (11145,6): error CS0579: Duplicate 'System.SerializableAttribute' attribute, (11147,6): error CS0579: Duplicate 'System.Xml.Serialization.XmlRootAttribute' attribute, (10023,26): error CS0146: Circular base class dependency involving 'User' and 'User'] at XmlSchemaClassGenerator.Tests.Compiler.Compile(String name, String[] contents) in XmlSchemaClassGenerator/XmlSchemaClassGenerator.Tests/Compiler.cs:line 152 ```
1 parent 2d290d0 commit 19bc2cb

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

XmlSchemaClassGenerator.Tests/Compiler.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.CodeAnalysis;
1+
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp;
33
using Microsoft.CodeAnalysis.Emit;
44
using System;
@@ -149,11 +149,9 @@ public static Assembly Compile(string name, params string[] contents)
149149
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
150150
var result = Compiler.GenerateAssembly(compilation);
151151

152+
Assert.Empty(result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error));
153+
Assert.Empty(result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning));
152154
Assert.True(result.Result.Success);
153-
var errors = result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).ToList();
154-
Assert.False(errors.Any(), string.Join("\n", errors.Select(e => e.GetMessage())));
155-
var warnings = result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning).ToList();
156-
Assert.False(warnings.Any(), string.Join("\n", errors.Select(w => w.GetMessage())));
157155
Assert.NotNull(result.Assembly);
158156

159157
Assemblies[name] = result.Assembly;

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.CodeDom;
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
@@ -157,7 +157,7 @@ public void TestListWithPrivatePropertySetters()
157157
var iListType = typeof(Collection<>);
158158
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p=>p.Name).ToList();
159159
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPrivate).OrderBy(p=>p.Name).ToList();
160-
Assert.True(collectionPropertyInfos.Count > 0);
160+
Assert.NotEmpty(collectionPropertyInfos);
161161
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
162162

163163
var myClassInstance = Activator.CreateInstance(myClassType);
@@ -189,7 +189,7 @@ public void TestListWithPublicPropertySetters()
189189
var iListType = typeof(Collection<>);
190190
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p=>p.Name).ToList();
191191
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p=>p.Name).ToList();
192-
Assert.True(collectionPropertyInfos.Count > 0);
192+
Assert.NotEmpty(collectionPropertyInfos);
193193
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
194194

195195
var myClassInstance = Activator.CreateInstance(myClassType);
@@ -221,7 +221,7 @@ public void TestListWithPublicPropertySettersWithoutConstructors()
221221
var iListType = typeof(Collection<>);
222222
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p=>p.Name).ToList();
223223
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p=>p.Name).ToList();
224-
Assert.True(collectionPropertyInfos.Count > 0);
224+
Assert.NotEmpty(collectionPropertyInfos);
225225
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
226226
var myClassInstance = Activator.CreateInstance(myClassType);
227227
foreach (var collectionPropertyInfo in publicCollectionPropertyInfos)
@@ -253,7 +253,7 @@ public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()
253253
var iListType = typeof(Collection<>);
254254
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p => p.Name).ToList();
255255
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p => p.Name).ToList();
256-
Assert.True(collectionPropertyInfos.Count > 0);
256+
Assert.NotEmpty(collectionPropertyInfos);
257257
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
258258
var myClassInstance = Activator.CreateInstance(myClassType);
259259

0 commit comments

Comments
 (0)