Skip to content

chore(tests): Extract test utils to a new test project for reusability#28

Merged
danielpindur merged 3 commits intomasterfrom
extract-test-utils
Jan 20, 2026
Merged

chore(tests): Extract test utils to a new test project for reusability#28
danielpindur merged 3 commits intomasterfrom
extract-test-utils

Conversation

@danielpindur
Copy link
Owner

@danielpindur danielpindur commented Jan 20, 2026

Summary by CodeRabbit

  • Tests

    • Reorganized test projects and solution configuration for clearer separation.
    • Added reusable test utilities to generate enum/registrar code, compile sources, and run generator scenarios.
    • Updated unit tests to use the new utilities for consistency and reduced duplication.
  • Chores

    • Adjusted solution setup to include the new test projects and nesting.

Note: No user-facing changes.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

Reorganizes tests: removes Dango.Tests, adds Dango.Tests.Utils (test utilities) and Dango.Unit.Tests (unit tests), updates the solution file and nested project mappings, and moves generator test helpers into the utilities project used by unit tests.

Changes

Cohort / File(s) Summary
Solution configuration
Dango.sln
Removed Dango.Tests, added Dango.Tests.Utils and Dango.Unit.Tests entries; updated SolutionConfigurationPlatforms and NestedProjects mappings with new project GUIDs.
New test utilities project
test/Dango.Tests.Utils/Dango.Tests.Utils.csproj, test/Dango.Tests.Utils/...
Added utilities project (net10.0, nullable, implicit usings, Roslyn ref) and utility sources: EnumDefinitionBuilder.cs (enum source generator), GeneratorTestHelper.cs (CreateCompilation + run DangoGenerator), MappingRegistrarBuilder.cs (build registrar code + MappingConfig/MappingStrategy types).
Unit test project & tests
test/Dango.Unit.Tests/Dango.Unit.Tests.csproj, test/Dango.Unit.Tests/DangoGenerator.Basic.cs, test/Dango.Unit.Tests/DangoGeneratorTests.*.cs
Added reference to Dango.Tests.Utils, removed BOM, moved namespaces to Dango.Unit.Tests, replaced local RunGenerator helper with GeneratorTestHelper.RunGenerator across multiple test files.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test File
    participant Helper as GeneratorTestHelper
    participant Roslyn as CSharpCompilation
    participant Generator as DangoGenerator
    participant Driver as GeneratorDriver

    Test->>Helper: RunGenerator(source)
    Helper->>Roslyn: CreateCompilation(source, additionalSources)
    Helper->>Driver: Create GeneratorDriver with Generator
    Driver->>Generator: Run against Compilation
    Driver-->>Helper: GeneratorDriverRunResult
    Helper-->>Test: Return run result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hopped through code, a tidy little feat,
