1+ using Microsoft . CodeAnalysis ;
2+ using Xunit . Abstractions ;
3+ using static NJsonSchema . SourceGenerators . CSharp . GeneratorConfigurationKeys ;
4+
5+ namespace NJsonSchema . SourceGenerators . CSharp . Tests
6+ {
7+ public class JsonSchemaSourceGeneratorTests ( ITestOutputHelper output ) : TestsBase ( output )
8+ {
9+ [ Fact ]
10+ public void When_no_additional_files_specified_then_no_source_is_generated ( )
11+ {
12+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput ( null , [ ] ) ;
13+
14+ Assert . Empty ( outputDiagnostics ) ;
15+ Assert . Empty ( compilation . SyntaxTrees ) ;
16+ }
17+
18+ [ Fact ]
19+ public void When_invalid_path_specified_then_nothing_is_generated ( )
20+ {
21+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput ( null , [ new AdditionalTextStub ( "not_existing.json" ) ] ) ;
22+
23+ Assert . NotEmpty ( outputDiagnostics ) ;
24+ Assert . Single ( outputDiagnostics ) ;
25+ var outputDiagnostic = outputDiagnostics [ 0 ] ;
26+ Assert . Equal ( "NJSG001" , outputDiagnostic . Id ) ;
27+ Assert . Equal ( DiagnosticSeverity . Error , outputDiagnostic . Severity ) ;
28+
29+ Assert . Empty ( compilation . SyntaxTrees ) ;
30+ }
31+
32+ [ Fact ]
33+ public void When_without_config_then_generated_with_default_values ( )
34+ {
35+ var firstName = "Alex" ;
36+ var defaultNamespace = "MyNamespace" ;
37+
38+ string source = $@ "
39+ namespace Example
40+ {{
41+ class Test
42+ {{
43+ public static string RunTest()
44+ {{
45+ var json = new { defaultNamespace } .Person()
46+ {{
47+ FirstName = ""{ firstName } ""
48+ }};
49+ return json.FirstName;
50+ }}
51+ }}
52+ }}" ;
53+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput ( source , [ new AdditionalTextStub ( "References/schema.json" ) ] ) ;
54+
55+ Assert . Empty ( outputDiagnostics ) ;
56+
57+ Assert . Equal ( 2 , compilation . SyntaxTrees . Count ( ) ) ;
58+
59+ Assert . Equal ( firstName , RunTest ( compilation ) ) ;
60+ }
61+
62+ [ Theory ]
63+ [ InlineData ( null , false ) ]
64+ [ InlineData ( "false" , false ) ]
65+ [ InlineData ( "False" , false ) ]
66+ [ InlineData ( "true" , true ) ]
67+ [ InlineData ( "True" , true ) ]
68+ public void When_GenerateOptionalPropertiesAsNullable_in_global_options_then_generate_according_to_config (
69+ string generateOptionalPropertiesAsNullable ,
70+ bool shouldBeNullable )
71+ {
72+ string source = $@ "
73+ namespace Example
74+ {{
75+ class Test
76+ {{
77+ public static string RunTest()
78+ {{
79+ var json = new MyNamespace.Person();
80+ return System.Convert.ToString(json.Age);
81+ }}
82+ }}
83+ }}" ;
84+ var globalOptions = new Dictionary < string , string >
85+ {
86+ { GenerateOptionalPropertiesAsNullable , generateOptionalPropertiesAsNullable }
87+ } ;
88+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput (
89+ source ,
90+ [ new AdditionalTextStub ( "References/schema.json" ) ] ,
91+ globalOptions ) ;
92+
93+ Assert . Empty ( outputDiagnostics ) ;
94+
95+ Assert . Equal ( 2 , compilation . SyntaxTrees . Count ( ) ) ;
96+
97+ var expectedOutput = shouldBeNullable ? string . Empty : "0" ;
98+ Assert . Equal ( expectedOutput , RunTest ( compilation ) ) ;
99+ }
100+
101+ [ Theory ]
102+ [ InlineData ( null , "true" , true ) ]
103+ [ InlineData ( "false" , "true" , false ) ]
104+ [ InlineData ( "False" , "true" , false ) ]
105+ [ InlineData ( "true" , "false" , true ) ]
106+ [ InlineData ( "True" , "false" , true ) ]
107+ public void When_GenerateOptionalPropertiesAsNullable_in_additional_files_then_generate_according_to_config_and_override_global_if_possible (
108+ string generateOptionalPropertiesAsNullableAdditionalFiles ,
109+ string generateOptionalPropertiesAsNullableGlobalOptions ,
110+ bool shouldBeNullable )
111+ {
112+ string source = $@ "
113+ namespace Example
114+ {{
115+ class Test
116+ {{
117+ public static string RunTest()
118+ {{
119+ var json = new MyNamespace.Person();
120+ return System.Convert.ToString(json.Age);
121+ }}
122+ }}
123+ }}" ;
124+ var globalOptions = new Dictionary < string , string >
125+ {
126+ { GenerateOptionalPropertiesAsNullable , generateOptionalPropertiesAsNullableGlobalOptions }
127+ } ;
128+ var additionalFilesOptions = new Dictionary < string , string >
129+ {
130+ { GenerateOptionalPropertiesAsNullable , generateOptionalPropertiesAsNullableAdditionalFiles }
131+ } ;
132+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput (
133+ source ,
134+ [ new AdditionalTextStub ( "References/schema.json" , additionalFilesOptions ) ] ,
135+ globalOptions ) ;
136+
137+ Assert . Empty ( outputDiagnostics ) ;
138+
139+ Assert . Equal ( 2 , compilation . SyntaxTrees . Count ( ) ) ;
140+
141+ var expectedOutput = shouldBeNullable ? string . Empty : "0" ;
142+ Assert . Equal ( expectedOutput , RunTest ( compilation ) ) ;
143+ }
144+
145+ [ Theory ]
146+ [ InlineData ( null , null , "MyNamespace" ) ]
147+ [ InlineData ( "" , null , "MyNamespace" ) ]
148+ [ InlineData ( null , "" , "MyNamespace" ) ]
149+ [ InlineData ( null , "NamespaceFromGlobalOptions" , "NamespaceFromGlobalOptions" ) ]
150+ [ InlineData ( "NamespaceFromLocalOptions" , null , "NamespaceFromLocalOptions" ) ]
151+ [ InlineData ( "NamespaceFromLocalOptions" , "NamespaceFromGlobalOptions" , "NamespaceFromLocalOptions" ) ]
152+ public void When_Namespace_in_config_then_generate (
153+ string namespaceAdditionalFiles ,
154+ string namespaceGlobalOptions ,
155+ string expectedNamespace )
156+ {
157+ string source = $@ "
158+ namespace Example
159+ {{
160+ class Test
161+ {{
162+ public static string RunTest()
163+ {{
164+ var json = new { expectedNamespace } .Person();
165+ return ""compiled"";
166+ }}
167+ }}
168+ }}" ;
169+ var globalOptions = new Dictionary < string , string >
170+ {
171+ { Namespace , namespaceGlobalOptions }
172+ } ;
173+ var additionalFilesOptions = new Dictionary < string , string >
174+ {
175+ { Namespace , namespaceAdditionalFiles }
176+ } ;
177+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput (
178+ source ,
179+ [ new AdditionalTextStub ( "References/schema.json" , additionalFilesOptions ) ] ,
180+ globalOptions ) ;
181+
182+ Assert . Empty ( outputDiagnostics ) ;
183+
184+ Assert . Equal ( 2 , compilation . SyntaxTrees . Count ( ) ) ;
185+
186+ Assert . Equal ( "compiled" , RunTest ( compilation ) ) ;
187+ }
188+
189+ [ Theory ]
190+ [ InlineData ( null , null , "Person" ) ]
191+ [ InlineData ( null , "" , "Person" ) ]
192+ [ InlineData ( "" , null , "Person" ) ]
193+ [ InlineData ( null , "ShouldNotOverride" , "Person" ) ]
194+ [ InlineData ( "ShouldOverride" , null , "ShouldOverride" ) ]
195+ public void When_TypeNameHint_in_config_then_generate_using_additional_files_only (
196+ string typeNameHintAdditionalFiles ,
197+ string typeNameHintGlobalOptions ,
198+ string expectedTypeName )
199+ {
200+ string source = $@ "
201+ namespace Example
202+ {{
203+ class Test
204+ {{
205+ public static string RunTest()
206+ {{
207+ var json = new MyNamespace.{ expectedTypeName } ();
208+ return ""compiled"";
209+ }}
210+ }}
211+ }}" ;
212+ var globalOptions = new Dictionary < string , string >
213+ {
214+ { TypeNameHint , typeNameHintGlobalOptions }
215+ } ;
216+ var additionalFilesOptions = new Dictionary < string , string >
217+ {
218+ { TypeNameHint , typeNameHintAdditionalFiles }
219+ } ;
220+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput (
221+ source ,
222+ [ new AdditionalTextStub ( "References/schema.json" , additionalFilesOptions ) ] ,
223+ globalOptions ) ;
224+
225+ Assert . Empty ( outputDiagnostics ) ;
226+
227+ Assert . Equal ( 2 , compilation . SyntaxTrees . Count ( ) ) ;
228+
229+ Assert . Equal ( "compiled" , RunTest ( compilation ) ) ;
230+ }
231+
232+ [ Theory ]
233+ [ InlineData ( null , null , "NJsonSchemaGenerated.g.cs" ) ]
234+ [ InlineData ( "" , null , "NJsonSchemaGenerated.g.cs" ) ]
235+ [ InlineData ( null , "" , "NJsonSchemaGenerated.g.cs" ) ]
236+ [ InlineData ( null , "ShouldNotOverride.g.cs" , "NJsonSchemaGenerated.g.cs" ) ]
237+ [ InlineData ( "ShouldOverride.g.cs" , null , "ShouldOverride.g.cs" ) ]
238+ public void When_FileName_in_config_then_generate_using_additional_files_only (
239+ string fileNameAdditionalFiles ,
240+ string fileNameGlobalOptions ,
241+ string expectedFileName )
242+ {
243+ var globalOptions = new Dictionary < string , string >
244+ {
245+ { FileName , fileNameGlobalOptions }
246+ } ;
247+ var additionalFilesOptions = new Dictionary < string , string >
248+ {
249+ { FileName , fileNameAdditionalFiles }
250+ } ;
251+ var ( compilation , outputDiagnostics ) = GetGeneratedOutput (
252+ null ,
253+ [ new AdditionalTextStub ( "References/schema.json" , additionalFilesOptions ) ] ,
254+ globalOptions ) ;
255+
256+ Assert . Empty ( outputDiagnostics ) ;
257+
258+ Assert . Single ( compilation . SyntaxTrees ) ;
259+ var syntaxTree = compilation . SyntaxTrees . First ( ) ;
260+ Assert . EndsWith ( expectedFileName , syntaxTree . FilePath ) ;
261+ }
262+ }
263+ }
0 commit comments