Testing

Vogen uses a two-pronged approach to testing. The first is a set of snapshot tests docs/site/Writerside/topics/how-to/Contributing.md, where the generated code is compared to a known good version. The second is a set of consumer tests docs/site/Writerside/topics/how-to/Testing.md which test how the generated code behaves in a real world application.

Snapshot Tests

The snapshot tests tests/SnapshotTests/obj/Debug/net8.0/SnapshotTests.AssemblyInfoInputs.cache are run as part of the CI build and cover a wide range of cases, including:

  • Different value object types: Record structs, classes, and readonly structs
  • Validation: Testing the validation rules generated for value objects
  • Serialization: Testing the generation of serializer code for Dapper and LinqToDb

These tests are run on different frameworks and in different locales to ensure consistency. They are also a good way to test the edge cases of Vogen’s code generation capabilities.

Consumer Tests

The consumer tests tests/ConsumerTests/obj/Debug/net8.0/ConsumerTests.AssemblyInfoInputs.cache are designed to test the generated code in a real-world scenario. This is done by using the generated code in a separate test project. The consumer tests test the following:

  • Normalization: Does the code correctly normalize the values of a value object?
  • Equality: Does the generated equality code work as expected?
  • Hashing: Does the generated hashing code produce consistent results?
  • ToString: Does the generated ToString() method output a correct representation of the value object?

These tests are much quicker to run than the snapshot tests and are ideal for verifying the behavior of specific value objects.

Analyzer Tests

The analyzer tests tests/AnalyzerTests/AnalyzerTests.csproj are designed to test the code analyzers that are part of Vogen. These tests verify that the analyzers correctly identify code that could cause issues with Vogen’s value objects.

For example, the DoNotUseNewAnalyzerTests tests/AnalyzerTests/DoNotUseNewAnalyzerTests.cs ensures that the analyzer will correctly detect when someone uses the new keyword to create an instance of a value object.

public class Test
          {
              public Task<MyVo> TestMethod()
              {
                  return Task.FromResult(new MyVo());
              }
          }
          

The analyzer tests are an important part of ensuring that Vogen’s value objects are used correctly and that developers are not accidentally introducing errors into their code.

Additional Information

  • The snapshot tests are run in the IDE and take about 5 minutes.
  • In the CI build, the THOROUGH flag is set which exercises more variations, and that can take up to several hours.
  • The consumer tests are run as part of the CI build.
  • To ensure that the consumer tests use the latest version of Vogen, the test.ps1 script builds Vogen and forces a NuGet update.

Top-Level Directory Explanations

tests/ - This directory contains unit tests and benchmarks for the project. It includes subdirectories for analyzer tests, consumer tests, snapshot tests, and Vogen benchmarks.

tests/AnalyzerTests/ - This subdirectory contains unit tests for the analyzer component of the Vogen library.

tests/ConsumerTests/ - This subdirectory contains unit tests for the consumer-side components of the Vogen library.

tests/SnapshotTests/ - This subdirectory contains snapshot tests, which test the output of the code generation and serialization components of the Vogen library.

tests/Vogen.Benchmarks/ - This subdirectory contains benchmarks for the performance of the Vogen library.