Skip to content

Commit 6b95268

Browse files
authored
Add unit tests for verify (#84)
1 parent 8a1e7c3 commit 6b95268

9 files changed

+417
-32
lines changed

.editorconfig

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ dotnet_diagnostic.CA2009.severity=error
4343
dotnet_diagnostic.CA2008.severity=error
4444
dotnet_diagnostic.CA2007.severity=warning
4545
dotnet_diagnostic.CA2000.severity=suggestion
46+
dotnet_style_coalesce_expression = true:suggestion
47+
dotnet_style_null_propagation = true:suggestion
48+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
49+
dotnet_style_prefer_auto_properties = true:suggestion
50+
dotnet_style_object_initializer = true:suggestion
51+
dotnet_style_collection_initializer = true:suggestion
52+
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
53+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
54+
dotnet_style_prefer_conditional_expression_over_return = true:silent
55+
dotnet_style_explicit_tuple_names = true:suggestion
56+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
57+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
58+
dotnet_style_prefer_compound_assignment = true:suggestion
59+
dotnet_style_operator_placement_when_wrapping = beginning_of_line
60+
tab_width = 4
61+
end_of_line = lf
62+
dotnet_style_prefer_simplified_interpolation = true:suggestion
63+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
4664

4765
[project.json]
4866
indent_size = 2
@@ -87,15 +105,15 @@ dotnet_style_predefined_type_for_member_access = true:suggestion
87105
# name all constant fields using PascalCase
88106
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
89107
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
90-
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
108+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
91109
dotnet_naming_symbols.constant_fields.applicable_kinds = field
92110
dotnet_naming_symbols.constant_fields.required_modifiers = const
93111
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
94112

95113
# static fields should have s_ prefix
96114
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
97115
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
98-
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
116+
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
99117
dotnet_naming_symbols.static_fields.applicable_kinds = field
100118
dotnet_naming_symbols.static_fields.required_modifiers = static
101119
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
@@ -105,7 +123,7 @@ dotnet_naming_style.static_prefix_style.capitalization = camel_case
105123
# internal and private fields should be _camelCase
106124
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
107125
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
108-
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
126+
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
109127
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
110128
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
111129
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
@@ -501,6 +519,11 @@ dotnet_diagnostic.RCS1180.severity=warning
501519
dotnet_diagnostic.RCS1190.severity=error
502520
dotnet_diagnostic.RCS1195.severity=error
503521
dotnet_diagnostic.RCS1214.severity=error
522+
csharp_style_namespace_declarations = file_scoped:silent
523+
csharp_style_prefer_method_group_conversion = true:silent
524+
csharp_style_prefer_top_level_statements = true:silent
525+
csharp_style_prefer_primary_constructors = true:suggestion
526+
csharp_style_prefer_local_over_anonymous_function = true:suggestion
504527

505528
# C++ Files
506529
[*.{cpp,h,in}]

src/Directory.Packages.props

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@
55
<CodeAnalysisVersion>4.0.1</CodeAnalysisVersion>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
9-
<PackageVersion Include="xunit" Version="2.9.2" />
10-
<PackageVersion Include="xunit.runner.console" Version="2.9.2" />
11-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
12-
<PackageVersion Include="Xunit.StaFact" Version="1.1.11" />
13-
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
14-
<PackageVersion Include="Microsoft.Reactive.Testing" Version="6.0.1" />
15-
<PackageVersion Include="PublicApiGenerator" Version="11.1.0" />
16-
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
17-
<PackageVersion Include="Verify.Xunit" Version="26.6.0" />
188
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
199
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.143" />
2010
<PackageVersion Include="stylecop.analyzers" Version="1.2.0-beta.556" />
@@ -36,5 +26,20 @@
3626

3727
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
3828
<PackageVersion Include="ReactiveUI.Maui" Version="20.1.63" />
29+
30+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
31+
<PackageVersion Include="xunit" Version="2.9.2" />
32+
<PackageVersion Include="xunit.runner.console" Version="2.9.2" />
33+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
34+
<PackageVersion Include="Xunit.StaFact" Version="1.1.11" />
35+
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
36+
<PackageVersion Include="Microsoft.Reactive.Testing" Version="6.0.1" />
37+
<PackageVersion Include="PublicApiGenerator" Version="11.1.0" />
38+
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
39+
<PackageVersion Include="Verify.Xunit" Version="26.6.0" />
40+
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
41+
<PackageVersion Include="ReactiveMarbles.SourceGenerator.TestNuGetHelper" Version="1.3.1" />
42+
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.7.9" />
43+
<PackageVersion Include="Basic.Reference.Assemblies.Net80Windows" Version="1.7.9" />
3944
</ItemGroup>
4045
</Project>

src/Directory.build.props

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@
3636
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
3737
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
3838
</PropertyGroup>
39-
<ItemGroup Condition="$(IsTestProject)">
40-
<PackageReference Include="Microsoft.NET.Test.Sdk" />
41-
<PackageReference Include="xunit" />
42-
<PackageReference Include="xunit.runner.console" />
43-
<PackageReference Include="xunit.runner.visualstudio" />
44-
<PackageReference Include="Xunit.StaFact" />
45-
<PackageReference Include="FluentAssertions" />
46-
<PackageReference Include="Microsoft.Reactive.Testing" />
47-
<PackageReference Include="PublicApiGenerator" />
48-
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
49-
<PackageReference Include="Verify.Xunit" />
50-
</ItemGroup>
5139
<ItemGroup Condition="'$(IsTestProject)' != 'true'">
5240
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
5341
</ItemGroup>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using System.Runtime.CompilerServices;
7+
8+
/// <summary>
9+
/// Initializes the source generator verifiers.
10+
/// </summary>
11+
public static class ModuleInitializer
12+
{
13+
/// <summary>
14+
/// Initializes the source generators.
15+
/// </summary>
16+
[ModuleInitializer]
17+
public static void Init() => VerifySourceGenerators.Initialize();
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
using ReactiveUI.SourceGenerators;
13+
14+
using Xunit.Abstractions;
15+
16+
namespace ReactiveUI.SourceGenerator.Tests;
17+
18+
/// <summary>
19+
/// Unit tests for the ObservableAsProperty generator.
20+
/// </summary>
21+
/// <param name="output">The output helper.</param>
22+
public class ObservableAsPropertyFromObservableGeneratorUnitTests(ITestOutputHelper output) : TestBase(output)
23+
{
24+
/// <summary>
25+
/// Tests that the source generator correctly generates observable properties.
26+
/// </summary>
27+
/// <returns>A task to monitor the async.</returns>
28+
[Fact]
29+
public Task Generator_ShouldGenerate_ObservablePropertiesCorrectly()
30+
{
31+
// Arrange: Setup the source code that matches the generator input expectations.
32+
var sourceCode = @"
33+
using ReactiveUI;
34+
using System.Reactive.Linq;
35+
36+
namespace TestNamespace
37+
{
38+
public partial class TestViewModel : ReactiveObject
39+
{
40+
[ObservableAsProperty]
41+
public IObservable<int> MyProperty => Observable.Return(42);
42+
}
43+
}
44+
";
45+
46+
// Act: Initialize the helper and run the generator.
47+
var driver = TestHelper.TestPass<ObservableAsPropertyFromObservableGenerator>(
48+
sourceCode,
49+
"MyProperty",
50+
typeof(ObservableAsPropertyFromObservableGenerator));
51+
52+
// Assert: Verify the generated code.
53+
return Verify(driver);
54+
}
55+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
<NoWarn>$(NoWarn);CA1812;CA1001</NoWarn>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="coverlet.msbuild" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
16+
<PackageReference Include="xunit" />
17+
<PackageReference Include="xunit.runner.visualstudio" />
18+
<PackageReference Include="ReactiveMarbles.SourceGenerator.TestNuGetHelper" />
19+
<PackageReference Include="Verify.Xunit" />
20+
<PackageReference Include="Verify.SourceGenerators" />
21+
<PackageReference Include="Basic.Reference.Assemblies.Net80" />
22+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" VersionOverride="4.11.0" />
23+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" VersionOverride="4.11.0" />
24+
<PackageReference Include="FluentAssertions" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<Using Include="Xunit" />
33+
</ItemGroup>
34+
35+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using Xunit.Abstractions;
7+
8+
namespace ReactiveUI.SourceGenerator.Tests;
9+
10+
/// <summary>
11+
/// A base class for handling test setup and teardown.
12+
/// </summary>
13+
/// <param name="testOutputHelper">The output helper.</param>
14+
public abstract class TestBase(ITestOutputHelper testOutputHelper) : IAsyncLifetime, IDisposable
15+
{
16+
/// <summary>
17+
/// Gets the TestHelper instance.
18+
/// </summary>
19+
protected TestHelper TestHelper { get; } = new(testOutputHelper);
20+
21+
/// <inheritdoc/>
22+
public Task InitializeAsync() => TestHelper.InitializeAsync();
23+
24+
/// <inheritdoc/>
25+
public Task DisposeAsync()
26+
{
27+
TestHelper.Dispose();
28+
return Task.CompletedTask;
29+
}
30+
31+
/// <inheritdoc/>
32+
public void Dispose()
33+
{
34+
Dispose(true);
35+
GC.SuppressFinalize(this);
36+
}
37+
38+
/// <summary>
39+
/// Disposes the resources used by the TestBase.
40+
/// </summary>
41+
/// <param name="isDisposing">True if called from Dispose method, false if called from finalizer.</param>
42+
protected virtual void Dispose(bool isDisposing)
43+
{
44+
if (isDisposing)
45+
{
46+
TestHelper.Dispose();
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)