Is there an ability to reuse the EqualTo methods on your own built extension? #1724
-
Hey there, I'm writing some of my own extensions for TUnit because I want to make custom Roslyn generator /analyzer testing a lot more easy for me as most of it boils down to "with this input, is this the exact correct output" (especially for some smaller tools that don't change). Down below is the code I've written so far for my own extension. public static class TUnitExtensionsGeneratorDriverRunResult {
public static InvokableValueAssertionBuilder<GeneratorDriverRunResult> HasSourceTextEqualTo(this IValueSource<GeneratorDriverRunResult> valueSource, string fileName, string expected, StringComparison stringComparison = StringComparison.Ordinal, bool ignoreWhiteSpace = false, bool withTrimming = false ,
[CallerArgumentExpression(nameof(fileName))] string doNotPopulateThisValue1 = "", [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue2 = "", [CallerArgumentExpression(nameof(stringComparison))] string doNotPopulateThisValue3 = "", [CallerArgumentExpression(nameof(ignoreWhiteSpace))] string doNotPopulateThisValue4 = "", [CallerArgumentExpression(nameof(withTrimming))] string doNotPopulateThisValue5 = "") {
return valueSource.RegisterAssertion(
assertCondition: new CompilationHasSourceTextEqualToCondition(fileName, expected, stringComparison, ignoreWhiteSpace, withTrimming),
argumentExpressions: [doNotPopulateThisValue1, doNotPopulateThisValue2, doNotPopulateThisValue3, doNotPopulateThisValue4, doNotPopulateThisValue5]
);
}
}
public class CompilationHasSourceTextEqualToCondition(string filename, string expected, StringComparison stringComparison, bool ignoreWhiteSpace, bool withTrimming)
: ExpectedValueAssertCondition<GeneratorDriverRunResult, string>(expected) {
protected override string GetExpectation() => throw new NotImplementedException();
protected override AssertionResult GetResult(GeneratorDriverRunResult? runResult, string? expectedValue) {
if (runResult is null) return AssertionResult.Fail("Compilation is null");
if (expectedValue is null) return AssertionResult.Fail("Expected string is null");
GeneratedSourceResult? generatedSource = runResult.Results
.SelectMany(result => result.GeneratedSources)
.SingleOrDefault(result => result.HintName == filename);
if (generatedSource is null) return AssertionResult.Fail($"Could not find source with name '{filename}'");
if (generatedSource.Value.SourceText is not {} sourceText) return AssertionResult.Fail("Source text is null");
string sourceTextString = sourceText.ToString();
if (ignoreWhiteSpace) sourceTextString = string.Join(string.Empty, sourceTextString.Where(c => !char.IsWhiteSpace(c)));
if (withTrimming) sourceTextString = sourceTextString.Trim();
return AssertionResult
.FailIf(!string.Equals(sourceTextString, expectedValue, stringComparison), "Source text does not match");
}
} Actual source code: https://github.com/code-of-chaos/cs_code-of_chaos-testing-tunit/blob/8e60a58ba85463fa53106db2eb664d6dcad91517/src/CodeOfChaos.Testing.TUnit/Extensions/TUnitExtensionsCompilation.cs |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hadn't given this much thought, hence why it's locked down, but it seems sensible to me to allow code sharing if assertions want to re-use bits from other assertions. I had to make a bit of code changes (make the var stringEqualsAssertCondition = new StringEqualsExpectedValueAssertCondition(expected, stringComparison);
if(withTrimming)
{
stringEqualsAssertCondition.WithTrimming();
}
if(ignoreWhiteSpace)
{
stringEqualsAssertCondition.IgnoringWhitespace();
}
return await stringEqualsAssertCondition.GetAssertionResult(generatedSource?.Value?.SourceText, null); Does that help? 😄 |
Beta Was this translation helpful? Give feedback.
Hadn't given this much thought, hence why it's locked down, but it seems sensible to me to allow code sharing if assertions want to re-use bits from other assertions.
I had to make a bit of code changes (make the
GetResult
method async and return a Task) - But now you could do this: