Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Nov 17, 2024
1 parent 42e0b8f commit 543bb23
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Lynx.Generator/GeneratedPackGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ private sealed record Model(string Namespace, string ClassName, string PropertyN
{
public const int DefaultValue = 999_999_999;
}
}
}
45 changes: 38 additions & 7 deletions tests/Lynx.Generator.Test/GeneratedPackTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class TestClass
}";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
return Verify(source);
}

[Test]
Expand All @@ -36,7 +36,7 @@ public partial class TestClass
}";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
return Verify(source);
}

[Test]
Expand All @@ -53,7 +53,7 @@ public partial class TestClass
}";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
return Verify(source);
}

[Test]
Expand All @@ -68,7 +68,7 @@ public partial class TestClass
}";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
return Verify(source);
}

[Test]
Expand All @@ -86,7 +86,7 @@ public partial class TestClass
}";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
return Verify(source);
}

[Test]
Expand All @@ -102,6 +102,37 @@ public partial class TestClass
}";

// Pass the source code to our helper and snapshot test the output
return TestHelper.Verify(source);
return Verify(source);
}
}

/// <summary>
/// Based on: https://andrewlock.net/creating-a-source-generator-part-2-testing-an-incremental-generator-with-snapshot-testing/
/// https://github.com/andrewlock/blog-examples/blob/c35edf1c1f0e1f9adf84c215e2ce7ab644b374f5/NetEscapades.EnumGenerators2/tests/NetEscapades.EnumGenerators.Tests/cs
/// </summary>
private static Task Verify(string source)
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);

// Create references for assemblies we require
// We could add multiple references if required
IEnumerable<PortableExecutableReference> references =
[
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
];

CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName: "Tests",
syntaxTrees: [syntaxTree],
references: references);

GeneratedPackGenerator generator = new GeneratedPackGenerator();

GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

driver = driver.RunGenerators(compilation);

return Verifier
.Verify(driver)
.UseDirectory("Snapshots");
}
}
37 changes: 0 additions & 37 deletions tests/Lynx.Generator.Test/TestHelper.cs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Lynx.Generator.Test/UtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class UtilsTest
{
[TestCase(1, 2, 131073)]
[TestCase(-1, 2, 131071)]
[TestCase(1, -2, -131073)]
[TestCase(-1, -2, 131073)]
[TestCase(-1, -2, -131073)]
public void Pack(short mg, short eg, int packedValue)
{
Assert.AreEqual(packedValue, Utils.Pack(mg, eg));
Expand Down

0 comments on commit 543bb23

Please sign in to comment.