Skip to content

Commit

Permalink
More extensive testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lookbusy1344 committed Oct 7, 2023
1 parent 33643e2 commit c806ed3
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
4 changes: 2 additions & 2 deletions RecordValueAnalyser.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static async Task MainX()
{
var tester = new RecordValueAnalyserUnitTest();

await tester.ValueTypesOnlyTest();
await tester.ReadOnlyListTest();
await tester.ValueTypesOnly();
await tester.ReadOnlyList();
}
}
106 changes: 104 additions & 2 deletions RecordValueAnalyser.Test/RecordValueAnalyserUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ namespace System.Runtime.CompilerServices { internal static class IsExternalInit
";

[TestMethod]
public async Task ValueTypesOnlyTest()
public async Task ValueTypesOnly()
{
// all good!
const string test = coGeneral + "public record class A(int I, string S, DateTime Dt);";

await VerifyCS.VerifyAnalyzerAsync(test);
}

[TestMethod]
public async Task ReadOnlyListTest()
public async Task ReadOnlyList()
{
// this fails because IReadOnlyList lacks value semantics
const string test = coGeneral + "public record class A(int I, string S, IReadOnlyList<int> Fail);";

var expected = VerifyCS.Diagnostic("JSV01")
Expand All @@ -38,5 +40,105 @@ public async Task ReadOnlyListTest()

await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[TestMethod]
public async Task NestedStructPass()
{
// the nested struct is ok, because all the members are value types
const string test = coGeneral
+ """
public struct StructA { public int I; public string S; }

public record class A(int I, string S, DateTime Dt, StructA Sa);
""";

await VerifyCS.VerifyAnalyzerAsync(test);
}

[TestMethod]
public async Task NestedStructFail()
{
// the array nested in the struct makes this fail
const string test = coGeneral
+ """
public struct StructA { public int I; public int[] Numbers; }

public record class A(int I, string S, DateTime Dt, StructA Sa);
""";

var expected = VerifyCS.Diagnostic("JSV01")
.WithSpan(8, 53, 8, 63)
.WithArguments("StructA Sa (int[])");

await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[TestMethod]
public async Task NestedStructEqualsPass()
{
// the Equals member makes this pass
const string test = coGeneral
+ """
public struct StructA { public int I; public int[] Numbers;
public bool Equals(StructA other) => false;
}

public record class A(int I, string S, DateTime Dt, StructA Sa);
""";

await VerifyCS.VerifyAnalyzerAsync(test);
}

[TestMethod]
public async Task NestedClassEqualsPass()
{
// the Equals member makes this pass
const string test = coGeneral
+ """
public struct ClassA { public int I; public int[] Numbers;
public bool Equals(ClassA other) => false;
}

public record class A(int I, string S, DateTime Dt, ClassA Ca);
""";

await VerifyCS.VerifyAnalyzerAsync(test);
}

[TestMethod]
public async Task NestedClassInvalidEqualsFail()
{
// the Equals member is invalid because it doesn't take a ClassA
const string test = coGeneral
+ """
public struct ClassA { public int I; public int[] Numbers;
public bool Equals(int junk) => false;
}

public record class A(int I, string S, DateTime Dt, ClassA Ca);
""";

var expected = VerifyCS.Diagnostic("JSV01")
.WithSpan(10, 53, 10, 62)
.WithArguments("ClassA Ca (int[])");

await VerifyCS.VerifyAnalyzerAsync(test, expected);
}

[TestMethod]
public async Task ObjectMemberFail()
{
// the Equals member is invalid because it doesn't take a ClassA
const string test = coGeneral
+ """
public record class A(object O, string S, DateTime Dt);
""";

var expected = VerifyCS.Diagnostic("JSV01")
.WithSpan(6, 23, 6, 31)
.WithArguments("object O");

await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
}
}

0 comments on commit c806ed3

Please sign in to comment.