Split helpers and tests to keep things neat.
Enums and registrars, generators too,
I stitched them together — a hop, then a woo! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: extracting test utilities into a new reusable test project (Dango.Tests.Utils), which aligns with the file restructuring and new public classes/methods added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@danielpindur danielpindur changed the title extract test utils chore(tests): Extract test utils to a new test project for reusability Jan 20, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@test/Dango.Tests.Utils/MappingRegistrarBuilder.cs`:
- Around line 12-22: The generated registrar emits the file-scoped namespace
before the using directives and omits System.Collections.Generic causing compile
errors; update the string construction in MappingRegistrarBuilder so the using
directives (including "using System.Collections.Generic;" and "using
Dango.Abstractions;") are written first, then append the file-scoped
namespaceDeclaration (if any), and keep the registrar class generation using
registrarClassName implementing IDangoMapperRegistrar and the
Register(IDangoMapperRegistry registry) method unchanged so Dictionary<>
references resolve.

In `@test/Dango.Unit.Tests/DangoGeneratorTests.Configuration.cs`:
- Line 151: The test contains a malformed closing sequence `}<` inside the
embedded C# source string in DangoGeneratorTests.Configuration; locate the
embedded test source (the string literal in the failing test) and replace the
stray `}<` with a proper closing brace `}` so the embedded C# is valid; ensure
the string now ends with the correct `}` and run the tests to confirm the
generator receives well-formed input.
🧹 Nitpick comments (3)
test/Dango.Tests.Utils/EnumDefinitionBuilder.cs (2)

5-21: Add basic input guards for clearer failures.
Line 5: enumName and valueCount are unchecked. A null/whitespace name or negative count will surface as less clear runtime errors later. Consider explicit validation for readability and debuggability.

♻️ Proposed refactor
 public static string BuildEnumDefinition(string enumName, int valueCount, string? namespaceName = null)
 {
+    if (string.IsNullOrWhiteSpace(enumName))
+    {
+        throw new ArgumentException("Enum name cannot be empty.", nameof(enumName));
+    }
+    if (valueCount < 0)
+    {
+        throw new ArgumentOutOfRangeException(nameof(valueCount), "Value count cannot be negative.");
+    }
+
     var values = Enumerable.Range(0, valueCount)
         .Select(i => $"Value{i}")
         .ToList();

23-35: Guard against null/empty value lists for deterministic output.
Line 23: values can be null or empty, which can lead to confusing failures or empty enum bodies. Consider validating and materializing the sequence once.

♻️ Proposed refactor
 public static string BuildEnumDefinitionWithValues(string enumName, IEnumerable<string> values, string? namespaceName = null)
 {
+    if (string.IsNullOrWhiteSpace(enumName))
+    {
+        throw new ArgumentException("Enum name cannot be empty.", nameof(enumName));
+    }
+    if (values is null)
+    {
+        throw new ArgumentNullException(nameof(values));
+    }
+
+    var valueList = values.ToList();
+    if (valueList.Count == 0)
+    {
+        throw new ArgumentException("Enum must contain at least one value.", nameof(values));
+    }
+
     var namespaceDeclaration = string.IsNullOrEmpty(namespaceName)
         ? ""
         : $"namespace {namespaceName};\n\n";
 
-    var enumValues = string.Join(",\n        ", values);
+    var enumValues = string.Join(",\n        ", valueList);
test/Dango.Tests.Utils/GeneratorTestHelper.cs (1)

16-22: Prefer deterministic reference assemblies over AppDomain-loaded ones.

Line 16-21 can vary across test runners/load order, which makes compilation inputs non-deterministic. Consider using the trusted platform assemblies list (TPA) to build a stable reference set.

♻️ Suggested refactor (stable references)
-        var references = AppDomain
-            .CurrentDomain.GetAssemblies()
-            .Where(a => !a.IsDynamic && !string.IsNullOrWhiteSpace(a.Location))
-            .Select(a => MetadataReference.CreateFromFile(a.Location))
-            .Cast<MetadataReference>()
-            .ToList();
+        var tpa = ((string?)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"))?
+            .Split(Path.PathSeparator)
+            .Where(p => !string.IsNullOrWhiteSpace(p))
+            .Distinct()
+            .Select(MetadataReference.CreateFromFile)
+            .Cast<MetadataReference>()
+            .ToList()
+            ?? new List<MetadataReference>();
+
+        var references = tpa;

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@test/Dango.Tests.Utils/EnumDefinitionBuilder.cs`:
- Around line 16-31: The method BuildEnumDefinitionWithValues should guard its
values parameter to avoid a null reference when iterating; at the start of
BuildEnumDefinitionWithValues add a null-check for the values parameter and
throw ArgumentNullException(nameof(values)) if null so callers fail fast and
avoid NREs during the foreach.
🧹 Nitpick comments (1)
test/Dango.Tests.Utils/EnumDefinitionBuilder.cs (1)

7-14: Guard against negative valueCount for clearer failures.

Enumerable.Range will throw if valueCount < 0, but the exception isn’t very descriptive. An explicit guard improves error clarity for callers.

🔧 Suggested change
 public static string BuildEnumDefinition(string enumName, int valueCount, string? namespaceName = null)
 {
+    if (valueCount < 0) throw new ArgumentOutOfRangeException(nameof(valueCount));
     var values = Enumerable.Range(0, valueCount)
         .Select(i => $"Value{i}")
         .ToList();
 
     return BuildEnumDefinitionWithValues(enumName, values, namespaceName);
 }

@danielpindur danielpindur merged commit ccb49cf into master Jan 20, 2026
5 checks passed
@danielpindur danielpindur deleted the extract-test-utils branch January 20, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant