-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathStrongApiStringLocalizersGeneratorTests.cs
More file actions
124 lines (98 loc) · 4.07 KB
/
StrongApiStringLocalizersGeneratorTests.cs
File metadata and controls
124 lines (98 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
namespace Havit.SourceGenerators.StrongApiStringLocalizers.Tests;
[TestClass]
public class StrongApiStringLocalizersGeneratorTests
{
[TestMethod]
public async Task StrongApiStringLocalizersGenerator_Test()
{
// Arrange
using var globalResxStream = File.OpenRead("Global.resx");
string projectDir = Environment.CurrentDirectory;
var test = new Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<StrongApiStringLocalizersGenerator, Microsoft.CodeAnalysis.Testing.DefaultVerifier>
{
TestState =
{
AnalyzerConfigFiles =
{
($"/.editorconfig", $@"""
is_global=true
build_property.RootNamespace = MyApp.Resources
build_property.ProjectDir = {projectDir}
""")
}
},
ReferenceAssemblies = ReferenceAssemblies.Net
.Net90
.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Localization", "9.0.1"))) // we are using IStringLocalizer from this package in the generated code
};
// resource file
test.TestState.AdditionalFiles.Add((Path.Combine(projectDir, "MyResources", "Global.resx"), SourceText.From(globalResxStream)));
// EXPECTED OUTPUT
test.TestState.GeneratedSources.Add((typeof(StrongApiStringLocalizersGenerator), "MyApp.Resources.MyResources.IGlobalLocalizer.g.cs", @"// <auto-generated />
namespace MyApp.Resources.MyResources;
using System.CodeDom.Compiler;
using Microsoft.Extensions.Localization;
[GeneratedCode(""Havit.SourceGenerators.StrongApiStringLocalizers.StrongApiStringLocalizersGenerator"", ""2.0.0.0"")]
public interface IGlobalLocalizer : IStringLocalizer
{
/// <summary>
/// Čeština je <b>skvělá</b>!
/// </summary>
LocalizedString CzechAndHtml { get; }
/// <summary>
/// Hello world resource comment.
/// </summary>
LocalizedString HelloWorld { get; }
}
"));
// TestProject - defined by the TestState implementation
test.TestState.GeneratedSources.Add((typeof(StrongApiStringLocalizersGenerator), $"MyApp.Resources.MyResources.GlobalLocalizer.g.cs", @"// <auto-generated />
namespace MyApp.Resources.MyResources;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using Microsoft.Extensions.Localization;
[GeneratedCode(""Havit.SourceGenerators.StrongApiStringLocalizers.StrongApiStringLocalizersGenerator"", ""2.0.0.0"")]
public class GlobalLocalizer : IGlobalLocalizer
{
private readonly IStringLocalizer _localizer;
public GlobalLocalizer(IStringLocalizerFactory stringLocalizerFactory)
{
_localizer = stringLocalizerFactory.Create(""MyResources.Global"", ""TestProject"");
}
/// <summary>
/// Čeština je <b>skvělá</b>!
/// </summary>
public LocalizedString CzechAndHtml => _localizer[""CzechAndHtml""];
/// <summary>
/// Hello world resource comment.
/// </summary>
public LocalizedString HelloWorld => _localizer[""HelloWorld""];
LocalizedString IStringLocalizer.this[string name] => _localizer[name];
LocalizedString IStringLocalizer.this[string name, params object[] arguments] => _localizer[name, arguments];
IEnumerable<LocalizedString> IStringLocalizer.GetAllStrings(bool includeParentCultures) => _localizer.GetAllStrings(includeParentCultures);
}
"));
test.TestState.GeneratedSources.Add((typeof(StrongApiStringLocalizersGenerator), "MyApp.Resources.ServiceCollectionExtensions.g.cs", @"// <auto-generated />
namespace MyApp.Resources;
using System.CodeDom.Compiler;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
[GeneratedCode(""Havit.SourceGenerators.StrongApiStringLocalizers.StrongApiStringLocalizersGenerator"", ""2.0.0.0"")]
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddGeneratedResourceWrappers(this IServiceCollection services)
{
services.AddTransient<MyApp.Resources.MyResources.IGlobalLocalizer, MyApp.Resources.MyResources.GlobalLocalizer>();
return services;
}
}
"));
// Act + Assert
await test.RunAsync();
}
}