Skip to content

Commit

Permalink
Fix analysis warnings in tests (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 23, 2024
1 parent 36bf0f1 commit 953ab69
Show file tree
Hide file tree
Showing 131 changed files with 2,013 additions and 2,006 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public abstract class AbstractClass
}

[Fact]
public async Task When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated()
public void When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated()
{
/// Arrange
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<AbstractClass>();

/// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
var code = generator.GenerateFile("AbstractClass");

/// Assert
// Assert
Assert.Contains("public abstract partial class AbstractClass", code);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AdditionalPropertiesTests
[Fact]
public async Task When_additionalProperties_schema_is_set_for_object_then_special_property_is_rendered()
{
//// Arrange
// Arrange
var json =
@"{
""properties"": {
Expand All @@ -33,11 +33,11 @@ public async Task When_additionalProperties_schema_is_set_for_object_then_specia
}";
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
var code = generator.GenerateFile("Person");

//// Assert
// Assert
Assert.Contains("[Newtonsoft.Json.JsonExtensionData]", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> AdditionalProperties", code);
Assert.Contains("get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }", code);
Expand All @@ -46,7 +46,7 @@ public async Task When_additionalProperties_schema_is_set_for_object_then_specia
[Fact]
public async Task When_using_SystemTextJson_additionalProperties_schema_is_set_for_object_then_special_property_is_rendered()
{
//// Arrange
// Arrange
var json =
@"{
""properties"": {
Expand All @@ -69,14 +69,14 @@ public async Task When_using_SystemTextJson_additionalProperties_schema_is_set_f
}";
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()
{
JsonLibrary = CSharpJsonLibrary.SystemTextJson
});
var code = generator.GenerateFile("Person");

//// Assert
// Assert
Assert.Contains("[System.Text.Json.Serialization.JsonExtensionData]", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> AdditionalProperties", code);
Assert.Contains("get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }", code);
Expand Down Expand Up @@ -128,14 +128,14 @@ public async Task When_using_SystemTextJson_additionalProperties_schema_is_set_f

var schema = await JsonSchema.FromJsonAsync(json);

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()
{
JsonLibrary = CSharpJsonLibrary.SystemTextJson
});
var code = generator.GenerateFile("Person");

//// Assert
// Assert
var matches = Regex.Matches(code, @"(\[System\.Text\.Json\.Serialization\.JsonExtensionData\])");

// There are two matches, the Person class and the Pet class
Expand Down Expand Up @@ -204,15 +204,15 @@ public async Task When_using_SystemTextJson_additionalProperties_schema_is_set_f

var schema = await JsonSchema.FromJsonAsync(json);

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()
{
JsonLibrary = CSharpJsonLibrary.SystemTextJson
});

var code = generator.GenerateFile("SommeDummyClass");

//// Assert
// Assert
var matches = Regex.Matches(code, @"(\[System\.Text\.Json\.Serialization\.JsonExtensionData\])");

// There are two matches, the SommeDummyClass class and the Animal class
Expand Down
36 changes: 18 additions & 18 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AllOfTests
[Fact]
public async Task When_allOf_has_two_schemas_then_referenced_schema_is_inherited()
{
//// Arrange
// Arrange
var json =
@"{
""allOf"": [
Expand Down Expand Up @@ -44,19 +44,19 @@ public async Task When_allOf_has_two_schemas_then_referenced_schema_is_inherited
}";
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
var code = generator.GenerateFile("A");

//// Assert
// Assert
Assert.DoesNotContain("Anonymous", code);
Assert.Contains("A : B", code);
}

[Fact]
public async Task When_allOf_has_one_schema_then_it_is_inherited()
{
//// Arrange
// Arrange
var json =
@"{
""type"": ""object"",
Expand Down Expand Up @@ -96,19 +96,19 @@ public async Task When_allOf_has_one_schema_then_it_is_inherited()
}";
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
var code = generator.GenerateFile("A");

//// Assert
// Assert
Assert.DoesNotContain("Anonymous", code);
Assert.Contains("A : B", code);
}

[Fact]
public async Task When_all_of_has_multiple_refs_then_the_properties_should_expand_to_single_class()
{
//// Arrange
// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'id': 'http://some.domain.com/foo.json',
Expand Down Expand Up @@ -148,13 +148,13 @@ public async Task When_all_of_has_multiple_refs_then_the_properties_should_expan
}
}";

