From 953ab692095dbc8903953b6204055a946049e465 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sat, 23 Nov 2024 15:35:13 +0200 Subject: [PATCH] Fix analysis warnings in tests (#1755) --- .../AbstractSchemaTests.cs | 8 +- .../AdditionalPropertiesTests.cs | 20 +- .../AllOfTests.cs | 36 +- .../AnnotationsTests.cs | 8 +- .../ArrayTests.cs | 10 +- .../CSharpJsonSerializerGeneratorTests.cs | 36 +- .../DefaultPropertyTests.cs | 4 +- .../DictionaryTests.cs | 16 +- .../EnumTests.cs | 92 ++-- .../GeneralGeneratorTests.cs | 416 +++++++++--------- .../InheritanceInterfaceTests.cs | 24 +- .../InheritanceTests.cs | 40 +- .../InterfaceTests.cs | 16 +- ...nSchema.CodeGeneration.CSharp.Tests.csproj | 4 +- .../NullableEnumTests.cs | 4 +- .../NullableReferenceTypesTests.cs | 46 +- .../NullableTests.cs | 16 +- .../NumberTests.cs | 60 +-- .../ObjectPropertyRequiredTests.cs | 32 +- .../ObsoleteTests.cs | 32 +- .../ReferencesTest.cs | 34 +- .../StringPropertyRequiredTests.cs | 32 +- .../UriTests.cs | 20 +- .../ValidationAttributesTests.cs | 72 +-- .../ValueGeneratorTests.cs | 20 +- .../DefaultGenerationTests.cs | 18 +- .../DefaultValueGeneratorTests.cs | 60 +-- .../EnumGenerationTests.cs | 66 +-- .../InheritanceSerializationTests.cs | 60 +-- .../Samples/SampleTests.cs | 12 +- .../AbstractGenerationTests.cs | 24 +- .../ClassGenerationTests.cs | 34 +- .../ClassOrderTests.cs | 6 +- .../ConstructorInterfaceTests.cs | 14 +- .../DateCodeGenerationTests.cs | 72 +-- .../DateTimeCodeGenerationTests.cs | 64 +-- .../DictionaryTests.cs | 64 +-- .../ExtensionCodeTests.cs | 20 +- .../InheritanceTests.cs | 52 +-- ...ema.CodeGeneration.TypeScript.Tests.csproj | 4 +- .../NullabilityTests.cs | 4 +- .../TypeScriptDictionaryTests.cs | 38 +- .../TypeScriptDiscriminatorTests.cs | 24 +- .../TypeScriptGeneratorTests.cs | 110 ++--- .../TypeScriptObjectTests.cs | 16 +- .../NJsonSchema.Demo.Performance.csproj | 20 +- src/NJsonSchema.Demo.Performance/Program.cs | 2 +- .../Generation/RecordTests.cs | 8 +- .../Generation/StructTests.cs | 8 +- .../NJsonSchema.NewtonsoftJson.Tests.csproj | 2 +- .../Conversion/ArrayTypeToSchemaTests.cs | 8 +- .../Conversion/SchemaReferenceTests.cs | 30 +- .../Conversion/TypeToSchemaTests.cs | 60 +-- .../Converters/JsonExceptionConverterTests.cs | 40 +- .../Generation/AbstractGenerationTests.cs | 8 +- .../Generation/AnnotationsGenerationTests.cs | 72 +-- .../Generation/ArrayGenerationTests.cs | 16 +- .../Generation/AttributeGenerationTests.cs | 76 ++-- .../Generation/ContractResolverTests.cs | 8 +- .../Generation/DataContractTests.cs | 12 +- .../DefaultReflectionServiceTests.cs | 12 +- .../DefaultSchemaNameGeneratorTests.cs | 4 +- .../DefaultTypeNameGeneratorTests.cs | 12 +- .../Generation/DictionaryTests.cs | 16 +- .../Generation/EnumGenerationTests.cs | 48 +- src/NJsonSchema.Tests/Generation/EnumTests.cs | 8 +- .../Generation/ExceptionTypeTests.cs | 6 +- .../ExcplicitInterfacePropertyTests.cs | 14 +- .../ExtensionDataGenerationTests.cs | 6 +- .../Generation/FieldGenerationTests.cs | 6 +- .../FlattenInheritanceHierarchyTests.cs | 40 +- .../Generation/GenericTests.cs | 12 +- .../Generation/IgnoredPropertyTests.cs | 18 +- .../Generation/InheritanceTests.cs | 82 ++-- .../Generation/InterfaceTests.cs | 6 +- .../Generation/JsonPropertyAttributeTests.cs | 24 +- ...emaExtensionDataAttributeGeneratorTests.cs | 8 +- .../JsonSchemaTypeAttributeTests.cs | 24 +- .../Generation/KnownTypeGenerationTests.cs | 12 +- .../Generation/NullPropertyTests.cs | 12 +- .../PrimitiveTypeGenerationTests.cs | 54 +-- .../PropertyNamesGenerationTests.cs | 18 +- .../Generation/RecordTests.cs | 8 +- .../SampleJsonDataGeneratorTests.cs | 76 ++-- .../SampleJsonSchemaGeneratorTests.cs | 24 +- .../Generation/SchemaGenerationTests.cs | 38 +- .../Generation/SchemaProcessorTests.cs | 4 +- .../Generation/ShouldSerializeTests.cs | 6 +- ...temTextJsonExtensionDataGenerationTests.cs | 12 +- .../SystemTextJsonInheritanceTests.cs | 18 +- .../SystemTextJson/SystemTextJsonTests.cs | 20 +- .../Generation/TypeMapperTests.cs | 12 +- .../Generation/XmlDocsTests.cs | 4 +- .../Generation/XmlObjectTests.cs | 8 +- ...ndIgnoreSerializerContractResolverTests.cs | 18 +- .../NJsonSchema.Tests.csproj | 2 +- .../References/LocalReferencesTests.cs | 54 +-- .../Schema/DataContractTests.cs | 28 +- .../JsonPathUtilitiesGetJsonPathTests.cs | 24 +- ...PathUtilitiesGetObjectFromJsonPathTests.cs | 30 +- .../Schema/JsonSchemaTests.cs | 118 ++--- .../AllowAdditionalPropertiesTests.cs | 12 +- .../DiscriminatorSerializationTests.cs | 18 +- .../Serialization/ExtensionDataTests.cs | 46 +- .../Validation/ArrayValidationTests.cs | 78 ++-- .../Validation/CustomValidationTests.cs | 6 +- .../Validation/EnumValidationTests.cs | 44 +- .../Validation/FormatBase64Tests.cs | 56 +-- .../Validation/FormatDateTests.cs | 18 +- .../Validation/FormatDateTimeTests.cs | 36 +- .../Validation/FormatEmailTests.cs | 12 +- .../Validation/FormatGuidTests.cs | 12 +- .../Validation/FormatHostnameTests.cs | 12 +- .../Validation/FormatIpV4Tests.cs | 12 +- .../Validation/FormatIpV6Tests.cs | 12 +- .../Validation/FormatTimeSpanTests.cs | 12 +- .../Validation/FormatTimeTests.cs | 36 +- .../Validation/FormatUriTests.cs | 12 +- .../Validation/FormatUuidTests.cs | 12 +- .../Validation/InheritanceTests.cs | 62 +-- .../Validation/LineInformationTest.cs | 12 +- .../Validation/NullPropertyTests.cs | 56 +-- .../Validation/NumberTests.cs | 23 +- .../Validation/ObjectValidationTests.cs | 56 +-- .../Validation/OneOfValidationTests.cs | 6 +- .../PatternPropertyValidationTests.cs | 18 +- .../Validation/SchemaTests.cs | 30 +- .../Validation/UniqueItemsTests.cs | 6 +- .../Validation/ValueTypeValidationTests.cs | 112 ++--- .../NJsonSchema.Yaml.Tests.csproj | 4 +- .../References/YamlReferencesTests.cs | 12 +- 131 files changed, 2013 insertions(+), 2006 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AbstractSchemaTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AbstractSchemaTests.cs index edeea7f30..58bf8c0cb 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AbstractSchemaTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AbstractSchemaTests.cs @@ -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(); - /// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()); var code = generator.GenerateFile("AbstractClass"); - /// Assert + // Assert Assert.Contains("public abstract partial class AbstractClass", code); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AdditionalPropertiesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AdditionalPropertiesTests.cs index 272709d27..5bb4247fe 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AdditionalPropertiesTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AdditionalPropertiesTests.cs @@ -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"": { @@ -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 AdditionalProperties", code); Assert.Contains("get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }", code); @@ -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"": { @@ -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 AdditionalProperties", code); Assert.Contains("get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary()); }", code); @@ -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 @@ -204,7 +204,7 @@ 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 @@ -212,7 +212,7 @@ public async Task When_using_SystemTextJson_additionalProperties_schema_is_set_f 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 diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs index 76c329069..24004c1cc 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs @@ -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"": [ @@ -44,11 +44,11 @@ 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); } @@ -56,7 +56,7 @@ public async Task When_allOf_has_two_schemas_then_referenced_schema_is_inherited [Fact] public async Task When_allOf_has_one_schema_then_it_is_inherited() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -96,11 +96,11 @@ 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); } @@ -108,7 +108,7 @@ public async Task When_allOf_has_one_schema_then_it_is_inherited() [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', @@ -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); @@ -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', @@ -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)] @@ -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', @@ -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); @@ -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 = @" { @@ -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); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AnnotationsTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AnnotationsTests.cs index 0c1e6d80e..c1d647381 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/AnnotationsTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/AnnotationsTests.cs @@ -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(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { @@ -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 Collection { get; } = new System.Collections.ObjectModel.Collection();", code); Assert.Contains("public System.Collections.Generic.IDictionary Dictionary { get; } = new System.Collections.Generic.Dictionary();", code); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs index ff207aec4..77323ce28 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs @@ -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(); var data = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -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 ArrayProperty { get; set; } = new Bar();", code); } @@ -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(); diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs index 4244eec55..2df496b97 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs @@ -7,25 +7,25 @@ public class CSharpJsonSerializerGeneratorTests [Fact] public void When_using_SystemTextJson_GenerateJsonSerializerParameterCode_generates_correctly() { - //// Arrange + // Arrange var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; var settings = new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.SystemTextJson }; - //// Act + // Act var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, additionalJsonConverters); //Console.WriteLine(output); - //// Assert + // Assert Assert.Equal("new System.Text.Json.JsonSerializerOptions()", output); } [Fact] public void When_using_NewtonsoftJson_with_JsonConverters_GenerateJsonSerializerParameterCode_generates_correctly() { - //// Arrange + // Arrange var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; var settings = new CSharpGeneratorSettings { @@ -33,36 +33,36 @@ public void When_using_NewtonsoftJson_with_JsonConverters_GenerateJsonSerializer JsonConverters = ["CustomConverter1", "CustomConverter2"] }; - //// Act + // Act var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, additionalJsonConverters); //Console.WriteLine(output); - //// Assert + // Assert Assert.Equal("new Newtonsoft.Json.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() }", output); } [Fact] public void When_using_SystemTextJson_with_JsonSerializerSettingsOrOptionsTransformationMethod_GenerateJsonSerializerParameterCode_generates_correctly() { - //// Arrange + // Arrange var settings = new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.SystemTextJson, JsonSerializerSettingsTransformationMethod = "TestJsonSerializerSettingsTransformationMethod" }; - //// Act + // Act var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, null); //Console.WriteLine(output); - //// Assert + // Assert Assert.Equal("TestJsonSerializerSettingsTransformationMethod(new System.Text.Json.JsonSerializerOptions())", output); } [Fact] public void When_using_NewtonsoftJson_with_HandleReferences_and_JsonConverters_and_JsonSerializerSettingsTransformationMethod_GenerateJsonSerializerParameterCode_generates_correctly() { - //// Arrange + // Arrange var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; var settings = new CSharpGeneratorSettings { @@ -72,18 +72,18 @@ public void When_using_NewtonsoftJson_with_HandleReferences_and_JsonConverters_a JsonSerializerSettingsTransformationMethod = "TestJsonSerializerSettingsTransformationMethod", }; - //// Act + // Act var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, additionalJsonConverters); //Console.WriteLine(output); - //// Assert + // Assert Assert.Equal("TestJsonSerializerSettingsTransformationMethod(new Newtonsoft.Json.JsonSerializerSettings { PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All, Converters = new Newtonsoft.Json.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() } })", output); } [Fact] public void When_using_SystemTextJson_with_JsonConverters_GenerateJsonConvertersArrayCode_generates_correctly() { - //// Arrange + // Arrange var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; var settings = new CSharpGeneratorSettings { @@ -91,18 +91,18 @@ public void When_using_SystemTextJson_with_JsonConverters_GenerateJsonConverters JsonConverters = ["CustomConverter1", "CustomConverter2"] }; - //// Act + // Act var output = CSharpJsonSerializerGenerator.GenerateJsonConvertersArrayCode(settings, additionalJsonConverters); //Console.WriteLine(output); - //// Assert + // Assert Assert.Equal("new System.Text.Json.Serialization.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() }", output); } [Fact] public void When_using_NewtonsoftJson_with_JsonConverters_GenerateJsonConvertersArrayCode_generates_correctly() { - //// Arrange + // Arrange var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; var settings = new CSharpGeneratorSettings { @@ -110,11 +110,11 @@ public void When_using_NewtonsoftJson_with_JsonConverters_GenerateJsonConverters JsonConverters = ["CustomConverter1", "CustomConverter2"] }; - //// Act + // Act var output = CSharpJsonSerializerGenerator.GenerateJsonConvertersArrayCode(settings, additionalJsonConverters); //Console.WriteLine(output); - //// Assert + // Assert Assert.Equal("new Newtonsoft.Json.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() }", output); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs index d1b207b55..e5cc12003 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs @@ -89,7 +89,7 @@ public async Task When_generating_CSharp_code_then_default_value_generates_expec } }"); - //// Act + // Act var settings = new CSharpGeneratorSettings { GenerateDefaultValues = true @@ -117,7 +117,7 @@ public async Task When_generating_CSharp_code_then_default_value_with_decimal_ge } }"); - //// Act + // Act var settings = new CSharpGeneratorSettings { GenerateDefaultValues = true diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/DictionaryTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/DictionaryTests.cs index 4fe94822c..dd6555ec0 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/DictionaryTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/DictionaryTests.cs @@ -21,29 +21,29 @@ public class EnumKeyDictionaryTest } [Fact] - public async Task When_dictionary_key_is_enum_then_csharp_has_enum_key() + public void When_dictionary_key_is_enum_then_csharp_has_enum_key() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Collections.Generic.IDictionary EnumDictionary ", code); Assert.Contains("public System.Collections.Generic.IDictionary EnumInterfaceDictionary ", code); } [Fact] - public async Task When_dictionary_property_is_required_then_dictionary_instance_can_be_changed() + public void When_dictionary_property_is_required_then_dictionary_instance_can_be_changed() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -52,7 +52,7 @@ public async Task When_dictionary_property_is_required_then_dictionary_instance_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public Foo EnumInterfaceDictionary { get; set; } = new Bar();", code); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs index f629dba18..d26cf98b1 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs @@ -11,7 +11,7 @@ public class EnumTests [Fact] public async Task When_enum_has_no_type_then_enum_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -27,17 +27,17 @@ public async Task When_enum_has_no_type_then_enum_is_generated() var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public enum MyClassCategory", code); } [Fact] public async Task When_enum_has_no_type_then_enum_is_generated_with_flags() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -57,10 +57,10 @@ public async Task When_enum_has_no_type_then_enum_is_generated_with_flags() var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { EnforceFlagEnums = true }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.Flags]", code); Assert.Contains("Commercial = 1,", code); Assert.Contains("Residential = 2,", code); @@ -72,7 +72,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated_with_flags() [Fact] public async Task When_enum_name_contains_colon_then_it_is_removed_and_next_word_converted_to_upper_case() { - //// Arrange + // Arrange var json = @" { ""type"": ""object"", @@ -110,10 +110,10 @@ public async Task When_enum_name_contains_colon_then_it_is_removed_and_next_word var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("PullrequestUpdated = 0,", code); } @@ -132,34 +132,34 @@ public enum MyStringEnum } [Fact] - public async Task When_enum_list_uses_string_enums_then_ItemConverterType_is_set() + public void When_enum_list_uses_string_enums_then_ItemConverterType_is_set() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter)", code); } [Fact] - public async Task When_enum_is_nullable_then_StringEnumConverter_is_set() + public void When_enum_is_nullable_then_StringEnumConverter_is_set() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]", code); } @@ -177,18 +177,18 @@ public class SomeClass } [Fact] - public async Task When_class_has_enum_array_property_then_enum_name_is_preserved() + public void When_class_has_enum_array_property_then_enum_name_is_preserved() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var json = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("SomeEnum", code); Assert.DoesNotContain("Anonymous", code); } @@ -196,7 +196,7 @@ public async Task When_class_has_enum_array_property_then_enum_name_is_preserved [Fact] public async Task When_type_name_hint_has_generics_then_they_are_converted() { - /// Arrange + // Arrange var json = @" { ""properties"": { @@ -221,7 +221,7 @@ public async Task When_type_name_hint_has_generics_then_they_are_converted() } } }"; - /// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings(); @@ -229,14 +229,14 @@ public async Task When_type_name_hint_has_generics_then_they_are_converted() var code = generator.GenerateFile("Foo"); - /// Assert + // Assert Assert.Contains("public enum FirstMetdodOfMetValueGroupChar", code); } [Fact] public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_nullable() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -259,10 +259,10 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { SchemaType = SchemaType.Swagger2 }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public MyClassStatus? Status { get; set; }", code); } @@ -299,7 +299,7 @@ public async Task When_enum_contains_operator_convert_to_string_equivalent() } }"; - /// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings(); @@ -307,7 +307,7 @@ public async Task When_enum_contains_operator_convert_to_string_equivalent() var code = generator.GenerateFile("Foo"); - /// Assert + // Assert Assert.DoesNotContain("__", code); Assert.Contains("Eq = 0", code); @@ -336,7 +336,7 @@ public async Task When_enum_starts_with_plus_or_minus_convert_to_string_equivale } }"; - /// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings(); @@ -344,7 +344,7 @@ public async Task When_enum_starts_with_plus_or_minus_convert_to_string_equivale var code = generator.GenerateFile("Foo"); - /// Assert + // Assert Assert.DoesNotContain("__", code); Assert.Contains("MinusFoo = 0", code); Assert.Contains("PlusFoo = 1", code); @@ -354,7 +354,7 @@ public async Task When_enum_starts_with_plus_or_minus_convert_to_string_equivale [Fact] public async Task When_array_item_enum_is_not_referenced_then_type_name_hint_is_property_name() { - /// Arrange + // Arrange var json = @" { ""properties"": { @@ -388,7 +388,7 @@ public async Task When_array_item_enum_is_not_referenced_then_type_name_hint_is_ } } }"; - /// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings(); @@ -396,7 +396,7 @@ public async Task When_array_item_enum_is_not_referenced_then_type_name_hint_is_ var code = generator.GenerateFile("Foo"); - /// Assert + // Assert Assert.DoesNotContain("public enum Anonymous", code); Assert.Contains("public enum Status", code); } @@ -404,7 +404,7 @@ public async Task When_array_item_enum_is_not_referenced_then_type_name_hint_is_ [Fact] public async Task When_enum_is_integer_flags_it_should_use_declared_values() { - //// Arrange + // Arrange var json = @" { ""properties"": { @@ -436,7 +436,7 @@ public async Task When_enum_is_integer_flags_it_should_use_declared_values() } } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings(); @@ -444,7 +444,7 @@ public async Task When_enum_is_integer_flags_it_should_use_declared_values() var code = generator.GenerateFile("Foo"); - //// Assert + // Assert Assert.DoesNotContain("public enum Anonymous", code); // Verify previous incorrect logic wasn't used to determine enum values (and doesn't generate duplicate incorrect values): Assert.DoesNotContain("None = 1,", code); @@ -468,7 +468,7 @@ public async Task When_enum_is_integer_flags_it_should_use_declared_values() [Fact] public async Task When_enum_is_nullable_not_required_it_should_be_nullable_with_converter() { - //// Arrange + // Arrange var json = @" { ""type"": ""object"", @@ -488,7 +488,7 @@ public async Task When_enum_is_nullable_not_required_it_should_be_nullable_with_ } } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings { EnforceFlagEnums = true }; @@ -505,7 +505,7 @@ public async Task When_enum_is_nullable_not_required_it_should_be_nullable_with_ [Fact] public async Task When_enum_is_nullable_required_it_should_be_nullable_with_converter() { - //// Arrange + // Arrange var json = @" { ""type"": ""object"", @@ -526,7 +526,7 @@ public async Task When_enum_is_nullable_required_it_should_be_nullable_with_conv } } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var settings = new CSharpGeneratorSettings { EnforceFlagEnums = true }; @@ -543,7 +543,7 @@ public async Task When_enum_is_nullable_required_it_should_be_nullable_with_conv [Fact] public async Task When_enum_is_nullable_and_has_default_then_question_mark_is_omitted() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -567,10 +567,10 @@ public async Task When_enum_is_nullable_and_has_default_then_question_mark_is_om GenerateOptionalPropertiesAsNullable = true, }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public MyClassCategory? Category { get; set; } = MyNamespace.MyClassCategory.Commercial;", code); } @@ -604,10 +604,10 @@ public async Task When_enum_has_a_format_then_enum_is_generated_with_correct_bas var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public enum ManyValuesTestEnum : long", code); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs index d5525e25c..2569c2aef 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs @@ -19,7 +19,7 @@ public class GeneralGeneratorTests [Fact] public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_array_is_generated() { - //// Arrange + // Arrange var json = @"{ 'required': [ 'emptySchema' ], 'properties': { @@ -28,12 +28,12 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_ }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns", }; var generator = new CSharpGenerator(schema, settings); var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Collections.Generic.ICollection EmptySchema { get; set; } = ", output); AssertCompile(output); @@ -42,7 +42,7 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_ [Fact] public async Task When_code_is_generated_then_toolchain_version_is_printed() { - //// Arrange + // Arrange var json = @"{ 'required': [ 'emptySchema' ], 'properties': { @@ -51,12 +51,12 @@ public async Task When_code_is_generated_then_toolchain_version_is_printed() }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns", }; var generator = new CSharpGenerator(schema, settings); var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(" (Newtonsoft.Json ", output); AssertCompile(output); @@ -126,9 +126,9 @@ public class Address } [Fact] - public async Task When_property_name_is_created_by_custom_fun_then_attribute_is_correct() + public void When_property_name_is_created_by_custom_fun_then_attribute_is_correct() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); var settings = new CSharpGeneratorSettings @@ -139,11 +139,11 @@ public async Task When_property_name_is_created_by_custom_fun_then_attribute_is_ var generator = new CSharpGenerator(schema, settings); - //// Act + // Act var output = generator.GenerateFile("Teacher"); //Console.WriteLine(output); - //// Assert + // Assert Assert.Contains(@"[Newtonsoft.Json.JsonProperty(""lastName""", output); Assert.Contains(@"public string MyCustomLastName", output); Assert.Contains(@"public partial class MyCustomTypeTeacher", output); @@ -155,7 +155,7 @@ public async Task When_property_name_is_created_by_custom_fun_then_attribute_is_ [Fact] public async Task When_schema_contains_ref_to_definition_that_refs_another_definition_then_result_should_contain_correct_target_ref_type() { - //// Arrange + // Arrange var schemaJson = @"{ 'x-typeName': 'foo', @@ -191,10 +191,10 @@ public async Task When_schema_contains_ref_to_definition_that_refs_another_defin }; var gen = new CSharpGenerator(schema, settings); - //// Act + // Act var output = gen.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Collections.Generic.ICollection", output); AssertCompile(output); @@ -203,13 +203,13 @@ public async Task When_schema_contains_ref_to_definition_that_refs_another_defin [Fact] public async Task When_namespace_is_set_then_it_should_appear_in_output() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("namespace MyNamespace", output); Assert.Contains("Dictionary", output); @@ -219,14 +219,14 @@ public async Task When_namespace_is_set_then_it_should_appear_in_output() [Fact] public async Task When_POCO_is_set_then_auto_properties_are_generated() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); generator.Settings.ClassStyle = CSharpClassStyle.Poco; - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("{ get; set; }", output); AssertCompile(output); @@ -235,13 +235,13 @@ public async Task When_POCO_is_set_then_auto_properties_are_generated() [Fact] public async Task When_property_name_does_not_match_property_name_then_attribute_is_correct() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"[Newtonsoft.Json.JsonProperty(""lastName""", output); Assert.Contains(@"public string LastName", output); @@ -249,17 +249,17 @@ public async Task When_property_name_does_not_match_property_name_then_attribute } [Fact] - public async Task When_property_is_timespan_than_csharp_timespan_is_used() + public void When_property_is_timespan_than_csharp_timespan_is_used() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"public System.TimeSpan TimeSpan", output); AssertCompile(output); @@ -268,30 +268,30 @@ public async Task When_property_is_timespan_than_csharp_timespan_is_used() [Fact] public async Task When_allOf_contains_one_schema_then_csharp_inheritance_is_generated() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); - //// Act + // Act var output = generator.GenerateFile("Teacher"); - //// Assert + // Assert Assert.Contains(@"class Teacher : Person", output); AssertCompile(output); } [Fact] - public async Task When_enum_has_description_then_csharp_has_xml_comment() + public void When_enum_has_description_then_csharp_has_xml_comment() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.AllOf.First().ActualSchema.Properties["Gender"].Description = "EnumDesc."; var generator = new CSharpGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert var summary = @" /// /// EnumDesc. @@ -302,17 +302,17 @@ public async Task When_enum_has_description_then_csharp_has_xml_comment() } [Fact] - public async Task When_class_has_description_then_csharp_has_xml_comment() + public void When_class_has_description_then_csharp_has_xml_comment() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.ActualSchema.Description = "ClassDesc."; var generator = new CSharpGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert var summary = @" /// /// ClassDesc. @@ -323,17 +323,17 @@ public async Task When_class_has_description_then_csharp_has_xml_comment() } [Fact] - public async Task When_property_has_description_then_csharp_has_xml_comment() + public void When_property_has_description_then_csharp_has_xml_comment() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.ActualProperties["Class"].Description = "PropertyDesc."; var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert var summary = @" /// /// PropertyDesc. @@ -349,7 +349,7 @@ public class File } [Fact] - public async Task Can_generate_type_from_string_property_with_byte_format() + public void Can_generate_type_from_string_property_with_byte_format() { // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); @@ -365,7 +365,7 @@ public async Task Can_generate_type_from_string_property_with_byte_format() } [Fact] - public async Task Can_generate_type_from_string_property_with_base64_format() + public void Can_generate_type_from_string_property_with_base64_format() { // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); @@ -384,7 +384,7 @@ public async Task Can_generate_type_from_string_property_with_base64_format() [Fact] public void When_name_contains_dash_then_it_is_converted_to_upper_case() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Properties["foo-bar"] = new JsonSchemaProperty { @@ -436,20 +436,20 @@ public void When_name_contains_unallowed_characters_then_they_are_converted_to_v [Fact] public void When_type_name_is_missing_then_anonymous_name_is_generated() { - //// Arrange + // Arrange var schema = new JsonSchema(); var generator = new CSharpGenerator(schema); // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain(@"class :", output); AssertCompile(output); } - private static async Task CreateGeneratorAsync() + private static Task CreateGeneratorAsync() { var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); @@ -458,7 +458,7 @@ private static async Task CreateGeneratorAsync() Namespace = "MyNamespace" }; var generator = new CSharpGenerator(schema, settings); - return generator; + return Task.FromResult(generator); } @@ -468,14 +468,14 @@ private class ObjectTestClass } [Fact] - public async Task When_property_is_object_then_any_type_is_generated() + public void When_property_is_object_then_any_type_is_generated() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal( @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"" @@ -483,17 +483,17 @@ public async Task When_property_is_object_then_any_type_is_generated() } [Fact] - public async Task When_property_is_object_then_object_property_is_generated() + public void When_property_is_object_then_object_property_is_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public object Foo { get; set; }", code); AssertCompile(code); @@ -515,13 +515,13 @@ public class ClassWithDefaultEnumProperty } [Fact] - public async Task When_enum_property_has_default_and_int_serialization_then_correct_csharp_code_generated() + public void When_enum_property_has_default_and_int_serialization_then_correct_csharp_code_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaJson = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -529,16 +529,16 @@ public async Task When_enum_property_has_default_and_int_serialization_then_corr }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public ConstructionCode ConstructionCode { get; set; } = Foo.ConstructionCode.NON_CBST;", code); AssertCompile(code); } [Fact] - public async Task When_enum_property_has_default_and_string_serialization_then_correct_csharp_code_generated() + public void When_enum_property_has_default_and_string_serialization_then_correct_csharp_code_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -549,7 +549,7 @@ public async Task When_enum_property_has_default_and_string_serialization_then_c var schemaJson = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -557,7 +557,7 @@ public async Task When_enum_property_has_default_and_string_serialization_then_c }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public ConstructionCode ConstructionCode { get; set; } = Foo.ConstructionCode.NON_CBST;", code); AssertCompile(code); @@ -566,7 +566,7 @@ public async Task When_enum_property_has_default_and_string_serialization_then_c [Fact] public async Task When_enum_type_name_is_missing_then_default_value_is_still_correctly_set() { - //// Arrange + // Arrange var schemaJson = @"{ ""type"": ""object"", ""additionalProperties"": false, @@ -591,7 +591,7 @@ public async Task When_enum_type_name_is_missing_then_default_value_is_still_cor }"; var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -599,7 +599,7 @@ public async Task When_enum_type_name_is_missing_then_default_value_is_still_cor }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public MyClassConstructionCode ConstructionCode { get; set; } = Foo.MyClassConstructionCode.JOIST_MAS;", code); AssertCompile(code); @@ -608,7 +608,7 @@ public async Task When_enum_type_name_is_missing_then_default_value_is_still_cor [Fact] public async Task When_property_has_same_name_as_class_then_it_is_renamed() { - //// Arrange + // Arrange var schemaJson = @"{ ""type"": ""object"", ""properties"": { @@ -619,11 +619,11 @@ public async Task When_property_has_same_name_as_class_then_it_is_renamed() }"; var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("Foo"); - //// Assert + // Assert Assert.Contains("[Newtonsoft.Json.JsonProperty(\"Foo\", Required = Newtonsoft.Json.Required.DisallowNull", code); Assert.Contains("public string Foo1 { get; set; }", code); @@ -633,7 +633,7 @@ public async Task When_property_has_same_name_as_class_then_it_is_renamed() [Fact] public async Task When_patternProperties_is_set_with_string_value_type_then_correct_dictionary_is_generated() { - //// Arrange + // Arrange var schemaJson = @"{ ""required"": [ ""dict"" ], ""properties"": { @@ -651,11 +651,11 @@ public async Task When_patternProperties_is_set_with_string_value_type_then_corr var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Collections.Generic.IDictionary Dict { get; set; } = new System.Collections.Generic.Dictionary();", code); AssertCompile(code); @@ -664,7 +664,7 @@ public async Task When_patternProperties_is_set_with_string_value_type_then_corr [Fact] public void When_object_has_generic_name_then_it_is_transformed() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.Object, @@ -677,11 +677,11 @@ public void When_object_has_generic_name_then_it_is_transformed() } }; - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("Foo[Bar[Inner]]"); - //// Assert + // Assert Assert.Contains("public partial class FooOfBarOfInner", code); AssertCompile(code); @@ -703,17 +703,17 @@ public partial class Person2 } [Fact] - public async Task When_property_is_required_then_CSharp_code_is_correct() + public void When_property_is_required_then_CSharp_code_is_correct() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaJson = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@" ""required"": [ ""FirstName"", ""Age"" @@ -755,7 +755,7 @@ public async Task When_property_is_required_then_CSharp_code_is_correct() [Fact] public void When_array_property_is_required_or_not_then_the_code_has_correct_initializer() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -783,7 +783,7 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini } }; - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -791,7 +791,7 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Collections.Generic.ICollection A { get; set; } = new System.Collections.ObjectModel.Collection();", code); Assert.DoesNotContain("public System.Collections.Generic.ICollection B { get; set; } = new System.Collections.ObjectModel.Collection();", code); @@ -801,7 +801,7 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini [Fact] public void When_dictionary_property_is_required_or_not_then_the_code_has_correct_initializer() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -829,7 +829,7 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc } }; - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -837,7 +837,7 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Collections.Generic.IDictionary A { get; set; } = new System.Collections.Generic.Dictionary();", code); Assert.DoesNotContain("public System.Collections.Generic.IDictionary B { get; set; } = new System.Collections.Generic.Dictionary();", code); @@ -847,7 +847,7 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc [Fact] public void When_object_property_is_required_or_not_then_the_code_has_correct_initializer() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -883,7 +883,7 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in } }; - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -891,7 +891,7 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public A A { get; set; } = new A();", code); Assert.DoesNotContain("public B B { get; set; } = new B();", code); @@ -901,7 +901,7 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in [Fact] public async Task When_definition_is_named_Object_then_JObject_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -919,7 +919,7 @@ public async Task When_definition_is_named_Object_then_JObject_is_generated() }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -928,7 +928,7 @@ public async Task When_definition_is_named_Object_then_JObject_is_generated() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public object Foo { get; set; }", code); AssertCompile(code); @@ -940,18 +940,18 @@ public class ObsClass } [Fact] - public async Task When_property_is_ObservableCollection_then_generated_code_uses_the_same_class() + public void When_property_is_ObservableCollection_then_generated_code_uses_the_same_class() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var settings = new CSharpGeneratorSettings { ArrayType = "ObservableCollection" }; var generator = new CSharpGenerator(schema, settings); - //// Act + // Act var output = generator.GenerateFile("MyClass"); //Console.WriteLine(output); - //// Assert + // Assert Assert.Contains("ObservableCollection", output); AssertCompile(output); @@ -960,17 +960,17 @@ public async Task When_property_is_ObservableCollection_then_generated_code_uses [Fact] public async Task When_enum_has_special_chars_then_they_should_be_converted() { - //// Arrange + // Arrange var schemaJson = @"{ ""type"": ""string"", ""enum"": [""application/json"",""application/vnd.ms-excel""] }"; var schema = await JsonSchema.FromJsonAsync(schemaJson); var settings = new CSharpGeneratorSettings(); var generator = new CSharpGenerator(schema, settings); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("Application_vnd_msExcel = 1,", output); AssertCompile(output); @@ -979,17 +979,17 @@ public async Task When_enum_has_special_chars_then_they_should_be_converted() [Fact] public async Task When_enum_has_special_char_questionmark_then_it_should_be_converted() { - //// Arrange + // Arrange var schemaJson = @"{ ""type"": ""string"", ""enum"": [""application/json"",""application/vnd.ms-excel?2""] }"; var schema = await JsonSchema.FromJsonAsync(schemaJson); var settings = new CSharpGeneratorSettings(); var generator = new CSharpGenerator(schema, settings); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("Application_vnd_msExcel_2 = 1,", output); AssertCompile(output); @@ -998,7 +998,7 @@ public async Task When_enum_has_special_char_questionmark_then_it_should_be_conv [Fact] public async Task When_property_has_not_supported_characters_then_they_are_removed() { - //// Arrange + // Arrange var schemaJson = @"{ ""type"": ""object"", @@ -1011,10 +1011,10 @@ public async Task When_property_has_not_supported_characters_then_they_are_remov var settings = new CSharpGeneratorSettings(); var generator = new CSharpGenerator(schema, settings); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public string OdataContext", output); AssertCompile(output); @@ -1023,7 +1023,7 @@ public async Task When_property_has_not_supported_characters_then_they_are_remov [Fact] public async Task When_definition_contains_minimum_a_range_attribute_is_added_with_minimum_and_max_double_maximum() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1036,7 +1036,7 @@ public async Task When_definition_contains_minimum_a_range_attribute_is_added_wi }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1044,7 +1044,7 @@ public async Task When_definition_contains_minimum_a_range_attribute_is_added_wi }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]", code); AssertCompile(code); @@ -1053,7 +1053,7 @@ public async Task When_definition_contains_minimum_a_range_attribute_is_added_wi [Fact] public async Task When_definition_contains_maximum_a_range_attribute_is_added_with_min_double_minimum_and_maximum() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1066,7 +1066,7 @@ public async Task When_definition_contains_maximum_a_range_attribute_is_added_wi }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1074,7 +1074,7 @@ public async Task When_definition_contains_maximum_a_range_attribute_is_added_wi }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Range(int.MinValue, 10)]", code); AssertCompile(code); @@ -1083,7 +1083,7 @@ public async Task When_definition_contains_maximum_a_range_attribute_is_added_wi [Fact] public async Task When_definition_contains_both_minimum_and_maximum_a_range_attribute_is_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1097,7 +1097,7 @@ public async Task When_definition_contains_both_minimum_and_maximum_a_range_attr }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1105,7 +1105,7 @@ public async Task When_definition_contains_both_minimum_and_maximum_a_range_attr }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Range(1, 10)]", code); AssertCompile(code); @@ -1114,7 +1114,7 @@ public async Task When_definition_contains_both_minimum_and_maximum_a_range_attr [Fact] public async Task When_definition_contains_maximum_a_range_attribute_is_not_added_for_anything_but_type_number_or_integer() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1127,7 +1127,7 @@ public async Task When_definition_contains_maximum_a_range_attribute_is_not_adde }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1135,7 +1135,7 @@ public async Task When_definition_contains_maximum_a_range_attribute_is_not_adde }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("System.ComponentModel.DataAnnotations.Range", code); AssertCompile(code); @@ -1144,7 +1144,7 @@ public async Task When_definition_contains_maximum_a_range_attribute_is_not_adde [Fact] public async Task When_definition_contains_min_length_a_string_length_attribute_is_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1157,7 +1157,7 @@ public async Task When_definition_contains_min_length_a_string_length_attribute_ }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1165,7 +1165,7 @@ public async Task When_definition_contains_min_length_a_string_length_attribute_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.StringLength(int.MaxValue, MinimumLength = 10)]", code); AssertCompile(code); @@ -1174,7 +1174,7 @@ public async Task When_definition_contains_min_length_a_string_length_attribute_ [Fact] public async Task When_definition_contains_max_length_a_string_length_attribute_is_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1187,7 +1187,7 @@ public async Task When_definition_contains_max_length_a_string_length_attribute_ }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1195,7 +1195,7 @@ public async Task When_definition_contains_max_length_a_string_length_attribute_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.StringLength(20)]", code); AssertCompile(code); @@ -1204,7 +1204,7 @@ public async Task When_definition_contains_max_length_a_string_length_attribute_ [Fact] public async Task When_definition_contains_both_min_and_max_length_a_string_length_attribute_is_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1218,7 +1218,7 @@ public async Task When_definition_contains_both_min_and_max_length_a_string_leng }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1226,7 +1226,7 @@ public async Task When_definition_contains_both_min_and_max_length_a_string_leng }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.StringLength(20, MinimumLength = 10)]", code); AssertCompile(code); @@ -1235,7 +1235,7 @@ public async Task When_definition_contains_both_min_and_max_length_a_string_leng [Fact] public async Task When_definition_contains_both_min_length_a_string_length_attribute_is_added_only_for_type_string() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1249,7 +1249,7 @@ public async Task When_definition_contains_both_min_length_a_string_length_attri }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1257,7 +1257,7 @@ public async Task When_definition_contains_both_min_length_a_string_length_attri }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("System.ComponentModel.DataAnnotations.StringLength", code); AssertCompile(code); @@ -1266,7 +1266,7 @@ public async Task When_definition_contains_both_min_length_a_string_length_attri [Fact] public async Task When_definition_contains_both_min_items_and_max_items_a_min_length_and_max_length_attributes_are_added_only_for_type_array() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1280,7 +1280,7 @@ public async Task When_definition_contains_both_min_items_and_max_items_a_min_le }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1288,7 +1288,7 @@ public async Task When_definition_contains_both_min_items_and_max_items_a_min_le }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("System.ComponentModel.DataAnnotations.MinLength(10)", code); Assert.Contains("System.ComponentModel.DataAnnotations.MaxLength(20)", code); @@ -1298,7 +1298,7 @@ public async Task When_definition_contains_both_min_items_and_max_items_a_min_le [Fact] public async Task When_definition_contains_pattern_a_regular_expression_attribute_is_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1311,7 +1311,7 @@ public async Task When_definition_contains_pattern_a_regular_expression_attribut }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1319,7 +1319,7 @@ public async Task When_definition_contains_pattern_a_regular_expression_attribut }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"[System.ComponentModel.DataAnnotations.RegularExpression(@""^[a-zA-Z''-'\s]{1,40}$"")]", code); AssertCompile(code); @@ -1328,7 +1328,7 @@ public async Task When_definition_contains_pattern_a_regular_expression_attribut [Fact] public async Task When_definition_contains_pattern_but_type_is_not_string_a_regular_expression_should_not_be_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1341,7 +1341,7 @@ public async Task When_definition_contains_pattern_but_type_is_not_string_a_regu }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1349,7 +1349,7 @@ public async Task When_definition_contains_pattern_but_type_is_not_string_a_regu }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain(@"System.ComponentModel.DataAnnotations.RegularExpression", code); AssertCompile(code); @@ -1358,7 +1358,7 @@ public async Task When_definition_contains_pattern_but_type_is_not_string_a_regu [Fact] public async Task When_definition_contains_restrictions_but_render_data_annotations_is_set_to_false_they_should_not_be_included() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1381,7 +1381,7 @@ public async Task When_definition_contains_restrictions_but_render_data_annotati }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1391,7 +1391,7 @@ public async Task When_definition_contains_restrictions_but_render_data_annotati }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain(@"System.ComponentModel.DataAnnotations", code); AssertCompile(code); @@ -1403,17 +1403,17 @@ public class MyByteTest } [Fact] - public async Task When_property_is_byte_then_its_type_is_preserved() + public void When_property_is_byte_then_its_type_is_preserved() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("public byte? Cell { get; set; }", code); AssertCompile(code); @@ -1426,17 +1426,17 @@ public class MyRequiredNullableTest } [Fact] - public async Task When_nullable_property_is_required_then_it_is_not_nullable_in_generated_csharp_code() + public void When_nullable_property_is_required_then_it_is_not_nullable_in_generated_csharp_code() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("public int Foo { get; set; }", code); AssertCompile(code); @@ -1445,7 +1445,7 @@ public async Task When_nullable_property_is_required_then_it_is_not_nullable_in_ [Fact] public async Task When_definition_contains_date_converter_should_be_added_for_datetime() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1458,7 +1458,7 @@ public async Task When_definition_contains_date_converter_should_be_added_for_da }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1467,7 +1467,7 @@ public async Task When_definition_contains_date_converter_should_be_added_for_da }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"class DateFormatConverter", code); Assert.Contains(@"[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]", code); @@ -1477,7 +1477,7 @@ public async Task When_definition_contains_date_converter_should_be_added_for_da [Fact] public async Task When_definition_contains_date_and_use_system_text_json_then_converter_should_be_added_for_datetime() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1490,7 +1490,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1500,7 +1500,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"class DateFormatConverter : System.Text.Json.Serialization.JsonConverter", code); Assert.Contains(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code); @@ -1510,7 +1510,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co [Fact] public async Task When_no_typeNameHint_is_available_then_title_is_used_as_class_name() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", ""title"": ""MyTestClass"", @@ -1532,7 +1532,7 @@ public async Task When_no_typeNameHint_is_available_then_title_is_used_as_class_ }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1541,7 +1541,7 @@ public async Task When_no_typeNameHint_is_available_then_title_is_used_as_class_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("Anonymous", code); AssertCompile(code); @@ -1552,7 +1552,7 @@ public async Task When_no_typeNameHint_is_available_then_title_is_used_as_class_ [InlineData(true)] public async Task When_tuple_types_has_ints_then_it_is_generated_correctly(bool inlineNamedTuples) { - //// Arrange + // Arrange var json = @" { ""$schema"": ""http://json-schema.org/draft-04/schema#"", @@ -1593,7 +1593,7 @@ public async Task When_tuple_types_has_ints_then_it_is_generated_correctly(bool }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1603,7 +1603,7 @@ public async Task When_tuple_types_has_ints_then_it_is_generated_correctly(bool }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("System.Linq.Enumerable+SelectIListIterator", code); if (inlineNamedTuples) @@ -1622,7 +1622,7 @@ public async Task When_tuple_types_has_ints_then_it_is_generated_correctly(bool [Fact] public async Task When_definition_contains_date_converter_should_be_added_for_datetimeoffset() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1635,7 +1635,7 @@ public async Task When_definition_contains_date_converter_should_be_added_for_da }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1644,7 +1644,7 @@ public async Task When_definition_contains_date_converter_should_be_added_for_da }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"class DateFormatConverter", code); Assert.Contains(@"[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]", code); @@ -1654,7 +1654,7 @@ public async Task When_definition_contains_date_converter_should_be_added_for_da [Fact] public async Task When_definition_contains_date_and_use_system_text_json_then_converter_should_be_added_for_datetimeoffset() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1667,7 +1667,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1677,7 +1677,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"class DateFormatConverter : System.Text.Json.Serialization.JsonConverter", code); Assert.Contains(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code); @@ -1687,7 +1687,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co [Fact] public async Task When_definition_contains_datetime_converter_should_not_be_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1700,7 +1700,7 @@ public async Task When_definition_contains_datetime_converter_should_not_be_adde }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1709,7 +1709,7 @@ public async Task When_definition_contains_datetime_converter_should_not_be_adde }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain(@"class DateFormatConverter", code); Assert.DoesNotContain(@"[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]", code); @@ -1719,7 +1719,7 @@ public async Task When_definition_contains_datetime_converter_should_not_be_adde [Fact] public async Task When_definition_contains_datetime_and_use_system_text_json_then_converter_should_not_be_added() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -1732,7 +1732,7 @@ public async Task When_definition_contains_datetime_and_use_system_text_json_the }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -1742,7 +1742,7 @@ public async Task When_definition_contains_datetime_and_use_system_text_json_the }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain(@"class DateFormatConverter", code); Assert.DoesNotContain(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code); @@ -1750,9 +1750,9 @@ public async Task When_definition_contains_datetime_and_use_system_text_json_the } [Fact] - public async Task When_record_no_setter_in_class_and_constructor_provided() + public void When_record_no_setter_in_class_and_constructor_provided() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType
(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings @@ -1760,10 +1760,10 @@ public async Task When_record_no_setter_in_class_and_constructor_provided() ClassStyle = CSharpClassStyle.Record }); - //// Act + // Act var output = generator.GenerateFile("Address"); - //// Assert + // Assert Assert.Contains(@"public string Street { get; }", output); Assert.DoesNotContain(@"public string Street { get; set; }", output); @@ -1773,9 +1773,9 @@ public async Task When_record_no_setter_in_class_and_constructor_provided() } [Fact] - public async Task When_native_record_no_setter_in_class_and_constructor_provided() + public void When_native_record_no_setter_in_class_and_constructor_provided() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType
(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings @@ -1784,10 +1784,10 @@ public async Task When_native_record_no_setter_in_class_and_constructor_provided GenerateNativeRecords = true }); - //// Act + // Act var output = generator.GenerateFile("Address"); - //// Assert + // Assert Assert.Contains(@"record Address", output); Assert.Contains(@"public string Street { get; init; }", output); Assert.DoesNotContain(@"public string Street { get; set; }", output); @@ -1819,9 +1819,9 @@ public class PersonAddress : PostAddress } [Fact] - public async Task When_class_is_abstract_constructor_is_protected_for_record() + public void When_class_is_abstract_constructor_is_protected_for_record() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings @@ -1829,10 +1829,10 @@ public async Task When_class_is_abstract_constructor_is_protected_for_record() ClassStyle = CSharpClassStyle.Record }); - //// Act + // Act var output = generator.GenerateFile("AbstractAddress"); - //// Assert + // Assert Assert.Contains(@"public string StreetName { get; }", output); Assert.DoesNotContain(@"public string StreetName { get; set; }", output); @@ -1846,9 +1846,9 @@ public async Task When_class_is_abstract_constructor_is_protected_for_record() } [Fact] - public async Task When_record_has_inheritance() + public void When_record_has_inheritance() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings @@ -1856,7 +1856,7 @@ public async Task When_record_has_inheritance() ClassStyle = CSharpClassStyle.Record }); - //// Act + // Act var output = generator.GenerateFile("PersonAddress"); Assert.Contains("protected AbstractAddress(string @city, string @streetName)", output); @@ -1879,9 +1879,9 @@ public class ClassWithExtensionData } [Fact] - public async Task When_schema_has_AdditionProperties_schema_then_JsonExtensionDataAttribute_is_generated() + public void When_schema_has_AdditionProperties_schema_then_JsonExtensionDataAttribute_is_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); @@ -1890,21 +1890,21 @@ public async Task When_schema_has_AdditionProperties_schema_then_JsonExtensionDa ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var output = generator.GenerateFile("PersonAddress"); - //// Assert + // Assert Assert.Contains("JsonExtensionData", output); } [Fact] public void When_schema_has_negative_value_of_enum_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange var settings = new CSharpGeneratorSettings { EnumNameGenerator = new DefaultEnumNameGenerator() }; var generator = new CSharpGenerator(null, settings); - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Integer, @@ -1920,7 +1920,7 @@ public void When_schema_has_negative_value_of_enum_it_is_generated_in_CSharp_and var types = generator.GenerateTypes(schema, "MyEnum"); - //// Assert + // Assert Assert.Contains("_1 = 1", types.First().Code); Assert.Contains("__1 = -1", types.First().Code); } @@ -1944,7 +1944,7 @@ private static void AssertCompile(string code) [Fact] public async Task When_using_SystemTextJson_without_JsonConverters_generates_FromJson_and_ToJson_correctly() { - //// Arrange + // Arrange var expectedToJsonMethod = @" public string ToJson() @@ -1967,14 +1967,14 @@ public static Person FromJson(string data) generator.Settings.JsonLibrary = CSharpJsonLibrary.SystemTextJson; generator.Settings.GenerateJsonMethods = true; - //// Act + // Act var output = generator.GenerateFile("MyClass"); //Remove the spaces from the string to avoid indentation change errors var normalizedOutput = Regex.Replace(output, @"\s+", string.Empty); var normalizedExpectedToJsonMethod = Regex.Replace(expectedToJsonMethod, @"\s+", string.Empty); var normalizedExpectedFromJsonMethodMethod = Regex.Replace(expectedFromJsonMethod, @"\s+", string.Empty); - //// Assert + // Assert Assert.Contains(normalizedExpectedToJsonMethod, normalizedOutput); Assert.Contains(normalizedExpectedFromJsonMethodMethod, normalizedOutput); @@ -1984,7 +1984,7 @@ public static Person FromJson(string data) [Fact] public async Task When_using_SystemTextJson_with_JsonConverters_generates_FromJson_and_ToJson_correctly() { - //// Arrange + // Arrange var expectedToJsonMethod = @" public string ToJson() @@ -2014,14 +2014,14 @@ public static Person FromJson(string data) generator.Settings.GenerateJsonMethods = true; generator.Settings.JsonConverters = ["CustomConverter1", "CustomConverter2"]; - //// Act + // Act var output = generator.GenerateFile("MyClass"); //Remove the spaces from the string to avoid indentation change errors var normalizedOutput = Regex.Replace(output, @"\s+", string.Empty); var normalizedExpectedToJsonMethod = Regex.Replace(expectedToJsonMethod, @"\s+", string.Empty); var normalizedExpectedFromJsonMethodMethod = Regex.Replace(expectedFromJsonMethod, @"\s+", string.Empty); - //// Assert + // Assert Assert.Contains(normalizedExpectedToJsonMethod, normalizedOutput); Assert.Contains(normalizedExpectedFromJsonMethodMethod, normalizedOutput); @@ -2031,7 +2031,7 @@ public static Person FromJson(string data) [Fact] public async Task When_using_NewtonsoftJson_without_JsonConverters_generates_FromJson_and_ToJson_correctly() { - //// Arrange + // Arrange var expectedToJsonMethod = @" public string ToJson() @@ -2052,14 +2052,14 @@ public static Person FromJson(string data) generator.Settings.JsonLibrary = CSharpJsonLibrary.NewtonsoftJson; generator.Settings.GenerateJsonMethods = true; - //// Act + // Act var output = generator.GenerateFile("MyClass"); //Remove the spaces from the string to avoid indentation change errors var normalizedOutput = Regex.Replace(output, @"\s+", string.Empty); var normalizedExpectedToJsonMethod = Regex.Replace(expectedToJsonMethod, @"\s+", string.Empty); var normalizedExpectedFromJsonMethodMethod = Regex.Replace(expectedFromJsonMethod, @"\s+", string.Empty); - //// Assert + // Assert Assert.Contains(normalizedExpectedToJsonMethod, normalizedOutput); Assert.Contains(normalizedExpectedFromJsonMethodMethod, normalizedOutput); @@ -2069,7 +2069,7 @@ public static Person FromJson(string data) [Fact] public async Task When_using_NewtonsoftJson_with_JsonConverters_generates_FromJson_and_ToJson_correctly() { - //// Arrange + // Arrange var expectedToJsonMethod = @" public string ToJson() @@ -2091,14 +2091,14 @@ public static Person FromJson(string data) generator.Settings.GenerateJsonMethods = true; generator.Settings.JsonConverters = ["CustomConverter1", "CustomConverter2"]; - //// Act + // Act var output = generator.GenerateFile("MyClass"); //Remove the spaces from the string to avoid indentation change errors var normalizedOutput = Regex.Replace(output, @"\s+", string.Empty); var normalizedExpectedToJsonMethod = Regex.Replace(expectedToJsonMethod, @"\s+", string.Empty); var normalizedExpectedFromJsonMethodMethod = Regex.Replace(expectedFromJsonMethod, @"\s+", string.Empty); - //// Assert + // Assert Assert.Contains(normalizedExpectedToJsonMethod, normalizedOutput); Assert.Contains(normalizedExpectedFromJsonMethodMethod, normalizedOutput); @@ -2119,7 +2119,7 @@ public class DocumentationTest } [Fact] - public async Task When_documentation_present_produces_valid_xml_documentation_syntax() + public void When_documentation_present_produces_valid_xml_documentation_syntax() { // Arrange var schema = JsonSchema.FromType(); diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs index 1c00e8bf7..bbb5879f8 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs @@ -40,18 +40,18 @@ public class Banana : Fruit, IBanana } [Fact] - public async Task When_schema_has_base_schema_then_it_is_referenced_with_Newtonsoft() + public void When_schema_has_base_schema_then_it_is_referenced_with_Newtonsoft() { - //// Arrange + // Arrange var json = NewtonsoftJsonSchemaGenerator.FromType(); var data = json.ToJson(); var generator = new CSharpGenerator(json, new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.NewtonsoftJson }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.True(json.Properties["Item"].ActualTypeSchema.AllOf.First().HasReference); Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), \"discriminator\")]", code); Assert.Contains("[JsonInheritanceAttribute(\"Banana\", typeof(Banana))]", code); @@ -59,18 +59,18 @@ public async Task When_schema_has_base_schema_then_it_is_referenced_with_Newtons } [Fact] - public async Task When_schema_has_base_schema_then_it_is_referenced_with_STJ() + public void When_schema_has_base_schema_then_it_is_referenced_with_STJ() { - //// Arrange + // Arrange var json = JsonSchema.FromType(); var data = json.ToJson(); var generator = new CSharpGenerator(json, new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.SystemTextJson }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.True(json.Properties["Item"].ActualTypeSchema.AllOf.First().HasReference); Assert.Contains("[JsonInheritanceConverter(typeof(Fruit), \"discriminator\")]", code); Assert.Contains("[JsonInheritanceAttribute(\"Banana\", typeof(Banana))]", code); @@ -78,9 +78,9 @@ public async Task When_schema_has_base_schema_then_it_is_referenced_with_STJ() } [Fact] - public async Task When_using_STJ_polymorphic_serialization_then_NSwag_inheritance_converter_and_attributes_are_not_generated() + public void When_using_STJ_polymorphic_serialization_then_NSwag_inheritance_converter_and_attributes_are_not_generated() { - //// Arrange + // Arrange var json = JsonSchema.FromType(); var data = json.ToJson(); @@ -90,10 +90,10 @@ public async Task When_using_STJ_polymorphic_serialization_then_NSwag_inheritanc JsonPolymorphicSerializationStyle = CSharpJsonPolymorphicSerializationStyle.SystemTextJson }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.DoesNotContain("[JsonInheritanceConverter", code); Assert.DoesNotContain("[JsonInheritanceAttribute", code); Assert.DoesNotContain("public class JsonInheritanceConverter", code); diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs index 502673225..2c1249c09 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs @@ -23,18 +23,18 @@ public sealed class EmptyClassInheritingDictionary : Dictionary } [Fact] - public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works() + public void When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert var dictionarySchema = schema.Definitions["EmptyClassInheritingDictionary"]; Assert.Empty(dictionarySchema.AllOf); @@ -68,18 +68,18 @@ public class ExceptionContainer } [Fact] - public async Task When_class_with_discriminator_has_base_class_then_csharp_is_generated_correctly() + public void When_class_with_discriminator_has_base_class_then_csharp_is_generated_correctly() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("Foobar.", data); Assert.Contains("Foobar.", code); @@ -90,7 +90,7 @@ public async Task When_class_with_discriminator_has_base_class_then_csharp_is_ge [Fact] public async Task When_property_references_any_schema_with_inheritance_then_property_type_is_correct() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", ""properties"": { @@ -124,27 +124,27 @@ public async Task When_property_references_any_schema_with_inheritance_then_prop var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("public Dog Dog { get; set; }", code); } [Fact] public async Task When_definitions_inherit_from_root_schema() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/Animal.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Record }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("public abstract partial class Animal", code); Assert.Contains("public partial class Cat : Animal", code); Assert.Contains("public partial class PersianCat : Cat", code); @@ -155,10 +155,10 @@ public async Task When_definitions_inherit_from_root_schema() [Fact] public async Task When_definitions_inherit_from_root_schema_and_STJ_polymorphism() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/Animal.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { @@ -167,10 +167,10 @@ public async Task When_definitions_inherit_from_root_schema_and_STJ_polymorphism JsonPolymorphicSerializationStyle = CSharpJsonPolymorphicSerializationStyle.SystemTextJson }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("public abstract partial class Animal", code); Assert.Contains("public partial class Cat : Animal", code); Assert.Contains("public partial class PersianCat : Cat", code); @@ -181,7 +181,9 @@ public async Task When_definitions_inherit_from_root_schema_and_STJ_polymorphism private string GetTestDirectory() { +#pragma warning disable SYSLIB0012 var codeBase = Assembly.GetExecutingAssembly().CodeBase; +#pragma warning restore SYSLIB0012 var uri = new UriBuilder(codeBase); return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InterfaceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InterfaceTests.cs index 179bb36e7..3ff86bac5 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InterfaceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InterfaceTests.cs @@ -18,12 +18,12 @@ public class Person : IPerson } [Fact] - public async Task When_interface_has_properties_then_properties_are_included_in_schema() + public void When_interface_has_properties_then_properties_are_included_in_schema() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -31,19 +31,19 @@ public async Task When_interface_has_properties_then_properties_are_included_in_ }); var code = generator.GenerateFile("Person"); - //// Assert + // Assert Assert.Equal(2, schema.Properties.Count); Assert.Contains("public string LastName { get; set; }\n", code); Assert.Contains("public string FirstName { get; set; }\n", code); } [Fact] - public async Task When_class_implements_interface_then_properties_are_included_in_schema() + public void When_class_implements_interface_then_properties_are_included_in_schema() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -51,7 +51,7 @@ public async Task When_class_implements_interface_then_properties_are_included_i }); var code = generator.GenerateFile("Person"); - //// Assert + // Assert Assert.Equal(2, schema.Properties.Count); Assert.Contains("public string LastName { get; set; }\n", code); Assert.Contains("public string FirstName { get; set; }\n", code); diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj index 094f9e152..7a70ea285 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj @@ -3,9 +3,9 @@ net8.0 false - $(NoWarn),1587,1998,1591,618,SYSLIB0012 disable - false + latest-Minimal + $(NoWarn);1591 diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableEnumTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableEnumTests.cs index 13e81f975..ec13323bd 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableEnumTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableEnumTests.cs @@ -33,10 +33,10 @@ public async Task When_Swagger2_enum_property_is_not_required_then_it_is_nullabl ClassStyle = CSharpClassStyle.Poco }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"public Sex? Sex", code); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs index c052ecd54..12df58b7d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs @@ -17,16 +17,16 @@ private class ClassWithRequiredObject } [Fact] - public async Task When_property_is_optional_and_GenerateNullableReferenceTypes_is_not_set_then_CSharp_property_is_not_nullable() + public void When_property_is_optional_and_GenerateNullableReferenceTypes_is_not_set_then_CSharp_property_is_not_nullable() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -35,22 +35,22 @@ public async Task When_property_is_optional_and_GenerateNullableReferenceTypes_i }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public object Property { get; set; }", code); Assert.Contains("public object Property2 { get; set; }", code); } [Fact] - public async Task When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable() + public void When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -59,7 +59,7 @@ public async Task When_property_is_optional_and_GenerateNullableOptionalProperti }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public object? Property { get; set; } = default!;", code); Assert.Contains("public object Property2 { get; set; } = default!;", code); } @@ -67,7 +67,7 @@ public async Task When_property_is_optional_and_GenerateNullableOptionalProperti [Fact] public async Task When_generating_from_json_schema_property_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property() { - //// Arrange + // Arrange // CSharpGenerator `new object()` adds = new object() initializer to property only if it's explicitly marked // as having `type: object` in json schema @@ -92,7 +92,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene var schema = await JsonSchema.FromJsonAsync(schemaJson); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -101,7 +101,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public object Property { get; set; }", code); Assert.Contains("public object Property2 { get; set; } = new object();", code); } @@ -109,7 +109,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene [Fact] public async Task When_generating_from_json_schema_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property() { - //// Arrange + // Arrange // CSharpGenerator `new object()` adds = new object() initializer to property only if it's explicitly marked // as having `type: object` in json schema @@ -134,7 +134,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene var schema = await JsonSchema.FromJsonAsync(schemaJson); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -143,7 +143,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public object? Property { get; set; } = default!;", code); Assert.Contains("public object Property2 { get; set; } = new object();", code); } @@ -155,7 +155,7 @@ public async Task When_generating_from_json_schema_property_is_optional_and_Gene [InlineData("time-span")] public async Task When_generating_from_json_schema_string_property_with_date_or_time_format_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property(string format) { - //// Arrange + // Arrange var schemaJson = @" { ""type"": ""object"", @@ -177,7 +177,7 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -191,7 +191,7 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public string Required { get; set; }", code); Assert.Contains("public string Optional { get; set; }", code); } @@ -203,7 +203,7 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ [InlineData("time-span")] public async Task When_generating_from_json_schema_string_property_with_date_or_time_format_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property(string format) { - //// Arrange + // Arrange var schemaJson = @" { ""type"": ""object"", @@ -225,7 +225,7 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -239,7 +239,7 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public string Required { get; set; } = default!;", code); Assert.Contains("public string? Optional { get; set; } = default!;", code); } @@ -248,7 +248,7 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ public async Task When_generating_from_json_schema_string_property_with_reference_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property() { - //// Arrange + // Arrange var schemaJson = @" { ""type"": ""object"", @@ -268,7 +268,7 @@ public async Task var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -282,7 +282,7 @@ public async Task }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public string Required { get; set; } = default!;", code); Assert.Contains("public string? Optional { get; set; } = default!;", code); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableTests.cs index a38325265..db930865a 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableTests.cs @@ -16,16 +16,16 @@ private class ClassWithRequiredObject } [Fact] - public async Task When_property_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property_is_not_nullable() + public void When_property_is_optional_and_GenerateNullableOptionalProperties_is_not_set_then_CSharp_property_is_not_nullable() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -34,22 +34,22 @@ public async Task When_property_is_optional_and_GenerateNullableOptionalProperti }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public int Property { get; set; }", code); Assert.Contains("public int Property2 { get; set; }", code); } [Fact] - public async Task When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable() + public void When_property_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property_is_nullable() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -58,7 +58,7 @@ public async Task When_property_is_optional_and_GenerateNullableOptionalProperti }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public int? Property { get; set; }", code); Assert.Contains("public int Property2 { get; set; }", code); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NumberTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NumberTests.cs index 219d6ca94..2625844d9 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NumberTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NumberTests.cs @@ -8,7 +8,7 @@ public class NumberTests [Fact] public async Task When_number_has_no_format_then_default_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -21,17 +21,17 @@ public async Task When_number_has_no_format_then_default_is_generated() var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public double Amount", code); } [Fact] public async Task When_number_has_decimal_format_then_decimal_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -45,17 +45,17 @@ public async Task When_number_has_decimal_format_then_decimal_is_generated() var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public decimal Amount", code); } [Fact] public async Task When_number_has_double_format_then_double_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -69,17 +69,17 @@ public async Task When_number_has_double_format_then_double_is_generated() var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public double Amount", code); } [Fact] public async Task When_number_has_float_format_then_float_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -93,17 +93,17 @@ public async Task When_number_has_float_format_then_float_is_generated() var schema = await JsonSchema.FromJsonAsync(json); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public float Amount", code); } [Fact] public async Task When_number_type_setting_is_defined_then_setting_type_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -119,17 +119,17 @@ public async Task When_number_type_setting_is_defined_then_setting_type_is_gener NumberType = "customNumberType" }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public customNumberType Amount", code); } [Fact] public async Task When_number_type_setting_is_whitespace_then_double_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -145,17 +145,17 @@ public async Task When_number_type_setting_is_whitespace_then_double_is_generate NumberType = " \t\n" }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public double Amount", code); } [Fact] public async Task When_number_type_setting_is_null_then_double_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -172,17 +172,17 @@ public async Task When_number_type_setting_is_null_then_double_is_generated() NumberType = null! }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public double? Amount", code); } [Fact] public async Task When_number_float_type_setting_is_defined_then_setting_type_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -199,17 +199,17 @@ public async Task When_number_float_type_setting_is_defined_then_setting_type_is NumberFloatType = "customFloatType" }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public customFloatType Amount", code); } [Fact] public async Task When_number_double_type_setting_is_defined_then_setting_type_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -226,17 +226,17 @@ public async Task When_number_double_type_setting_is_defined_then_setting_type_i NumberDoubleType = "customDoubleType" }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public customDoubleType Amount", code); } [Fact] public async Task When_number_decimal_type_setting_is_defined_then_setting_type_is_generated() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", @@ -253,10 +253,10 @@ public async Task When_number_decimal_type_setting_is_defined_then_setting_type_ NumberDecimalType = "customDecimalType" }); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public customDecimalType Amount", code); } } \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObjectPropertyRequiredTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObjectPropertyRequiredTests.cs index 9e57188f5..f63317ac7 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObjectPropertyRequiredTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObjectPropertyRequiredTests.cs @@ -14,16 +14,16 @@ private class ClassWithRequiredObject } [Fact] - public async Task When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode() + public void When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -31,26 +31,26 @@ public async Task When_property_is_required_then_required_attribute_is_rendered_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Required]", code); Assert.Contains("public object Property { get; set; }", code); } [Fact] - public async Task When_property_is_required_then_required_attribute_is_rendered() + public void When_property_is_required_then_required_attribute_is_rendered() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Required]", code); Assert.Contains("public object Property { get; set; }", code); } @@ -61,16 +61,16 @@ private class ClassWithoutRequiredObject } [Fact] - public async Task When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode() + public void When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -78,27 +78,27 @@ public async Task When_property_is_not_required_then_required_attribute_is_not_r }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("[Required]", code); Assert.Contains("public object Property { get; set; }", code); } [Fact] - public async Task When_property_is_not_required_then_required_attribute_is_not_rendered() + public void When_property_is_not_required_then_required_attribute_is_not_rendered() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("[Required]", code); Assert.Contains("public object Property { get; set; }", code); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs index a68166a7c..f40b05559 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs @@ -31,65 +31,65 @@ public class ObsoleteWithMessageTestClass } [Fact] - public async Task When_property_is_obsolete_then_obsolete_attribute_is_rendered() + public void When_property_is_obsolete_then_obsolete_attribute_is_rendered() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("[System.Obsolete]", code); Assert.Contains("public string Property { get; set; }", code); } [Fact] - public async Task When_property_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered() + public void When_property_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("[System.Obsolete(\"Reason property is \\\"obsolete\\\"\")]", code); Assert.Contains("public string Property { get; set; }", code); } [Fact] - public async Task When_class_is_obsolete_then_obsolete_attribute_is_rendered() + public void When_class_is_obsolete_then_obsolete_attribute_is_rendered() { - //// Arrange + // Arrange #pragma warning disable 612 var schema = NewtonsoftJsonSchemaGenerator.FromType(); #pragma warning restore 612 var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("[System.Obsolete]", code); Assert.Contains("public partial class ObsoleteTestClass", code); } [Fact] - public async Task When_class_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered() + public void When_class_is_obsolete_with_a_message_then_obsolete_attribute_with_a_message_is_rendered() { - //// Arrange + // Arrange #pragma warning disable 618 var schema = NewtonsoftJsonSchemaGenerator.FromType(); #pragma warning restore 618 var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("[System.Obsolete(\"Reason class is \\\"obsolete\\\"\")]", code); Assert.Contains("public partial class ObsoleteWithMessageTestClass", code); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ReferencesTest.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ReferencesTest.cs index 131d76a3b..50ab85235 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ReferencesTest.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ReferencesTest.cs @@ -11,17 +11,17 @@ public class ReferencesTest [Fact] public async Task When_ref_is_definitions_no_types_are_duplicated() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/E.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public enum C", code); Assert.DoesNotContain("public enum C2", code); } @@ -29,17 +29,17 @@ public async Task When_ref_is_definitions_no_types_are_duplicated() [Fact] public async Task When_ref_is_file_no_types_are_duplicated() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/A.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public enum C", code); Assert.DoesNotContain("public enum C2", code); } @@ -47,10 +47,10 @@ public async Task When_ref_is_file_no_types_are_duplicated() [Fact] public async Task When_ref_is_file_and_it_contains_nullable_property_then_generated_property_is_also_nullable() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/F.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var generatorSettings = new CSharpGeneratorSettings { @@ -58,16 +58,18 @@ public async Task When_ref_is_file_and_it_contains_nullable_property_then_genera }; var generator = new CSharpGenerator(schema, generatorSettings); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public string? Name", code); } private string GetTestDirectory() { +#pragma warning disable SYSLIB0012 var codeBase = Assembly.GetExecutingAssembly().CodeBase; +#pragma warning restore SYSLIB0012 var uri = new UriBuilder(codeBase); return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)); } @@ -118,13 +120,13 @@ public async Task When_inheritance_with_object_without_props_is_generated_then_a }"; var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); - var schema = await JsonSchemaSerialization.FromJsonAsync(json, SchemaType.OpenApi3, null, factory, new DefaultContractResolver()); + var schema = await JsonSchemaSerialization.FromJsonAsync(json, SchemaType.OpenApi3, null, factory, new DefaultContractResolver(), CancellationToken.None); // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var code = generator.GenerateFile("MyClass"); - //// Act + // Act Assert.Contains("class BusinessException", code); Assert.Contains("class ValidationException", code); Assert.DoesNotContain("AdditionalProperties", code); @@ -163,13 +165,13 @@ public async Task When_date_reference_is_generated_from_swagger2_schema_then_gen }"; var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); - var schema = await JsonSchemaSerialization.FromJsonAsync(json, SchemaType.Swagger2, null, factory, new DefaultContractResolver()); + var schema = await JsonSchemaSerialization.FromJsonAsync(json, SchemaType.Swagger2, null, factory, new DefaultContractResolver(), CancellationToken.None); // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { SchemaType = SchemaType.Swagger2 }); var code = generator.GenerateFile("MyClass"); - //// Act + // Act Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]", code); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/StringPropertyRequiredTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/StringPropertyRequiredTests.cs index 3f94a0178..af82be28a 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/StringPropertyRequiredTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/StringPropertyRequiredTests.cs @@ -16,13 +16,13 @@ private class ClassWithRequiredObject } [Fact] - public async Task When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode() + public void When_property_is_required_then_required_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -30,7 +30,7 @@ public async Task When_property_is_required_then_required_attribute_is_rendered_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Equal(1, schema.Properties["Property"].MinLength); Assert.Null(schema.Properties["Property2"].MinLength); @@ -42,13 +42,13 @@ public async Task When_property_is_required_then_required_attribute_is_rendered_ } [Fact] - public async Task When_property_is_required_then_required_attribute_is_rendered() + public void When_property_is_required_then_required_attribute_is_rendered() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -56,7 +56,7 @@ public async Task When_property_is_required_then_required_attribute_is_rendered( }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Required]\n" + " public string Property { get; set; }\n", code); @@ -70,16 +70,16 @@ public class ClassWithoutRequiredObject } [Fact] - public async Task When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode() + public void When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -87,19 +87,19 @@ public async Task When_property_is_not_required_then_required_attribute_is_not_r }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("[Required]", code); Assert.Contains("public string Property { get; set; }", code); } [Fact] - public async Task When_property_is_not_required_then_required_attribute_is_not_rendered() + public void When_property_is_not_required_then_required_attribute_is_not_rendered() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -107,7 +107,7 @@ public async Task When_property_is_not_required_then_required_attribute_is_not_r }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("[Required]", code); Assert.Contains("public string Property { get; set; }", code); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/UriTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/UriTests.cs index 2645676b8..f49073505 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/UriTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/UriTests.cs @@ -12,31 +12,31 @@ public class ClassWithUri } [Fact] - public async Task When_property_is_uri_then_csharp_output_is_also_uri() + public void When_property_is_uri_then_csharp_output_is_also_uri() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); var generator = new CSharpGenerator(schema); - //// Act + // Act var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public System.Uri MyUri", code); } [Fact] public void When_uri_is_relative_then_it_is_serialized_and_deserialized_correctly() { - //// Arrange + // Arrange var obj = new ClassWithUri { MyUri = new Uri("abc/def", UriKind.Relative) }; - //// Act + // Act var json = JsonConvert.SerializeObject(obj); var obj2 = JsonConvert.DeserializeObject(json); - //// Assert + // Assert Assert.Equal("{\"MyUri\":\"abc/def\"}", json); Assert.Equal(obj.MyUri, obj2.MyUri); } @@ -44,14 +44,14 @@ public void When_uri_is_relative_then_it_is_serialized_and_deserialized_correctl [Fact] public void When_uri_is_absolute_then_it_is_serialized_and_deserialized_correctly() { - //// Arrange + // Arrange var obj = new ClassWithUri { MyUri = new Uri("https://abc/def", UriKind.Absolute) }; - //// Act + // Act var json = JsonConvert.SerializeObject(obj); var obj2 = JsonConvert.DeserializeObject(json); - //// Assert + // Assert Assert.Equal("{\"MyUri\":\"https://abc/def\"}", json); Assert.Equal(obj.MyUri, obj2.MyUri); } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValidationAttributesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValidationAttributesTests.cs index b6790cf34..38c289ee5 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValidationAttributesTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValidationAttributesTests.cs @@ -7,7 +7,7 @@ public class ValidationAttributesTests [Fact] public async Task When_string_property_has_maxlength_then_stringlength_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -25,7 +25,7 @@ public async Task When_string_property_has_maxlength_then_stringlength_attribute }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -33,7 +33,7 @@ public async Task When_string_property_has_maxlength_then_stringlength_attribute }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].MaxLength); Assert.Equal(50, schema.Properties["value"].ActualSchema.MaxLength); @@ -44,7 +44,7 @@ public async Task When_string_property_has_maxlength_then_stringlength_attribute [Fact] public async Task When_string_property_has_minlength_then_stringlength_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -62,7 +62,7 @@ public async Task When_string_property_has_minlength_then_stringlength_attribute }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -70,7 +70,7 @@ public async Task When_string_property_has_minlength_then_stringlength_attribute }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].MinLength); Assert.Equal(40, schema.Properties["value"].ActualSchema.MinLength); @@ -81,7 +81,7 @@ public async Task When_string_property_has_minlength_then_stringlength_attribute [Fact] public async Task When_int_property_has_maximum_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -100,7 +100,7 @@ public async Task When_int_property_has_maximum_then_range_attribute_is_rendered }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -108,7 +108,7 @@ public async Task When_int_property_has_maximum_then_range_attribute_is_rendered }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Maximum); Assert.Equal(20, schema.Properties["value"].ActualSchema.Maximum); @@ -119,7 +119,7 @@ public async Task When_int_property_has_maximum_then_range_attribute_is_rendered [Fact] public async Task When_int32_property_has_minimum_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -138,7 +138,7 @@ public async Task When_int32_property_has_minimum_then_range_attribute_is_render }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -146,7 +146,7 @@ public async Task When_int32_property_has_minimum_then_range_attribute_is_render }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Minimum); Assert.Equal(10, schema.Properties["value"].ActualSchema.Minimum); @@ -157,7 +157,7 @@ public async Task When_int32_property_has_minimum_then_range_attribute_is_render [Fact] public async Task When_int64_property_has_minimum_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -176,7 +176,7 @@ public async Task When_int64_property_has_minimum_then_range_attribute_is_render }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -184,7 +184,7 @@ public async Task When_int64_property_has_minimum_then_range_attribute_is_render }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Minimum); Assert.Equal(10, schema.Properties["value"].ActualSchema.Minimum); @@ -195,7 +195,7 @@ public async Task When_int64_property_has_minimum_then_range_attribute_is_render [Fact] public async Task When_integer_property_has_minimum_and_maximum_that_are_int64_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -214,7 +214,7 @@ public async Task When_integer_property_has_minimum_and_maximum_that_are_int64_t }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -222,7 +222,7 @@ public async Task When_integer_property_has_minimum_and_maximum_that_are_int64_t }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Minimum); Assert.Equal(-10000000000m, schema.Properties["value"].ActualSchema.Minimum); Assert.Equal(10000000000m, schema.Properties["value"].ActualSchema.Maximum); @@ -235,7 +235,7 @@ public async Task When_integer_property_has_minimum_and_maximum_that_are_int64_t [Fact] public async Task When_integer_property_has_minimum_and_maximum_with_exclusive_true_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -256,7 +256,7 @@ public async Task When_integer_property_has_minimum_and_maximum_with_exclusive_t }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -264,7 +264,7 @@ public async Task When_integer_property_has_minimum_and_maximum_with_exclusive_t }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Minimum); Assert.Equal(-100000000m, schema.Properties["value"].ActualSchema.Minimum); Assert.Equal(100000000m, schema.Properties["value"].ActualSchema.Maximum); @@ -279,7 +279,7 @@ public async Task When_integer_property_has_minimum_and_maximum_with_exclusive_t [Fact] public async Task When_number_property_has_minimum_and_maximum_with_exclusive_true_and_multipleof_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -301,7 +301,7 @@ public async Task When_number_property_has_minimum_and_maximum_with_exclusive_tr }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -309,7 +309,7 @@ public async Task When_number_property_has_minimum_and_maximum_with_exclusive_tr }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Minimum); Assert.Equal(-100000000.5m, schema.Properties["value"].ActualSchema.Minimum); Assert.Equal(100000000.5m, schema.Properties["value"].ActualSchema.Maximum); @@ -324,7 +324,7 @@ public async Task When_number_property_has_minimum_and_maximum_with_exclusive_tr [Fact] public async Task When_number_property_has_minimum_and_maximum_that_are_double_then_range_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -343,7 +343,7 @@ public async Task When_number_property_has_minimum_and_maximum_that_are_double_t }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -351,7 +351,7 @@ public async Task When_number_property_has_minimum_and_maximum_that_are_double_t }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Minimum); Assert.Equal(-10000000000m, schema.Properties["value"].ActualSchema.Minimum); Assert.Equal(10000000000m, schema.Properties["value"].ActualSchema.Maximum); @@ -364,7 +364,7 @@ public async Task When_number_property_has_minimum_and_maximum_that_are_double_t [Fact] public async Task When_string_property_has_pattern_then_regularexpression_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string regularExpression = "[a-zA-Z0-9]{5,56}"; const string json = @"{ 'type': 'object', @@ -383,7 +383,7 @@ public async Task When_string_property_has_pattern_then_regularexpression_attrib }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -391,7 +391,7 @@ public async Task When_string_property_has_pattern_then_regularexpression_attrib }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Null(schema.Properties["value"].Pattern); Assert.Equal(regularExpression, schema.Properties["value"].ActualSchema.Pattern); @@ -402,7 +402,7 @@ public async Task When_string_property_has_pattern_then_regularexpression_attrib [Fact] public async Task When_array_property_has_maxitems_then_maxlength_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -423,7 +423,7 @@ public async Task When_array_property_has_maxitems_then_maxlength_attribute_is_r }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -431,7 +431,7 @@ public async Task When_array_property_has_maxitems_then_maxlength_attribute_is_r }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Equal(0, schema.Properties["value"].MaxItems); Assert.Equal(10, schema.Properties["value"].ActualSchema.MaxItems); @@ -442,7 +442,7 @@ public async Task When_array_property_has_maxitems_then_maxlength_attribute_is_r [Fact] public async Task When_array_property_has_minitems_then_minlength_attribute_is_rendered_in_Swagger_mode() { - //// Arrange + // Arrange const string json = @"{ 'type': 'object', 'required': [ 'value' ], @@ -463,7 +463,7 @@ public async Task When_array_property_has_minitems_then_minlength_attribute_is_r }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -472,7 +472,7 @@ public async Task When_array_property_has_minitems_then_minlength_attribute_is_r }); var code = generator.GenerateFile("Message"); - //// Assert + // Assert Assert.Equal(0, schema.Properties["value"].MinItems); Assert.Equal(10, schema.Properties["value"].ActualSchema.MinItems); diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValueGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValueGeneratorTests.cs index 1469a6253..00c406980 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValueGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ValueGeneratorTests.cs @@ -14,12 +14,12 @@ public class RangeClass } [Fact] - public async Task When_schema_contains_range_then_code_is_correctly_generated() + public void When_schema_contains_range_then_code_is_correctly_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -27,14 +27,14 @@ public async Task When_schema_contains_range_then_code_is_correctly_generated() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.ComponentModel.DataAnnotations.Range(2, int.MaxValue)]", code); } [Fact] public async Task When_property_is_integer_and_no_format_is_available_then_default_value_is_int32() { - /// Arrange + // Arrange var json = @"{ ""type"": ""object"", ""properties"": { @@ -52,7 +52,7 @@ public async Task When_property_is_integer_and_no_format_is_available_then_defau }"; var schema = await JsonSchema.FromJsonAsync(json); - /// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -60,14 +60,14 @@ public async Task When_property_is_integer_and_no_format_is_available_then_defau }); var code = generator.GenerateFile("MyClass"); - /// Assert + // Assert Assert.Contains("public int? PageSize { get; set; } = 10;", code); } [Fact] public async Task When_property_is_string_and_format_is_date_time_then_assign_default_value() { - /// Arrange + // Arrange var json = @"{ ""type"": ""object"", ""properties"": { @@ -80,7 +80,7 @@ public async Task When_property_is_string_and_format_is_date_time_then_assign_de }"; var schema = await JsonSchema.FromJsonAsync(json); - /// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, @@ -89,7 +89,7 @@ public async Task When_property_is_string_and_format_is_date_time_then_assign_de }); var code = generator.GenerateFile("MyClass"); - /// Assert + // Assert Assert.Contains("public System.DateTime? DateTime { get; set; } = System.DateTime.Parse(\"31.12.9999 23:59:59\");", code); } } diff --git a/src/NJsonSchema.CodeGeneration.Tests/DefaultGenerationTests.cs b/src/NJsonSchema.CodeGeneration.Tests/DefaultGenerationTests.cs index 3846f0689..9dc8f452f 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/DefaultGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/DefaultGenerationTests.cs @@ -16,42 +16,42 @@ public class DefaultPropertyGenerationClass [Fact] public async Task When_property_has_default_attribute_then_default_is_in_schema() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Assert + // Assert Assert.Equal("foo", schema.Properties["Test"].Default); } [Fact] public async Task When_property_has_default_attribute_then_default_value_is_set_in_generated_INPC_CSharp_code() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Act + // Act var generator = new CSharpGenerator(schema); generator.Settings.ClassStyle = CSharpClassStyle.Inpc; var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("private string _test = \"foo\";", code); } [Fact] public async Task When_property_has_default_attribute_then_default_value_is_set_in_generated_Poco_CSharp_code() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Act + // Act var generator = new CSharpGenerator(schema); generator.Settings.ClassStyle = CSharpClassStyle.Poco; var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("public string Test { get; set; } = \"foo\";", code); } } diff --git a/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs index e50e03ed3..e48a99218 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs @@ -25,9 +25,9 @@ public DefaultValueGeneratorTests() [Fact] public void When_schema_has_default_value_of_int_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Integer, @@ -39,7 +39,7 @@ public void When_schema_has_default_value_of_int_it_is_generated_in_CSharp_and_T var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "int", "int", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "int", "int", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("6", csharpValue); Assert.Equal("6", typescriptValue); } @@ -47,9 +47,9 @@ public void When_schema_has_default_value_of_int_it_is_generated_in_CSharp_and_T [Fact] public void When_schema_has_default_value_of_long_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Integer, @@ -62,7 +62,7 @@ public void When_schema_has_default_value_of_long_it_is_generated_in_CSharp_and_ var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "long", "long", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "long", "long", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("6000000000L", csharpValue); Assert.Equal("6000000000", typescriptValue); } @@ -70,9 +70,9 @@ public void When_schema_has_default_value_of_long_it_is_generated_in_CSharp_and_ [Fact] public void When_schema_has_default_value_of_double_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Number, @@ -85,7 +85,7 @@ public void When_schema_has_default_value_of_double_it_is_generated_in_CSharp_an var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "double", "double", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "double", "double", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("1234.567D", csharpValue); Assert.Equal("1234.567", typescriptValue); } @@ -93,9 +93,9 @@ public void When_schema_has_default_value_of_double_it_is_generated_in_CSharp_an [Fact] public void When_schema_has_default_value_of_double_without_format_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Number, @@ -107,7 +107,7 @@ public void When_schema_has_default_value_of_double_without_format_it_is_generat var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "double", "double", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "double", "double", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("1234.567D", csharpValue); Assert.Equal("1234.567", typescriptValue); } @@ -115,9 +115,9 @@ public void When_schema_has_default_value_of_double_without_format_it_is_generat [Fact] public void When_schema_has_default_value_of_float_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Number, @@ -130,7 +130,7 @@ public void When_schema_has_default_value_of_float_it_is_generated_in_CSharp_and var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "float", "float", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "float", "float", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("1234.567F", csharpValue); Assert.Equal("1234.567", typescriptValue); } @@ -138,9 +138,9 @@ public void When_schema_has_default_value_of_float_it_is_generated_in_CSharp_and [Fact] public void When_schema_has_default_value_of_bool_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Boolean, @@ -152,7 +152,7 @@ public void When_schema_has_default_value_of_bool_it_is_generated_in_CSharp_and_ var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "bool", "bool", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "bool", "bool", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("true", csharpValue); Assert.Equal("true", typescriptValue); } @@ -160,9 +160,9 @@ public void When_schema_has_default_value_of_bool_it_is_generated_in_CSharp_and_ [Fact] public void When_schema_has_default_value_of_string_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.String, @@ -174,7 +174,7 @@ public void When_schema_has_default_value_of_string_it_is_generated_in_CSharp_an var csharpValue = _csharpGenerator.GetDefaultValue(schema, true, "string", "string", true, _csharpTypeResolver); var typescriptValue = _typescriptGenerator.GetDefaultValue(schema, true, "string", "string", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("\"test\\\\test\\\"test\\r\\ntest\"", csharpValue); Assert.Equal("\"test\\\\test\\\"test\\r\\ntest\"", typescriptValue); } @@ -190,12 +190,12 @@ public string Generate(int index, string name, object value, JsonSchema schema) [Fact] public void When_schema_has_default_value_of_enum_it_is_generated_in_CSharp_and_TypeScript_correctly() { - //// Arrange + // Arrange var csharpSettings = new CSharpGeneratorSettings { EnumNameGenerator = new MyEnumNameGenerator(), Namespace = "Ns" }; var csharpGenerator = new CSharpValueGenerator(csharpSettings); var csharpTypeResolver = new CSharpTypeResolver(csharpSettings); - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.String, @@ -214,7 +214,7 @@ public void When_schema_has_default_value_of_enum_it_is_generated_in_CSharp_and_ var csharpValue = csharpGenerator.GetDefaultValue(schema, true, "MyEnum", "MyEnum", true, csharpTypeResolver); var typescriptValue = typescriptGenerator.GetDefaultValue(schema, true, "MyEnum", "MyEnum", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("Ns.MyEnum.bar", csharpValue); Assert.Equal("MyEnum.bar", typescriptValue); } @@ -225,7 +225,7 @@ public void When_schema_has_default_value_of_enum_it_is_generated_in_CSharp_and_ [Fact] public void When_schema_has_a_default_value_for_an_enum_and_uses_enumstyle_stringliteral_it_defaults_to_the_stringliteral() { - //// Arrange + // Arrange var schema = new JsonSchema() { Type = JsonObjectType.String, @@ -241,22 +241,22 @@ public void When_schema_has_a_default_value_for_an_enum_and_uses_enumstyle_strin var typescriptGenerator = new TypeScriptValueGenerator(typescriptSettings); var typescriptTypeResolver = new TypeScriptTypeResolver(typescriptSettings); - //// Act + // Act var typescriptValue = typescriptGenerator.GetDefaultValue(schema, true, "MyEnum", "MyEnum", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Equal("\"Bar\"", typescriptValue); } [Fact] public void When_schema_has_required_abstract_class_it_generates_no_default_value_for_in_CSharp_and_TypeScript() { - //// Arrange + // Arrange var csharpSettings = new CSharpGeneratorSettings(); var csharpGenerator = new CSharpValueGenerator(csharpSettings); var csharpTypeResolver = new CSharpTypeResolver(csharpSettings); - //// Act + // Act var schema = new JsonSchema() { Type = JsonObjectType.Object, @@ -270,7 +270,7 @@ public void When_schema_has_required_abstract_class_it_generates_no_default_valu var csharpValue = csharpGenerator.GetDefaultValue(schema, false, "BaseClass", "BaseClass", true, csharpTypeResolver); var typescriptValue = typescriptGenerator.GetDefaultValue(schema, false, "BaseClass", "BaseClass", true, typescriptTypeResolver); - //// Assert + // Assert Assert.Null(csharpValue); Assert.Null(typescriptValue); } diff --git a/src/NJsonSchema.CodeGeneration.Tests/EnumGenerationTests.cs b/src/NJsonSchema.CodeGeneration.Tests/EnumGenerationTests.cs index e2db0bd14..83d4ca0fb 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/EnumGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/EnumGenerationTests.cs @@ -32,22 +32,22 @@ public enum Bar [Fact] public async Task When_string_and_integer_enum_used_then_two_enums_are_generated_in_typescript() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Equal(3, code.Split(["export enum "], StringSplitOptions.None).Count()); // two found } [Fact] public async Task When_export_types_is_true_add_export_before_enum_in_typescript() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); @@ -56,18 +56,18 @@ public async Task When_export_types_is_true_add_export_before_enum_in_typescript ExportTypes = true }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, typeScriptGeneratorSettings); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export enum", code); } [Fact] public async Task When_add_export_keyword_is_false_dont_add_export_before_enum_in_typescript() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); @@ -76,26 +76,26 @@ public async Task When_add_export_keyword_is_false_dont_add_export_before_enum_i ExportTypes = false }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, typeScriptGeneratorSettings); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("export enum", code); } [Fact] public async Task When_string_and_integer_enum_used_then_one_enum_is_generated_in_CSharp() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema); var code = generator.GenerateFile("MyClass").Replace("\r", ""); - //// Assert + // Assert Assert.DoesNotContain("Ref_", code); Assert.Contains("public enum Bar\n", code); Assert.Contains("public enum Bar2\n", code); @@ -109,15 +109,15 @@ public async Task When_string_and_integer_enum_used_then_one_enum_is_generated_i [Fact] public async Task When_byte_enum_is_generated_then_no_exception_occurs() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert // No exception } @@ -136,15 +136,15 @@ public enum ByteBar : byte [Fact] public async Task When_enum_has_string_value_then_CS_code_has_EnumMember_attribute() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.Runtime.Serialization.EnumMember(Value = @\"0562\")]", code); Assert.Contains("_0562 = 0,", code); Assert.Contains("[System.Runtime.Serialization.EnumMember(Value = @\"0532\")]", code); @@ -154,15 +154,15 @@ public async Task When_enum_has_string_value_then_CS_code_has_EnumMember_attribu [Fact] public async Task When_enum_has_string_value_then_TS_code_has_string_value() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 1.8m }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("_0562 = \"0562\",", code); Assert.Contains("_0532 = \"0532\",", code); } @@ -186,15 +186,15 @@ public enum StringEnum [Fact] public async Task When_enum_has_integer_value_then_CS_code_has_EnumMember_attribute() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new CSharpGenerator(schema); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("[EnumMember(Value = \"0562\")]", code); Assert.Contains("_0562 = 10,", code); Assert.DoesNotContain("[EnumMember(Value = \"0532\")]", code); @@ -204,15 +204,15 @@ public async Task When_enum_has_integer_value_then_CS_code_has_EnumMember_attrib [Fact] public async Task When_enum_has_integer_value_then_TS_code_has_string_value() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("_0562 = 10,", code); Assert.Contains("_0532 = 15,", code); } @@ -231,7 +231,7 @@ public enum NumberEnum [Fact] public async Task When_enum_has_no_names_and_string_value_starts_with_number_then_underline_is_generated() { - //// Arrange + // Arrange var schemaData = @"{ ""type"": ""object"", ""properties"": { @@ -257,11 +257,11 @@ public async Task When_enum_has_no_names_and_string_value_starts_with_number_the }"; var schema = await JsonSchema.FromJsonAsync(schemaData); - //// Act + // Act var generator = new CSharpGenerator(schema); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("[System.Runtime.Serialization.EnumMember(Value = @\"0562\")]", code); Assert.Contains("_0562 = 0,", code); Assert.Contains("[System.Runtime.Serialization.EnumMember(Value = @\"\"\"0532\"\"\")]", code); @@ -274,7 +274,7 @@ public async Task When_enum_has_no_names_and_string_value_starts_with_number_the [Fact] public async Task When_property_is_nullable_and_enum_allows_null_then_no_exception_is_thrown() { - //// Arrange + // Arrange var json = @"{    ""type"":""object"",    ""properties"":{ @@ -296,11 +296,11 @@ public async Task When_property_is_nullable_and_enum_allows_null_then_no_excepti }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()); var code = generator.GenerateFile("Foo"); - //// Assert + // Assert Assert.NotNull(code); } } diff --git a/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs b/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs index f3fb72588..caf535b69 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs @@ -64,19 +64,19 @@ public class InheritanceSerializationTests [Fact] public void When_JsonInheritanceConverter_is_passed_null_it_deserializes_to_null() { - //// Arrange + // Arrange - //// Act + // Act var result = JsonConvert.DeserializeObject("null"); - //// Assert + // Assert Assert.Null(result); } [Fact] public async Task When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized() { - //// Arrange + // Arrange var container = new Container { Animal = new Dog @@ -91,7 +91,7 @@ public async Task When_JsonInheritanceConverter_is_used_then_inheritance_is_corr } }; - //// Act + // Act var json = JsonConvert.SerializeObject(container, Formatting.Indented); var deserializedContainer = JsonConvert.DeserializeObject(json); @@ -99,7 +99,7 @@ public async Task When_JsonInheritanceConverter_is_used_then_inheritance_is_corr var schemaJson = schema.ToJson(); var errors = schema.Validate(json); - //// Assert + // Assert Assert.True(deserializedContainer.Animal is Dog); Assert.True((deserializedContainer.Animal as Dog).SubElements.First() is SubClass1); Assert.True((deserializedContainer.Animal as Dog).SubElements[1] is SubClass3); @@ -108,7 +108,7 @@ public async Task When_JsonInheritanceConverter_is_used_then_inheritance_is_corr [Fact] public async Task When_serializer_setting_is_changed_then_converter_uses_correct_settings() { - //// Arrange + // Arrange var container = new Container { Animal = new Dog @@ -123,12 +123,12 @@ public async Task When_serializer_setting_is_changed_then_converter_uses_correct } }; - //// Act + // Act var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; var json = JsonConvert.SerializeObject(container, Formatting.Indented, settings); var deserializedContainer = JsonConvert.DeserializeObject(json); - //// Assert + // Assert Assert.Contains("prop3", json); Assert.DoesNotContain("Prop3", json); } @@ -156,7 +156,7 @@ public class C : B [Fact] public async Task When_dates_are_converted_then_JsonInheritanceConverter_should_inherit_settings() { - //// Arrange + // Arrange var offset = new TimeSpan(10, 0, 0); var x = new A { @@ -167,12 +167,12 @@ public async Task When_dates_are_converted_then_JsonInheritanceConverter_should_ } }; - //// Act + // Act var settings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.DateTimeOffset }; var json = JsonConvert.SerializeObject(x, Formatting.Indented, settings); var deserialized = JsonConvert.DeserializeObject(json, settings); - //// Assert + // Assert Assert.Equal(deserialized.created.Offset, offset); Assert.Equal(deserialized.subclass.created.Offset, offset); } @@ -180,34 +180,34 @@ public async Task When_dates_are_converted_then_JsonInheritanceConverter_should_ [Fact] public async Task JsonInheritanceConverter_is_thread_safe() { - //// Arrange + // Arrange var tasks = new List(); for (int i = 0; i < 100; i++) { tasks.Add(Task.Run(When_JsonInheritanceConverter_is_used_then_inheritance_is_correctly_serialized_and_deserialized)); } - //// Act + // Act await Task.WhenAll([.. tasks]); - //// Assert + // Assert // No exceptions } [Fact] public async Task When_JsonInheritanceConverter_is_set_then_discriminator_field_is_set() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var baseSchema = schema.Properties["Animal"].ActualTypeSchema.ActualSchema; var discriminator = baseSchema.ActualDiscriminator; var property = baseSchema.Properties["discriminator"]; var json = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(property); Assert.True(property.IsRequired); Assert.Equal("discriminator", discriminator); @@ -216,14 +216,14 @@ public async Task When_JsonInheritanceConverter_is_set_then_discriminator_field_ [Fact] public async Task When_JsonInheritanceConverter_is_set_then_discriminator_mappings_are_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var baseSchema = schema.Definitions["SubClass"].ActualSchema; - //// Assert + // Assert Assert.Equal(3, baseSchema.ActualDiscriminatorObject.Mapping.Count); Assert.True(baseSchema.ActualDiscriminatorObject.Mapping.ContainsKey("SubClass1")); Assert.True(baseSchema.ActualDiscriminatorObject.Mapping.ContainsKey("SubClass2")); @@ -233,14 +233,14 @@ public async Task When_JsonInheritanceConverter_is_set_then_discriminator_mappin [Fact] public async Task When_schema_contains_discriminator_and_inheritance_hierarchy_then_CSharp_is_correctly_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("public string Discriminator {", code); // discriminator property is not generated Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), \"discriminator\")]", code); // attribute is generated Assert.Contains("class JsonInheritanceConverter", code); // converter is generated @@ -249,18 +249,18 @@ public async Task When_schema_contains_discriminator_and_inheritance_hierarchy_t [Fact] public async Task When_schema_contains_discriminator_and_inheritance_hierarchy_then_TypeScript_is_correctly_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class }); var code = generator.GenerateFile("Container"); - //// Assert + // Assert Assert.Contains("export class Container", code); Assert.Contains("export class Animal", code); Assert.Contains("export class Dog", code); @@ -282,20 +282,20 @@ public async Task When_schema_contains_discriminator_and_inheritance_hierarchy_t #endif public async Task Subtypes_are_serialized_with_correct_discriminator() { - //// Arrange + // Arrange var json = await JsonSchema.FromJsonAsync(@"{""title"":""foo"",""type"":""object"",""discriminator"":""discriminator"",""properties"":{""discriminator"":{""type"":""string""}},""definitions"":{""bar"":{""type"":""object"",""allOf"":[{""$ref"":""#""}]}}}"); var data = json.ToJson(); var generator = new CSharpGenerator(json, new CSharpGeneratorSettings() { ClassStyle = CSharpClassStyle.Poco, Namespace = "foo" }); - //// Act + // Act var code = generator.GenerateFile(); var assembly = Compile(code); var type = assembly.GetType("foo.Foo") ?? throw new Exception("Foo not found in " + String.Join(", ", assembly.GetTypes().Select(t => t.Name))); var bar = JsonConvert.DeserializeObject(@"{""discriminator"":""bar""}", type); - //// Assert + // Assert Assert.Contains(@"""bar""", JsonConvert.SerializeObject(bar)); } diff --git a/src/NJsonSchema.CodeGeneration.Tests/Samples/SampleTests.cs b/src/NJsonSchema.CodeGeneration.Tests/Samples/SampleTests.cs index c72a1e265..2fcc70fe7 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/Samples/SampleTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/Samples/SampleTests.cs @@ -73,7 +73,7 @@ public async Task Demo2() [Fact] public async Task When_JSON_contains_DateTime_is_available_then_string_validator_validates_correctly() { - //// Arrange + // Arrange var schemaJson = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""type"": ""object"", @@ -95,17 +95,17 @@ public async Task When_JSON_contains_DateTime_is_available_then_string_validator ""PatternDate"":""2012-11-07T00:00:00Z"" }"; - //// Act + // Act var errors = schema.Validate(dataJson); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public async Task When_JSON_contains_DateTime_is_available_then_JObject_validator_validates_correctly() { - //// Arrange + // Arrange var schemaJson = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""type"": ""object"", @@ -129,10 +129,10 @@ public async Task When_JSON_contains_DateTime_is_available_then_JObject_validato var value = data["SimpleDate"].Value(); // not original format - //// Act + // Act var errors = schema.Validate(data); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/AbstractGenerationTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/AbstractGenerationTests.cs index af0d72dc6..b6bc08cce 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/AbstractGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/AbstractGenerationTests.cs @@ -14,17 +14,17 @@ public abstract class AbstractClass : BaseClass } [Fact] - public async Task When_class_is_abstract_then_is_abstract_TypeScript_keyword_is_generated() + public void When_class_is_abstract_then_is_abstract_TypeScript_keyword_is_generated() { - /// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - /// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m }); var code = generator.GenerateFile("AbstractClass"); - /// Assert + // Assert Assert.Contains("export abstract class AbstractClass", code); Assert.Contains("base: string", code); @@ -39,17 +39,17 @@ public class ContainerClass } [Fact] - public async Task When_property_is_required_and_abstract_then_it_is_not_instantiated() + public void When_property_is_required_and_abstract_then_it_is_not_instantiated() { - /// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - /// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m }); var code = generator.GenerateFile("ContainerClass"); - /// Assert + // Assert Assert.Contains("foo: AbstractClass", code); Assert.Contains("this.foo = _data[\"Foo\"] ? AbstractClass.fromJS(_data[\"Foo\"]) : undefined;", code); } @@ -68,16 +68,16 @@ public class SuperClass : AbstractClass } [Fact] - public async Task When_abstract_class_is_in_inheritance_hierarchy_then_it_is_newer_instantiated() + public void When_abstract_class_is_in_inheritance_hierarchy_then_it_is_newer_instantiated() { - /// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - /// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m }); var code = generator.GenerateFile("AbstractClass"); - /// Assert + // Assert Assert.DoesNotContain("new AbstractClass();", code); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassGenerationTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassGenerationTests.cs index d5a5c2e8d..406d34101 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassGenerationTests.cs @@ -69,21 +69,21 @@ public async Task Verify_output(TypeScriptTypeStyle style, decimal version) await Verify(output).UseParameters(style, version); } - private static async Task PrepareAsync(TypeScriptGeneratorSettings settings) + private static Task PrepareAsync(TypeScriptGeneratorSettings settings) { var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, settings); var code = generator.GenerateFile("MyClass"); - return code; + return Task.FromResult(code); } [Fact] public void When_array_property_is_required_or_not_then_the_code_has_correct_initializer() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -111,7 +111,7 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini } }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -120,7 +120,7 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("a: string[];", code); Assert.Contains("this.a = [];", code); Assert.Contains("b: string[];", code); @@ -129,7 +129,7 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini [Fact] public void When_dictionary_property_is_required_or_not_then_the_code_has_correct_initializer() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -157,7 +157,7 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc } }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -166,7 +166,7 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("a: { [key: string]: string; };", code); Assert.Contains("this.a = {};", code); Assert.Contains("b: { [key: string]: string; };", code); @@ -175,7 +175,7 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc [Fact] public void When_object_property_is_required_or_not_then_the_code_has_correct_initializer() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -211,7 +211,7 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in } }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -220,7 +220,7 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("a: A;", code); Assert.Contains("this.a = new A();", code); Assert.Contains("this.a = _data[\"A\"] ? A.fromJS(_data[\"A\"]) : new A();", code); @@ -234,7 +234,7 @@ public async Task When_export_types_is_false_dont_add_export_before_class_and_in { var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, ExportTypes = false }); - //// Assert + // Assert Assert.DoesNotContain("export class Student extends Person implements IStudent {", code); Assert.DoesNotContain("export interface IStudent extends IPerson {", code); Assert.DoesNotContain("export interface IPerson {", code); @@ -245,7 +245,7 @@ public async Task When_add_export_keyword_is_false_with_knockout_class_dont_add_ { var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.KnockoutClass, ExportTypes = false }); - //// Assert + // Assert Assert.DoesNotContain("export class Student extends Person {", code); } @@ -258,7 +258,7 @@ public async Task When_GenerateConstructorInterface_then_no_interfaces_are_gener GenerateConstructorInterface = false }); - //// Assert + // Assert Assert.DoesNotContain("interface IStudent extends IPerson {", code); Assert.DoesNotContain("interface IPerson {", code); } @@ -273,12 +273,12 @@ public async Task When_Knockout_class_is_generated_then_initializers_are_correct TypeScriptVersion = 2.0m }); - //// Assert + // Assert Assert.DoesNotContain("let firstName_ = data[\"FirstName\"];", code); } [Fact] - public async Task When_GenerateConstructorInterface_is_disabled_then_data_is_not_checked_and_default_initialization_is_always_exectued() + public void When_GenerateConstructorInterface_is_disabled_then_data_is_not_checked_and_default_initialization_is_always_exectued() { // Assert var schema = NewtonsoftJsonSchemaGenerator.FromType( diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassOrderTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassOrderTests.cs index 8dfec3c9a..f7d1351a1 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassOrderTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ClassOrderTests.cs @@ -5,7 +5,7 @@ public class ClassOrderTests [Fact] public void When_class_order_is_wrong_then_classes_are_correctly_reordered() { - //// Arrange + // Arrange var classes = new List { new CodeArtifact("Car", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, ""), @@ -16,11 +16,11 @@ public void When_class_order_is_wrong_then_classes_are_correctly_reordered() new CodeArtifact("Person", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, "") }; - //// Act + // Act classes = classes.OrderByBaseDependency().ToList(); var order = string.Join(", ", classes.Select(c => c.TypeName)); - //// Assert + // Assert Assert.Equal("Car, Fruit, Apple, Person, Teacher, Professor", order); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ConstructorInterfaceTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ConstructorInterfaceTests.cs index f70d5ac1f..a9d764edd 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ConstructorInterfaceTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ConstructorInterfaceTests.cs @@ -48,13 +48,13 @@ public class Skill } [Fact] - public async Task When_constructor_interface_and_conversion_code_is_generated_then_it_is_correct() + public void When_constructor_interface_and_conversion_code_is_generated_then_it_is_correct() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var json = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { GenerateConstructorInterface = true, @@ -64,7 +64,7 @@ public async Task When_constructor_interface_and_conversion_code_is_generated_th var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("new MyClass(", output); // address property is converted: @@ -90,7 +90,7 @@ public async Task When_constructor_interface_and_conversion_code_is_generated_th [Fact] public async Task When_array_of_string_dictionary_is_used_with_ConvertConstructorInterfaceData_then_it_should_be_ignored() { - //// Arrange + // Arrange var json = @" { ""type"": ""object"", @@ -108,7 +108,7 @@ public async Task When_array_of_string_dictionary_is_used_with_ConvertConstructo }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { GenerateConstructorInterface = true, @@ -118,7 +118,7 @@ public async Task When_array_of_string_dictionary_is_used_with_ConvertConstructo var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("custom4: { [key: string]: string; }[];", output); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateCodeGenerationTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateCodeGenerationTests.cs index 745f4d736..363a92d94 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateCodeGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateCodeGenerationTests.cs @@ -17,10 +17,10 @@ public class DateCodeGenerationTests [InlineData(true)] public async Task When_date_handling_is_string_then_string_property_is_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -29,7 +29,7 @@ public async Task When_date_handling_is_string_then_string_property_is_generated }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: string", code); Assert.Contains("this.myDate = _data[\"myDate\"];", code); Assert.Contains("data[\"myDate\"] = this.myDate;", code); @@ -40,10 +40,10 @@ public async Task When_date_handling_is_string_then_string_property_is_generated [InlineData(true)] public async Task When_date_handling_is_moment_then_moment_property_is_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -52,7 +52,7 @@ public async Task When_date_handling_is_moment_then_moment_property_is_generated }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: moment.Moment", code); Assert.Contains("this.myDate = _data[\"myDate\"] ? moment(_data[\"myDate\"].toString()) : undefined;", code); Assert.Contains("data[\"myDate\"] = this.myDate ? this.myDate.format('YYYY-MM-DD') : undefined;", code); @@ -61,10 +61,10 @@ public async Task When_date_handling_is_moment_then_moment_property_is_generated [Fact] public async Task When_date_handling_is_moment_then_duration_property_is_generated_in_class() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -72,7 +72,7 @@ public async Task When_date_handling_is_moment_then_duration_property_is_generat }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myTimeSpan: moment.Duration", code); Assert.Contains("this.myTimeSpan = _data[\"myTimeSpan\"] ? moment.duration(_data[\"myTimeSpan\"].toString()) : undefined;", code); Assert.Contains("data[\"myTimeSpan\"] = this.myTimeSpan ? this.myTimeSpan.format('d.hh:mm:ss.SS', { trim: false }) : undefined;", code); @@ -83,10 +83,10 @@ public async Task When_date_handling_is_moment_then_duration_property_is_generat [InlineData(true)] public async Task When_date_handling_is_luxon_then_datetime_property_is_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -95,7 +95,7 @@ public async Task When_date_handling_is_luxon_then_datetime_property_is_generate }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: DateTime", code); Assert.Contains("this.myDate = _data[\"myDate\"] ? DateTime.fromISO(_data[\"myDate\"].toString()) : undefined;", code); Assert.Contains("data[\"myDate\"] = this.myDate ? this.myDate.toFormat('yyyy-MM-dd') : undefined;", code); @@ -104,10 +104,10 @@ public async Task When_date_handling_is_luxon_then_datetime_property_is_generate [Fact] public async Task When_date_handling_is_luxon_then_duration_property_is_generated_in_class() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -115,7 +115,7 @@ public async Task When_date_handling_is_luxon_then_duration_property_is_generate }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myTimeSpan: Duration", code); Assert.Contains("this.myTimeSpan = _data[\"myTimeSpan\"] ? Duration.fromISO(_data[\"myTimeSpan\"].toString()) : undefined;", code); Assert.Contains("data[\"myTimeSpan\"] = this.myTimeSpan ? this.myTimeSpan.toString() : undefined;", code); @@ -126,10 +126,10 @@ public async Task When_date_handling_is_luxon_then_duration_property_is_generate [InlineData(true)] public async Task When_date_handling_is_dayjs_then_dayjs_property_is_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -138,7 +138,7 @@ public async Task When_date_handling_is_dayjs_then_dayjs_property_is_generated_i }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: dayjs.Dayjs", code); Assert.Contains("this.myDate = _data[\"myDate\"] ? dayjs(_data[\"myDate\"].toString()) : undefined;", code); Assert.Contains("data[\"myDate\"] = this.myDate ? this.myDate.format('YYYY-MM-DD') : undefined;", code); @@ -147,10 +147,10 @@ public async Task When_date_handling_is_dayjs_then_dayjs_property_is_generated_i [Fact] public async Task When_date_handling_is_date_then_date_property_is_generated_in_class() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -158,7 +158,7 @@ public async Task When_date_handling_is_date_then_date_property_is_generated_in_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: Date", code); Assert.Contains("this.myDate = _data[\"myDate\"] ? new Date(_data[\"myDate\"].toString()) : undefined;", code); Assert.Contains("data[\"myDate\"] = this.myDate ? formatDate(this.myDate) : undefined;", code); @@ -168,10 +168,10 @@ public async Task When_date_handling_is_date_then_date_property_is_generated_in_ [Fact] public async Task When_date_handling_is_date_then_date_property_is_generated_in_class_with_local_timezone_conversion() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -180,7 +180,7 @@ public async Task When_date_handling_is_date_then_date_property_is_generated_in_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: Date", code); Assert.Contains("this.myDate = _data[\"myDate\"] ? parseDateOnly(_data[\"myDate\"].toString()) : undefined;", code); Assert.Contains("data[\"myDate\"] = this.myDate ? formatDate(this.myDate) : undefined;", code); @@ -192,10 +192,10 @@ public async Task When_date_handling_is_date_then_date_property_is_generated_in_ [InlineData(true)] public async Task When_date_handling_is_offset_moment_then_date_property_is_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -204,7 +204,7 @@ public async Task When_date_handling_is_offset_moment_then_date_property_is_gene }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: moment.Moment", code); Assert.Contains("this.myDate = _data[\"myDate\"] ? moment.parseZone(_data[\"myDate\"].toString()) : undefined;", code); Assert.Contains("data[\"myDate\"] = this.myDate ? this.myDate.format('YYYY-MM-DD') : undefined;", code); @@ -213,10 +213,10 @@ public async Task When_date_handling_is_offset_moment_then_date_property_is_gene [Fact] public async Task When_date_handling_is_date_then_date_property_is_generated_in_interface() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -224,17 +224,17 @@ public async Task When_date_handling_is_date_then_date_property_is_generated_in_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: Date;", code); } [Fact] public async Task When_date_handling_is_moment_then_moment_property_is_generated_in_interface() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -242,7 +242,7 @@ public async Task When_date_handling_is_moment_then_moment_property_is_generated }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: moment.Moment;", code); } @@ -250,10 +250,10 @@ public async Task When_date_handling_is_moment_then_moment_property_is_generated [Fact] public async Task When_date_handling_is_string_then_string_property_is_generated_in_interface() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync(Json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -261,7 +261,7 @@ public async Task When_date_handling_is_string_then_string_property_is_generated }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDate: string;", code); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateTimeCodeGenerationTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateTimeCodeGenerationTests.cs index 20cbf13e0..f31172543 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateTimeCodeGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DateTimeCodeGenerationTests.cs @@ -12,12 +12,12 @@ public class ClassWithDateTimeProperty [Theory] [InlineData(false)] [InlineData(true)] - public async Task When_date_handling_is_string_then_string_property_are_generated_in_class(bool convertDateToLocalTimezone) + public void When_date_handling_is_string_then_string_property_are_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -26,7 +26,7 @@ public async Task When_date_handling_is_string_then_string_property_are_generate }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDateTime: string", code); Assert.Contains("this.myDateTime = _data[\"MyDateTime\"];", code); Assert.Contains("data[\"MyDateTime\"] = this.myDateTime;", code); @@ -35,12 +35,12 @@ public async Task When_date_handling_is_string_then_string_property_are_generate [Theory] [InlineData(false)] [InlineData(true)] - public async Task When_date_handling_is_moment_then_moment_property_are_generated_in_class(bool convertDateToLocalTimezone) + public void When_date_handling_is_moment_then_moment_property_are_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -49,7 +49,7 @@ public async Task When_date_handling_is_moment_then_moment_property_are_generate }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDateTime: moment.Moment", code); Assert.Contains("this.myDateTime = _data[\"MyDateTime\"] ? moment(_data[\"MyDateTime\"].toString()) : undefined;", code); Assert.Contains("data[\"MyDateTime\"] = this.myDateTime ? this.myDateTime.toISOString() : undefined;", code); @@ -58,12 +58,12 @@ public async Task When_date_handling_is_moment_then_moment_property_are_generate [Theory] [InlineData(false)] [InlineData(true)] - public async Task When_date_handling_is_offset_moment_then_moment_property_are_generated_in_class(bool convertDateToLocalTimezone) + public void When_date_handling_is_offset_moment_then_moment_property_are_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -72,7 +72,7 @@ public async Task When_date_handling_is_offset_moment_then_moment_property_are_g }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDateTime: moment.Moment", code); Assert.Contains("this.myDateTime = _data[\"MyDateTime\"] ? moment.parseZone(_data[\"MyDateTime\"].toString()) : undefined;", code); Assert.Contains("data[\"MyDateTime\"] = this.myDateTime ? this.myDateTime.toISOString(true) : undefined;", code); @@ -81,12 +81,12 @@ public async Task When_date_handling_is_offset_moment_then_moment_property_are_g [Theory] [InlineData(false)] [InlineData(true)] - public async Task When_date_handling_is_dayjs_then_dayjs_property_are_generated_in_class(bool convertDateToLocalTimezone) + public void When_date_handling_is_dayjs_then_dayjs_property_are_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -95,7 +95,7 @@ public async Task When_date_handling_is_dayjs_then_dayjs_property_are_generated_ }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDateTime: dayjs.Dayjs", code); Assert.Contains("this.myDateTime = _data[\"MyDateTime\"] ? dayjs(_data[\"MyDateTime\"].toString()) : undefined;", code); Assert.Contains("data[\"MyDateTime\"] = this.myDateTime ? this.myDateTime.toISOString() : undefined;", code); @@ -104,12 +104,12 @@ public async Task When_date_handling_is_dayjs_then_dayjs_property_are_generated_ [Theory] [InlineData(false)] [InlineData(true)] - public async Task When_date_handling_is_date_then_date_property_are_generated_in_class(bool convertDateToLocalTimezone) + public void When_date_handling_is_date_then_date_property_are_generated_in_class(bool convertDateToLocalTimezone) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -118,19 +118,19 @@ public async Task When_date_handling_is_date_then_date_property_are_generated_in }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("myDateTime: Date", code); Assert.Contains("this.myDateTime = _data[\"MyDateTime\"] ? new Date(_data[\"MyDateTime\"].toString()) : undefined;", code); Assert.Contains("data[\"MyDateTime\"] = this.myDateTime ? this.myDateTime.toISOString() : undefined;", code); } [Fact] - public async Task When_date_handling_is_date_then_date_property_are_generated_in_interface() + public void When_date_handling_is_date_then_date_property_are_generated_in_interface() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -138,17 +138,17 @@ public async Task When_date_handling_is_date_then_date_property_are_generated_in }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("MyDateTime: Date;", code); } [Fact] - public async Task When_date_handling_is_moment_then_moment_property_are_generated_in_interface() + public void When_date_handling_is_moment_then_moment_property_are_generated_in_interface() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -156,18 +156,18 @@ public async Task When_date_handling_is_moment_then_moment_property_are_generate }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("MyDateTime: moment.Moment;", code); } [Fact] - public async Task When_date_handling_is_string_then_string_property_are_generated_in_interface() + public void When_date_handling_is_string_then_string_property_are_generated_in_interface() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -175,7 +175,7 @@ public async Task When_date_handling_is_string_then_string_property_are_generate }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("MyDateTime: string;", code); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DictionaryTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DictionaryTests.cs index 311e35993..94ced7680 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DictionaryTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/DictionaryTests.cs @@ -16,9 +16,9 @@ public class StringDictionary : Dictionary } [Fact] - public async Task When_class_inherits_from_any_dictionary_then_interface_has_indexer_property() + public void When_class_inherits_from_any_dictionary_then_interface_has_indexer_property() { - //// Arrange + // Arrange var schemaGenerator = new JsonSchemaGenerator(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 @@ -27,19 +27,19 @@ public async Task When_class_inherits_from_any_dictionary_then_interface_has_ind var schema = schemaGenerator.Generate(typeof(AnyDictionary)); var json = schema.ToJson(); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface }); var code = codeGenerator.GenerateFile("MetadataDictionary"); - //// Assert + // Assert Assert.DoesNotContain("extends { [key: string]: any; }", code); Assert.Contains("[key: string]: any;", code); } [Fact] - public async Task When_class_inherits_from_any_dictionary_then_class_has_indexer_property() + public void When_class_inherits_from_any_dictionary_then_class_has_indexer_property() { - //// Arrange + // Arrange var schemaGenerator = new JsonSchemaGenerator(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 @@ -48,20 +48,20 @@ public async Task When_class_inherits_from_any_dictionary_then_class_has_indexer var schema = schemaGenerator.Generate(typeof(AnyDictionary)); var json = schema.ToJson(); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class }); var code = codeGenerator.GenerateFile("MetadataDictionary"); - //// Assert + // Assert Assert.DoesNotContain("extends { [key: string]: any; }", code); Assert.DoesNotContain("super()", code); Assert.Contains("[key: string]: any;", code); } [Fact] - public async Task When_class_inherits_from_string_dictionary_then_interface_has_indexer_property() + public void When_class_inherits_from_string_dictionary_then_interface_has_indexer_property() { - //// Arrange + // Arrange var schemaGenerator = new JsonSchemaGenerator(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 @@ -70,19 +70,19 @@ public async Task When_class_inherits_from_string_dictionary_then_interface_has_ var schema = schemaGenerator.Generate(typeof(StringDictionary)); var json = schema.ToJson(); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface }); var code = codeGenerator.GenerateFile("MetadataDictionary"); - //// Assert + // Assert Assert.DoesNotContain("extends { [key: string]: string; }", code); Assert.Contains("[key: string]: string | any;", code); } [Fact] - public async Task When_class_inherits_from_string_dictionary_then_class_has_indexer_property() + public void When_class_inherits_from_string_dictionary_then_class_has_indexer_property() { - //// Arrange + // Arrange var schemaGenerator = new JsonSchemaGenerator(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 @@ -91,11 +91,11 @@ public async Task When_class_inherits_from_string_dictionary_then_class_has_inde var schema = schemaGenerator.Generate(typeof(StringDictionary)); var json = schema.ToJson(); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class }); var code = codeGenerator.GenerateFile("MetadataDictionary"); - //// Assert + // Assert Assert.DoesNotContain("extends { [key: string]: string; }", code); Assert.DoesNotContain("super()", code); Assert.Contains("[key: string]: string | any;", code); @@ -104,7 +104,7 @@ public async Task When_class_inherits_from_string_dictionary_then_class_has_inde [Fact] public async Task When_property_is_dto_dictionary_then_assignment_may_create_new_instance() { - //// Arrange + // Arrange var json = @"{ ""required"": [ ""resource"" ], ""properties"": { @@ -126,7 +126,7 @@ public async Task When_property_is_dto_dictionary_then_assignment_may_create_new }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -135,14 +135,14 @@ public async Task When_property_is_dto_dictionary_then_assignment_may_create_new }); var code = codeGenerator.GenerateFile("Test"); - //// Assert + // Assert Assert.Contains("(this.resource)[key] = _data[\"resource\"][key] ? MyItem.fromJS(_data[\"resource\"][key]) : new MyItem();", code); } [Fact] public async Task When_property_is_object_and_not_dictionary_it_should_be_assigned_in_init_method() { - //// Arrange + // Arrange var json = @"{ ""properties"": { ""resource"": { @@ -152,7 +152,7 @@ public async Task When_property_is_object_and_not_dictionary_it_should_be_assign }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -160,7 +160,7 @@ public async Task When_property_is_object_and_not_dictionary_it_should_be_assign }); var code = codeGenerator.GenerateFile("Test"); - //// Assert + // Assert Assert.Contains("resource: any;", code); Assert.DoesNotContain("this.resource[key] = _data[\"resource\"][key];", code); Assert.DoesNotContain(" : new any();", code); @@ -169,7 +169,7 @@ public async Task When_property_is_object_and_not_dictionary_it_should_be_assign [Fact] public async Task When_property_is_string_dictionary_then_assignment_is_correct() { - //// Arrange + // Arrange var json = @"{ ""properties"": { ""resource"": { @@ -187,7 +187,7 @@ public async Task When_property_is_string_dictionary_then_assignment_is_correct( }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -196,7 +196,7 @@ public async Task When_property_is_string_dictionary_then_assignment_is_correct( }); var code = codeGenerator.GenerateFile("Test"); - //// Assert + // Assert Assert.Contains("(this.resource)[key] = _data[\"resource\"][key];", code); } @@ -214,13 +214,13 @@ public class DisplayValueDictionary : Dictionary [InlineData(true, true)] [InlineData(false, false)] [InlineData(true, false)] - public async Task When_property_uses_custom_dictionary_class_then_class_is_generated(bool inlineNamedDictionaries, bool convertConstructorInterfaceData) + public void When_property_uses_custom_dictionary_class_then_class_is_generated(bool inlineNamedDictionaries, bool convertConstructorInterfaceData) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -231,7 +231,7 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener }); var code = codeGenerator.GenerateFile("Test"); - //// Assert + // Assert if (inlineNamedDictionaries) { Assert.Contains("foo: { [key: string]: string; };", code); @@ -265,7 +265,7 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener [Fact] public async Task When_dictionary_has_arbitrary_nonenum_key_then_generated_typescript_uses_plain_string_key() { - //// Arrange + // Arrange var json = @"{ ""properties"": { ""myDict"": { @@ -293,7 +293,7 @@ public async Task When_dictionary_has_arbitrary_nonenum_key_then_generated_types var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { ConvertConstructorInterfaceData = true, @@ -303,7 +303,7 @@ public async Task When_dictionary_has_arbitrary_nonenum_key_then_generated_types var code = codeGenerator.GenerateFile("Test"); - //// Assert + // Assert Assert.DoesNotContain("[key in keyof typeof Istring]", code); } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ExtensionCodeTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ExtensionCodeTests.cs index 132b57c5d..0e20c0a3b 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ExtensionCodeTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/ExtensionCodeTests.cs @@ -50,13 +50,13 @@ export abstract class BaseClass { [Fact] public void When_extension_code_is_processed_then_code_and_classes_are_correctly_detected_and_converted() { - //// Arrange + // Arrange var code = Code; - //// Act + // Act var extensionCode = new TypeScriptExtensionCode(code, ["Foo", "Bar"], ["BaseClass"]); - //// Assert + // Assert Assert.True(extensionCode.ExtensionClasses.ContainsKey("Foo")); Assert.StartsWith("export class Foo extends Foo {", extensionCode.ExtensionClasses["Foo"]); @@ -92,12 +92,12 @@ public class Bar } [Fact] - public async Task When_classes_have_extension_code_then_class_body_is_copied() + public void When_classes_have_extension_code_then_class_body_is_copied() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { ExtendedClasses = ["Foo", "Bar"], @@ -105,7 +105,7 @@ public async Task When_classes_have_extension_code_then_class_body_is_copied() }); var code = generator.GenerateFile(); - //// Assert + // Assert Assert.DoesNotContain("FooBase", code); Assert.DoesNotContain("BarBase", code); Assert.DoesNotContain("generated.", code); @@ -117,7 +117,7 @@ public async Task When_classes_have_extension_code_then_class_body_is_copied() [Fact] public void When_extension_code_has_comment_then_it_is_processed_correctly() { - //// Arrange + // Arrange var code = @"// This is the class that uses HTTP export class UseHttpCookiesForApi { @@ -127,10 +127,10 @@ protected transformOptions(options: RequestInit): Promise { } }"; - //// Act + // Act var extensionCode = new TypeScriptExtensionCode(code, [], ["UseHttpCookiesForApi"]); - //// Assert + // Assert Assert.Empty(extensionCode.ExtensionClasses); Assert.DoesNotContain("UseHttpCookiesForApi", extensionCode.BottomCode); Assert.Contains("UseHttpCookiesForApi", extensionCode.TopCode); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs index 58530f9a9..41b6d9797 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs @@ -26,9 +26,9 @@ public sealed class EmptyClassInheritingDictionary : Dictionary [InlineData(true, true)] [InlineData(false, false)] [InlineData(true, false)] - public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works(bool inlineNamedDictionaries, bool convertConstructorInterfaceData) + public void When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works(bool inlineNamedDictionaries, bool convertConstructorInterfaceData) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); @@ -39,10 +39,10 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan ConvertConstructorInterfaceData = convertConstructorInterfaceData }); - //// Act + // Act var code = generator.GenerateFile("ContainerClass"); - //// Assert + // Assert var dschema = schema.Definitions["EmptyClassInheritingDictionary"]; Assert.Empty(dschema.AllOf); @@ -93,18 +93,18 @@ public class ExceptionContainer } [Fact] - public async Task When_class_with_discriminator_has_base_class_then_csharp_is_generated_correctly() + public void When_class_with_discriminator_has_base_class_then_csharp_is_generated_correctly() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("Foobar.", data); Assert.Contains("Foobar.", code); @@ -116,9 +116,9 @@ public async Task When_class_with_discriminator_has_base_class_then_csharp_is_ge } [Fact] - public async Task When_interfaces_are_generated_with_inheritance_then_type_check_methods_are_generated() + public void When_interfaces_are_generated_with_inheritance_then_type_check_methods_are_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); @@ -129,18 +129,18 @@ public async Task When_interfaces_are_generated_with_inheritance_then_type_check GenerateTypeCheckFunctions = true }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("export function isExceptionBase(object: any): object is ExceptionBase {", code); Assert.Contains("function isMyException(object: any): object is MyException {", code); } [Fact] - public async Task When_discriminator_does_not_match_typename_then_TypeScript_is_correct() + public void When_discriminator_does_not_match_typename_then_TypeScript_is_correct() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.Definitions["ExceptionBase"].AllOf.Last().DiscriminatorObject.Mapping["FooBar"] = @@ -150,10 +150,10 @@ public async Task When_discriminator_does_not_match_typename_then_TypeScript_is_ var data = schema.ToJson(); var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.Contains("class ExceptionBase extends Exception", code); Assert.Contains("class MyException extends ExceptionBase", code); @@ -237,13 +237,13 @@ public async Task When_schema_with_inheritance_to_object_type_is_generated_then_ }"; var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); - var schema = await JsonSchemaSerialization.FromJsonAsync(json, schemaType, null, factory, new DefaultContractResolver()); + var schema = await JsonSchemaSerialization.FromJsonAsync(json, schemaType, null, factory, new DefaultContractResolver(), CancellationToken.None); var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m, SchemaType = schemaType }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.DoesNotContain("request!: any;", code); Assert.DoesNotContain("request: any;", code); Assert.Contains("this.request = new RequestBodyBase()", code); @@ -328,21 +328,21 @@ public async Task When_schema_with_inheritance_and_references_is_generated_then_ "; var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); - var schema = await JsonSchemaSerialization.FromJsonAsync(json, schemaType, null, factory, new DefaultContractResolver()); + var schema = await JsonSchemaSerialization.FromJsonAsync(json, schemaType, null, factory, new DefaultContractResolver(), CancellationToken.None); var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m, SchemaType = schemaType }); - //// Act + // Act var code = generator.GenerateFile(); - //// Assert + // Assert Assert.DoesNotContain("SkillLevel2", code); } [Fact] - public async Task When_class_has_baseclass_and_extension_code_baseclass_is_preserved() + public void When_class_has_baseclass_and_extension_code_baseclass_is_preserved() { - //// Arrange + // Arrange string extensionCode = @" import * as generated from ""./generated""; @@ -358,10 +358,10 @@ export class ExceptionBase extends generated.ExceptionBase { ExtendedClasses = ["ExceptionBase"], }); - //// Act + // Act var output = generator.GenerateTypes(schema, null); - //// Assert + // Assert var outputArray = output.ToArray(); Assert.Equal(4, outputArray.Length); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NJsonSchema.CodeGeneration.TypeScript.Tests.csproj b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NJsonSchema.CodeGeneration.TypeScript.Tests.csproj index 80b8c0c9e..efd540d19 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NJsonSchema.CodeGeneration.TypeScript.Tests.csproj +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NJsonSchema.CodeGeneration.TypeScript.Tests.csproj @@ -3,9 +3,9 @@ net8.0;net472 false - $(NoWarn),1587,1998,1591,618 disable - false + latest-Minimal + $(NoWarn);1591 diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NullabilityTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NullabilityTests.cs index fd48bfeab..347c666e9 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NullabilityTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/NullabilityTests.cs @@ -8,7 +8,7 @@ namespace NJsonSchema.CodeGeneration.TypeScript.Tests public class NullabilityTests { [Fact] - public async Task Strict_nullability_in_TypeScript2() + public void Strict_nullability_in_TypeScript2() { var schema = NewtonsoftJsonSchemaGenerator.FromType( new NewtonsoftJsonSchemaGeneratorSettings @@ -168,7 +168,7 @@ public class ClassWithNullableArrayItems } [Fact] - public async Task When_array_item_is_nullable_then_generated_TypeScript_is_correct() + public void When_array_item_is_nullable_then_generated_TypeScript_is_correct() { // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDictionaryTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDictionaryTests.cs index 6dbd6e78d..9bd83a9f7 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDictionaryTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDictionaryTests.cs @@ -20,13 +20,13 @@ public class EnumKeyDictionaryTest } [Fact] - public async Task When_dictionary_key_is_enum_then_typescript_has_string_key() + public void When_dictionary_key_is_enum_then_typescript_has_string_key() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -34,36 +34,36 @@ public async Task When_dictionary_key_is_enum_then_typescript_has_string_key() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("Mapping: { [key: string]: string; };", code); Assert.Contains("Mapping2: { [key: string]: string; };", code); } [Fact] - public async Task When_dictionary_key_is_enum_then_typescript_has_enum_key_ts_2_1() + public void When_dictionary_key_is_enum_then_typescript_has_enum_key_ts_2_1() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, TypeScriptVersion = 2.1m }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export enum PropertyName {\n Name = 0,\n Gender = 1,\n}", code); Assert.Contains("Mapping: { [key in keyof typeof PropertyName]?: string; } | undefined;", code); Assert.Contains("Mapping2: { [key in keyof typeof PropertyName]?: string; } | undefined;", code); } [Fact] - public async Task When_dictionary_key_is_string_literal_then_typescript_has_string_literal_key_ts_2_1() + public void When_dictionary_key_is_string_literal_then_typescript_has_string_literal_key_ts_2_1() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -73,7 +73,7 @@ public async Task When_dictionary_key_is_string_literal_then_typescript_has_stri }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export type PropertyName = 0 | 1;", code); Assert.Contains("Mapping: { [key in PropertyName]?: string; } | undefined;", code); Assert.Contains("Mapping2: { [key in PropertyName]?: string; } | undefined;", code); @@ -94,13 +94,13 @@ public class EnumValueDictionaryTest } [Fact] - public async Task When_dictionary_value_is_enum_then_typescript_has_enum_value() + public void When_dictionary_value_is_enum_then_typescript_has_enum_value() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -108,7 +108,7 @@ public async Task When_dictionary_value_is_enum_then_typescript_has_enum_value() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("Mapping: { [key: string]: Gender; };", code); Assert.Contains("Mapping2: { [key: string]: Gender; };", code); } @@ -123,7 +123,7 @@ public class ObjectValueDictionaryTest [Fact] public async Task When_dictionary_value_is_object_then_typescript_has_any_value() { - //// Arrange + // Arrange var json = @" { ""required"": [ ""resource"" ], @@ -151,7 +151,7 @@ public async Task When_dictionary_value_is_object_then_typescript_has_any_value( } }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, @@ -161,7 +161,7 @@ public async Task When_dictionary_value_is_object_then_typescript_has_any_value( }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("extensions: { [key: string]: any; } | undefined;", code); Assert.DoesNotContain("extensions?: { [key: string]: Iany; } | null;", code); Assert.DoesNotContain("this.extensions[key] = item && !(item).toJSON ? new any(item) : item;", code); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs index e132121d2..e59a26c1a 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptDiscriminatorTests.cs @@ -47,7 +47,7 @@ public class Nested [Fact] public async Task When_generating_interface_contract_add_discriminator() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { GenerateAbstractProperties = true, @@ -55,7 +55,7 @@ public async Task When_generating_interface_contract_add_discriminator() var data = schema.ToJson(); var json = JsonConvert.SerializeObject(new OneChild()); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -63,7 +63,7 @@ public async Task When_generating_interface_contract_add_discriminator() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export interface Base {\n Type: EBase;\n}", code); await VerifyHelper.Verify(code); } @@ -71,14 +71,14 @@ public async Task When_generating_interface_contract_add_discriminator() [Fact] public async Task When_generating_interface_contract_add_discriminator_string_literal() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { GenerateAbstractProperties = true, }); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -87,7 +87,7 @@ public async Task When_generating_interface_contract_add_discriminator_string_li }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export interface Base {\n Type: EBase;\n}", code); await VerifyHelper.Verify(code); } @@ -95,11 +95,11 @@ public async Task When_generating_interface_contract_add_discriminator_string_li [Fact] public async Task When_parameter_is_abstract_then_generate_union_interface() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { UseLeafType = true, @@ -108,7 +108,7 @@ public async Task When_parameter_is_abstract_then_generate_union_interface() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export interface OneChild extends Base", code); Assert.Contains("export interface SecondChild extends Base", code); Assert.Contains("Child: OneChild | SecondChild;", code); @@ -119,11 +119,11 @@ public async Task When_parameter_is_abstract_then_generate_union_interface() [Fact] public async Task When_parameter_is_abstract_then_generate_union_class() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { UseLeafType = true, @@ -132,7 +132,7 @@ public async Task When_parameter_is_abstract_then_generate_union_class() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("export class OneChild extends Base", code); Assert.Contains("export class SecondChild extends Base", code); Assert.Contains("child: OneChild | SecondChild;", code); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptGeneratorTests.cs index 9ed39cf82..849aa99fd 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptGeneratorTests.cs @@ -9,7 +9,7 @@ public class TypeScriptGeneratorTests [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', @@ -31,7 +31,7 @@ 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 TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { @@ -40,7 +40,7 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a }); var code = generator.GenerateFile("Foo").Replace("\r\n", "\n"); - //// Assert + // Assert Assert.Contains(@"export class Foo extends Anonymous implements IFoo { prop1: string; prop2: string; @@ -51,7 +51,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', @@ -73,25 +73,25 @@ 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 TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class }); var code = generator.GenerateFile("Foo"); - //// Assert + // Assert Assert.Contains("class Foo extends Bar", code); } [Fact] public async Task When_property_name_does_not_match_property_name_then_casing_is_correct_in_output() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); - //// Act + // Act var output = generator.GenerateFile("Teacher"); - //// Assert + // Assert Assert.Contains(@"lastName: string;", output); Assert.Contains(@"Dictionary: { [key: string]: number; };", output); } @@ -99,41 +99,41 @@ public async Task When_property_name_does_not_match_property_name_then_casing_is [Fact] public async Task When_property_is_required_name_then_TypeScript_property_is_not_optional() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); - //// Act + // Act var output = generator.GenerateFile("Teacher"); - //// Assert + // Assert Assert.Contains(@"FirstName: string;", output); } [Fact] public async Task When_allOf_contains_one_schema_then_csharp_inheritance_is_generated() { - //// Arrange + // Arrange var generator = await CreateGeneratorAsync(); - //// Act + // Act var output = generator.GenerateFile("Teacher"); - //// Assert + // Assert Assert.Contains(@"interface Teacher extends Person", output); } [Fact] - public async Task When_enum_has_description_then_typescript_has_comment() + public void When_enum_has_description_then_typescript_has_comment() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.AllOf.First().ActualSchema.Properties["Gender"].Description = "EnumDesc."; var generator = new TypeScriptGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"/** EnumDesc. *", output); } @@ -143,7 +143,7 @@ public async Task When_enum_has_description_then_typescript_has_comment() [Fact] public async Task When_enum_has_default_and_using_enumstyle_stringliteral_it_defaults_to_stringliteral() { - //// Arrange + // Arrange var jsonSchema = @" { ""$schema"": ""http://json-schema.org/draft-04/schema#"", @@ -166,10 +166,10 @@ public async Task When_enum_has_default_and_using_enumstyle_stringliteral_it_def TypeScriptVersion = 5m, }); - //// Act + // Act var code = generator.GenerateFile("MyFile"); - //// Assert + // Assert Assert.Contains("export type MyFileColor = \"red\" | \"green\" | \"blue\" | \"black\";", code); Assert.Contains("this.color = _data[\"color\"] !== undefined ? _data[\"color\"] : \"green\";", code); Assert.Contains("this.color = \"green\";", code); @@ -179,41 +179,41 @@ public async Task When_enum_has_default_and_using_enumstyle_stringliteral_it_def } [Fact] - public async Task When_class_has_description_then_typescript_has_comment() + public void When_class_has_description_then_typescript_has_comment() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.Description = "ClassDesc."; var generator = new TypeScriptGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"/** ClassDesc. *", output); } [Fact] - public async Task When_property_has_description_then_csharp_has_xml_comment() + public void When_property_has_description_then_csharp_has_xml_comment() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); schema.ActualProperties["Class"].Description = "PropertyDesc."; var json = schema.ToJson(); var generator = new TypeScriptGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"/** PropertyDesc. *", output); } [Fact] - public async Task When_property_is_readonly_then_ts_property_is_also_readonly() + public void When_property_is_readonly_then_ts_property_is_also_readonly() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { @@ -221,17 +221,17 @@ public async Task When_property_is_readonly_then_ts_property_is_also_readonly() TypeScriptVersion = 2.0m }); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"readonly Birthday", output); } [Fact] public void When_name_contains_dash_then_it_is_converted_to_upper_case() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Properties["foo-bar"] = new JsonSchemaProperty { @@ -240,29 +240,29 @@ public void When_name_contains_dash_then_it_is_converted_to_upper_case() var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface }); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains(@"""foo-bar"": string;", output); } [Fact] public void When_type_name_is_missing_then_anonymous_name_is_generated() { - //// Arrange + // Arrange var schema = new JsonSchema(); var generator = new TypeScriptGenerator(schema); - //// Act + // Act var output = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain(@"interface {", output); } - private static async Task CreateGeneratorAsync() + private static Task CreateGeneratorAsync() { var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); @@ -271,13 +271,13 @@ private static async Task CreateGeneratorAsync() TypeStyle = TypeScriptTypeStyle.Interface, TypeScriptVersion = 1.8m }); - return generator; + return Task.FromResult(generator); } [Fact] public async Task When_patternProperties_is_set_with_string_value_type_then_correct_dictionary_is_generated() { - //// Arrange + // Arrange var schemaJson = @"{ ""required"": [ ""dict"" ], ""properties"": { @@ -295,11 +295,11 @@ public async Task When_patternProperties_is_set_with_string_value_type_then_corr var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("dict: { [key: string]: string; };", code); // property not nullable Assert.Contains("this.dict = {};", code); // must be initialized with {} } @@ -386,18 +386,18 @@ public async Task When_default_is_generated_then_no_liquid_error_is_in_output() var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.DoesNotContain("Liquid error: ", code); } [Fact] - public async Task When_a_nullable_array_property_exists_and_typestyle_is_null_then_init_should_assign_null() + public void When_a_nullable_array_property_exists_and_typestyle_is_null_then_init_should_assign_null() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -416,7 +416,7 @@ public async Task When_a_nullable_array_property_exists_and_typestyle_is_null_th } }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { NullValue = TypeScriptNullValue.Null, @@ -425,16 +425,16 @@ public async Task When_a_nullable_array_property_exists_and_typestyle_is_null_th }); var code = generator.GenerateFile("Foo").Replace("\r\n", "\n"); - //// Assert + // Assert Assert.Matches(new Regex( @"init\(.*\)\s{.*}\s*else\s{\s*this\.prop\s=\snull;\s*}", RegexOptions.Singleline), code); } [Fact] - public async Task When_a_nullable_dict_property_exists_and_typestyle_is_null_then_init_should_assign_null() + public void When_a_nullable_dict_property_exists_and_typestyle_is_null_then_init_should_assign_null() { - //// Arrange + // Arrange var schema = new JsonSchema { Properties = @@ -453,7 +453,7 @@ public async Task When_a_nullable_dict_property_exists_and_typestyle_is_null_the } }; - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { NullValue = TypeScriptNullValue.Null, @@ -462,7 +462,7 @@ public async Task When_a_nullable_dict_property_exists_and_typestyle_is_null_the }); var code = generator.GenerateFile("Foo").Replace("\r\n", "\n"); - //// Assert + // Assert Assert.Matches(new Regex( @"init\(.*\)\s{.*}\s*else\s{\s*this\.prop\s=\snull;\s*}", RegexOptions.Singleline), code); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptObjectTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptObjectTests.cs index 26677b412..96d91e762 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptObjectTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/TypeScriptObjectTests.cs @@ -10,13 +10,13 @@ public class ObjectTest } [Fact] - public async Task When_property_is_object_then_jsonProperty_has_no_reference_and_is_any() + public void When_property_is_object_then_jsonProperty_has_no_reference_and_is_any() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -24,7 +24,7 @@ public async Task When_property_is_object_then_jsonProperty_has_no_reference_and }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("Test: any;", code); } @@ -34,13 +34,13 @@ public class DictionaryObjectTest } [Fact] - public async Task When_dictionary_value_is_object_then_typescript_uses_any() + public void When_dictionary_value_is_object_then_typescript_uses_any() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, @@ -48,7 +48,7 @@ public async Task When_dictionary_value_is_object_then_typescript_uses_any() }); var code = generator.GenerateFile("MyClass"); - //// Assert + // Assert Assert.Contains("Test: { [key: string]: any; };", code); } } diff --git a/src/NJsonSchema.Demo.Performance/NJsonSchema.Demo.Performance.csproj b/src/NJsonSchema.Demo.Performance/NJsonSchema.Demo.Performance.csproj index d85c7955c..daa29b2f9 100644 --- a/src/NJsonSchema.Demo.Performance/NJsonSchema.Demo.Performance.csproj +++ b/src/NJsonSchema.Demo.Performance/NJsonSchema.Demo.Performance.csproj @@ -1,15 +1,15 @@  - - Exe - net8.0 - false - false - + + Exe + net8.0 + false + $(NoWarn);1591;CS8618 + - - - - + + + + diff --git a/src/NJsonSchema.Demo.Performance/Program.cs b/src/NJsonSchema.Demo.Performance/Program.cs index b6fcd0cd0..9bd103e01 100644 --- a/src/NJsonSchema.Demo.Performance/Program.cs +++ b/src/NJsonSchema.Demo.Performance/Program.cs @@ -6,7 +6,7 @@ namespace NJsonSchema.Demo.Performance { public class Program { - static void Main(string[] args) + private static void Main(string[] args) { Run().GetAwaiter().GetResult(); } diff --git a/src/NJsonSchema.NewtonsoftJson.Tests/Generation/RecordTests.cs b/src/NJsonSchema.NewtonsoftJson.Tests/Generation/RecordTests.cs index 3a9a8c19e..094b8c12c 100644 --- a/src/NJsonSchema.NewtonsoftJson.Tests/Generation/RecordTests.cs +++ b/src/NJsonSchema.NewtonsoftJson.Tests/Generation/RecordTests.cs @@ -13,14 +13,14 @@ public record Address [Fact] public void Should_have_only_one_property() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType
(); var data = schema.ToJson(); - //// Assert - Assert.Equal(1, schema.Properties.Count); + // Assert + Assert.Single(schema.Properties); } } } \ No newline at end of file diff --git a/src/NJsonSchema.NewtonsoftJson.Tests/Generation/StructTests.cs b/src/NJsonSchema.NewtonsoftJson.Tests/Generation/StructTests.cs index 5e2debd62..b3a563601 100644 --- a/src/NJsonSchema.NewtonsoftJson.Tests/Generation/StructTests.cs +++ b/src/NJsonSchema.NewtonsoftJson.Tests/Generation/StructTests.cs @@ -24,14 +24,14 @@ public class UserDefinedClass [Fact] public void Should_have_a_shared_struct_schema() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert - Assert.Equal(1, schema.Definitions.Count); + // Assert + Assert.Single(schema.Definitions); } } } \ No newline at end of file diff --git a/src/NJsonSchema.NewtonsoftJson.Tests/NJsonSchema.NewtonsoftJson.Tests.csproj b/src/NJsonSchema.NewtonsoftJson.Tests/NJsonSchema.NewtonsoftJson.Tests.csproj index 76f205d3c..55ef72720 100644 --- a/src/NJsonSchema.NewtonsoftJson.Tests/NJsonSchema.NewtonsoftJson.Tests.csproj +++ b/src/NJsonSchema.NewtonsoftJson.Tests/NJsonSchema.NewtonsoftJson.Tests.csproj @@ -3,8 +3,8 @@ net8.0;net472 false - false disable + latest-Minimal diff --git a/src/NJsonSchema.Tests/Conversion/ArrayTypeToSchemaTests.cs b/src/NJsonSchema.Tests/Conversion/ArrayTypeToSchemaTests.cs index 60eaedb89..d27f0900a 100644 --- a/src/NJsonSchema.Tests/Conversion/ArrayTypeToSchemaTests.cs +++ b/src/NJsonSchema.Tests/Conversion/ArrayTypeToSchemaTests.cs @@ -20,7 +20,7 @@ public class DictionaryType : Dictionary> [Fact] public async Task When_converting_type_inheriting_from_dictionary_then_it_should_be_correct() { - //// Act + // Act var dict = new DictionarySubType(); dict.Foo = "abc"; dict.Add("bar", new List { "a", "b" }); @@ -29,7 +29,7 @@ public async Task When_converting_type_inheriting_from_dictionary_then_it_should var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Object, schema.ActualTypeSchema.Type); Assert.DoesNotContain("Foo", json); Assert.DoesNotContain("foo", json); @@ -87,11 +87,11 @@ public class MySubtype private async Task When_converting_smth_then_items_must_correctly_be_loaded(string propertyName) { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert var property = schema.Properties[propertyName]; Assert.Equal(JsonObjectType.Array | JsonObjectType.Null, property.Type); diff --git a/src/NJsonSchema.Tests/Conversion/SchemaReferenceTests.cs b/src/NJsonSchema.Tests/Conversion/SchemaReferenceTests.cs index 3e5baf01b..75d4d7eb7 100644 --- a/src/NJsonSchema.Tests/Conversion/SchemaReferenceTests.cs +++ b/src/NJsonSchema.Tests/Conversion/SchemaReferenceTests.cs @@ -8,58 +8,58 @@ public class SchemaReferenceTests [Fact] public async Task When_converting_a_circular_referencing_person_type_then_references_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(schema, schema.Properties["Car"].ActualTypeSchema.Properties["Person"].ActualTypeSchema); } [Fact] public async Task When_converting_a_circular_referencing_car_type_then_references_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(schema, schema.Properties["Person"].ActualTypeSchema.Properties["Car"].ActualTypeSchema); } [Fact] public async Task When_converting_a_referencing_type_then_path_is_in_json() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Contains(@"""$ref"": ""#""", json); } [Fact] public async Task When_converting_a_referencing_type_then_absolute_reference_path_is_in_json() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Contains(@"""$ref"": ""#/definitions/Person", json); } [Fact] public async Task When_ref_is_nested_then_it_should_be_resolved() { - /// Arrange + // Arrange var json = @" { ""$schema"": ""http://json-schema.org/draft-04/schema#"", @@ -84,10 +84,10 @@ public async Task When_ref_is_nested_then_it_should_be_resolved() } }"; - /// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - /// Assert + // Assert var jsonOutput = schema.ToJson(); Assert.NotNull(jsonOutput); } diff --git a/src/NJsonSchema.Tests/Conversion/TypeToSchemaTests.cs b/src/NJsonSchema.Tests/Conversion/TypeToSchemaTests.cs index f0ffbd0ba..e6dc5b0b0 100644 --- a/src/NJsonSchema.Tests/Conversion/TypeToSchemaTests.cs +++ b/src/NJsonSchema.Tests/Conversion/TypeToSchemaTests.cs @@ -13,26 +13,26 @@ public class TypeToSchemaTests [Fact] public async Task When_converting_in_round_trip_then_json_should_be_the_same() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - /// Act + // Act var schemaData1 = JsonConvert.SerializeObject(schema, Formatting.Indented); var schema2 = JsonConvert.DeserializeObject(schemaData1); var schemaData2 = JsonConvert.SerializeObject(schema2, Formatting.Indented); - //// Assert + // Assert Assert.Equal(schemaData1, schemaData2); } [Fact] public async Task When_converting_simple_property_then_property_must_be_in_schema() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["Integer"].Type); Assert.Equal(JsonObjectType.Number, schema.Properties["Decimal"].Type); Assert.Equal(JsonObjectType.Number, schema.Properties["Double"].Type); @@ -44,10 +44,10 @@ public async Task When_converting_simple_property_then_property_must_be_in_schem [Fact] public async Task When_converting_nullable_simple_property_then_property_must_be_in_schema() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer | JsonObjectType.Null, schema.Properties["NullableInteger"].Type); Assert.Equal(JsonObjectType.Number | JsonObjectType.Null, schema.Properties["NullableDecimal"].Type); Assert.Equal(JsonObjectType.Number | JsonObjectType.Null, schema.Properties["NullableDouble"].Type); @@ -57,40 +57,40 @@ public async Task When_converting_nullable_simple_property_then_property_must_be [Fact] public async Task When_converting_property_with_description_then_description_should_be_in_schema() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal("Test", schema.Properties["Integer"].Description); } [Fact] public async Task When_converting_required_property_then_it_should_be_required_in_schema() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties["RequiredReference"].IsRequired); } [Fact] public async Task When_converting_regex_property_then_it_should_be_set_as_pattern() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal("regex", schema.Properties["RegexString"].Pattern); } [Fact] public async Task When_converting_range_property_then_it_should_be_set_as_min_max() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(5, schema.Properties["RangeInteger"].Minimum); Assert.Equal(10, schema.Properties["RangeInteger"].Maximum); } @@ -98,10 +98,10 @@ public async Task When_converting_range_property_then_it_should_be_set_as_min_ma [Fact] public async Task When_converting_not_nullable_properties_then_they_should_have_null_type() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.False(schema.Properties["Integer"].IsRequired); Assert.False(schema.Properties["Decimal"].IsRequired); Assert.False(schema.Properties["Double"].IsRequired); @@ -118,10 +118,10 @@ public async Task When_converting_not_nullable_properties_then_they_should_have_ [Fact] public async Task When_generating_nullable_primitive_properties_then_they_should_have_null_type() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties["NullableInteger"].Type.HasFlag(JsonObjectType.Null)); Assert.True(schema.Properties["NullableDecimal"].Type.HasFlag(JsonObjectType.Null)); Assert.True(schema.Properties["NullableDouble"].Type.HasFlag(JsonObjectType.Null)); @@ -131,10 +131,10 @@ public async Task When_generating_nullable_primitive_properties_then_they_should [Fact] public async Task When_property_is_renamed_then_the_name_must_be_correct() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("abc")); Assert.False(schema.Properties.ContainsKey("ChangedName")); } @@ -142,11 +142,11 @@ public async Task When_property_is_renamed_then_the_name_must_be_correct() [Fact] public async Task When_converting_object_then_it_should_be_correct() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert var property = schema.Properties["Reference"]; Assert.True(property.IsNullable(SchemaType.JsonSchema)); Assert.Contains(schema.Definitions, d => d.Key == "MySubtype"); @@ -155,11 +155,11 @@ public async Task When_converting_object_then_it_should_be_correct() [Fact] public async Task When_converting_enum_then_enum_array_must_be_set() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var property = schema.Properties["Color"]; - //// Assert + // Assert Assert.Equal(3, property.ActualTypeSchema.Enumeration.Count); // Color property has StringEnumConverter Assert.True(property.ActualTypeSchema.Enumeration.Contains("Red")); Assert.True(property.ActualTypeSchema.Enumeration.Contains("Green")); @@ -174,16 +174,16 @@ public class ClassWithJObjectProperty [Fact] public async Task When_type_is_JObject_then_generated_type_is_any() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); var property = schema.Properties["Property"]; - //// Assert + // Assert Assert.True(property.IsNullable(SchemaType.JsonSchema)); Assert.True(property.ActualTypeSchema.IsAnyType); Assert.True(property.ActualTypeSchema.AllowAdditionalItems); - Assert.Equal(0, property.Properties.Count); + Assert.Empty(property.Properties); } [Fact] @@ -206,10 +206,10 @@ public async Task When_converting_list_then_items_must_correctly_be_loaded() private async Task When_converting_smth_then_items_must_correctly_be_loaded(string propertyName) { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert var property = schema.Properties[propertyName]; Assert.Equal(JsonObjectType.Array | JsonObjectType.Null, property.Type); diff --git a/src/NJsonSchema.Tests/Converters/JsonExceptionConverterTests.cs b/src/NJsonSchema.Tests/Converters/JsonExceptionConverterTests.cs index 43d5664fc..e01a0a937 100644 --- a/src/NJsonSchema.Tests/Converters/JsonExceptionConverterTests.cs +++ b/src/NJsonSchema.Tests/Converters/JsonExceptionConverterTests.cs @@ -29,7 +29,7 @@ public class JsonExceptionConverterTests [Fact(Skip = "Broken with Newtonsoft.Json v13")] public void When_custom_exception_is_serialized_then_everything_works() { - //// Arrange + // Arrange var settings = CreateSettings(); try { @@ -41,12 +41,12 @@ public void When_custom_exception_is_serialized_then_everything_works() } catch (CompanyNotFoundException exception) { - //// Act + // Act var json = JsonConvert.SerializeObject(exception, settings); var newException = JsonConvert.DeserializeObject(json, settings) as CompanyNotFoundException; var newJson = JsonConvert.SerializeObject(newException, settings); - //// Assert + // Assert Assert.Equal(exception.CompanyKey, newException.CompanyKey); Assert.Equal(exception.Message, newException.Message); @@ -61,7 +61,7 @@ public void When_custom_exception_is_serialized_then_everything_works() [Fact] public void When_stack_trace_hiding_is_enabled_then_stack_trace_is_HIDDEN() { - //// Arrange + // Arrange var settings = CreateSettings(true); try { @@ -69,19 +69,19 @@ public void When_stack_trace_hiding_is_enabled_then_stack_trace_is_HIDDEN() } catch (CompanyNotFoundException exception) { - //// Act + // Act var json = JsonConvert.SerializeObject(exception, settings); var newException = JsonConvert.DeserializeObject(json, settings) as CompanyNotFoundException; - //// Assert + // Assert Assert.Equal("HIDDEN", newException.StackTrace); } } [Fact(Skip = "Broken with Newtonsoft.Json v13")] - public void JsonExceptionConverter_is_thread_safe() + public async Task JsonExceptionConverter_is_thread_safe() { - //// Arrange + // Arrange var tasks = new List(); for (int i = 0; i < 100; i++) { @@ -91,10 +91,10 @@ public void JsonExceptionConverter_is_thread_safe() })); } - //// Act - Task.WaitAll(tasks.ToArray()); + // Act + await Task.WhenAll(tasks.ToArray()); - //// Assert + // Assert // No exceptions } @@ -119,7 +119,7 @@ private static JsonSerializerSettings CreateSettings(bool hideStackTrace = false [Fact(Skip = "Broken with Newtonsoft.Json v13")] public void When_ArgumentException_is_thrown_then_it_is_serialized_with_all_properties() { - //// Arrange + // Arrange var settings = CreateSettings(); try @@ -128,12 +128,12 @@ public void When_ArgumentException_is_thrown_then_it_is_serialized_with_all_prop } catch (ArgumentException exception) { - //// Act + // Act var json = JsonConvert.SerializeObject(exception, settings); var newException = JsonConvert.DeserializeObject(json, settings) as ArgumentException; var newJson = JsonConvert.SerializeObject(newException, settings); - //// Assert + // Assert Assert.Equal(exception.ParamName, newException.ParamName); } } @@ -141,7 +141,7 @@ public void When_ArgumentException_is_thrown_then_it_is_serialized_with_all_prop [Fact(Skip = "Broken with Newtonsoft.Json v13")] public void When_InvalidOperationException_is_thrown_then_it_is_serialized_with_all_properties() { - //// Arrange + // Arrange var settings = CreateSettings(); try @@ -150,12 +150,12 @@ public void When_InvalidOperationException_is_thrown_then_it_is_serialized_with_ } catch (InvalidOperationException exception) { - //// Act + // Act var json = JsonConvert.SerializeObject(exception, settings); var newException = JsonConvert.DeserializeObject(json, settings) as InvalidOperationException; var newJson = JsonConvert.SerializeObject(newException, settings); - //// Assert + // Assert Assert.Equal(exception.Message, newException.Message); } } @@ -173,7 +173,7 @@ public class Person [Fact(Skip = "Broken with Newtonsoft.Json v13")] public void When_ArgumentOutOfRangeException_is_thrown_then_it_is_serialized_with_all_properties() { - //// Arrange + // Arrange var settings = CreateSettings(); try @@ -182,12 +182,12 @@ public void When_ArgumentOutOfRangeException_is_thrown_then_it_is_serialized_wit } catch (ArgumentOutOfRangeException exception) { - //// Act + // Act var json = JsonConvert.SerializeObject(exception, settings); var newException = JsonConvert.DeserializeObject(json, settings) as ArgumentOutOfRangeException; var newJson = JsonConvert.SerializeObject(newException, settings); - //// Assert + // Assert Assert.NotNull(newException.ActualValue); Assert.Equal(exception.ParamName, newException.ParamName); } diff --git a/src/NJsonSchema.Tests/Generation/AbstractGenerationTests.cs b/src/NJsonSchema.Tests/Generation/AbstractGenerationTests.cs index c058db8e4..e4881dc11 100644 --- a/src/NJsonSchema.Tests/Generation/AbstractGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/AbstractGenerationTests.cs @@ -13,11 +13,11 @@ public abstract class AbstractClass [Fact] public async Task When_class_is_abstract_then_is_abstract_is_true() { - /// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - /// Assert + // Assert Assert.Contains("x-abstract", json); Assert.True(schema.IsAbstract); } @@ -30,11 +30,11 @@ public class NotAbstractClass [Fact] public async Task When_class_is_not_abstract_then_is_abstract_is_false() { - /// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - /// Assert + // Assert Assert.DoesNotContain("x-abstract", json); Assert.False(schema.IsAbstract); } diff --git a/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs b/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs index b34cc5985..609e539dd 100644 --- a/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs @@ -27,14 +27,14 @@ public class MyPoint [Fact] public async Task When_class_annotation_is_available_then_type_and_format_can_be_customized() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var property = schema.Properties["Point"]; - //// Assert + // Assert Assert.True(property.Type.HasFlag(JsonObjectType.String)); Assert.Equal("point", property.Format); } @@ -42,14 +42,14 @@ public async Task When_class_annotation_is_available_then_type_and_format_can_be [Fact] public async Task When_property_annotation_is_available_then_type_and_format_can_be_customized() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var property = schema.Properties["ClassAsString"]; - //// Assert + // Assert Assert.True(property.Type.HasFlag(JsonObjectType.String)); Assert.Equal("point", property.Format); } @@ -63,14 +63,14 @@ public class DateAttributeClass [Fact] public async Task When_DateTime_property_has_JsonSchemaDate_attribute_then_format_and_type_is_correct() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Act + // Act var property = schema.Properties["Date"]; - //// Assert + // Assert Assert.True(property.Type.HasFlag(JsonObjectType.String)); Assert.Equal("date", property.Format); } @@ -84,13 +84,13 @@ public class MultipleOfClass [Fact] public async Task When_multipleOf_attribute_is_available_then_value_is_set_in_schema() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Number"]; - //// Assert + // Assert Assert.Equal(4.5m, property.MultipleOf.Value); } @@ -108,7 +108,7 @@ public SimpleClass(decimal number) [Fact] public async Task When_multipleOf_is_fraction_then_it_is_validated_correctly() { - //// Arrange + // Arrange List testClasses = []; for (int i = 0; i < 100; i++) { @@ -135,11 +135,11 @@ public async Task When_multipleOf_is_fraction_then_it_is_validated_correctly() } }"); - //// Act + // Act var errors = schema.Validate(jsonData); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [JsonSchema(JsonObjectType.Array, ArrayItem = typeof(string))] @@ -159,13 +159,13 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() [Fact] public async Task When_class_has_array_item_type_defined_then_schema_has_this_item_type() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Item.Type); } @@ -177,13 +177,13 @@ public class ArrayModel : List [Fact] public async Task When_class_has_array_item_type_defined_then_schema_has_this_item_type2() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(); - //// Act + // Act var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Item.Type); } @@ -202,13 +202,13 @@ public struct MyStruct [Fact] public async Task When_property_is_struct_then_it_is_not_nullable() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Struct"].Type); Assert.Equal(JsonObjectType.String | JsonObjectType.Null, schema.Properties["NullableStruct"].Type); } @@ -222,12 +222,12 @@ public class StringLengthAttributeClass [Fact] public async Task When_StringLengthAttribute_is_set_then_minLength_and_maxLenght_is_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert var property = schema.Properties["Foo"]; Assert.Equal(5, property.MinLength); @@ -285,12 +285,12 @@ public class StringRequiredClass [Fact] public async Task When_RequiredAttribute_is_set_with_AllowEmptyStrings_false_then_minLength_and_required_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert var property = schema.Properties["Foo"]; Assert.Equal(1, property.MinLength); @@ -306,13 +306,13 @@ public class DtoRequiredClass [Fact] public async Task When_RequiredAttribute_is_set_with_AllowEmptyStrings_false_on_class_property_then_minLength_is_not_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert var property = schema.Properties["Foo"]; Assert.Null(property.MinLength); @@ -385,11 +385,11 @@ public class Student : Person [Fact] public async Task When_class_is_ignored_then_it_is_not_in_definitions() { - /// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - /// Assert + // Assert Assert.False(schema.Definitions.ContainsKey("BaseObject")); } } diff --git a/src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs b/src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs index 145badb3c..caaad2c92 100644 --- a/src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs @@ -16,11 +16,11 @@ public class ClassWithJArray [Fact] public async Task When_property_is_JArray_then_schema_with_any_array_is_generated() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(2, schema.ActualProperties.Count); var arrayProperty = schema.ActualProperties["Array"].ActualTypeSchema; Assert.Equal(JsonObjectType.Array, arrayProperty.Type); @@ -39,11 +39,11 @@ public class ClassWithArrayOfNullable [Fact] public async Task When_property_is_Array_of_nullable_then_schema_with_array_of_nullable_is_generated() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(2, schema.ActualProperties.Count); var arrayProperty = schema.ActualProperties["Array"].ActualTypeSchema; @@ -70,11 +70,11 @@ public class SomeModelCollectionResponse : List { } [Fact] public async Task When_class_inherits_from_list_then_schema_is_inlined_and_type_is_array() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Array, schema.Definitions["SomeModelCollectionResponse"].Type); Assert.NotNull(schema.Definitions["SomeModelCollectionResponse"].Item); } @@ -96,11 +96,11 @@ public class Apple [Fact] public async Task When_property_is_async_numerable_then_item_type_is_correct() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); - //// Assert + // Assert var asyncProperty = schema.ActualProperties["AsyncApples"].ActualTypeSchema; Assert.Equal(JsonObjectType.Array, asyncProperty.Type); Assert.True(asyncProperty.Item.HasReference); diff --git a/src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs b/src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs index 86c028ffd..b35e426ae 100644 --- a/src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs @@ -11,13 +11,13 @@ public class AttributeGenerationTests [Fact] public async Task When_minLength_and_maxLength_attribute_are_set_on_array_then_minItems_and_maxItems_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Items"]; - //// Assert + // Assert Assert.Equal(3, property.MinItems); Assert.Equal(5, property.MaxItems); } @@ -25,13 +25,13 @@ public async Task When_minLength_and_maxLength_attribute_are_set_on_array_then_m [Fact] public async Task When_minLength_and_maxLength_attribute_are_set_on_string_then_minLength_and_maxLenght_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["String"]; - //// Assert + // Assert Assert.Equal(3, property.MinLength); Assert.Equal(5, property.MaxLength); } @@ -39,13 +39,13 @@ public async Task When_minLength_and_maxLength_attribute_are_set_on_string_then_ [Fact] public async Task When_Range_attribute_is_set_on_double_then_minimum_and_maximum_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Double"]; - //// Assert + // Assert Assert.Equal(5.5m, property.Minimum); Assert.Equal(10.5m, property.Maximum); } @@ -53,13 +53,13 @@ public async Task When_Range_attribute_is_set_on_double_then_minimum_and_maximum [Fact] public async Task When_Range_attribute_has_double_max_then_max_is_not_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["DoubleOnlyMin"]; - //// Assert + // Assert Assert.Equal(5.5m, property.Minimum); Assert.Null(property.Maximum); } @@ -67,13 +67,13 @@ public async Task When_Range_attribute_has_double_max_then_max_is_not_set() [Fact] public async Task When_Range_attribute_is_set_on_integer_then_minimum_and_maximum_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Integer"]; - //// Assert + // Assert Assert.Equal(5, property.Minimum); Assert.Equal(10, property.Maximum); } @@ -81,14 +81,14 @@ public async Task When_Range_attribute_is_set_on_integer_then_minimum_and_maximu [Fact] public async Task When_display_attribute_is_available_then_name_and_description_are_read() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Display"]; - //// Assert + // Assert Assert.Equal("Foo", property.Title); Assert.Equal("Bar", property.Description); } @@ -96,14 +96,14 @@ public async Task When_display_attribute_is_available_then_name_and_description_ [Fact] public async Task When_display_attribute_with_resource_type_is_available_then_name_and_description_are_read() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["DisplayWithResource"]; - //// Assert + // Assert Assert.Equal(AttributeGenerationTestsResources.AttributeGenerationsTests_Name, property.Title); Assert.Equal(AttributeGenerationTestsResources.AttributeGenerationsTests_Description, property.Description); } @@ -111,42 +111,42 @@ public async Task When_display_attribute_with_resource_type_is_available_then_na [Fact] public async Task When_description_attribute_is_available_then_description_are_read() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Description"]; - //// Assert + // Assert Assert.Equal("Abc", property.Description); } [Fact] public async Task When_required_attribute_is_available_then_property_is_required() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Required"]; - //// Assert + // Assert Assert.True(property.IsRequired); } [Fact] public async Task When_required_attribute_is_not_available_then_property_is_can_be_null() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Description"]; - //// Assert + // Assert Assert.False(property.IsRequired); Assert.True(property.Type.HasFlag(JsonObjectType.Null)); } @@ -154,14 +154,14 @@ public async Task When_required_attribute_is_not_available_then_property_is_can_ [Fact] public async Task When_ReadOnly_is_set_then_readOnly_is_set_in_schema() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["ReadOnly"]; - //// Assert + // Assert Assert.True(property.IsReadOnly); } @@ -213,13 +213,13 @@ public class ClassWithTypedRange [Fact] public async Task When_range_has_type_and_strings_then_it_is_processed_correctly() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var property = schema.Properties["Foo"]; - //// Assert + // Assert Assert.Equal(0.0m, property.Minimum); Assert.Equal(1.0m, property.Maximum); } @@ -270,11 +270,11 @@ public class ClassWithRegexDictionaryProperty [Fact] public async Task When_dictionary_property_has_regex_attribute_then_regex_is_added_to_additionalProperties() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.Null(schema.Properties["Versions"].Pattern); Assert.NotNull(schema.Properties["Versions"].AdditionalPropertiesSchema.ActualSchema.Pattern); } diff --git a/src/NJsonSchema.Tests/Generation/ContractResolverTests.cs b/src/NJsonSchema.Tests/Generation/ContractResolverTests.cs index e1e1c9e3c..503c2eb46 100644 --- a/src/NJsonSchema.Tests/Generation/ContractResolverTests.cs +++ b/src/NJsonSchema.Tests/Generation/ContractResolverTests.cs @@ -23,7 +23,7 @@ public async Task Properties_should_match_custom_resolver() var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("firstName")); Assert.Equal("firstName", schema.Properties["firstName"].Name); @@ -65,7 +65,7 @@ protected override JsonContract CreateContract(Type objectType) return contract; } - static HashSet _systemConverters = + private static HashSet _systemConverters = [ ..new[] { @@ -121,13 +121,13 @@ public string StringValue public override string ToString() => StringValue; } - interface IStringConvertable + private interface IStringConvertable { string StringValue { get; set; } string ToString(); } - class StringConverter : TypeConverter where T : IStringConvertable, new() + private sealed class StringConverter : TypeConverter where T : IStringConvertable, new() { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => sourceType == typeof(string) ? true : base.CanConvertFrom(context, sourceType); diff --git a/src/NJsonSchema.Tests/Generation/DataContractTests.cs b/src/NJsonSchema.Tests/Generation/DataContractTests.cs index d5fd7a9a8..54afa1858 100644 --- a/src/NJsonSchema.Tests/Generation/DataContractTests.cs +++ b/src/NJsonSchema.Tests/Generation/DataContractTests.cs @@ -17,14 +17,14 @@ public class Person [Fact] public void When_DataContractRequired_is_set_then_undefined_is_not_allowed() { - //// Assert + // Assert Assert.Throws(() => JsonConvert.DeserializeObject(@"{}")); } [Fact] public void When_DataContractRequired_is_set_then_null_is_allowed() { - //// Act + // Act JsonConvert.DeserializeObject(@"{""middleName"": null}"); // Assert: Does not throw } @@ -32,11 +32,11 @@ public void When_DataContractRequired_is_set_then_null_is_allowed() [Fact] public void When_DataContractRequired_is_set_property_is_nullable_in_OpenApi3() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.ActualProperties["middleName"].IsRequired); Assert.True(schema.ActualProperties["middleName"].IsNullable(SchemaType.OpenApi3)); } @@ -44,11 +44,11 @@ public void When_DataContractRequired_is_set_property_is_nullable_in_OpenApi3() [Fact] public void When_DataContractRequired_is_set_property_is_not_nullable_in_Swagger2() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.ActualProperties["middleName"].IsRequired); Assert.False(schema.ActualProperties["middleName"].IsNullable(SchemaType.Swagger2)); diff --git a/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs b/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs index 57fb5589c..2caadb00d 100644 --- a/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs +++ b/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs @@ -10,7 +10,7 @@ public class DefaultReflectionServiceTests [Fact] public void When_ReferenceTypeNullHandling_is_Null_then_nullability_is_correct() { - //// Arrange + // Arrange var checks = new Dictionary { { typeof(bool), false }, @@ -34,10 +34,10 @@ public void When_ReferenceTypeNullHandling_is_Null_then_nullability_is_correct() { typeof(Dictionary), true }, }; - //// Act + // Act var svc = new NewtonsoftJsonReflectionService(); - //// Assert + // Assert foreach (var check in checks) { Assert.Equal(check.Value, svc.IsNullable(check.Key.ToContextualType(), ReferenceTypeNullHandling.Null)); @@ -47,7 +47,7 @@ public void When_ReferenceTypeNullHandling_is_Null_then_nullability_is_correct() [Fact] public void When_ReferenceTypeNullHandling_is_NotNull_then_nullability_is_correct() { - //// Arrange + // Arrange var checks = new Dictionary { { typeof(bool), false }, @@ -71,10 +71,10 @@ public void When_ReferenceTypeNullHandling_is_NotNull_then_nullability_is_correc { typeof(Dictionary), false }, }; - //// Act + // Act var svc = new NewtonsoftJsonReflectionService(); - //// Assert + // Assert foreach (var check in checks) { Assert.Equal(check.Value, svc.IsNullable(check.Key.ToContextualType(), ReferenceTypeNullHandling.NotNull)); diff --git a/src/NJsonSchema.Tests/Generation/DefaultSchemaNameGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/DefaultSchemaNameGeneratorTests.cs index e06e8f2e7..c599d35f3 100644 --- a/src/NJsonSchema.Tests/Generation/DefaultSchemaNameGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/DefaultSchemaNameGeneratorTests.cs @@ -13,11 +13,11 @@ public class DefaultSchemaNameGeneratorTests [InlineData(typeof(Dictionary), "DictionaryOfNullableDecimalAndNullableShort")] public void When_display_name_is_retrieved_then_string_is_correct(Type type, string expectedName) { - //// Act + // Act var generator = new DefaultSchemaNameGenerator(); var name = generator.Generate(type); - //// Assert + // Assert Assert.Equal(expectedName, name); } } diff --git a/src/NJsonSchema.Tests/Generation/DefaultTypeNameGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/DefaultTypeNameGeneratorTests.cs index 2bd81f704..ce3c32abc 100644 --- a/src/NJsonSchema.Tests/Generation/DefaultTypeNameGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/DefaultTypeNameGeneratorTests.cs @@ -19,15 +19,15 @@ public void When_type_name_is_genererated_then_it_corret() { " Foo.ActionResponse [System.Net.HttpStatusCode, User]", "ActionResponseOfHttpStatusCodeAndUser" } }; - //// Arrange + // Arrange var generator = new DefaultTypeNameGenerator(); foreach (var p in tests) { - //// Act + // Act var name = generator.Generate(new JsonSchema(), p.Key, new List()); - //// Assert + // Assert Assert.Equal(p.Value, name); } } @@ -48,15 +48,15 @@ public void When_type_name_is_genererated_then_it_removes_Illegal_characters() { " Foo.ActionRes+ponse [System.Net.HttpSta+tusCode, Us+er]", "ActionRes_ponseOfHttpSta_tusCodeAndUs_er" } }; - //// Arrange + // Arrange var generator = new DefaultTypeNameGenerator(); foreach (var p in tests) { - //// Act + // Act var name = generator.Generate(new JsonSchema(), p.Key, new List()); - //// Assert + // Assert Assert.Equal(p.Value, name); } } diff --git a/src/NJsonSchema.Tests/Generation/DictionaryTests.cs b/src/NJsonSchema.Tests/Generation/DictionaryTests.cs index 5a60afd67..b47906bc5 100644 --- a/src/NJsonSchema.Tests/Generation/DictionaryTests.cs +++ b/src/NJsonSchema.Tests/Generation/DictionaryTests.cs @@ -25,11 +25,11 @@ public class EnumKeyDictionaryTest [Fact] public async Task When_dictionary_key_is_enum_then_csharp_has_enum_key() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Mapping"].IsDictionary); Assert.True(schema.Properties["Mapping"].DictionaryKey.ActualSchema.IsEnumeration); @@ -43,11 +43,11 @@ public async Task When_dictionary_key_is_enum_then_csharp_has_enum_key() [Fact] public async Task When_value_type_is_nullable_then_json_schema_is_nullable() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Mapping3"].IsDictionary); Assert.True(schema.Properties["Mapping3"].AdditionalPropertiesSchema.IsNullable(SchemaType.JsonSchema)); @@ -58,7 +58,7 @@ public async Task When_value_type_is_nullable_then_json_schema_is_nullable() [Fact] public async Task When_value_type_is_nullable_then_json_schema_is_nullable_Swagger2() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2, @@ -66,7 +66,7 @@ public async Task When_value_type_is_nullable_then_json_schema_is_nullable_Swagg }); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Mapping3"].IsDictionary); Assert.True(schema.Properties["Mapping3"].AdditionalPropertiesSchema.IsNullable(SchemaType.Swagger2)); @@ -77,7 +77,7 @@ public async Task When_value_type_is_nullable_then_json_schema_is_nullable_Swagg [Fact] public async Task When_value_type_is_nullable_then_json_schema_is_nullable_OpenApi3() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3, @@ -85,7 +85,7 @@ public async Task When_value_type_is_nullable_then_json_schema_is_nullable_OpenA }); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Mapping3"].IsDictionary); Assert.True(schema.Properties["Mapping3"].AdditionalPropertiesSchema.IsNullable(SchemaType.OpenApi3)); diff --git a/src/NJsonSchema.Tests/Generation/EnumGenerationTests.cs b/src/NJsonSchema.Tests/Generation/EnumGenerationTests.cs index b1264535c..842e8d947 100644 --- a/src/NJsonSchema.Tests/Generation/EnumGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/EnumGenerationTests.cs @@ -30,17 +30,17 @@ public enum Bar [Fact] public async Task When_property_is_integer_enum_then_schema_has_enum() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { GenerateEnumMappingDescription = true }); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["Bar"].ActualTypeSchema.Type); Assert.Equal(3, schema.Properties["Bar"].ActualTypeSchema.Enumeration.Count); Assert.Equal(0, schema.Properties["Bar"].ActualTypeSchema.Enumeration.ElementAt(0)); @@ -54,14 +54,14 @@ public async Task When_property_is_integer_enum_then_schema_has_enum() [Fact] public async Task When_string_and_integer_enum_used_then_two_refs_are_generated() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(schema.Properties["Bar"].ActualTypeSchema); Assert.NotNull(schema.Properties["Bar2"].ActualTypeSchema); // must not be a reference but second enum declaration Assert.NotEqual(schema.Properties["Bar"].ActualTypeSchema, schema.Properties["Bar2"].ActualTypeSchema); @@ -72,10 +72,10 @@ public async Task When_string_and_integer_enum_used_then_two_refs_are_generated( [Fact] public async Task When_property_is_string_enum_then_schema_has_enum() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -86,7 +86,7 @@ public async Task When_property_is_string_enum_then_schema_has_enum() }); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Bar"].ActualTypeSchema.Type); Assert.Equal(3, schema.Properties["Bar"].ActualTypeSchema.Enumeration.Count); Assert.Equal("A", schema.Properties["Bar"].ActualTypeSchema.Enumeration.ElementAt(0)); @@ -99,13 +99,13 @@ public async Task When_property_is_string_enum_then_schema_has_enum() [Fact] public async Task When_enum_is_generated_then_names_are_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Assert + // Assert Assert.Equal(3, schema.Properties["Bar"].ActualTypeSchema.EnumerationNames.Count); Assert.Equal("A", schema.Properties["Bar"].ActualTypeSchema.EnumerationNames.ElementAt(0)); Assert.Equal("B", schema.Properties["Bar"].ActualTypeSchema.EnumerationNames.ElementAt(1)); @@ -121,17 +121,17 @@ public class EnumProperty [Fact] public async Task When_enum_property_is_generated_then_enum_is_referenced() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(Bar.C, schema.Properties["Bar"].Default); Assert.True(schema.Properties["Bar"].HasReference); } @@ -153,13 +153,13 @@ public enum MyEnumeration [Fact] public async Task When_string_enum_property_has_default_then_default_is_converted_to_string() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal("C", schema.Properties["MyEnumeration"].Default); } @@ -176,13 +176,13 @@ public bool ShouldSerializeEnumValue() [Fact] public async Task When_enum_property_has_should_serialize_then_no_npe() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("EnumValue")); Assert.NotNull(json); } @@ -198,10 +198,10 @@ public class RequiredEnumProperty [Fact] public async Task When_enum_property_is_required_then_MinLength_is_not_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3, @@ -212,7 +212,7 @@ public async Task When_enum_property_is_required_then_MinLength_is_not_set() }); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.RequiredProperties.Contains("Bar")); Assert.True(schema.Properties["Bar"].OneOf.Count == 0); Assert.True(schema.Properties["Bar"].Reference != null); diff --git a/src/NJsonSchema.Tests/Generation/EnumTests.cs b/src/NJsonSchema.Tests/Generation/EnumTests.cs index 5b1cb1591..56531db31 100644 --- a/src/NJsonSchema.Tests/Generation/EnumTests.cs +++ b/src/NJsonSchema.Tests/Generation/EnumTests.cs @@ -37,7 +37,7 @@ public async Task When_enum_is_used_multiple_times_in_array_then_it_is_always_re { // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var json = schema.ToJson(); @@ -56,7 +56,7 @@ public async Task When_property_is_dictionary_with_enum_value_then_it_is_referen { // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var json = schema.ToJson(); @@ -110,7 +110,7 @@ public async Task When_enum_has_FlagsAttribute_then_custom_property_is_set() { // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -137,7 +137,7 @@ public async Task When_enum_does_not_have_FlagsAttribute_then_custom_property_is { // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = diff --git a/src/NJsonSchema.Tests/Generation/ExceptionTypeTests.cs b/src/NJsonSchema.Tests/Generation/ExceptionTypeTests.cs index 597c9650b..cb2011e1a 100644 --- a/src/NJsonSchema.Tests/Generation/ExceptionTypeTests.cs +++ b/src/NJsonSchema.Tests/Generation/ExceptionTypeTests.cs @@ -15,14 +15,14 @@ public class MyException : Exception [Fact] public async Task When_exception_schema_is_generated_then_special_properties_are_generated_and_JsonProperty_attribute_used() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Act + // Act var exceptionSchema = schema.InheritedSchema.ActualSchema; - //// Assert + // Assert Assert.True(schema.ActualProperties.ContainsKey("foo")); Assert.True(exceptionSchema.ActualProperties.ContainsKey("InnerException")); } diff --git a/src/NJsonSchema.Tests/Generation/ExcplicitInterfacePropertyTests.cs b/src/NJsonSchema.Tests/Generation/ExcplicitInterfacePropertyTests.cs index 4e0f9af6d..6525fc7e2 100644 --- a/src/NJsonSchema.Tests/Generation/ExcplicitInterfacePropertyTests.cs +++ b/src/NJsonSchema.Tests/Generation/ExcplicitInterfacePropertyTests.cs @@ -21,27 +21,27 @@ public class Foo : IFoo [Fact] public void When_a_property_is_explicit_interface_then_it_is_not_serialized() { - //// Arrange + // Arrange var foo = new Foo(); ((IFoo)foo).Prop = "foobar"; - //// Act + // Act var json = JsonConvert.SerializeObject(foo); - //// Assert + // Assert Assert.Equal(@"{""MyProp"":null}", json); } [Fact] public async Task When_a_property_is_explicit_interface_then_it_is_not_added_to_schema() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert - Assert.Equal(1, schema.Properties.Count); + // Assert + Assert.Single(schema.Properties); } } } diff --git a/src/NJsonSchema.Tests/Generation/ExtensionDataGenerationTests.cs b/src/NJsonSchema.Tests/Generation/ExtensionDataGenerationTests.cs index d8f71ef62..b3197a1a0 100644 --- a/src/NJsonSchema.Tests/Generation/ExtensionDataGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/ExtensionDataGenerationTests.cs @@ -17,12 +17,12 @@ public class ClassWithExtensionData [Fact] public async Task When_class_has_property_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); - //// Assert - Assert.Equal(1, schema.ActualProperties.Count); + // Assert + Assert.Single(schema.ActualProperties); Assert.True(schema.AllowAdditionalProperties); Assert.True(schema.AdditionalPropertiesSchema.ActualSchema.IsAnyType); } diff --git a/src/NJsonSchema.Tests/Generation/FieldGenerationTests.cs b/src/NJsonSchema.Tests/Generation/FieldGenerationTests.cs index c2f5c3570..d66b529a9 100644 --- a/src/NJsonSchema.Tests/Generation/FieldGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/FieldGenerationTests.cs @@ -13,14 +13,14 @@ public class MyTest [Fact] public async Task When_public_field_is_available_then_it_is_added_as_property() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["MyField"].Type.HasFlag(JsonObjectType.String)); } } diff --git a/src/NJsonSchema.Tests/Generation/FlattenInheritanceHierarchyTests.cs b/src/NJsonSchema.Tests/Generation/FlattenInheritanceHierarchyTests.cs index 5e625fec9..8d2a60b25 100644 --- a/src/NJsonSchema.Tests/Generation/FlattenInheritanceHierarchyTests.cs +++ b/src/NJsonSchema.Tests/Generation/FlattenInheritanceHierarchyTests.cs @@ -23,15 +23,15 @@ public class Teacher : Person public class Schedule { - int a { get; set; } + private int a { get; set; } - int b { get; set; } + private int b { get; set; } } [Fact] public async Task When_FlattenInheritanceHierarchy_is_enabled_then_all_properties_are_in_one_schema() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -41,11 +41,11 @@ public async Task When_FlattenInheritanceHierarchy_is_enabled_then_all_propertie FlattenInheritanceHierarchy = true }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(typeof(Teacher), settings); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("Name")); Assert.True(schema.Properties.ContainsKey("Class")); } @@ -73,7 +73,7 @@ public interface ISame [Fact] public async Task When_FlattenInheritanceHierarchy_is_enabled_then_all_interface_properties_are_in_one_schema() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -84,11 +84,11 @@ public async Task When_FlattenInheritanceHierarchy_is_enabled_then_all_interface FlattenInheritanceHierarchy = true }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(3, schema.Properties.Count); Assert.True(schema.Properties.ContainsKey("Foo")); Assert.True(schema.Properties.ContainsKey("Bar")); @@ -107,34 +107,34 @@ public class Metadata : Dictionary { } [Fact] public async Task When_class_inherits_from_dictionary_then_flatten_inheritance_and_generate_abstract_properties_works() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { GenerateAbstractProperties = true, FlattenInheritanceHierarchy = true }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var data = schema.ToJson(); - //// Assert + // Assert } [Fact] public async Task When_class_inherits_from_dictionary_then_flatten_inheritance_works() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { FlattenInheritanceHierarchy = true }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var data = schema.ToJson(); - //// Assert + // Assert } public class A : B @@ -156,14 +156,14 @@ public class C [Fact] public async Task When_JsonSchemaFlattenAttribute_is_used_on_class_then_inherited_classed_are_merged() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings(); - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Definitions.ContainsKey("B")); Assert.False(schema.Definitions.ContainsKey("C")); @@ -175,20 +175,20 @@ public async Task When_JsonSchemaFlattenAttribute_is_used_on_class_then_inherite [Fact] public async Task When_class_inherited_and_json_flattened_then_ignore_base_property_with_same_name() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { FlattenInheritanceHierarchy = true, }; - //// Act + // Act var TeacherSchema = NewtonsoftJsonSchemaGenerator.FromType(typeof(Teacher), settings); var TacherData = TeacherSchema.ToJson(); var PersonSchema = NewtonsoftJsonSchemaGenerator.FromType(typeof(Person), settings); var PersonData = PersonSchema.ToJson(); - //// Assert + // Assert // Teacher correct schema Assert.True(TeacherSchema.Definitions.ContainsKey("Schedule")); Assert.True(TeacherSchema.Properties["Schedule"].Item.HasReference); diff --git a/src/NJsonSchema.Tests/Generation/GenericTests.cs b/src/NJsonSchema.Tests/Generation/GenericTests.cs index 292a1f292..d3e2aeddd 100644 --- a/src/NJsonSchema.Tests/Generation/GenericTests.cs +++ b/src/NJsonSchema.Tests/Generation/GenericTests.cs @@ -22,13 +22,13 @@ public IEnumerator GetEnumerator() [Fact] public async Task When_class_inherits_from_IEnumerable_then_it_should_become_a_json_array_type() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Array, schema.Type); Assert.Equal(JsonObjectType.String, schema.Item.Type); } @@ -41,13 +41,13 @@ public class A [Fact] public async Task When_open_generic_type_is_generated_then_no_exception_is_thrown() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(typeof(A<>)); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(json); } } diff --git a/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs b/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs index 8d38afd65..55673c595 100644 --- a/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs +++ b/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs @@ -19,13 +19,13 @@ public class Mno [Fact] public async Task When_field_has_JsonIgnoreAttribute_then_it_is_ignored() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.DoesNotContain("IgnoreMe", json); } @@ -41,13 +41,13 @@ public class Xyz [Fact] public async Task When_field_has_no_DataMemberAttribute_then_it_is_ignored() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.DoesNotContain("IgnoreMe", json); } @@ -73,13 +73,13 @@ public Foo() [Fact] public async Task When_indexer_property_has_ignore_attribute_then_it_is_ignored() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(2, schema.Properties.Count); } } diff --git a/src/NJsonSchema.Tests/Generation/InheritanceTests.cs b/src/NJsonSchema.Tests/Generation/InheritanceTests.cs index 3d7de88e2..3e83a7f8d 100644 --- a/src/NJsonSchema.Tests/Generation/InheritanceTests.cs +++ b/src/NJsonSchema.Tests/Generation/InheritanceTests.cs @@ -12,7 +12,7 @@ public class InheritanceTests [Fact] public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_all_of_contains_all_properties() { - //// Arrange + // Arrange var json = @"{ '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', @@ -35,10 +35,10 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a ] }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Assert + // Assert Assert.NotNull(schema.InheritedSchema); Assert.Equal(2, schema.ActualProperties.Count); Assert.True(schema.ActualProperties.ContainsKey("prop1")); @@ -48,7 +48,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_schema() { - //// Arrange + // Arrange var json = @"{ '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', @@ -67,22 +67,22 @@ public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_schem ] }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Assert + // Assert Assert.NotNull(schema.InheritedSchema); - Assert.Equal(1, schema.ActualProperties.Count); + Assert.Single(schema.ActualProperties); Assert.True(schema.ActualProperties.ContainsKey("prop1")); } [Fact] public async Task When_generating_type_with_inheritance_then_allOf_has_one_item() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.NotNull(schema.ActualProperties["Class"]); Assert.Equal(2, schema.AllOf.Count); @@ -103,16 +103,16 @@ public class Person [Fact] public async Task When_generating_type_with_inheritance_and_flattening_then_schema_has_all_properties_of_inherited_classes() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { FlattenInheritanceHierarchy = true }); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal(4, schema.Properties.Count); } @@ -154,14 +154,14 @@ public class Animal [Fact] public async Task When_root_schema_is_inherited_then_schema_is_generated() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); } @@ -186,7 +186,7 @@ public class ViewModelThing [Fact] public async Task When_discriminator_is_externally_defined_then_it_is_generated() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaProcessors = @@ -195,11 +195,11 @@ public async Task When_discriminator_is_externally_defined_then_it_is_generated( } }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Definitions.ContainsKey(nameof(CommonThingBase))); Assert.True(schema.Definitions.ContainsKey(nameof(ACommonThing))); Assert.True(schema.Definitions.ContainsKey(nameof(BCommonThing))); @@ -218,7 +218,7 @@ public class ViewModelThingWithTwoProperties [Fact] public async Task When_discriminator_is_externally_defined_then_it_is_generated_without_exception() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaProcessors = @@ -227,11 +227,11 @@ public async Task When_discriminator_is_externally_defined_then_it_is_generated_ } }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Definitions.ContainsKey(nameof(CommonThingBase))); Assert.True(schema.Definitions.ContainsKey(nameof(ACommonThing))); Assert.True(schema.Definitions.ContainsKey(nameof(BCommonThing))); @@ -243,26 +243,26 @@ public async Task When_discriminator_is_externally_defined_then_it_is_generated_ [Fact] public async Task When_serializing_object_with_inheritance_then_discriminator_is_added() { - /// Arrange + // Arrange var thing = new ViewModelThing { CommonThing = new ACommonThing() }; - /// Act + // Act var json = JsonConvert.SerializeObject(thing, Formatting.Indented, new[] { new JsonInheritanceConverter(typeof(CommonThingBase), "discriminator") }); - /// Assert + // Assert Assert.Contains("\"discriminator\": \"ACommonThing\"", json); } [Fact] public async Task When_deserializing_object_with_inheritance_then_correct_type_is_generated() { - /// Arrange + // Arrange var json = @"{ ""CommonThing"": { @@ -270,7 +270,7 @@ public async Task When_deserializing_object_with_inheritance_then_correct_type_i } }"; - /// Act + // Act var vm = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { Converters = @@ -279,7 +279,7 @@ public async Task When_deserializing_object_with_inheritance_then_correct_type_i ] }); - /// Assert + // Assert Assert.Equal(typeof(ACommonThing), vm.CommonThing.GetType()); } @@ -298,12 +298,12 @@ public class InheritedClass_WithStringDiscriminant : BaseClass_WithStringDiscrim [Fact] public async Task Existing_string_property_can_be_discriminant() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.NotNull(schema.Properties["Kind"]); } @@ -322,13 +322,13 @@ public class InheritedClass_WithIntDiscriminant : BaseClass_WithStringDiscrimina [Fact] public async Task Existing_non_string_property_cant_be_discriminant() { - //// Arrange + // Arrange - //// Act + // Act JsonSchema GetSchema() => NewtonsoftJsonSchemaGenerator.FromType(); Action getSchemaAction = () => GetSchema(); - //// Assert + // Assert Assert.Throws(getSchemaAction); } @@ -345,17 +345,17 @@ public class Bar : Dictionary [Fact] public async Task When_class_inherits_from_dictionary_then_allOf_contains_base_dictionary_schema_and_actual_schema() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var json = schema.ToJson(); - //// Assert + // Assert var bar = schema.Definitions["Bar"]; Assert.Equal(2, bar.AllOf.Count); @@ -387,17 +387,17 @@ public class ExceptionContainer [Fact] public async Task When_class_with_discriminator_has_base_class_then_mapping_is_placed_in_type_schema_and_not_root() { - //// Arrange + // Arrange var settings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }; - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); var json = schema.ToJson(); - //// Assert + // Assert var exceptionBase = schema.Definitions["ExceptionBase"]; Assert.Null(exceptionBase.DiscriminatorObject); @@ -426,11 +426,11 @@ public class Fruit [Fact] public async Task When_using_JsonInheritanceAttribute_then_schema_is_correct() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); Assert.Contains(@"""a"": """, data); Assert.Contains(@"""o"": """, data); diff --git a/src/NJsonSchema.Tests/Generation/InterfaceTests.cs b/src/NJsonSchema.Tests/Generation/InterfaceTests.cs index 1a59f04b2..ca2e31d87 100644 --- a/src/NJsonSchema.Tests/Generation/InterfaceTests.cs +++ b/src/NJsonSchema.Tests/Generation/InterfaceTests.cs @@ -26,9 +26,9 @@ public interface ICategory [Fact] public async Task When_class_inherits_from_interface_then_properties_for_interface_are_generated() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { GenerateAbstractProperties = true @@ -36,7 +36,7 @@ public async Task When_class_inherits_from_interface_then_properties_for_interfa var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(2, schema.Definitions["ICategory"].Properties.Count); } } diff --git a/src/NJsonSchema.Tests/Generation/JsonPropertyAttributeTests.cs b/src/NJsonSchema.Tests/Generation/JsonPropertyAttributeTests.cs index 0b32aa38a..8620e7905 100644 --- a/src/NJsonSchema.Tests/Generation/JsonPropertyAttributeTests.cs +++ b/src/NJsonSchema.Tests/Generation/JsonPropertyAttributeTests.cs @@ -10,13 +10,13 @@ public class JsonPropertyAttributeTests [Fact] public async Task When_name_of_JsonPropertyAttribute_is_set_then_it_is_used_as_json_property_name() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var property = schema.Properties["NewName"]; - //// Assert + // Assert Assert.Equal("NewName", property.Name); } @@ -34,13 +34,13 @@ public async Task When_name_of_JsonPropertyAttribute_is_set_then_it_is_used_as_j } }; - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); - //// Act + // Act var property = schema.Properties["NewName"]; - //// Assert + // Assert Assert.Equal("NewName", property.Name); } @@ -55,26 +55,26 @@ public async Task Use_JsonSchemaGeneratorSettings_ContractResolver_For_JsonPrope } }; - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(settings); - //// Act + // Act var property = schema.Properties["newName"]; - //// Assert + // Assert Assert.Equal("newName", property.Name); } [Fact] public async Task When_required_is_always_in_JsonPropertyAttribute_then_the_property_is_required() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var property = schema.Properties["Required"]; - //// Assert + // Assert Assert.True(property.IsRequired); } diff --git a/src/NJsonSchema.Tests/Generation/JsonSchemaExtensionDataAttributeGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/JsonSchemaExtensionDataAttributeGeneratorTests.cs index dbf15c689..8301f93ad 100644 --- a/src/NJsonSchema.Tests/Generation/JsonSchemaExtensionDataAttributeGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/JsonSchemaExtensionDataAttributeGeneratorTests.cs @@ -38,7 +38,7 @@ public class RootType [Fact] public async Task When_class_has_property_with_JsonSchemaExtensionDataAttribute_on_property_then_extensiondata_schema_is_set_on_property_level() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); var json = schema.ToJson(); @@ -117,7 +117,7 @@ public async Task When_class_has_property_with_JsonSchemaExtensionDataAttribute_ } }"; - //// Assert generated JSON matches schema, ignores whitespace and spaces + // Assert generated JSON matches schema, ignores whitespace and spaces var result = String.Compare(json, expectedJSON, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols); Assert.Equal(0, result); @@ -126,7 +126,7 @@ public async Task When_class_has_property_with_JsonSchemaExtensionDataAttribute_ [Fact] public async Task When_class_has_property_with_JsonSchemaExtensionDataAttribute_on_property_then_extensiondata_is_set_to_property() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 @@ -168,7 +168,7 @@ public async Task When_class_has_property_with_JsonSchemaExtensionDataAttribute_ } }"; - //// Assert generated JSON matches schema, ignores whitespace and spaces + // Assert generated JSON matches schema, ignores whitespace and spaces var result = string.Compare(json, expectedJSON, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols); Assert.Equal(0, result); diff --git a/src/NJsonSchema.Tests/Generation/JsonSchemaTypeAttributeTests.cs b/src/NJsonSchema.Tests/Generation/JsonSchemaTypeAttributeTests.cs index e5bbbef18..37ec08450 100644 --- a/src/NJsonSchema.Tests/Generation/JsonSchemaTypeAttributeTests.cs +++ b/src/NJsonSchema.Tests/Generation/JsonSchemaTypeAttributeTests.cs @@ -20,14 +20,14 @@ public class ClassPrimitiveJsonSchemaTypeAttributes [Fact] public async Task When_type_of_primitive_properties_are_changed_then_the_schemas_are_correct() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Bar"].IsNullable(SchemaType.JsonSchema)); Assert.True(schema.Properties["Bar"].Type.HasFlag(JsonObjectType.Integer)); Assert.False(schema.Properties["Bar"].HasReference); @@ -51,14 +51,14 @@ public class ClassArrayJsonSchemaTypeAttributes [Fact] public async Task When_type_of_array_properties_are_changed_then_the_schemas_are_correct() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Bar"].IsNullable(SchemaType.JsonSchema)); Assert.True(schema.Properties["Bar"].Type.HasFlag(JsonObjectType.Array)); Assert.False(schema.Properties["Bar"].HasReference); @@ -89,14 +89,14 @@ public class ClassComplexJsonSchemaTypeAttributes [Fact] public async Task When_type_of_complex_properties_are_changed_then_the_schemas_are_correct() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Bar"].IsNullable(SchemaType.JsonSchema)); Assert.False(schema.Properties["Baz"].ActualTypeSchema.IsNullable(SchemaType.JsonSchema)); Assert.True(schema.Properties["Bar"].ActualTypeSchema.Type.HasFlag(JsonObjectType.Object)); @@ -124,14 +124,14 @@ public class ClassWithReference [Fact] public async Task When_JsonSchemaTypeAttribute_is_on_class_then_property_defines_nullability() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Abc"].IsNullable(SchemaType.JsonSchema)); Assert.False(schema.Properties["Def"].IsNullable(SchemaType.JsonSchema)); diff --git a/src/NJsonSchema.Tests/Generation/KnownTypeGenerationTests.cs b/src/NJsonSchema.Tests/Generation/KnownTypeGenerationTests.cs index d5b2b05d0..84a4f0eeb 100644 --- a/src/NJsonSchema.Tests/Generation/KnownTypeGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/KnownTypeGenerationTests.cs @@ -56,13 +56,13 @@ public class Container [Fact] public async Task When_KnownType_attribute_exists_then_specified_classes_are_also_generated() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert Assert.Contains(schema.Definitions, s => s.Key == "Teacher"); Assert.Contains(schema.Definitions, s => s.Key == "SpecialTeacher"); } @@ -70,13 +70,13 @@ public async Task When_KnownType_attribute_exists_then_specified_classes_are_als [Fact] public async Task When_KnownType_attribute_includes_method_name_then_specified_classes_are_also_generated() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert Assert.Contains(schema.Definitions, s => s.Key == "Pen"); Assert.Contains(schema.Definitions, s => s.Key == "Pencil"); } diff --git a/src/NJsonSchema.Tests/Generation/NullPropertyTests.cs b/src/NJsonSchema.Tests/Generation/NullPropertyTests.cs index 28cc00e23..cf08b67db 100644 --- a/src/NJsonSchema.Tests/Generation/NullPropertyTests.cs +++ b/src/NJsonSchema.Tests/Generation/NullPropertyTests.cs @@ -18,13 +18,13 @@ public class ClassRoom [Fact] public async Task When_property_is_nullable_then_property_schema_type_is_also_null() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.False(schema.Properties["Id"].IsRequired); Assert.False(schema.Properties["Name"].IsRequired); Assert.False(schema.Properties["Size"].IsRequired); @@ -45,13 +45,13 @@ public class NotNullAttributeClass [Fact] public async Task When_NotNullAttribute_is_available_then_property_is_not_nullable() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Foo"].IsNullable(SchemaType.JsonSchema)); Assert.False(schema.Properties["Bar"].IsNullable(SchemaType.JsonSchema)); } diff --git a/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs b/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs index b576b62c5..d61868b2c 100644 --- a/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs @@ -34,13 +34,13 @@ public class Foo [Fact] public async Task When_property_is_byte_array_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Bytes"].Type); Assert.Equal(JsonFormatStrings.Byte, schema.Properties["Bytes"].Format); } @@ -48,13 +48,13 @@ public async Task When_property_is_byte_array_then_schema_type_is_string() [Fact] public async Task When_property_is_byte_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["Byte"].Type); Assert.Equal(JsonFormatStrings.Byte, schema.Properties["Byte"].Format); } @@ -62,13 +62,13 @@ public async Task When_property_is_byte_then_schema_type_is_string() [Fact] public async Task When_property_is_timespan_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["TimeSpan"].Type); Assert.Equal(JsonFormatStrings.Duration, schema.Properties["TimeSpan"].Format); } @@ -76,27 +76,27 @@ public async Task When_property_is_timespan_then_schema_type_is_string() [Fact] public async Task When_property_is_type_then_schema_type_is_string() { - //// Arrange + // Arrange var data = JsonConvert.SerializeObject(new Foo { Type = typeof(Foo) }); // Type property is serialized as string - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Type"].Type); } [Fact] public async Task When_property_is_localdate_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Date"].Type); Assert.Equal(JsonFormatStrings.Date, schema.Properties["Date"].Format); } @@ -104,13 +104,13 @@ public async Task When_property_is_localdate_then_schema_type_is_string() [Fact] public async Task When_property_is_zoneddatetime_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["ZonedDateTime"].Type); Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["ZonedDateTime"].Format); } @@ -118,13 +118,13 @@ public async Task When_property_is_zoneddatetime_then_schema_type_is_string() [Fact] public async Task When_property_is_offsetdatetime_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["OffsetDateTime"].Type); Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["OffsetDateTime"].Format); } @@ -132,13 +132,13 @@ public async Task When_property_is_offsetdatetime_then_schema_type_is_string() [Fact] public async Task When_property_is_instant_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Instant"].Type); Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["Instant"].Format); } @@ -146,13 +146,13 @@ public async Task When_property_is_instant_then_schema_type_is_string() [Fact] public async Task When_property_is_duration_then_schema_type_is_string() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Duration"].Type); Assert.Equal(JsonFormatStrings.Duration, schema.Properties["Duration"].Format); } diff --git a/src/NJsonSchema.Tests/Generation/PropertyNamesGenerationTests.cs b/src/NJsonSchema.Tests/Generation/PropertyNamesGenerationTests.cs index 4044c332b..5957a412a 100644 --- a/src/NJsonSchema.Tests/Generation/PropertyNamesGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/PropertyNamesGenerationTests.cs @@ -21,9 +21,9 @@ public class Foo [Fact] public async Task When_property_name_is_default_then_schema_has_reflected_names() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -34,7 +34,7 @@ public async Task When_property_name_is_default_then_schema_has_reflected_names( var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("BarBar1JsonProperty")); Assert.Equal("BarBar1JsonProperty", schema.Properties["BarBar1JsonProperty"].Name); Assert.True(schema.Properties.ContainsKey("BarBar2DataMember")); @@ -44,9 +44,9 @@ public async Task When_property_name_is_default_then_schema_has_reflected_names( [Fact] public async Task When_property_name_is_camel_then_schema_has_camel_names() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -57,7 +57,7 @@ public async Task When_property_name_is_camel_then_schema_has_camel_names() var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("barBar1JsonProperty")); Assert.Equal("barBar1JsonProperty", schema.Properties["barBar1JsonProperty"].Name); Assert.True(schema.Properties.ContainsKey("barBar2DataMember")); @@ -67,9 +67,9 @@ public async Task When_property_name_is_camel_then_schema_has_camel_names() [Fact] public async Task When_property_name_is_snake_then_schema_has_snake_names() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = @@ -80,7 +80,7 @@ public async Task When_property_name_is_snake_then_schema_has_snake_names() var data = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("bar_bar1_json_property")); Assert.Equal("bar_bar1_json_property", schema.Properties["bar_bar1_json_property"].Name); Assert.True(schema.Properties.ContainsKey("bar_bar2_data_member")); diff --git a/src/NJsonSchema.Tests/Generation/RecordTests.cs b/src/NJsonSchema.Tests/Generation/RecordTests.cs index a4fa0fac8..1b2cffbf9 100644 --- a/src/NJsonSchema.Tests/Generation/RecordTests.cs +++ b/src/NJsonSchema.Tests/Generation/RecordTests.cs @@ -12,16 +12,16 @@ public record Address [Fact] public void Should_have_only_one_property() { - //// Arrange + // Arrange - //// Act + // Act var schema = JsonSchema.FromType
(); var data = schema.ToJson(); var add = new Address(); - //// Assert - Assert.Equal(1, schema.Properties.Count); + // Assert + Assert.Single(schema.Properties); } } } \ No newline at end of file diff --git a/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs index b861b09ea..53f018314 100644 --- a/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs @@ -39,15 +39,15 @@ public class Measurements [Fact] public void When_sample_data_is_generated_from_schema_then_properties_are_set() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var generator = new SampleJsonDataGenerator(); - //// Act + // Act var token = generator.Generate(schema); var obj = token as JObject; - //// Assert + // Assert Assert.NotNull(obj.Property(nameof(Person.FirstName))); Assert.NotNull(obj.Property(nameof(Person.LastName))); Assert.NotNull(obj.Property(nameof(Person.MainAddress))); @@ -57,15 +57,15 @@ public void When_sample_data_is_generated_from_schema_then_properties_are_set() [Fact] public void When_sample_data_is_generated_from_schema_with_base_then_properties_are_set() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var generator = new SampleJsonDataGenerator(); - //// Act + // Act var token = generator.Generate(schema); var obj = token as JObject; - //// Assert + // Assert Assert.NotNull(obj.Property(nameof(Student.Course))); Assert.NotNull(obj.Property(nameof(Person.FirstName))); Assert.NotNull(obj.Property(nameof(Person.LastName))); @@ -76,22 +76,22 @@ public void When_sample_data_is_generated_from_schema_with_base_then_properties_ [Fact] public void Default_values_are_set_for_arrays() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); var generator = new SampleJsonDataGenerator(); - //// Act + // Act var token = generator.Generate(schema); var obj = token as JObject; - //// Assert + // Assert Assert.Equal(new JArray(new int[] { 1, 2, 3 }), obj.GetValue(nameof(Measurements.Weights))); } [Fact] public async Task When_generateOptionalProperties_is_false_then_optional_properties_are_not_set() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -125,11 +125,11 @@ public async Task When_generateOptionalProperties_is_false_then_optional_propert GenerateOptionalProperties = false }); - //// Act + // Act var token = generator.Generate(schema); var obj = token as JObject; - //// Assert + // Assert Assert.NotNull(obj.Property("isrequired")); Assert.Null(obj.Property("isoptional")); } @@ -137,7 +137,7 @@ public async Task When_generateOptionalProperties_is_false_then_optional_propert [Fact] public async Task PropertyWithIntegerMinimumDefiniton() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -175,20 +175,20 @@ public async Task PropertyWithIntegerMinimumDefiniton() }"; var generator = new SampleJsonDataGenerator(); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var validationResult = schema.Validate(testJson); Assert.NotNull(validationResult); - Assert.Equal(0, validationResult.Count); + Assert.Empty(validationResult); Assert.Equal(1, testJson.SelectToken("body.numberContent.value").Value()); } [Fact] public async Task SchemaWithRecursiveDefinition() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -232,10 +232,10 @@ public async Task SchemaWithRecursiveDefinition() }"; var generator = new SampleJsonDataGenerator(); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var footerToken = testJson.SelectToken("body.numberContent.data.numberContent.value"); Assert.NotNull(footerToken); @@ -248,7 +248,7 @@ public async Task SchemaWithRecursiveDefinition() [Fact] public async Task GeneratorAdheresToMaxRecursionLevel() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -276,10 +276,10 @@ public async Task GeneratorAdheresToMaxRecursionLevel() }"; var generator = new SampleJsonDataGenerator(new SampleJsonDataGeneratorSettings() { MaxRecursionLevel = 2 }); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var secondBodyToken = testJson.SelectToken("body.body"); Assert.NotNull(secondBodyToken); @@ -295,7 +295,7 @@ public async Task GeneratorAdheresToMaxRecursionLevel() [Fact] public async Task SchemaWithDefinitionUseMultipleTimes() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -337,23 +337,23 @@ public async Task SchemaWithDefinitionUseMultipleTimes() var generator = new SampleJsonDataGenerator(); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var footerToken = testJson.SelectToken("footer.value"); Assert.NotNull(footerToken); var validationResult = schema.Validate(testJson); Assert.NotNull(validationResult); - Assert.Equal(0, validationResult.Count); + Assert.Empty(validationResult); Assert.Equal(1.000012, testJson.SelectToken("body.numberContent.value").Value()); } [Fact] public async Task PropertyWithFloatMinimumDefinition() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -391,20 +391,20 @@ public async Task PropertyWithFloatMinimumDefinition() }"; var generator = new SampleJsonDataGenerator(); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var validationResult = schema.Validate(testJson); Assert.NotNull(validationResult); - Assert.Equal(0, validationResult.Count); + Assert.Empty(validationResult); Assert.Equal(1.000012, testJson.SelectToken("body.numberContent.value").Value()); } [Fact] public async Task PropertyWithDefaultDefiniton() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -441,13 +441,13 @@ public async Task PropertyWithDefaultDefiniton() }"; var generator = new SampleJsonDataGenerator(); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var validationResult = schema.Validate(testJson); Assert.NotNull(validationResult); - Assert.Equal(0, validationResult.Count); + Assert.Empty(validationResult); Assert.Equal(42, testJson.SelectToken("body.numberContent.value").Value()); } @@ -456,7 +456,7 @@ public async Task PropertyWithDefaultDefiniton() [Fact] public async Task PropertyExclusiveMinimumDefiniton() { - //// Arrange + // Arrange var data = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""test schema"", @@ -495,13 +495,13 @@ public async Task PropertyExclusiveMinimumDefiniton() }"; var generator = new SampleJsonDataGenerator(); var schema = await JsonSchema.FromJsonAsync(data); - //// Act + // Act var testJson = generator.Generate(schema); - //// Assert + // Assert var validationResult = schema.Validate(testJson); Assert.NotNull(validationResult); - Assert.Equal(0, validationResult.Count); + Assert.Empty(validationResult); Assert.Equal(1.1, testJson.SelectToken("body.numberContent.value").Value()); } } diff --git a/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs index 2053e3a4f..2aba96fa1 100644 --- a/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs @@ -7,7 +7,7 @@ public class SampleJsonSchemaGeneratorTests [Fact] public void PrimitiveProperties() { - //// Arrange + // Arrange var data = @"{ int: 1, str: ""abc"", @@ -18,11 +18,11 @@ public void PrimitiveProperties() }"; var generator = new SampleJsonSchemaGenerator(); - //// Act + // Act var schema = generator.Generate(data); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["int"].Type); Assert.Equal(JsonObjectType.String, schema.Properties["str"].Type); Assert.Equal(JsonObjectType.Boolean, schema.Properties["bool"].Type); @@ -40,7 +40,7 @@ public void PrimitiveProperties() [Fact] public void ComplexArrayProperty() { - //// Arrange + // Arrange var data = @"{ persons: [ { @@ -54,12 +54,12 @@ public void ComplexArrayProperty() ] }"; - //// Act + // Act var schema = JsonSchema.FromSampleJson(data); var json = schema.ToJson(); var property = schema.Properties["persons"].ActualTypeSchema; - //// Assert + // Assert Assert.Equal(JsonObjectType.Array, property.Type); Assert.Equal(3, property.Item.ActualSchema.Properties.Count); Assert.True(schema.Definitions.ContainsKey("Person")); @@ -68,7 +68,7 @@ public void ComplexArrayProperty() [Fact] public void MergedSchemas() { - //// Arrange + // Arrange var data = @"{ ""Address"": { ""Street"": [ @@ -92,11 +92,11 @@ public void MergedSchemas() } }"; - //// Act + // Act var schema = JsonSchema.FromSampleJson(data); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(3, schema.Definitions.Count); Assert.True(schema.Definitions.ContainsKey("Street")); Assert.True(schema.Definitions.ContainsKey("House")); @@ -106,18 +106,18 @@ public void MergedSchemas() [Fact] public void PrimitiveArrayProperty() { - //// Arrange + // Arrange var data = @"{ array: [ 1, true ] }"; var generator = new SampleJsonSchemaGenerator(); - //// Act + // Act var schema = generator.Generate(data); var json = schema.ToJson(); var property = schema.Properties["array"].ActualTypeSchema; - //// Assert + // Assert Assert.Equal(JsonObjectType.Array, property.Type); Assert.Equal(JsonObjectType.Integer, property.Item.ActualSchema.Type); } diff --git a/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs b/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs index 5ac15100a..1ab574dd6 100644 --- a/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs @@ -25,26 +25,26 @@ public class Bar [Fact] public async Task When_generating_schema_with_object_property_then_additional_properties_are_not_allowed() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert Assert.False(schema.Properties["Bar"].ActualTypeSchema.AllowAdditionalProperties); } [Fact] public async Task When_generating_DateTimeOffset_property_then_format_datetime_must_be_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.String, schema.Properties["Time"].Type); Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["Time"].Format); } @@ -52,13 +52,13 @@ public async Task When_generating_DateTimeOffset_property_then_format_datetime_m [Fact] public async Task When_generating_schema_with_dictionary_property_then_it_must_allow_additional_properties() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Properties["Dictionary"].ActualSchema.AllowAdditionalProperties); Assert.Equal(JsonObjectType.String, schema.Properties["Dictionary"].ActualSchema.AdditionalPropertiesSchema.ActualSchema.Type); // "#/definitions/ref_7de8187d_d860_41fa_a17b_3f395c053cae" @@ -67,13 +67,13 @@ public async Task When_generating_schema_with_dictionary_property_then_it_must_a [Fact] public async Task When_output_schema_contains_reference_then_schema_reference_path_is_human_readable() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var schemaData = schema.ToJson(); - //// Assert + // Assert Assert.Contains("#/definitions/Bar", schemaData); } @@ -118,7 +118,7 @@ public async Task When_dictionary_value_is_null_then_string_values_are_allowed() var errors = schema.Validate(data); // Assert - Assert.Equal(0, errors.Count); + Assert.Empty(errors); } [Fact] @@ -167,7 +167,7 @@ public async Task When_property_is_static_then_it_is_ignored() var json = schema.ToJson(); // Assert - Assert.Equal(1, schema.ActualProperties.Count); + Assert.Single(schema.ActualProperties); Assert.True(schema.ActualProperties.ContainsKey("Bar")); } @@ -198,12 +198,12 @@ public async Task When_private_field_is_dataMember_then_it_is_not_ignored1() var json = schema.ToJson(); // Assert - Assert.Equal(1, schema.ActualProperties.Count); + Assert.Single(schema.ActualProperties); Assert.True(schema.ActualProperties.ContainsKey("MyField")); } [DataContract] - class ClassWithPrivateDataMember2 + private class ClassWithPrivateDataMember2 { #pragma warning disable CS0169 [DataMember(Name = nameof(MyField))] @@ -223,13 +223,13 @@ public async Task When_private_field_is_dataMember_then_it_is_not_ignored2() var json = schema.ToJson(); // Assert - Assert.Equal(1, schema.ActualProperties.Count); + Assert.Single(schema.ActualProperties); Assert.True(schema.ActualProperties.ContainsKey("MyField")); Assert.Equal(JsonObjectType.Integer, schema.Properties["MyField"].Type); } [DataContract] - class ClassWithPrivateDataMember3 + private class ClassWithPrivateDataMember3 { [DataMember] private string MyField { get; set; } @@ -243,7 +243,7 @@ public async Task When_private_property_is_dataMember_then_it_is_not_ignored() var json = schema.ToJson(); // Assert - Assert.Equal(1, schema.ActualProperties.Count); + Assert.Single(schema.ActualProperties); Assert.True(schema.ActualProperties.ContainsKey("MyField")); } diff --git a/src/NJsonSchema.Tests/Generation/SchemaProcessorTests.cs b/src/NJsonSchema.Tests/Generation/SchemaProcessorTests.cs index 5511293c3..90e60d448 100644 --- a/src/NJsonSchema.Tests/Generation/SchemaProcessorTests.cs +++ b/src/NJsonSchema.Tests/Generation/SchemaProcessorTests.cs @@ -31,11 +31,11 @@ public class ClassWithSchemaProcessor [Fact] public void When_class_has_schema_processor_attribute_then_it_is_processed() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings()); var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal("example123", schema.Example); } } diff --git a/src/NJsonSchema.Tests/Generation/ShouldSerializeTests.cs b/src/NJsonSchema.Tests/Generation/ShouldSerializeTests.cs index 34c612257..753902739 100644 --- a/src/NJsonSchema.Tests/Generation/ShouldSerializeTests.cs +++ b/src/NJsonSchema.Tests/Generation/ShouldSerializeTests.cs @@ -18,13 +18,13 @@ public bool ShouldSerializeName() [Fact] public void When_ShouldSerialize_method_exists_then_schema_is_generated() { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Act + // Act - //// Assert + // Assert Assert.NotNull(schema); } } diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs index cadad1fea..bd90e0cef 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs @@ -76,14 +76,14 @@ public double this[string indexer] [Fact] public void SystemTextJson_When_class_has_object_Dictionary_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() { - //// Act + // Act var schema = JsonSchemaGenerator.FromType(new SystemTextJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); - //// Assert - Assert.Equal(1, schema.ActualProperties.Count); + // Assert + Assert.Single(schema.ActualProperties); Assert.True(schema.AllowAdditionalProperties); Assert.True(schema.AdditionalPropertiesSchema.ActualSchema.IsAnyType); } @@ -91,14 +91,14 @@ public void SystemTextJson_When_class_has_object_Dictionary_with_JsonExtensionDa [Fact] public void SystemTextJson_When_class_has_JsonElement_Dictionary_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() { - //// Act + // Act var schema = JsonSchemaGenerator.FromType(new SystemTextJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 }); - //// Assert - Assert.Equal(1, schema.ActualProperties.Count); + // Assert + Assert.Single(schema.ActualProperties); Assert.True(schema.AllowAdditionalProperties); Assert.True(schema.AdditionalPropertiesSchema.ActualSchema.IsAnyType); } diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs index 1191f401e..03f1c497c 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs @@ -1,6 +1,10 @@ using NJsonSchema.Converters; using System.Text.Json; + +#if !NETFRAMEWORK using System.Text.Json.Serialization; +#endif + using Xunit; namespace NJsonSchema.Tests.Generation.SystemTextJson @@ -43,11 +47,11 @@ public class Fruit [Fact] public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_schema_is_correct() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson().ReplaceLineEndings(); - //// Assert + // Assert Assert.NotNull(data); Assert.Contains(@"""a"": """, data); Assert.Contains(@"""o"": """, data); @@ -66,7 +70,7 @@ public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_sc [Fact] public async Task When_discriminator_is_wrong_then_no_stackoverflow() { - //// Act & Assert + // Act & Assert Assert.Throws(() => // Throws "Could not find subtype of..." { JsonSerializer.Deserialize("{\"k\": \"invalid\"}"); @@ -96,11 +100,11 @@ public class Fruit2 [Fact] public async Task When_using_native_attributes_in_SystemTextJson_then_schema_is_correct() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson().ReplaceLineEndings(); - //// Assert + // Assert Assert.NotNull(data); Assert.Contains(@"""a"": """, data); Assert.Contains(@"""o"": """, data); @@ -137,11 +141,11 @@ public class Dog [Fact] public async Task When_using_native_attributes_and_integer_discriminator_in_SystemTextJson_then_schema_is_correct() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson().ReplaceLineEndings(); - //// Assert + // Assert Assert.NotNull(data); Assert.Contains(@"""1"": """, data); Assert.Contains(@"""2"": """, data); diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs index 1fb46a3e3..f56d17d55 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs @@ -26,11 +26,11 @@ public class HealthCheckResult [Fact] public async Task When_property_is_readonly_then_its_in_the_schema() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); Assert.Contains(@"Name", data); Assert.Contains(@"Description", data); @@ -50,14 +50,14 @@ public class NestedType1 [Fact] public async Task When_type_is_excluded_then_it_should_not_be_in_the_schema() { - //// Act + // Act var schema = JsonSchema.FromType(new SystemTextJsonSchemaGeneratorSettings { ExcludedTypeNames = [typeof(NestedType1).FullName] }); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); Assert.DoesNotContain(@"NestedType1", data); Assert.Contains(@"Property", data); @@ -79,11 +79,11 @@ public class NestedType2 [Fact] public async Task When_type_is_excluded_with_json_schema_ignore_attribute_then_it_should_not_be_in_the_schema() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); Assert.DoesNotContain(@"NestedType2", data); Assert.Contains(@"Property", data); @@ -92,11 +92,11 @@ public async Task When_type_is_excluded_with_json_schema_ignore_attribute_then_i [Fact] public async Task When_property_is_private_and_readonly_then_its_not_in_the_schema() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); Assert.False(data.Contains("PrivateReadOnlyProperty1"), data); Assert.False(data.Contains("PrivateReadOnlyProperty2"), data); @@ -105,11 +105,11 @@ public async Task When_property_is_private_and_readonly_then_its_not_in_the_sche [Fact] public async Task When_property_is_static_readonly_then_its_not_in_the_schema() { - //// Act + // Act var schema = JsonSchema.FromType(); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(data); Assert.False(data.Contains("PublicReadOnlyStaticProperty"), data); Assert.False(data.Contains("PrivateReadOnlyStaticProperty"), data); diff --git a/src/NJsonSchema.Tests/Generation/TypeMapperTests.cs b/src/NJsonSchema.Tests/Generation/TypeMapperTests.cs index 01e884903..56a4e936d 100644 --- a/src/NJsonSchema.Tests/Generation/TypeMapperTests.cs +++ b/src/NJsonSchema.Tests/Generation/TypeMapperTests.cs @@ -21,7 +21,7 @@ public class Bar [Fact] public async Task When_primitive_type_mapping_is_available_for_type_then_it_is_called() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { TypeMappers = @@ -30,7 +30,7 @@ public async Task When_primitive_type_mapping_is_available_for_type_then_it_is_c } }); - //// Assert + // Assert var json = schema.ToJson(); var property = schema.Properties["Bar1"].ActualTypeSchema; @@ -41,7 +41,7 @@ public async Task When_primitive_type_mapping_is_available_for_type_then_it_is_c [Fact] public async Task When_object_type_mapping_is_available_for_type_then_it_is_called() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { TypeMappers = @@ -65,7 +65,7 @@ public async Task When_object_type_mapping_is_available_for_type_then_it_is_call } }); - //// Assert + // Assert var json = schema.ToJson(); var property1 = schema.Properties["Bar1"]; @@ -109,7 +109,7 @@ public void GenerateSchema(JsonSchema schema, TypeMapperContext context) [Fact] public async Task When_generic_type_mapper_is_defined_then_it_is_called_and_the_refs_are_correct() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings { TypeMappers = @@ -118,7 +118,7 @@ public async Task When_generic_type_mapper_is_defined_then_it_is_called_and_the_ } }); - //// Assert + // Assert var json = schema.ToJson(); Assert.True(schema.Definitions.ContainsKey("MyBar")); Assert.False(schema.Definitions.ContainsKey("MyWrapperOfMyBar")); diff --git a/src/NJsonSchema.Tests/Generation/XmlDocsTests.cs b/src/NJsonSchema.Tests/Generation/XmlDocsTests.cs index 365eb727c..b868aefe8 100644 --- a/src/NJsonSchema.Tests/Generation/XmlDocsTests.cs +++ b/src/NJsonSchema.Tests/Generation/XmlDocsTests.cs @@ -24,11 +24,11 @@ public abstract class AbstractClass [Fact] public async Task When_example_xml_docs_is_defined_then_examples_can_be_defined() { - /// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); var json = schema.ToJson(Formatting.None); - /// Assert + // Assert Assert.Contains(@"Foobar.", json); Assert.Contains(@"""x-example"":{""foo"":""bar""}", json); Assert.Contains(@"""x-example"":{""abc"":""def""}", json); diff --git a/src/NJsonSchema.Tests/Generation/XmlObjectTests.cs b/src/NJsonSchema.Tests/Generation/XmlObjectTests.cs index 915430ac6..c6f289cd4 100644 --- a/src/NJsonSchema.Tests/Generation/XmlObjectTests.cs +++ b/src/NJsonSchema.Tests/Generation/XmlObjectTests.cs @@ -92,7 +92,7 @@ public async Task When_xmlobject_generation_is_active_with_a_type_without_xml_at { var schema = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings() { GenerateXmlObjects = true }); - //// Assert + // Assert AssertTypeWithoutXmlAttributes(schema); } @@ -156,7 +156,7 @@ public async Task When_xmlobject_generation_is_active_with_a_arraytype_as_parent var schemaData = schema.ToJson(); var schemaObject = JObject.Parse(schemaData); - //// Assert + // Assert var propertyXml = schemaObject["xml"]; Assert.Equal("ArrayOfWithoutXmlAttributesDefined", propertyXml["name"]); Assert.True(propertyXml["wrapped"].Value()); @@ -328,7 +328,7 @@ public async Task When_xmlobject_generation_is_active_with_a_type_with_xml_attri GenerateXmlObjects = true }); - //// Assert + // Assert AssertTypeWithXmlAttributes(schema, schema); } @@ -488,7 +488,7 @@ public async Task When_xmlobject_generation_is_active_with_a_type_with_xml_attri GenerateXmlObjects = true }); - //// Assert + // Assert var fooProperty = schema.Properties["Foo"]; Assert.Null(fooProperty.Xml); diff --git a/src/NJsonSchema.Tests/Infrastructure/PropertyRenameAndIgnoreSerializerContractResolverTests.cs b/src/NJsonSchema.Tests/Infrastructure/PropertyRenameAndIgnoreSerializerContractResolverTests.cs index 9cbe85e87..f6e9c922d 100644 --- a/src/NJsonSchema.Tests/Infrastructure/PropertyRenameAndIgnoreSerializerContractResolverTests.cs +++ b/src/NJsonSchema.Tests/Infrastructure/PropertyRenameAndIgnoreSerializerContractResolverTests.cs @@ -9,17 +9,17 @@ public class PropertyRenameAndIgnoreSerializerContractResolverTests [Fact] public void When_property_is_renamed_then_it_does_not_land_in_extension_data() { - //// Arrange + // Arrange var resolver = new PropertyRenameAndIgnoreSerializerContractResolver(); resolver.RenameProperty(typeof(JsonSchemaProperty), "x-readOnly", "readOnly"); resolver.RenameProperty(typeof(JsonSchema), "x-nullable", "nullable"); var json = "{ \"readOnly\": true, \"nullable\": true, \"additionalProperties\": { \"nullable\": true } }"; - //// Act + // Act var obj = JsonSchemaSerialization.FromJson(json, resolver); - //// Assert + // Assert Assert.True(obj.IsReadOnly); Assert.True(obj.IsNullableRaw); Assert.True(obj.AdditionalPropertiesSchema.IsNullableRaw); @@ -34,18 +34,18 @@ public class MyClass [Fact] public void When_property_is_renamed_then_json_is_correct() { - //// Arrange + // Arrange var resolver = new PropertyRenameAndIgnoreSerializerContractResolver(); resolver.RenameProperty(typeof(MyClass), "foo", "bar"); var obj = new MyClass(); obj.Foo = "abc"; - //// Act + // Act var json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings { ContractResolver = resolver }); obj = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { ContractResolver = resolver }); - //// Assert + // Assert Assert.Contains("bar", json); Assert.Contains("abc", obj.Foo); } @@ -65,7 +65,7 @@ public class ClassWithDoubleProperties [Fact] public void When_property_is_ignored_then_refs_ignore_it() { - //// Arrange + // Arrange var contractResolver = new PropertyRenameAndIgnoreSerializerContractResolver(); contractResolver.IgnoreProperty(typeof(ClassWithDoubleProperties), "definitions1"); @@ -82,11 +82,11 @@ public void When_property_is_ignored_then_refs_ignore_it() } }; - //// Act + // Act JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(foo, false, contractResolver); var json = JsonConvert.SerializeObject(foo, Formatting.Indented, new JsonSerializerSettings { ContractResolver = contractResolver }); - //// Assert + // Assert Assert.Contains("#/definitions2/Bar", json); Assert.DoesNotContain("#/definitions1/Bar", json); } diff --git a/src/NJsonSchema.Tests/NJsonSchema.Tests.csproj b/src/NJsonSchema.Tests/NJsonSchema.Tests.csproj index b5ddbded6..8f96a8ec6 100644 --- a/src/NJsonSchema.Tests/NJsonSchema.Tests.csproj +++ b/src/NJsonSchema.Tests/NJsonSchema.Tests.csproj @@ -3,8 +3,8 @@ net8.0;net472 false - false disable + latest-Minimal diff --git a/src/NJsonSchema.Tests/References/LocalReferencesTests.cs b/src/NJsonSchema.Tests/References/LocalReferencesTests.cs index 96d614ba7..bf64eec64 100644 --- a/src/NJsonSchema.Tests/References/LocalReferencesTests.cs +++ b/src/NJsonSchema.Tests/References/LocalReferencesTests.cs @@ -12,7 +12,7 @@ public class LocalReferencesTests [Fact] public async Task When_definitions_is_nested_then_refs_work() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", ""properties"": { @@ -29,41 +29,41 @@ public async Task When_definitions_is_nested_then_refs_work() } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var j = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["foo"].ActualTypeSchema.Type); } [Fact] public async Task When_schema_references_collection_in_definitions_it_works() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/LocalReferencesTests/schema_with_collection_reference.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["foo"].ActualTypeSchema.Type); - Assert.Equal(1, schema.Definitions.Count); + Assert.Single(schema.Definitions); Assert.Equal("./collection.json", schema.Definitions["collection"].DocumentPath); } [Fact] public async Task When_schema_references_external_schema_then_it_is_inlined_with_ToJson() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/LocalReferencesTests/schema_with_reference.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var json = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.Definitions.ContainsKey("Animal")); Assert.Contains("\"$ref\": \"#/definitions/Animal\"", json); } @@ -71,37 +71,37 @@ public async Task When_schema_references_external_schema_then_it_is_inlined_with [Fact] public async Task When_document_has_indirect_external_ref_than_it_is_loaded() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/LocalReferencesTests/schema_with_indirect_reference.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var json = schema.ToJson(); - //// Assert - Assert.Equal(1, schema.Definitions.Count); + // Assert + Assert.Single(schema.Definitions); Assert.Equal("FooAnimal", schema.Definitions.Single().Key); } [Fact] public async Task When_document_has_indirect_external_ref_to_a_definition_than_it_is_loaded() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/LocalReferencesTests/schema_with_indirect_subreference.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var json = schema.ToJson(); - //// Assert - Assert.Equal(1, schema.Definitions.Count); + // Assert + Assert.Single(schema.Definitions); Assert.Equal("SubAnimal", schema.Definitions.Single().Key); } [Fact] public async Task When_reference_is_registered_in_custom_resolver_it_should_not_try_to_access_file() { - //// Arrange + // Arrange var externalSchema = await JsonSchema.FromJsonAsync( @"{ ""type"": ""object"", @@ -131,10 +131,10 @@ public async Task When_reference_is_registered_in_custom_resolver_it_should_not_ } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(schemaJson, ".", factory); - //// Assert + // Assert Assert.NotNull(schema); } @@ -143,7 +143,7 @@ public async Task When_reference_is_registered_in_custom_resolver_it_should_not_ [InlineData("b#r")] // Non-escaped ill-formed JSON Pointer public async Task When_definitions_have_sharp_in_type_name(string referenceTypeName) { - //// Arrange + // Arrange var json = $@"{{ ""type"": ""object"", ""properties"": {{ @@ -158,25 +158,25 @@ public async Task When_definitions_have_sharp_in_type_name(string referenceTypeN }} }}"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var j = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["foo"].ActualTypeSchema.Type); } [Fact] public async Task When_schema_references_external_schema_placed_in_directory_with_sharp_in_name() { - //// Arrange + // Arrange var path = GetTestDirectory() + "/References/LocalReferencesTests/dir_with_#/first.json"; - //// Act + // Act var schema = await JsonSchema.FromFileAsync(path); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.ActualTypeSchema.Type); } diff --git a/src/NJsonSchema.Tests/Schema/DataContractTests.cs b/src/NJsonSchema.Tests/Schema/DataContractTests.cs index aaee4d6ab..93a570af7 100644 --- a/src/NJsonSchema.Tests/Schema/DataContractTests.cs +++ b/src/NJsonSchema.Tests/Schema/DataContractTests.cs @@ -16,10 +16,10 @@ public class MissingDataContract [Fact] public async Task When_DataContractAttribute_is_missing_then_DataMember_is_ignored() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("Bar")); } @@ -36,10 +36,10 @@ public class NotMissingDataContract [Fact] public async Task When_DataContractAttribute_is_not_missing_then_DataMember_is_checked() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("bar")); Assert.False(schema.Properties.ContainsKey("Foo")); } @@ -55,11 +55,11 @@ public class DataContractWithoutDataMember [Fact] public async Task When_class_has_DataContractAttribute_then_properties_without_DataMemberAttributes_are_ignored() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert - Assert.Equal(0, schema.Properties.Count); + // Assert + Assert.Empty(schema.Properties); } @@ -73,10 +73,10 @@ public class DataContractWithoutDataMemberWithJsonProperty [Fact] public async Task When_class_has_DataContractAttribute_then_property_without_DataMemberAttribute_and_with_JsonPropertyAttribute_is_not_ignored() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("bar")); } @@ -92,11 +92,11 @@ public class DataContractWitDataMemberWithJsonProperty [Fact] public async Task When_class_has_DataContractAttribute_then_property_with_DataMemberAttribute_and_JsonIgnoreAttribute_is_ignored() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert - Assert.Equal(0, schema.Properties.Count); + // Assert + Assert.Empty(schema.Properties); } [DataContract] @@ -112,10 +112,10 @@ public async Task When_DataMemberAttribute_is_required_then_schema_property_is_r // Newtonsoft.Json also respects DataMemberAttribute.IsRequired => this throws an exception //var json = JsonConvert.DeserializeObject("{}"); - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.True(schema.Properties["req"].IsRequired); } } diff --git a/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetJsonPathTests.cs b/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetJsonPathTests.cs index 4bce20f5c..dcd0f768d 100644 --- a/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetJsonPathTests.cs +++ b/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetJsonPathTests.cs @@ -7,7 +7,7 @@ public class JsonPathUtilitiesGetJsonPathTests [Fact] public void When_object_is_in_property_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); var obj = new { @@ -18,17 +18,17 @@ public void When_object_is_in_property_then_path_should_be_built_correctly() } }; - //// Act + // Act var path = JsonPathUtilities.GetJsonPath(obj, objectToSearch); - //// Assert + // Assert Assert.Equal("#/Property/Property2", path); } [Fact] public void When_object_is_in_list_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); var obj = new { @@ -43,17 +43,17 @@ public void When_object_is_in_list_then_path_should_be_built_correctly() } }; - //// Act + // Act var path = JsonPathUtilities.GetJsonPath(obj, objectToSearch); - //// Assert + // Assert Assert.Equal("#/Property/List/2", path); } [Fact] public void When_object_is_in_dictionary_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); var obj = new { @@ -68,23 +68,23 @@ public void When_object_is_in_dictionary_then_path_should_be_built_correctly() } }; - //// Act + // Act var path = JsonPathUtilities.GetJsonPath(obj, objectToSearch); - //// Assert + // Assert Assert.Equal("#/Property/List/Test3", path); } [Fact] public void When_object_is_root_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); - //// Act + // Act var path = JsonPathUtilities.GetJsonPath(objectToSearch, objectToSearch); - //// Assert + // Assert Assert.Equal("#", path); } } diff --git a/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetObjectFromJsonPathTests.cs b/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetObjectFromJsonPathTests.cs index 533d760be..c381901f4 100644 --- a/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetObjectFromJsonPathTests.cs +++ b/src/NJsonSchema.Tests/Schema/JsonPathUtilitiesGetObjectFromJsonPathTests.cs @@ -8,7 +8,7 @@ public class JsonPathUtilitiesGetObjectFromJsonPathTests [Fact] public async Task When_object_is_in_property_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); var obj = new { @@ -19,18 +19,18 @@ public async Task When_object_is_in_property_then_path_should_be_built_correctly } }; - //// Act + // Act var resolver = new JsonReferenceResolver(null); var foundObject = await resolver.ResolveReferenceAsync(obj, "#/Property/Property2", typeof(JsonSchema), new DefaultContractResolver()); - //// Assert + // Assert Assert.Equal(foundObject, objectToSearch); } [Fact] public async Task When_object_is_in_list_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); var obj = new { @@ -45,18 +45,18 @@ public async Task When_object_is_in_list_then_path_should_be_built_correctly() } }; - //// Act + // Act var resolver = new JsonReferenceResolver(null); var foundObject = await resolver.ResolveReferenceAsync(obj, "#/Property/List/2", typeof(JsonSchema), new DefaultContractResolver()); - //// Assert + // Assert Assert.Equal(foundObject, objectToSearch); } [Fact] public async Task When_object_is_in_dictionary_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); var obj = new { @@ -71,32 +71,32 @@ public async Task When_object_is_in_dictionary_then_path_should_be_built_correct } }; - //// Act + // Act var resolver = new JsonReferenceResolver(null); var foundObject = await resolver.ResolveReferenceAsync(obj, "#/Property/List/Test3", typeof(JsonSchema), new DefaultContractResolver()); - //// Assert + // Assert Assert.Equal(foundObject, objectToSearch); } [Fact] public async Task When_object_is_root_then_path_should_be_built_correctly() { - //// Arrange + // Arrange var objectToSearch = new JsonSchema(); - //// Act + // Act var resolver = new JsonReferenceResolver(null); var foundObject = await resolver.ResolveReferenceAsync(objectToSearch, "#", typeof(JsonSchema), new DefaultContractResolver()); - //// Assert + // Assert Assert.Equal(foundObject, objectToSearch); } //[Fact] //public async Task When_object_is_in_external_file_then_path_should_be_built_correctly() //{ - // //// Arrange + // // Arrange // var schemaToReference = new JsonSchema4 {DocumentPath = "some_schema.json"}; // var referencingSchema = new JsonSchema4 // { @@ -104,10 +104,10 @@ public async Task When_object_is_root_then_path_should_be_built_correctly() // Reference = schemaToReference // }; - // //// Act + // // Act // var result = referencingSchema.ToJson(); - // //// Assert + // // Assert // Assert.True(result.Contains("some_schema.json#")); //} } diff --git a/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs b/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs index 653643b60..fd3f7ac26 100644 --- a/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs +++ b/src/NJsonSchema.Tests/Schema/JsonSchemaTests.cs @@ -15,13 +15,13 @@ public async Task Ensure_NJS_does_not_run_in_legacy_mode() [Fact] public void When_creating_schema_without_setting_properties_then_it_is_empty() { - //// Arrange + // Arrange var schema = new JsonSchema(); - //// Act + // Act var data = schema.ToJson(); - //// Assert + // Assert Assert.Equal( @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"" @@ -32,7 +32,7 @@ public void When_creating_schema_without_setting_properties_then_it_is_empty() [Fact] public async Task When_schema_contains_refs_then_they_should_be_resolved() { - //// Arrange + // Arrange var data = @"{ ""id"": ""http://some.site.somewhere/entry-schema#"", @@ -70,10 +70,10 @@ public async Task When_schema_contains_refs_then_they_should_be_resolved() } "; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(data); - //// Assert + // Assert Assert.NotNull(schema.Definitions["diskDevice"]); Assert.True(schema.Properties["storage"].OneOf.All(p => p != null)); } @@ -81,7 +81,7 @@ public async Task When_schema_contains_refs_then_they_should_be_resolved() [Fact] public async Task When_deserializing_schema_then_it_should_be_read_correctly() { - //// Arrange + // Arrange var data = @"{ ""title"": ""Example Schema"", @@ -102,11 +102,11 @@ public async Task When_deserializing_schema_then_it_should_be_read_correctly() ""required"": [""firstName"", ""lastName""] }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(data); var x = schema.ToJson(); - //// Assert + // Assert Assert.Equal(3, schema.Properties.Count); Assert.Equal(JsonObjectType.Object, schema.Type); } @@ -114,7 +114,7 @@ public async Task When_deserializing_schema_then_it_should_be_read_correctly() [Fact] public async Task When_deserializing_multiple_types_then_flags_should_be_set_correctly() { - //// Arrange + // Arrange var data = @"{ ""type"": [ @@ -123,10 +123,10 @@ public async Task When_deserializing_multiple_types_then_flags_should_be_set_cor ] }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(data); - //// Assert + // Assert Assert.True(schema.Type.HasFlag(JsonObjectType.String)); Assert.True(schema.Type.HasFlag(JsonObjectType.Null)); } @@ -134,16 +134,16 @@ public async Task When_deserializing_multiple_types_then_flags_should_be_set_cor [Fact] public async Task When_deserializing_single_type_then_flags_should_be_set_correctly() { - //// Arrange + // Arrange var data = @"{ ""type"": ""string"" }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(data); - //// Assert + // Assert Assert.True(schema.Type.HasFlag(JsonObjectType.String)); Assert.Equal(JsonObjectType.String, schema.Type); } @@ -151,45 +151,45 @@ public async Task When_deserializing_single_type_then_flags_should_be_set_correc [Fact] public async Task When_deserializing_schema_it_should_not_stackoverflow() { - //// Arrange + // Arrange var data = @"{ ""x-dateTime"": ""2016-07-28T14:39:37.937Z"" }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(data); var x = schema.ToJson(); - //// Assert + // Assert Assert.True(schema.ExtensionData.First().Value is DateTime); } [Fact] public void When_setting_single_type_then_it_should_be_serialized_correctly() { - //// Arrange + // Arrange var schema = new JsonSchema(); - //// Act + // Act schema.Type = JsonObjectType.Integer; var data = schema.ToJson(); - //// Assert + // Assert Assert.Contains(@"""type"": ""integer""", data); } [Fact] public void When_setting_multiple_type_then_it_should_be_serialized_correctly() { - //// Arrange + // Arrange var schema = new JsonSchema(); - //// Act + // Act schema.Type = JsonObjectType.Integer | JsonObjectType.Object; var data = schema.ToJson(); - //// Assert + // Assert Assert.Contains(@" ""type"": [ ""integer"", ""object"" @@ -199,13 +199,13 @@ public void When_setting_multiple_type_then_it_should_be_serialized_correctly() [Fact] public void When_adding_property_to_schema_then_parent_should_be_set() { - //// Arrange + // Arrange var schema = new JsonSchema(); - //// Act + // Act schema.Properties.Add("test", new JsonSchemaProperty()); - //// Assert + // Assert Assert.True(schema.Properties.ContainsKey("test")); Assert.Equal(schema, schema.Properties["test"].ParentSchema); } @@ -213,36 +213,36 @@ public void When_adding_property_to_schema_then_parent_should_be_set() [Fact] public void When_setting_property_required_then_the_key_should_be_added() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Properties["test"] = new JsonSchemaProperty(); - //// Act + // Act schema.Properties["test"].IsRequired = true; - //// Assert + // Assert Assert.True(schema.RequiredProperties.Contains("test")); } [Fact] public void When_setting_property_not_required_then_the_key_should_be_added() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Properties["test"] = new JsonSchemaProperty(); schema.RequiredProperties.Add("test"); - //// Act + // Act schema.Properties["test"].IsRequired = false; - //// Assert + // Assert Assert.False(schema.RequiredProperties.Contains("test")); } [Fact] public void When_number_property_is_null_and_not_required_then_it_is_invalid() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Properties["test"] = new JsonSchemaProperty { @@ -250,17 +250,17 @@ public void When_number_property_is_null_and_not_required_then_it_is_invalid() IsRequired = false }; - //// Act + // Act var errors = schema.Validate("{ test: null }"); - //// Assert - Assert.Equal(1, errors.Count); + // Assert + Assert.Single(errors); } [Fact] public void When_property_matches_one_of_the_types_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty @@ -271,17 +271,17 @@ public void When_property_matches_one_of_the_types_then_it_should_succeed() var token = new JObject(); token["Foo"] = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_property_type_not_specified_then_anything_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty(); @@ -290,17 +290,17 @@ public void When_property_type_not_specified_then_anything_should_succeed() var token = new JObject(); token["Foo"] = new JValue(5); token["Bar"] = new JValue("Bar"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_DateTimeOffset_is_validated_then_it_should_not_throw() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String @@ -310,12 +310,12 @@ public void When_DateTimeOffset_is_validated_then_it_should_not_throw() try { - //// Act + // Act schema.Validate(token); } catch { - //// Assert + // Assert throw new Exception("Validating JToken with a DateTimeOffset value threw an exception."); } } @@ -323,7 +323,7 @@ public void When_DateTimeOffset_is_validated_then_it_should_not_throw() [Fact] public async Task When_schema_has_cyclic_references_then_exception_is_thrown() { - //// Arrange + // Arrange var json = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""type"": ""object"", @@ -335,11 +335,11 @@ public async Task When_schema_has_cyclic_references_then_exception_is_thrown() }"; await Assert.ThrowsAsync(async () => { - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var data = schema.ToJson(); - //// Assert + // Assert var propertySchema = schema.Properties["topProp"].ActualTypeSchema; }); } @@ -347,7 +347,7 @@ await Assert.ThrowsAsync(async () => [Fact] public async Task When_schema_is_loaded_then_all_refs_are_resolved() { - //// Arrange + // Arrange var json = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""type"": ""object"", @@ -371,11 +371,11 @@ public async Task When_schema_is_loaded_then_all_refs_are_resolved() } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var data = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(schema.AllOf.First().Reference); } @@ -384,29 +384,29 @@ public async Task When_json_schema_is_loaded_then_no_collection_modified_excepti { // https://github.com/NJsonSchema/NJsonSchema/issues/288 - //// Arrange + // Arrange - //// Act + // Act var schema = await JsonSchema.FromUrlAsync("http://schemas.sportradar.com/bsa/json/v1/endpoints/soccer/team_profile.json"); var json = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(json); } [Fact] public async Task When_schema_has_metadata_properties_it_can_still_be_read() { - //// Arrange + // Arrange var json = @"{ ""type"": ""object"", ""additionalProperties"": false, ""properties"": { ""$type"": { ""type"": ""string"", ""enum"": [ ""file"" ] }, ""Id"": { ""type"": ""string"", ""format"": ""guid"" }, ""Name"": { ""type"": ""string"" } }, ""required"": [ ""$type"", ""Id"", ""Name"" ] }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Assert + // Assert // No exception } @@ -419,7 +419,7 @@ public async Task When_azure_schema_is_loaded_then_no_exception() @"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json"); var json = schema.ToJson(); - //// Assert + // Assert Assert.NotNull(schema); Assert.Contains("The identity type.", json); } diff --git a/src/NJsonSchema.Tests/Serialization/AllowAdditionalPropertiesTests.cs b/src/NJsonSchema.Tests/Serialization/AllowAdditionalPropertiesTests.cs index fe41944a7..a3fb6c5ec 100644 --- a/src/NJsonSchema.Tests/Serialization/AllowAdditionalPropertiesTests.cs +++ b/src/NJsonSchema.Tests/Serialization/AllowAdditionalPropertiesTests.cs @@ -20,11 +20,11 @@ public class AllowAdditionalPropertiesTests [InlineData(SchemaType.Swagger2, "{\"additionalProperties\":{}}", true)] public async Task When_schema_JSON_is_deserialized_then_AllowAdditionalProperties_is_correct(SchemaType schemaType, string json, bool expectedAllowAdditionalProperties) { - //// Act + // Act var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); var schema = await JsonSchemaSerialization.FromJsonAsync(json, schemaType, null, factory, new DefaultContractResolver()); - //// Assert + // Assert Assert.Equal(expectedAllowAdditionalProperties, schema.AllowAdditionalProperties); } @@ -36,11 +36,11 @@ public void When_default_schema_is_serialized_then_AllowAdditionalProperties_is_ { // default schema (new JsonSchema) has always AllowAdditionalProperties = true - //// Act + // Act var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); var json = JsonSchemaSerialization.ToJson(new JsonSchema(), schemaType, new DefaultContractResolver(), Formatting.None); - //// Assert + // Assert Assert.Equal(expectedJson, json); } @@ -53,11 +53,11 @@ public void When_default_schema_is_serialized_then_AllowAdditionalProperties_is_ [InlineData(SchemaType.Swagger2, false, "{}")] public void When_AllowAdditionalProperties_is_true_then_JSON_is_correct(SchemaType schemaType, bool allowAdditionalProperties, string expectedJson) { - //// Act + // Act var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); var json = JsonSchemaSerialization.ToJson(new JsonSchema { AllowAdditionalProperties = allowAdditionalProperties }, schemaType, new DefaultContractResolver(), Formatting.None); - //// Assert + // Assert Assert.Equal(expectedJson, json); } } diff --git a/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs b/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs index dddc9e214..bfee510a2 100644 --- a/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs +++ b/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs @@ -10,7 +10,7 @@ public class DiscriminatorSerializationTests [Fact] public async Task When_discriminator_object_is_set_then_schema_is_correctly_serialized() { - //// Arrange + // Arrange var childSchema = new JsonSchema { Type = JsonObjectType.Object, @@ -33,12 +33,12 @@ public async Task When_discriminator_object_is_set_then_schema_is_correctly_seri } }; - //// Act + // Act var json = schema.ToJson(); var schema2 = await JsonSchema.FromJsonAsync(json); var json2 = schema2.ToJson(); - //// Assert + // Assert Assert.Contains(@"""propertyName"": ""discr""", json); Assert.Contains(@"""Bar"": ""#/definitions/Foo""", json); @@ -50,21 +50,21 @@ public async Task When_discriminator_object_is_set_then_schema_is_correctly_seri [Fact] public void When_discriminator_is_set_then_discriminator_object_is_created() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Discriminator = "discr"; - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Contains(@"""discr""", json); } [Fact] public void When_schema_is_serialized_for_Swagger_then_discriminator_is_string() { - //// Arrange + // Arrange var childSchema = new JsonSchema { Type = JsonObjectType.Object, @@ -87,10 +87,10 @@ public void When_schema_is_serialized_for_Swagger_then_discriminator_is_string() } }; - //// Act + // Act var json = JsonSchemaSerialization.ToJson(schema, SchemaType.Swagger2, new DefaultContractResolver(), Formatting.Indented); - //// Assert + // Assert Assert.Contains(@"""discriminator"": ""discr""", json); Assert.DoesNotContain(@"""Bar"": ""#/definitions/Foo""", json); } diff --git a/src/NJsonSchema.Tests/Serialization/ExtensionDataTests.cs b/src/NJsonSchema.Tests/Serialization/ExtensionDataTests.cs index 5f8583e0e..836a043ee 100644 --- a/src/NJsonSchema.Tests/Serialization/ExtensionDataTests.cs +++ b/src/NJsonSchema.Tests/Serialization/ExtensionDataTests.cs @@ -10,14 +10,14 @@ public class ExtensionDataTests [Fact] public async Task When_definition_entry_is_null_then_it_is_deserialized_correctly() { - //// Arrange + // Arrange var json = @"{ ""definitions"": { ""abc"": null } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var json2 = schema.ToJson(); - //// Assert + // Assert Assert.False(schema.Definitions.ContainsKey("abc")); Assert.DoesNotContain("abc", json2); } @@ -25,17 +25,17 @@ public async Task When_definition_entry_is_null_then_it_is_deserialized_correctl [Fact] public void When_schema_has_extension_data_property_then_property_is_in_serialized_json() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.ExtensionData = new Dictionary { { "Test", 123 } }; - //// Act + // Act var json = schema.ToJson(); - //// Assert + // Assert Assert.Contains(@"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""Test"": 123 @@ -45,33 +45,33 @@ public void When_schema_has_extension_data_property_then_property_is_in_serializ [Fact] public async Task When_json_schema_contains_unknown_data_then_extension_data_is_filled() { - //// Arrange + // Arrange var json = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""Test"": 123 }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Assert + // Assert Assert.Equal((long)123, schema.ExtensionData["Test"]); } [Fact] public async Task When_no_extension_data_is_available_then_property_is_null() { - //// Arrange + // Arrange var json = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Assert + // Assert Assert.Null(schema.ExtensionData); } @@ -86,26 +86,26 @@ public class MyTest [Fact] public async Task When_extension_data_attribute_is_used_on_class_then_extension_data_property_is_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(123, schema.ExtensionData["MyClass"]); } [Fact] public async Task When_extension_data_attribute_is_used_on_property_then_extension_data_property_is_set() { - //// Arrange + // Arrange - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal(2, schema.Properties["Property"].ExtensionData["Foo"]); Assert.Equal(3, schema.Properties["Property"].ExtensionData["Bar"]); } @@ -143,10 +143,10 @@ public class MyCustomAttributeTest [Fact] public async Task When_extension_data_interface_is_used_on_property_then_extension_data_property_is_set() { - //// Act + // Act var schema = NewtonsoftJsonSchemaGenerator.FromType(); - //// Assert + // Assert Assert.Equal("My custom logic", schema.Properties["Property"].ExtensionData["My custom key"]); Assert.Equal("My custom logic", schema.Properties["Property"].ExtensionData["My custom key"]); } @@ -154,7 +154,7 @@ public async Task When_extension_data_interface_is_used_on_property_then_extensi [Fact] public async Task When_reference_references_schema_in_custom_properties_then_the_references_are_resolved() { - //// Arrange + // Arrange var json = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", @@ -205,11 +205,11 @@ public async Task When_reference_references_schema_in_custom_properties_then_the } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); var json2 = schema.ToJson(); - //// Assert + // Assert Assert.Equal(json.Replace("\r", string.Empty), json2.Replace("\r", string.Empty)); } } diff --git a/src/NJsonSchema.Tests/Validation/ArrayValidationTests.cs b/src/NJsonSchema.Tests/Validation/ArrayValidationTests.cs index a500bf29d..b6aeb9c5c 100644 --- a/src/NJsonSchema.Tests/Validation/ArrayValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/ArrayValidationTests.cs @@ -9,20 +9,20 @@ public class ArrayValidationTests [Fact] public async Task When_json_is_array_then_validate_should_not_throw_an_exception() { - //// Act + // Act var svc = await JsonSchema.FromJsonAsync(@"{ ""type"": ""array"", ""items"": { ""type"":""string"" } }"); - //// Assert - Assert.Equal(0, svc.Validate(JToken.Parse("[]")).Count); - Assert.Equal(0, svc.Validate(JToken.Parse(@"[""test""]")).Count); - Assert.Equal(0, svc.Validate("[]").Count); - Assert.Equal(0, svc.Validate(@"[""test""]").Count); + // Assert + Assert.Empty(svc.Validate(JToken.Parse("[]"))); + Assert.Empty(svc.Validate(JToken.Parse(@"[""test""]"))); + Assert.Empty(svc.Validate("[]")); + Assert.Empty(svc.Validate(@"[""test""]")); } [Fact] public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_items_are_allowed() { - //// Arrange + // Arrange var json = @"{ 'properties': { 'emptySchema': { 'type': 'array' } @@ -30,28 +30,28 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_ }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var errors1 = schema.Validate("{ 'emptySchema': [1, 2, 'abc'] }"); var errors2 = schema.Validate("{ 'emptySchema': 123 }"); - //// Assert - Assert.Equal(0, errors1.Count); - Assert.Equal(1, errors2.Count); + // Assert + Assert.Empty(errors1); + Assert.Single(errors2); } [Fact] public void When_token_is_not_array_then_validation_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.ArrayExpected, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -59,7 +59,7 @@ public void When_token_is_not_array_then_validation_should_fail() [Fact] public void When_tuple_correct_then_it_should_pass() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.Items.Add(new JsonSchema { Type = JsonObjectType.String }); @@ -69,17 +69,17 @@ public void When_tuple_correct_then_it_should_pass() token.Add(new JValue("Foo")); token.Add(new JValue(5)); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_tuple_too_large_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.Items.Add(new JsonSchema { Type = JsonObjectType.String }); @@ -91,10 +91,10 @@ public void When_tuple_too_large_then_it_should_fail() token.Add(new JValue(5)); token.Add(new JValue(5)); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Single(errors); Assert.Equal(ValidationErrorKind.TooManyItemsInTuple, errors.First().Kind); Assert.Same(schema, errors.First().Schema); @@ -103,7 +103,7 @@ public void When_tuple_too_large_then_it_should_fail() [Fact] public void When_array_item_are_valid_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.Item = new JsonSchema(); @@ -113,17 +113,17 @@ public void When_array_item_are_valid_then_it_should_succeed() token.Add(new JValue("Foo")); token.Add(new JValue("Bar")); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_second_item_validation_fails_then_path_should_be_correct() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.Item = new JsonSchema(); @@ -133,10 +133,10 @@ public void When_second_item_validation_fails_then_path_should_be_correct() token.Add(new JValue("Foo")); token.Add(new JValue(10)); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Single(errors); Assert.Equal(ValidationErrorKind.ArrayItemNotValid, errors.First().Kind); @@ -150,7 +150,7 @@ public void When_second_item_validation_fails_then_path_should_be_correct() [Fact] public void When_max_item_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.MaxItems = 1; @@ -161,10 +161,10 @@ public void When_max_item_does_not_match_then_it_should_fail() token.Add(new JValue("Foo")); token.Add(new JValue("Bar")); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Single(errors); Assert.Equal(ValidationErrorKind.TooManyItems, errors.First().Kind); Assert.Same(schema, errors.First().Schema); @@ -173,7 +173,7 @@ public void When_max_item_does_not_match_then_it_should_fail() [Fact] public void When_min_items_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.MinItems = 2; @@ -183,10 +183,10 @@ public void When_min_items_does_not_match_then_it_should_fail() var token = new JArray(); token.Add(new JValue("Foo")); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Single(errors); Assert.Equal(ValidationErrorKind.TooFewItems, errors.First().Kind); Assert.Same(schema, errors.First().Schema); @@ -195,7 +195,7 @@ public void When_min_items_does_not_match_then_it_should_fail() [Fact] public void When_unique_items_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Array; schema.UniqueItems = true; @@ -206,10 +206,10 @@ public void When_unique_items_does_not_match_then_it_should_fail() token.Add(new JValue("Foo")); token.Add(new JValue("Foo")); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Single(errors); Assert.Equal(ValidationErrorKind.ItemsNotUnique, errors.First().Kind); Assert.Same(schema, errors.First().Schema); @@ -218,7 +218,7 @@ public void When_unique_items_does_not_match_then_it_should_fail() [Fact] public async Task When_null_is_allowed_then_properties_are_not_checked() { - //// Arrange + // Arrange var schemaJson = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""type"": ""array"", @@ -233,11 +233,11 @@ public async Task When_null_is_allowed_then_properties_are_not_checked() }"; var schema = await JsonSchema.FromJsonAsync(schemaJson); - //// Act + // Act var errors = schema.Validate("[{\"value\":2},null]"); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } } } diff --git a/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs b/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs index 0754c8911..d47786ac1 100644 --- a/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs @@ -11,7 +11,7 @@ public class CustomValidationTests [Fact] public void When_format_date_time_correct_with_custom_validator_passed_then_no_errors() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -20,13 +20,13 @@ public void When_format_date_time_correct_with_custom_validator_passed_then_no_e var token = new JValue("2014-12-01 11:00:01:55"); - //// Act + // Act var settings = new JsonSchemaValidatorSettings(); settings.AddCustomFormatValidator(new CustomDateTimeFormatValidator()); var errors = schema.Validate(token, settings); - //// Assert + // Assert Assert.Empty(errors); } diff --git a/src/NJsonSchema.Tests/Validation/EnumValidationTests.cs b/src/NJsonSchema.Tests/Validation/EnumValidationTests.cs index 5769810c0..d40c98283 100644 --- a/src/NJsonSchema.Tests/Validation/EnumValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/EnumValidationTests.cs @@ -7,7 +7,7 @@ public class EnumValidationTests [Fact] public async Task When_enum_is_defined_without_type_then_validation_succeeds_for_correct_value() { - //// Arrange + // Arrange var json = @"{ ""enum"": [ @@ -17,17 +17,17 @@ public async Task When_enum_is_defined_without_type_then_validation_succeeds_for }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var errors = schema.Validate(@"""commercial"""); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public async Task When_enum_is_defined_without_type_then_validation_fails_for_wrong_value() { - //// Arrange + // Arrange var json = @"{ ""enum"": [ @@ -37,17 +37,17 @@ public async Task When_enum_is_defined_without_type_then_validation_fails_for_wr }"; var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var errors = schema.Validate(@"""wrong"""); - //// Assert - Assert.Equal(1, errors.Count); + // Assert + Assert.Single(errors); } [Fact] public async Task When_enumeration_has_null_then_validation_works() { - //// Arrange + // Arrange var json = @" { ""properties"": { @@ -67,20 +67,20 @@ public async Task When_enumeration_has_null_then_validation_works() } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var errors = schema.Validate(@"{ ""SalutationType"": ""Prof"" }"); - //// Assert - Assert.Equal(1, errors.Count); + // Assert + Assert.Single(errors); } [Fact] public async Task When_enumeration_has_null_and_value_is_null_then_no_validation_errors() { - //// Arrange + // Arrange var json = @" { ""properties"": { @@ -99,20 +99,20 @@ public async Task When_enumeration_has_null_and_value_is_null_then_no_validation } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var errors = schema.Validate(@"{ ""SalutationType"": null }"); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public async Task When_enumeration_doesnt_have_null_and_value_is_null_then_validation_fails() { - //// Arrange + // Arrange var json = @" { ""properties"": { @@ -130,13 +130,13 @@ public async Task When_enumeration_doesnt_have_null_and_value_is_null_then_valid } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(json); - //// Act + // Act var errors = schema.Validate(@"{ ""SalutationType"": null }"); - //// Assert + // Assert Assert.Equal(2, errors.Count); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs b/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs index 6381beb21..683a7f180 100644 --- a/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs @@ -9,7 +9,7 @@ public class FormatBase64Tests [Fact] public void Validation_should_fail_if_string_is_not_base64_formatted() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -18,18 +18,18 @@ public void Validation_should_fail_if_string_is_not_base64_formatted() var token = new JValue("invalid"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(1, errors.Count); + // Assert + Assert.Single(errors); Assert.Equal(ValidationErrorKind.Base64Expected, errors.Single().Kind); } [Fact] public void Validation_should_fail_if_string_is_not_byte_formatted() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -38,18 +38,18 @@ public void Validation_should_fail_if_string_is_not_byte_formatted() var token = new JValue("invalid"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(1, errors.Count); + // Assert + Assert.Single(errors); Assert.Equal(ValidationErrorKind.Base64Expected, errors.Single().Kind); } [Fact] public void Validation_should_succeed_if_string_is_base64_formatted_with_trailing_equals() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -59,17 +59,17 @@ public void Validation_should_succeed_if_string_is_base64_formatted_with_trailin var value = Convert.ToBase64String([101, 22, 87, 25]); var token = new JValue(value); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void Validation_should_succeed_if_string_is_byte_formatted_with_trailing_equals() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -79,17 +79,17 @@ public void Validation_should_succeed_if_string_is_byte_formatted_with_trailing_ var value = Convert.ToBase64String([101, 22, 87, 25]); var token = new JValue(value); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void Validation_should_succeed_if_string_is_base64_formatted_without_trailing_equals() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -99,17 +99,17 @@ public void Validation_should_succeed_if_string_is_base64_formatted_without_trai var value = Convert.ToBase64String([1, 2, 3]); var token = new JValue(value); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void Validation_should_succeed_if_string_is_byte_formatted_without_trailing_equals() { - //// Arrange + // Arrange var schema = new JsonSchema { Type = JsonObjectType.String, @@ -119,17 +119,17 @@ public void Validation_should_succeed_if_string_is_byte_formatted_without_traili var value = Convert.ToBase64String([1, 2, 3]); var token = new JValue(value); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void Numeric_type_should_not_trigger_validation_if_has_byte_format() { - //// Arrange + // Arrange var numericSchema = new JsonSchema { Type = JsonObjectType.Integer, @@ -138,11 +138,11 @@ public void Numeric_type_should_not_trigger_validation_if_has_byte_format() var token = new JValue(1); - //// Act + // Act var numericErrors = numericSchema.Validate(token); - //// Assert - Assert.Equal(0, numericErrors.Count); + // Assert + Assert.Empty(numericErrors); } } } diff --git a/src/NJsonSchema.Tests/Validation/FormatDateTests.cs b/src/NJsonSchema.Tests/Validation/FormatDateTests.cs index 5a03d25b5..f78214e62 100644 --- a/src/NJsonSchema.Tests/Validation/FormatDateTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatDateTests.cs @@ -9,51 +9,51 @@ public class FormatDateTests [Fact] public void When_format_date_incorrect_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Date; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.DateExpected, errors.First().Kind); } [Fact] public void When_format_date_time_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Date; var token = new JValue("2014-12-01 11:54"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.DateExpected, errors.First().Kind); } [Fact] public void When_format_date_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Date; var token = new JValue("2014-12-01"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs b/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs index b2ef352fa..aa68202d7 100644 --- a/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs @@ -9,102 +9,102 @@ public class FormatDateTimeTests [Fact] public void When_format_date_time_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.DateTime; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.DateTimeExpected, errors.First().Kind); } [Fact] public void When_format_date_time_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.DateTime; var token = new JValue("2014-12-01 11:00:01"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_format_date_time_with_non_iso8601_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.DateTime; var token = new JValue("25/01/2015"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.DateTimeExpected, errors.First().Kind); } [Fact] public void When_format_date_time_with_iso8601_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.DateTime; var token = new JValue("2015-01-25T15:43:30Z"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_format_date_time_with_iso8601_with_timezone_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.DateTime; var token = new JValue("2015-01-25T15:43:30+10:00"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_format_date_time_with_iso8601_and_fractional_seconds_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.DateTime; var token = new JValue("2015-01-25T15:43:30.1234567Z"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs b/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs index 565b41cc8..65c59c585 100644 --- a/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs @@ -9,34 +9,34 @@ public class FormatEmailTests [Fact] public void When_format_email_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Email; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.EmailExpected, errors.First().Kind); } [Fact] public void When_format_email_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Email; var token = new JValue("mail@rsuter.com"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs b/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs index 546d41322..9cb7535ed 100644 --- a/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs @@ -9,24 +9,24 @@ public class FormatGuidTests [Fact] public void When_format_guid_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Guid; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.GuidExpected, errors.First().Kind); } [Fact] public void When_format_guid_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Guid; @@ -34,10 +34,10 @@ public void When_format_guid_correct_then_validation_succeeds() var guid = Guid.NewGuid().ToString(); var token = new JValue(guid); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs b/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs index 36af83774..df59fa307 100644 --- a/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs @@ -9,34 +9,34 @@ public class FormatHostnameTests [Fact] public void When_format_hostname_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Hostname; var token = new JValue("foo:bar"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.HostnameExpected, errors.First().Kind); } [Fact] public void When_format_hostname_is_ip_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Hostname; var token = new JValue("rsuter.com"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs b/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs index 74daa7774..33cbba1b0 100644 --- a/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs @@ -9,34 +9,34 @@ public class FormatIpV4Tests [Fact] public void When_format_ipv4_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.IpV4; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.IpV4Expected, errors.First().Kind); } [Fact] public void When_format_ipv4_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.IpV4; var token = new JValue("192.168.0.1"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs b/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs index 1cc250227..73c245f52 100644 --- a/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs @@ -9,34 +9,34 @@ public class FormatIpV6Tests [Fact] public void When_format_ipv6_incorrect_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.IpV6; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.IpV6Expected, errors.First().Kind); } [Fact] public void When_format_ipv6_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.IpV6; var token = new JValue("::1"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs b/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs index 5242b4342..0155d0796 100644 --- a/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs @@ -9,34 +9,34 @@ public class FormatTimeSpanTests [Fact] public void When_format_time_span_incorrect_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.TimeSpan; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.TimeSpanExpected, errors.First().Kind); } [Fact] public void When_format_time_span_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.TimeSpan; var token = new JValue("1:30:45"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs b/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs index c01b0e841..a3e351873 100644 --- a/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs @@ -13,85 +13,85 @@ public class FormatTimeTests [Fact] public void When_format_time_incorrect_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Time; var token = new JValue("10 am"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.TimeExpected, errors.First().Kind); } [Fact] public void When_format_time_positive_offset_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Time; var token = new JValue("14:30:00+02:00"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_format_time_has_negative_offset_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Time; var token = new JValue("14:30:00-02:00"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_format_time_is_utc_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Time; var token = new JValue("14:30:00Z"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_format_time_secfrac_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Time; var token = new JValue("14:30:00.1234567+02:00"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } @@ -102,17 +102,17 @@ public void When_format_time_secfrac_correct_then_validation_succeeds() [Fact] public void When_format_time_is_not_utc_or_offset_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Time; var token = new JValue("14:30:00"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatUriTests.cs b/src/NJsonSchema.Tests/Validation/FormatUriTests.cs index 3f87cf2dd..0b30a4656 100644 --- a/src/NJsonSchema.Tests/Validation/FormatUriTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatUriTests.cs @@ -9,34 +9,34 @@ public class FormatUriTests [Fact] public void When_format_uri_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Uri; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.UriExpected, errors.First().Kind); } [Fact] public void When_format_uri_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Uri; var token = new JValue("http://rsuter.com"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs index 7c0a01881..1c769a5d5 100644 --- a/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs @@ -9,24 +9,24 @@ public class FormatUuidTests [Fact] public void When_format_uuid_incorrect_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Uuid; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.UuidExpected, errors.First().Kind); } [Fact] public void When_format_uuid_correct_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Format = JsonFormatStrings.Uuid; @@ -34,10 +34,10 @@ public void When_format_uuid_correct_then_validation_succeeds() var uuid = Guid.NewGuid().ToString(); var token = new JValue(uuid); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/InheritanceTests.cs b/src/NJsonSchema.Tests/Validation/InheritanceTests.cs index 8c85e909e..781599002 100644 --- a/src/NJsonSchema.Tests/Validation/InheritanceTests.cs +++ b/src/NJsonSchema.Tests/Validation/InheritanceTests.cs @@ -9,7 +9,7 @@ public class InheritanceTests [Fact] public void When_is_any_of_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.AnyOf.Add(new JsonSchema { @@ -22,17 +22,17 @@ public void When_is_any_of_then_it_should_succeed() var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void When_is_not_any_of_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.AnyOf.Add(new JsonSchema { @@ -45,10 +45,10 @@ public void When_is_not_any_of_then_it_should_fail() var token = new JValue(1.5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert var error = (ChildSchemaValidationError)errors.First(); Assert.Equal(ValidationErrorKind.NotAnyOf, error.Kind); Assert.Same(schema, error.Schema); @@ -59,7 +59,7 @@ public void When_is_not_any_of_then_it_should_fail() [Fact] public void When_is_all_of_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.AnyOf.Add(new JsonSchema { @@ -72,17 +72,17 @@ public void When_is_all_of_then_it_should_succeed() var token = new JValue("Foo"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void When_is_not_all_of_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.AllOf.Add(new JsonSchema { @@ -95,10 +95,10 @@ public void When_is_not_all_of_then_it_should_fail() var token = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NotAllOf, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -106,7 +106,7 @@ public void When_is_not_all_of_then_it_should_fail() [Fact] public void When_is_one_of_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.OneOf.Add(new JsonSchema { @@ -119,17 +119,17 @@ public void When_is_one_of_then_it_should_succeed() var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void When_is_not_one_of_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.OneOf.Add(new JsonSchema { @@ -142,10 +142,10 @@ public void When_is_not_one_of_then_it_should_fail() var token = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NotOneOf, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -153,7 +153,7 @@ public void When_is_not_one_of_then_it_should_fail() [Fact] public void When_one_of_matches_multiple_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.OneOf.Add(new JsonSchema { @@ -170,10 +170,10 @@ public void When_one_of_matches_multiple_then_it_should_fail() var token = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NotOneOf, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -181,7 +181,7 @@ public void When_one_of_matches_multiple_then_it_should_fail() [Fact] public void When_matches_excluded_schema_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Not = new JsonSchema { @@ -190,10 +190,10 @@ public void When_matches_excluded_schema_then_it_should_fail() var token = new JValue("Foo"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.ExcludedSchemaValidates, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -201,7 +201,7 @@ public void When_matches_excluded_schema_then_it_should_fail() [Fact] public void When_not_matches_excluded_schema_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Not = new JsonSchema { @@ -210,11 +210,11 @@ public void When_not_matches_excluded_schema_then_it_should_succeed() var token = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } } } diff --git a/src/NJsonSchema.Tests/Validation/LineInformationTest.cs b/src/NJsonSchema.Tests/Validation/LineInformationTest.cs index 6f8d49067..86c13313d 100644 --- a/src/NJsonSchema.Tests/Validation/LineInformationTest.cs +++ b/src/NJsonSchema.Tests/Validation/LineInformationTest.cs @@ -70,13 +70,13 @@ private async Task InitAsync() [Fact] public async Task When_validating_from_string_parse_line_information() { - //// Arrange + // Arrange await InitAsync(); - //// Act + // Act var errors = Schema.Validate(Json); - //// Assert + // Assert Assert.Equal(7, errors.Count); // Seven validation errors expected. Assert.Equal(3, errors.OfType().Single(error => error.Kind == ValidationErrorKind.NotOneOf).Errors.Count); // Three NotOneOf clause violations expected Assert.Equal(2, errors.OfType().Single(error => error.Kind == ValidationErrorKind.NotAllOf).Errors.Count); // Two NotAllOf clause violations expected @@ -86,10 +86,10 @@ public async Task When_validating_from_string_parse_line_information() [Fact] public async Task When_validating_from_jtoken_parse_line_information_if_exists() { - //// Arrange + // Arrange await InitAsync(); - //// Act + // Act var tokenWithInfo = JToken.Parse(Json, new JsonLoadSettings() { LineInfoHandling = LineInfoHandling.Load }); var errorsWithInfo = Schema.Validate(tokenWithInfo); var tokenNoInfoParse = JToken.Parse(Json, new JsonLoadSettings() { LineInfoHandling = LineInfoHandling.Ignore }); @@ -97,7 +97,7 @@ public async Task When_validating_from_jtoken_parse_line_information_if_exists() var tokenNoInfoDeserialize = JsonConvert.DeserializeObject(Json); var errorsNoInfoDeserialize = Schema.Validate(tokenNoInfoDeserialize); - //// Assert + // Assert ValidateErrors(errorsWithInfo, true); ValidateErrors(errorsNoInfoParse, false); ValidateErrors(errorsNoInfoDeserialize, false); diff --git a/src/NJsonSchema.Tests/Validation/NullPropertyTests.cs b/src/NJsonSchema.Tests/Validation/NullPropertyTests.cs index d319a199f..04292f844 100644 --- a/src/NJsonSchema.Tests/Validation/NullPropertyTests.cs +++ b/src/NJsonSchema.Tests/Validation/NullPropertyTests.cs @@ -8,7 +8,7 @@ public class NullPropertyTests [Fact] public void When_property_can_be_null_then_null_is_allowed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Properties["test"] = new JsonSchemaProperty { @@ -19,12 +19,12 @@ public void When_property_can_be_null_then_null_is_allowed() Type = JsonObjectType.String }; - //// Act + // Act var data = "{ 'test': null }"; var errors = schema.Validate(data); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } public class NullablePropertyClass @@ -43,18 +43,18 @@ public class QueryRule [InlineData(SchemaType.Swagger2)] public async Task When_object_property_can_be_null_then_null_is_allowed(SchemaType schemaType) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = schemaType }); - //// Act + // Act var data = "{ 'Value': null }"; var errors = schema.Validate(data, schemaType); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Theory] @@ -63,18 +63,18 @@ public async Task When_object_property_can_be_null_then_null_is_allowed(SchemaTy [InlineData(SchemaType.Swagger2)] public async Task When_number_property_can_be_null_then_null_is_allowed(SchemaType schemaType) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = schemaType }); - //// Act + // Act var data = "{ 'Value': null }"; var errors = schema.Validate(data, schemaType); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Theory] @@ -83,18 +83,18 @@ public async Task When_number_property_can_be_null_then_null_is_allowed(SchemaTy [InlineData(SchemaType.Swagger2)] public async Task When_string_property_can_be_null_then_null_is_allowed(SchemaType schemaType) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = schemaType }); - //// Act + // Act var data = "{ 'Value': null }"; var errors = schema.Validate(data, schemaType); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Theory] @@ -103,18 +103,18 @@ public async Task When_string_property_can_be_null_then_null_is_allowed(SchemaTy [InlineData(SchemaType.Swagger2)] public async Task When_boolean_property_can_be_null_then_null_is_allowed(SchemaType schemaType) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = schemaType }); - //// Act + // Act var data = "{ 'Value': null }"; var errors = schema.Validate(data, schemaType); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Theory] @@ -123,18 +123,18 @@ public async Task When_boolean_property_can_be_null_then_null_is_allowed(SchemaT [InlineData(SchemaType.Swagger2)] public async Task When_array_property_can_be_null_then_null_is_allowed(SchemaType schemaType) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = schemaType }); - //// Act + // Act var data = "{ 'Value': null }"; var errors = schema.Validate(data, schemaType); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Theory] @@ -143,18 +143,18 @@ public async Task When_array_property_can_be_null_then_null_is_allowed(SchemaTyp [InlineData(SchemaType.Swagger2)] public async Task When_enum_property_can_be_null_then_null_is_allowed(SchemaType schemaType) { - //// Arrange + // Arrange var schema = NewtonsoftJsonSchemaGenerator.FromType>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = schemaType }); - //// Act + // Act var data = "{ 'Value': null }"; var errors = schema.Validate(data, schemaType); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } } } \ No newline at end of file diff --git a/src/NJsonSchema.Tests/Validation/NumberTests.cs b/src/NJsonSchema.Tests/Validation/NumberTests.cs index 374b08b96..0a5f186ec 100644 --- a/src/NJsonSchema.Tests/Validation/NumberTests.cs +++ b/src/NJsonSchema.Tests/Validation/NumberTests.cs @@ -7,7 +7,7 @@ public class NumberTests [Fact] public async Task When_double_is_bigger_then_decimal_then_validation_works() { - /// Arrange + // Arrange const string json = @"{ 'schema': 'http://json-schema.org/draft-04/schema', 'title': 'NumberWithCircleVisualisationData', @@ -28,13 +28,13 @@ public async Task When_double_is_bigger_then_decimal_then_validation_works() 'UpperLimit': 1.1111111111111111E+101 }"; - var validationSchema = JsonSchema.FromJsonAsync(json).Result; + var validationSchema = await JsonSchema.FromJsonAsync(json); - /// Act + // Act var errors = validationSchema.Validate(data); - /// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact(Skip = "Ignored")] @@ -42,7 +42,7 @@ public async Task When_integer_is_big_integer_then_validation_works() { // See https://github.com/RicoSuter/NJsonSchema/issues/568 - /// Arrange + // Arrange const string json = @"{ ""type"": ""object"", ""properties"": { @@ -59,14 +59,13 @@ public async Task When_integer_is_big_integer_then_validation_works() ""property1"": 34545734242323232423434 }"; - var validationSchema = JsonSchema.FromJsonAsync(json).Result; + var validationSchema = await JsonSchema.FromJsonAsync(json); - /// Act + // Act var errors = validationSchema.Validate(data); - /// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } - } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs b/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs index 8cb3fe71b..c9e167359 100644 --- a/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs @@ -9,17 +9,17 @@ public class ObjectValidationTests [Fact] public void When_token_is_not_object_then_validation_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty(); var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.ObjectExpected, errors.First().Kind); Assert.Equal("10", errors.First().Token?.ToString()); } @@ -27,7 +27,7 @@ public void When_token_is_not_object_then_validation_should_fail() [Fact] public void When_required_property_is_missing_then_it_should_be_in_error_list() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty @@ -37,10 +37,10 @@ public void When_required_property_is_missing_then_it_should_be_in_error_list() var token = new JObject(); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Single(errors); Assert.Equal("Foo", errors.First().Property); Assert.Equal("#/Foo", errors.First().Path); @@ -51,7 +51,7 @@ public void When_required_property_is_missing_then_it_should_be_in_error_list() [Fact] public void When_property_matches_one_of_the_types_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty @@ -62,17 +62,17 @@ public void When_property_matches_one_of_the_types_then_it_should_succeed() var token = new JObject(); token["Foo"] = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_optional_property_is_missing_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty @@ -82,17 +82,17 @@ public void When_optional_property_is_missing_then_it_should_succeed() var token = new JObject(); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_string_property_is_available_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty @@ -104,17 +104,17 @@ public void When_string_property_is_available_then_it_should_succeed() var token = new JObject(); token["Foo"] = new JValue("Bar"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_string_property_required_but_integer_provided_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.Properties["Foo"] = new JsonSchemaProperty @@ -126,10 +126,10 @@ public void When_string_property_required_but_integer_provided_then_it_should_fa var token = new JObject(); token["Foo"] = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.StringExpected, errors.First().Kind); Assert.Equal("Foo", errors.First().Property); Assert.Equal("#/Foo", errors.First().Path); @@ -139,7 +139,7 @@ public void When_string_property_required_but_integer_provided_then_it_should_fa [Fact] public async Task When_type_property_has_integer_type_then_it_is_validated_correctly() { - //// Arrange + // Arrange var schema = await JsonSchema.FromJsonAsync( @"{ ""$schema"": ""http://json-schema.org/draft-06/schema#"", @@ -150,20 +150,20 @@ public async Task When_type_property_has_integer_type_then_it_is_validated_corre } }"); - //// Act + // Act var errors = schema.Validate( @"{ ""type"": 1 }"); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void When_case_sensitive_and_property_has_different_casing_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.AllowAdditionalProperties = false; @@ -175,10 +175,10 @@ public void When_case_sensitive_and_property_has_different_casing_then_it_should var token = new JObject(); token["foo"] = new JValue(5); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NoAdditionalPropertiesAllowed, errors.First().Kind); Assert.Equal("\"foo\": 5", errors.First().Token?.ToString()); } @@ -186,7 +186,7 @@ public void When_case_sensitive_and_property_has_different_casing_then_it_should [Fact] public void When_case_insensitive_and_property_has_different_casing_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.AllowAdditionalProperties = false; @@ -203,10 +203,10 @@ public void When_case_insensitive_and_property_has_different_casing_then_it_shou PropertyStringComparer = StringComparer.OrdinalIgnoreCase, }); - //// Act + // Act var errors = validator.Validate(token, schema); - //// Assert + // Assert Assert.Empty(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/OneOfValidationTests.cs b/src/NJsonSchema.Tests/Validation/OneOfValidationTests.cs index 2d3edfc57..ee1de99b9 100644 --- a/src/NJsonSchema.Tests/Validation/OneOfValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/OneOfValidationTests.cs @@ -34,7 +34,7 @@ public async Task When_has_one_of_then_it_is_validated_correctly() { var errors = schema.Validate(match); - Assert.Equal(0, errors.Count); + Assert.Empty(errors); } } @@ -45,7 +45,7 @@ public async Task When_does_not_have_one_of_then_it_is_invalid() var errors = schema.Validate(@"{ ""A"": ""string"" }"); - Assert.Equal(1, errors.Count); + Assert.Single(errors); } [Fact] @@ -55,7 +55,7 @@ public async Task When_has_more_than_one_of_then_it_is_invalid() var errors = schema.Validate(@"{ ""A"": ""string"", ""B"": 1, ""C"": 2 }"); - Assert.Equal(1, errors.Count); + Assert.Single(errors); } } } diff --git a/src/NJsonSchema.Tests/Validation/PatternPropertyValidationTests.cs b/src/NJsonSchema.Tests/Validation/PatternPropertyValidationTests.cs index d14f5e58c..f2d0bbe46 100644 --- a/src/NJsonSchema.Tests/Validation/PatternPropertyValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/PatternPropertyValidationTests.cs @@ -9,7 +9,7 @@ public class PatternPropertyValidationTests [Fact] public void When_there_are_no_properties_matching_pattern_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.AllowAdditionalProperties = false; @@ -19,10 +19,10 @@ public void When_there_are_no_properties_matching_pattern_then_validation_fails( token.Add("123", new JObject()); token.Add("qwe123", new JObject()); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(2, errors.Count()); foreach (var validationError in errors) { @@ -33,7 +33,7 @@ public void When_there_are_no_properties_matching_pattern_then_validation_fails( [Fact] public void When_there_are_properties_matching_pattern_but_types_doesnt_match_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.AllowAdditionalProperties = false; @@ -43,10 +43,10 @@ public void When_there_are_properties_matching_pattern_but_types_doesnt_match_th token.Add("qwerty", new JArray()); token.Add("wsad", new JValue("test")); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(2, errors.Count()); Assert.True(errors.All(error => error.Kind == ValidationErrorKind.AdditionalPropertiesNotValid)); } @@ -54,7 +54,7 @@ public void When_there_are_properties_matching_pattern_but_types_doesnt_match_th [Fact] public void When_there_are_properties_matching_pattern_and_types_matches_then_validation_succeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Object; schema.AllowAdditionalProperties = false; @@ -64,10 +64,10 @@ public void When_there_are_properties_matching_pattern_and_types_matches_then_va token.Add("qwerty", new JObject()); token.Add("wsad", new JObject()); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } diff --git a/src/NJsonSchema.Tests/Validation/SchemaTests.cs b/src/NJsonSchema.Tests/Validation/SchemaTests.cs index 5b734df80..5615dd416 100644 --- a/src/NJsonSchema.Tests/Validation/SchemaTests.cs +++ b/src/NJsonSchema.Tests/Validation/SchemaTests.cs @@ -8,7 +8,7 @@ public class SchemaTests [Fact] public async Task When_no_additional_properties_are_allowed_then_this_error_is_returned() { - //// Arrange + // Arrange var schemaData = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""type"": ""object"", @@ -316,11 +316,11 @@ public async Task When_no_additional_properties_are_allowed_then_this_error_is_r "; var schema = await JsonSchema.FromJsonAsync(schemaData); - //// Act + // Act var errors = schema.Validate(@"{""Key"": ""Value""}"); var error = errors.SingleOrDefault(e => e.Kind == ValidationErrorKind.NoAdditionalPropertiesAllowed); - //// Assert + // Assert Assert.NotNull(error); Assert.Equal("#/Key", error.Path); Assert.Same(schema, error.Schema); @@ -329,7 +329,7 @@ public async Task When_no_additional_properties_are_allowed_then_this_error_is_r [Fact] public async Task When_multiple_types_fail_with_errors_take_the_best_group() { - //// Arrange + // Arrange var schemaJson = @"{ ""$schema"": ""http://json-schema.org/schema#"", ""type"": ""object"", @@ -369,19 +369,19 @@ public async Task When_multiple_types_fail_with_errors_take_the_best_group() } }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(schemaJson); var errors = schema.Validate(json); - //// Assert - Assert.Equal(1, errors.Count); + // Assert + Assert.Single(errors); Assert.Contains(errors, e => e.Kind == ValidationErrorKind.NoTypeValidates); } [Fact] public async Task When_datetime_with_regex_validation_then_datetime_is_not_altered() { - //// Arrange + // Arrange var schemaJson = @" { ""$schema"": ""http://json-schema.org/draft-07/schema#"", @@ -402,18 +402,18 @@ public async Task When_datetime_with_regex_validation_then_datetime_is_not_alter ""my_datetime"": ""2018-12-19T16:58:07.270Z"" }"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(schemaJson); var errors = schema.Validate(json); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public async Task When_property_name_is_ref_then_validation_works() { - //// Arrange + // Arrange var jsonSchema = @"{ ""$schema"": ""http://json-schema.org/draft-07/schema"", ""$ref"": ""#/definitions/reference_to_other_object"", @@ -441,17 +441,17 @@ public async Task When_property_name_is_ref_then_validation_works() } }"; - //// Act + // Act var jsonContent = @"{ ""$ref"": ""#/datatypes/MyCustomDataType"" }"; - //// Arrange + // Arrange var validator = new JsonSchemaValidator(new JsonSchemaValidatorSettings()); var schema = await JsonSchema.FromJsonAsync(jsonSchema); var result = validator.Validate(jsonContent, schema); - //// Assert + // Assert Assert.Empty(result); } } diff --git a/src/NJsonSchema.Tests/Validation/UniqueItemsTests.cs b/src/NJsonSchema.Tests/Validation/UniqueItemsTests.cs index 7bc1f86aa..a333d81cd 100644 --- a/src/NJsonSchema.Tests/Validation/UniqueItemsTests.cs +++ b/src/NJsonSchema.Tests/Validation/UniqueItemsTests.cs @@ -7,7 +7,7 @@ public class UniqueItemsTests [Fact] public async Task When_unique_items_is_set_and_items_are_objects_then_validation_works() { - //// Arrange + // Arrange var jsonSchema = @"{ ""$schema"": ""http://json-schema.org/draft-04/schema#"", ""title"": ""Config"", @@ -40,11 +40,11 @@ public async Task When_unique_items_is_set_and_items_are_objects_then_validation ""value"": ""b"" }]"; - //// Act + // Act var schema = await JsonSchema.FromJsonAsync(jsonSchema); var errors = schema.Validate(jsonData).ToList(); - //// Assert + // Assert Assert.Single(errors); } } diff --git a/src/NJsonSchema.Tests/Validation/ValueTypeValidationTests.cs b/src/NJsonSchema.Tests/Validation/ValueTypeValidationTests.cs index ec3f56fa4..801c7aa42 100644 --- a/src/NJsonSchema.Tests/Validation/ValueTypeValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/ValueTypeValidationTests.cs @@ -9,32 +9,32 @@ public class ValueTypeValidationTests [Fact] public void When_string_required_and_string_provided_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; var token = new JValue("test"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_string_required_but_integer_provided_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.StringExpected, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -42,32 +42,32 @@ public void When_string_required_but_integer_provided_then_validation_fails() [Fact] public void When_number_required_and_integer_provided_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Number; var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_number_required_but_string_provided_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Number; var token = new JValue("foo"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NumberExpected, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -75,32 +75,32 @@ public void When_number_required_but_string_provided_then_validation_fails() [Fact] public void When_integer_required_and_integer_provided_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Integer; var token = new JValue(10); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_integer_required_but_string_provided_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Integer; var token = new JValue("foo"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.IntegerExpected, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -108,32 +108,32 @@ public void When_integer_required_but_string_provided_then_validation_fails() [Fact] public void When_boolean_required_and_boolean_provided_then_validation_succeeds() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Boolean; var token = new JValue(true); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Empty(errors); } [Fact] public void When_boolean_required_but_string_provided_then_validation_fails() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Boolean; var token = new JValue("foo"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.BooleanExpected, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -141,17 +141,17 @@ public void When_boolean_required_but_string_provided_then_validation_fails() [Fact] public void When_string_pattern_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Pattern = "aa(.*)aa"; var token = new JValue("aaccbb"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.PatternMismatch, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -159,17 +159,17 @@ public void When_string_pattern_does_not_match_then_it_should_fail() [Fact] public void When_string_min_length_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.MinLength = 2; var token = new JValue("a"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.StringTooShort, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -177,17 +177,17 @@ public void When_string_min_length_does_not_match_then_it_should_fail() [Fact] public void When_string_max_length_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.MaxLength = 2; var token = new JValue("aaa"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.StringTooLong, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -195,17 +195,17 @@ public void When_string_max_length_does_not_match_then_it_should_fail() [Fact] public void When_integer_minimum_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Integer; schema.Minimum = 2; var token = new JValue(1); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NumberTooSmall, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -213,17 +213,17 @@ public void When_integer_minimum_does_not_match_then_it_should_fail() [Fact] public void When_integer_maximum_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Integer; schema.Maximum = 2; var token = new JValue(3); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NumberTooBig, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -231,17 +231,17 @@ public void When_integer_maximum_does_not_match_then_it_should_fail() [Fact] public void When_number_minimum_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Number; schema.Minimum = 1.5m; var token = new JValue(1.4); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NumberTooSmall, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -249,17 +249,17 @@ public void When_number_minimum_does_not_match_then_it_should_fail() [Fact] public void When_number_maximum_does_not_match_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.Number; schema.Maximum = 1.5m; var token = new JValue(1.6); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NumberTooBig, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -267,7 +267,7 @@ public void When_number_maximum_does_not_match_then_it_should_fail() [Fact] public void When_value_not_in_enumeration_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Enumeration.Add("Red"); @@ -276,10 +276,10 @@ public void When_value_not_in_enumeration_then_it_should_fail() var token = new JValue("Yellow"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert + // Assert Assert.Equal(ValidationErrorKind.NotInEnumeration, errors.First().Kind); Assert.Same(schema, errors.First().Schema); } @@ -287,7 +287,7 @@ public void When_value_not_in_enumeration_then_it_should_fail() [Fact] public void When_value_in_enumeration_then_it_should_succeed() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Enumeration.Add("Red"); @@ -296,17 +296,17 @@ public void When_value_in_enumeration_then_it_should_succeed() var token = new JValue("Red"); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(0, errors.Count); + // Assert + Assert.Empty(errors); } [Fact] public void When_value_is_wrong_type_in_enumeration_then_it_should_fail() { - //// Arrange + // Arrange var schema = new JsonSchema(); schema.Type = JsonObjectType.String; schema.Enumeration.Add("1"); @@ -315,11 +315,11 @@ public void When_value_is_wrong_type_in_enumeration_then_it_should_fail() var token = new JValue(3); - //// Act + // Act var errors = schema.Validate(token); - //// Assert - Assert.Equal(1, errors.Count); // wrong type + // Assert + Assert.Single(errors); // wrong type } } } \ No newline at end of file diff --git a/src/NJsonSchema.Yaml.Tests/NJsonSchema.Yaml.Tests.csproj b/src/NJsonSchema.Yaml.Tests/NJsonSchema.Yaml.Tests.csproj index 5a0271afc..cb0077bab 100644 --- a/src/NJsonSchema.Yaml.Tests/NJsonSchema.Yaml.Tests.csproj +++ b/src/NJsonSchema.Yaml.Tests/NJsonSchema.Yaml.Tests.csproj @@ -3,9 +3,9 @@ net8.0;net472 false - false disable - $(NoWarn);CS1591 + latest-Minimal + $(NoWarn);1591 diff --git a/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTests.cs b/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTests.cs index 7ad503db8..f23e127da 100644 --- a/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTests.cs +++ b/src/NJsonSchema.Yaml.Tests/References/YamlReferencesTests.cs @@ -15,14 +15,14 @@ public class LocalReferencesTests [InlineData("/References/YamlReferencesTest/json_schema_with_yaml_reference.json", "./collection.yaml")] public async Task When_yaml_schema_has_references_it_works(string relativePath, string documentPath) { - //// Arrange + // Arrange var path = GetTestDirectory() + relativePath; - //// Act + // Act var schema = await JsonSchemaYaml.FromFileAsync(path); var json = schema.ToJson(); - //// Assert + // Assert Assert.Equal(JsonObjectType.Integer, schema.Properties["foo"].ActualTypeSchema.Type); Assert.Single(schema.Definitions); Assert.Equal(documentPath, schema.Definitions["collection"].DocumentPath); @@ -34,7 +34,7 @@ public async Task When_yaml_OpenAPI_spec_has_external_schema_refs_they_are_resol { var path = GetTestDirectory() + relativePath; - //// Act + // Act OpenApiDocument doc = await OpenApiYamlDocument.FromFileAsync(path); IDictionary docPaths = doc.Paths; OpenApiPathItem pathItem = docPaths[docPath]; @@ -63,7 +63,7 @@ public async Task When_yaml_OpenAPI_spec_has_external_schema_refs_they_are_resol [InlineData("https://developer.zuora.com/yaml/swagger.yaml", "https://rest.zuora.com/")] public async Task When_yaml_OpenAPI_spec_is__served_with_gzip_compression__it_works(string inputYamlUrl, string expectedBaseUrl) { - //// Act + // Act OpenApiDocument doc = await OpenApiYamlDocument.FromUrlAsync(inputYamlUrl); ////Assert @@ -107,7 +107,7 @@ public async Task When_yaml_OpenAPI_spec_has_relative_external_schema_refs_in_su Assert.Equal(2, schema.Schema.ActualSchema.ActualProperties["status"].ActualSchema.Enumeration.Count); } - private string GetTestDirectory() + private static string GetTestDirectory() { var codeBase = Assembly.GetExecutingAssembly().CodeBase; var uri = new UriBuilder(codeBase);