Skip to content

Commit

Permalink
Adding lots more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lookbusy1344 committed Oct 8, 2023
1 parent f3e858e commit aeb2639
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 145 deletions.
2 changes: 1 addition & 1 deletion RecordValueAnalyser.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;

namespace RecordValueAnalyser.Test;
namespace RecordValueAnalyser.TestClasses;

#pragma warning disable RCS1046 // Asynchronous method name should end with 'Async'.

Expand Down
144 changes: 0 additions & 144 deletions RecordValueAnalyser.Test/RecordValueAnalyserUnitTests.cs

This file was deleted.

176 changes: 176 additions & 0 deletions RecordValueAnalyser.Test/UnitTestsClasses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using VerifyCS = RecordValueAnalyser.Test.CSharpCodeFixVerifier<RecordValueAnalyser.RecordValueAnalyser, RecordValueAnalyser.RecordValueAnalyserCodeFixProvider>;

// if tests weirdly fail, try deleting files in:
// C:\Users\ps\AppData\Local\Temp\test-packages

#pragma warning disable RCS1046 // Asynchronous method name should end with 'Async'.

namespace RecordValueAnalyser.TestClasses;

// tests for record classes

[TestClass]
public class RecordValueAnalyserUnitTest
{
private const string coGeneral = @"
using System;
using System.Collections.Generic;
namespace System.Runtime.CompilerServices { internal static class IsExternalInit { } }
";

[TestMethod]
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 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")
.WithSpan(6, 40, 6, 63)
.WithArguments("System.Collections.Generic.IReadOnlyList<int> Fail");

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(ClassA) 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);
}

[TestMethod]
public async Task NestedClassObjEqualsPass()
{
// the Equals(object) is also ok
const string test = coGeneral
+ """
public struct ClassA { public int I; public int[] Numbers;
public override bool Equals(object other) => false;
}

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

await VerifyCS.VerifyAnalyzerAsync(test);
}

[TestMethod]
public async Task RecordEqualsMethod()
{
// If the record has an Equals method, it's ok
const string test = coGeneral
+ """
public record class A(int I, string S, IReadOnlyList<int> Fail)
{
public virtual bool Equals(A other) => false;
}
""";

await VerifyCS.VerifyAnalyzerAsync(test);
}
}
Loading

0 comments on commit aeb2639

Please sign in to comment.