Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
YarinOmesi committed Aug 5, 2023
1 parent 8d3e24c commit 608af94
Show file tree
Hide file tree
Showing 27 changed files with 541 additions and 471 deletions.
21 changes: 21 additions & 0 deletions TestsHelper.SourceGenerator.Tests/DictionaryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;

namespace TestsHelper.SourceGenerator.Tests;

public static class DictionaryHelper
{
public static Dictionary<TKey, TValue> MergeRight<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();

foreach (Dictionary<TKey,TValue> d in dictionaries)
{
foreach ((TKey key, TValue value) in d)
{
dictionary[key] = value;
}
}
return dictionary;
}
}
54 changes: 54 additions & 0 deletions TestsHelper.SourceGenerator.Tests/Diff.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using DiffPlex;
using DiffPlex.Chunkers;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;

namespace TestsHelper.SourceGenerator.Tests;

public static class Diff
{
private static readonly IChunker _lineChunker = new LineChunker();
private static readonly IChunker _lineEndingsPreservingChunker = new LineEndingsPreservingChunker();
private static readonly InlineDiffBuilder _diffBuilder = new InlineDiffBuilder(new Differ());

public static bool IsDifferent(string expected, string actual, [NotNullWhen(true)] out string? displayMessage)
{
displayMessage = default;
if (expected == actual) return false;

DiffPaneModel? diff = _diffBuilder.BuildDiffModel(expected, actual, ignoreWhitespace: false, ignoreCase: false, _lineChunker);
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine("Actual and expected values differ. Expected shown in baseline of diff:");

if (!diff.Lines.Any(line => line.Type is ChangeType.Inserted or ChangeType.Deleted))
{
// We have a failure only caused by line ending differences; recalculate with line endings visible
diff = _diffBuilder.BuildDiffModel(expected, actual, ignoreWhitespace: false, ignoreCase: false,
_lineEndingsPreservingChunker);
}

foreach (DiffPiece? line in diff.Lines)
{
switch (line.Type)
{
case ChangeType.Inserted:
messageBuilder.Append('+');
break;
case ChangeType.Deleted:
messageBuilder.Append('-');
break;
default:
messageBuilder.Append(' ');
break;
}

messageBuilder.AppendLine(line.Text.Replace("\r", "<CR>").Replace("\n", "<LF>"));
}

displayMessage = messageBuilder.ToString();
return true;
}
}
44 changes: 44 additions & 0 deletions TestsHelper.SourceGenerator.Tests/DiffConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NUnit.Framework.Constraints;

namespace TestsHelper.SourceGenerator.Tests;

public class DiffConstraint : Constraint
{
private readonly string _expected;

public DiffConstraint(string expected)
{
_expected = expected;
}

public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
if (actual is string actualValue)
{
if (Diff.IsDifferent(_expected, actualValue, out var message))
{
return new Result(this, actual, ConstraintStatus.Failure, message);
}

return new Result(this, actual, ConstraintStatus.Success, string.Empty);
}

return new Result(this, actual, ConstraintStatus.Failure, "Expected is not string");
}

private sealed class Result : ConstraintResult
{
private readonly string _diffDisplay;

public Result(IConstraint constraint, object actualValue, ConstraintStatus status, string diffDisplay)
: base(constraint, actualValue, status)
{
_diffDisplay = diffDisplay;
}

public override void WriteMessageTo(MessageWriter writer)
{
writer.WriteMessageLine(_diffDisplay);
}
}
}
228 changes: 0 additions & 228 deletions TestsHelper.SourceGenerator.Tests/MockFillerSourceGeneratorTests.cs

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 608af94

Please sign in to comment.