Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using System.Reflection;

namespace NJsonSchema.CodeGeneration.CSharp
{
/// <summary>The generator settings.</summary>
Expand Down
3 changes: 2 additions & 1 deletion src/NJsonSchema.CodeGeneration.Tests/VerifyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static SettingsTask Verify(string output, [CallerFilePath] string sourceF
"Generated using the NSwag toolchain",
"Generated using the NJsonSchema",
"[System.CodeDom.Compiler.GeneratedCode(\"NJsonSchema\"")
.UseDirectory("Snapshots");
.UseDirectory("Snapshots")
.AutoVerify(includeBuildServer: false);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using NJsonSchema.CodeGeneration.Tests;
using NJsonSchema.NewtonsoftJson.Converters;
using NJsonSchema.NewtonsoftJson.Generation;

Expand All @@ -14,7 +15,7 @@ public abstract class AbstractClass : BaseClass
}

[Fact]
public void When_class_is_abstract_then_is_abstract_TypeScript_keyword_is_generated()
public async Task When_class_is_abstract_then_is_abstract_TypeScript_keyword_is_generated()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<AbstractClass>();
Expand All @@ -25,11 +26,8 @@ public void When_class_is_abstract_then_is_abstract_TypeScript_keyword_is_genera
var code = generator.GenerateFile("AbstractClass");

// Assert
Assert.Contains("export abstract class AbstractClass", code);

Assert.Contains("base: string", code);
Assert.Contains("super: string", code);
Assert.Contains("foo: string", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

public class ContainerClass
Expand All @@ -39,7 +37,7 @@ public class ContainerClass
}

[Fact]
public void When_property_is_required_and_abstract_then_it_is_not_instantiated()
public async Task When_property_is_required_and_abstract_then_it_is_not_instantiated()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<ContainerClass>();
Expand All @@ -50,8 +48,8 @@ public void When_property_is_required_and_abstract_then_it_is_not_instantiated()
var code = generator.GenerateFile("ContainerClass");

// Assert
Assert.Contains("foo: AbstractClass", code);
Assert.Contains("this.foo = _data[\"Foo\"] ? AbstractClass.fromJS(_data[\"Foo\"]) : undefined as any;", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[KnownType(typeof(SuperClass))]
Expand All @@ -68,7 +66,7 @@ public class SuperClass : AbstractClass
}

[Fact]
public void When_abstract_class_is_in_inheritance_hierarchy_then_it_is_newer_instantiated()
public async Task When_abstract_class_is_in_inheritance_hierarchy_then_it_is_newer_instantiated()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<AbstractClass>();
Expand All @@ -78,7 +76,8 @@ public void When_abstract_class_is_in_inheritance_hierarchy_then_it_is_newer_ins
var code = generator.GenerateFile("AbstractClass");

// Assert
Assert.DoesNotContain("new AbstractClass();", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static Task<string> PrepareAsync(TypeScriptGeneratorSettings settings)
}

[Fact]
public void When_array_property_is_required_or_not_then_the_code_has_correct_initializer()
public async Task When_array_property_is_required_or_not_then_the_code_has_correct_initializer()
{
// Arrange
var schema = new JsonSchema
Expand Down Expand Up @@ -121,13 +121,12 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini
var code = generator.GenerateFile("MyClass");

// Assert
Assert.Contains("a: string[];", code);
Assert.Contains("this.a = [];", code);
Assert.Contains("b: string[];", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
public void When_dictionary_property_is_required_or_not_then_the_code_has_correct_initializer()
public async Task When_dictionary_property_is_required_or_not_then_the_code_has_correct_initializer()
{
// Arrange
var schema = new JsonSchema
Expand Down Expand Up @@ -167,13 +166,12 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc
var code = generator.GenerateFile("MyClass");

// Assert
Assert.Contains("a: { [key: string]: string; };", code);
Assert.Contains("this.a = {};", code);
Assert.Contains("b: { [key: string]: string; };", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
public void When_object_property_is_required_or_not_then_the_code_has_correct_initializer()
public async Task When_object_property_is_required_or_not_then_the_code_has_correct_initializer()
{
// Arrange
var schema = new JsonSchema
Expand Down Expand Up @@ -221,12 +219,8 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in
var code = generator.GenerateFile("MyClass");

// 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);

Assert.Contains("b: B;", code);
Assert.Contains("this.b = _data[\"B\"] ? B.fromJS(_data[\"B\"]) : undefined as any;", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -235,9 +229,8 @@ 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.DoesNotContain("export class Student extends Person implements IStudent {", code);
Assert.DoesNotContain("export interface IStudent extends IPerson {", code);
Assert.DoesNotContain("export interface IPerson {", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -246,7 +239,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.DoesNotContain("export class Student extends Person {", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -259,8 +252,8 @@ public async Task When_GenerateConstructorInterface_then_no_interfaces_are_gener
});

// Assert
Assert.DoesNotContain("interface IStudent extends IPerson {", code);
Assert.DoesNotContain("interface IPerson {", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -274,11 +267,11 @@ public async Task When_Knockout_class_is_generated_then_initializers_are_correct
});

// Assert
Assert.DoesNotContain("let firstName_ = data[\"FirstName\"];", code);
await VerifyHelper.Verify(code);
}

[Fact]
public void When_GenerateConstructorInterface_is_disabled_then_data_is_not_checked_and_default_initialization_is_always_exectued()
public async Task When_GenerateConstructorInterface_is_disabled_then_data_is_not_checked_and_default_initialization_is_always_exectued()
{
// Assert
var schema = NewtonsoftJsonSchemaGenerator.FromType(
Expand All @@ -297,7 +290,8 @@ public void When_GenerateConstructorInterface_is_disabled_then_data_is_not_check
var output = generator.GenerateFile();

// Assert
Assert.DoesNotContain("if (!data) {", output);
await VerifyHelper.Verify(output);
CodeCompiler.AssertCompile(output);
}

[JsonConverter(typeof(JsonInheritanceConverter))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.Serialization;
using Newtonsoft.Json;
using NJsonSchema.CodeGeneration.Tests;
using NJsonSchema.NewtonsoftJson.Converters;
using NJsonSchema.NewtonsoftJson.Generation;

Expand Down Expand Up @@ -48,7 +49,7 @@ public class Skill
}

[Fact]
public void When_constructor_interface_and_conversion_code_is_generated_then_it_is_correct()
public async Task When_constructor_interface_and_conversion_code_is_generated_then_it_is_correct()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<Person>(new NewtonsoftJsonSchemaGeneratorSettings());
Expand All @@ -65,26 +66,8 @@ public void When_constructor_interface_and_conversion_code_is_generated_then_it_
var output = generator.GenerateFile("MyClass");

// Assert

Assert.DoesNotContain("new MyClass(", output);
// address property is converted:
Assert.Contains("this.address = data.address && !(data.address as any).toJSON ? new Address(data.address) : this.address as Address;", output);
// cars items are converted:
Assert.Contains("this.cars[i] = item && !(item as any).toJSON ? new Car(item) : item as Car;", output);
// skills values are converted:
Assert.Contains("this.skills[key] = item && !(item as any).toJSON ? new Skill(item) : item as Skill;", output);
// student car is converted:
Assert.Contains("this.car = data.car && !(data.car as any).toJSON ? new Car(data.car) : this.car as Car;", output);

// interface is correct
Assert.Contains(@"export interface IMyClass {
supervisor: MyClass;
address: IAddress;
cars: ICar[];
skills: { [key: string]: ISkill; };
foo: Car[][];
bar: { [key: string]: Skill[]; };
}".Replace("\r", "").Replace("\n", ""), output.Replace("\r", "").Replace("\n", ""));
await VerifyHelper.Verify(output);
CodeCompiler.AssertCompile(output);
}

[Fact]
Expand Down Expand Up @@ -119,7 +102,8 @@ public async Task When_array_of_string_dictionary_is_used_with_ConvertConstructo
var output = generator.GenerateFile("MyClass");

// Assert
Assert.Contains("custom4: { [key: string]: string; }[];", output);
await VerifyHelper.Verify(output);
CodeCompiler.AssertCompile(output);
}
}
}
Loading