//// Act
// Act
var schema = await JsonSchema.FromJsonAsync(json);
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns" };
var generator = new CSharpGenerator(schema, settings);
var output = generator.GenerateFile("Foo");

//// Assert
// Assert
Assert.Contains("public partial class TAgg", output);
Assert.Contains("public string Val1 { get; set; }", output);
Assert.Contains("public string Val2 { get; set; }", output);
Expand All @@ -164,7 +164,7 @@ public async Task When_all_of_has_multiple_refs_then_the_properties_should_expan
[Fact]
public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_all_of_contains_all_properties_in_generated_code()
{
//// Arrange
// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
Expand All @@ -186,12 +186,12 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a
]
}";

//// Act
// Act
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var code = generator.GenerateFile("Foo").Replace("\r\n", "\n");

//// Assert
// Assert
Assert.Contains(@" public partial class Foo : Anonymous
{
[Newtonsoft.Json.JsonProperty(""prop1"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
Expand All @@ -207,7 +207,7 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a
[Fact]
public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_class_in_generated_code()
{
//// Arrange
// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
Expand All @@ -229,12 +229,12 @@ public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_class
}
}";

//// Act
// Act
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var code = generator.GenerateFile("Foo");

//// Assert
// Assert
Assert.Contains("class Foo : Bar", code);
Assert.Contains("public string Prop1 { get; set; }", code);
Assert.Contains("public string Prop2 { get; set; }", code);
Expand All @@ -243,7 +243,7 @@ public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_class
[Fact]
public async Task When_allOf_schema_contains_two_anonymous_nodes_without_type_specifier_an_anonymous_class_is_generated()
{
//// Arrange
// Arrange
// The issue here is that the 'type' specifier has been (legally) omitted.
var json = @"
{
Expand All @@ -263,12 +263,12 @@ public async Task When_allOf_schema_contains_two_anonymous_nodes_without_type_sp
]
}";

//// Act
// Act
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var code = generator.GenerateFile("Foo");

//// Assert
// Assert
Assert.Contains("class Foo", code);
Assert.Contains("class Anonymous", code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class MyRequiredTest
}

[Fact]
public async Task When_array_property_is_not_nullable_then_it_does_not_have_a_setter()
public void When_array_property_is_not_nullable_then_it_does_not_have_a_setter()
{
//// Arrange
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<MyRequiredTest>();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
Expand All @@ -31,10 +31,10 @@ public async Task When_array_property_is_not_nullable_then_it_does_not_have_a_se
GenerateDefaultValues = false
});

//// Act
// Act
var code = generator.GenerateFile();

//// Assert
// Assert
Assert.Contains("public System.Collections.Generic.ICollection<string> Collection { get; } = new System.Collections.ObjectModel.Collection<string>();", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> Dictionary { get; } = new System.Collections.Generic.Dictionary<string, object>();", code);
}
Expand Down
10 changes: 5 additions & 5 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class ArrayTest
}

[Fact]
public async Task When_array_property_is_required_then_array_instance_can_be_changed()
public void When_array_property_is_required_then_array_instance_can_be_changed()
{
//// Arrange
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<ArrayTest>();
var data = schema.ToJson();

//// Act
// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Expand All @@ -29,7 +29,7 @@ public async Task When_array_property_is_required_then_array_instance_can_be_cha
});
var code = generator.GenerateFile("MyClass");

//// Assert
// Assert
Assert.Contains("public Foo<string> ArrayProperty { get; set; } = new Bar<string>();", code);
}

Expand All @@ -41,7 +41,7 @@ public class ClassWithNullableArrayItems
}

[Fact]
public async Task When_array_item_is_nullable_then_generated_CSharp_is_correct()
public void When_array_item_is_nullable_then_generated_CSharp_is_correct()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<ClassWithNullableArrayItems>();
Expand Down
Loading

0 comments on commit 953ab69

Please sign in to comment.