Skip to content

Commit

Permalink
Improve collection assertions
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
0xced committed May 26, 2021
1 parent 2d290d0 commit 19bc2cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 3 additions & 5 deletions XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System;
Expand Down Expand Up @@ -149,11 +149,9 @@ public static Assembly Compile(string name, params string[] contents)
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var result = Compiler.GenerateAssembly(compilation);

Assert.Empty(result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error));
Assert.Empty(result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning));
Assert.True(result.Result.Success);
var errors = result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).ToList();
Assert.False(errors.Any(), string.Join("\n", errors.Select(e => e.GetMessage())));
var warnings = result.Result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning).ToList();
Assert.False(warnings.Any(), string.Join("\n", errors.Select(w => w.GetMessage())));
Assert.NotNull(result.Assembly);

Assemblies[name] = result.Assembly;
Expand Down
10 changes: 5 additions & 5 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -157,7 +157,7 @@ public void TestListWithPrivatePropertySetters()
var iListType = typeof(Collection<>);
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p=>p.Name).ToList();
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPrivate).OrderBy(p=>p.Name).ToList();
Assert.True(collectionPropertyInfos.Count > 0);
Assert.NotEmpty(collectionPropertyInfos);
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);

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

var myClassInstance = Activator.CreateInstance(myClassType);
Expand Down Expand Up @@ -221,7 +221,7 @@ public void TestListWithPublicPropertySettersWithoutConstructors()
var iListType = typeof(Collection<>);
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p=>p.Name).ToList();
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p=>p.Name).ToList();
Assert.True(collectionPropertyInfos.Count > 0);
Assert.NotEmpty(collectionPropertyInfos);
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
var myClassInstance = Activator.CreateInstance(myClassType);
foreach (var collectionPropertyInfo in publicCollectionPropertyInfos)
Expand Down Expand Up @@ -253,7 +253,7 @@ public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()
var iListType = typeof(Collection<>);
var collectionPropertyInfos = myClassType.GetProperties().Where(p => p.PropertyType.IsGenericType && iListType.IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition())).OrderBy(p => p.Name).ToList();
var publicCollectionPropertyInfos = collectionPropertyInfos.Where(p => p.SetMethod.IsPublic).OrderBy(p => p.Name).ToList();
Assert.True(collectionPropertyInfos.Count > 0);
Assert.NotEmpty(collectionPropertyInfos);
Assert.Equal(collectionPropertyInfos, publicCollectionPropertyInfos);
var myClassInstance = Activator.CreateInstance(myClassType);

Expand Down

0 comments on commit 19bc2cb

Please sign in to comment.