Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to XUnit #580

Merged
merged 17 commits into from Dec 11, 2017
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -14,4 +14,5 @@ src/packages/Newtonsoft.Json**
[Bb]in/
[Oo]bj/
/src/packages
.vs
.vs
/src/NJsonSchema.CodeGeneration.Tests2
3 changes: 3 additions & 0 deletions build/04_RunTests.bat
@@ -0,0 +1,3 @@
dotnet test "%~dp0/../src\NJsonSchema.Tests\NJsonSchema.Tests.csproj" -c Release
dotnet test "%~dp0/../src\NJsonSchema.CodeGeneration\NJsonSchema.CodeGeneration.csproj" -c Release
dotnet test "%~dp0/../src\NJsonSchema.CodeGeneration.CSharp.Tests\NJsonSchema.CodeGeneration.CSharp.Tests.csproj" -c Release
@@ -1,18 +1,17 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NJsonSchema.CodeGeneration.CSharp;
using NJsonSchema.CodeGeneration.CSharp;
using System.Threading.Tasks;
using Xunit;

namespace NJsonSchema.CodeGeneration.Tests.CSharp.Generation
{
[TestClass]
public class AbstractGenerationTests
public class AbstractSchemaTests
{
public abstract class AbstractClass
{
public string Foo { get; set; }
}

[TestMethod]
[Fact]
public async Task When_class_is_abstract_then_is_abstract_CSharp_keyword_is_generated()
{
/// Arrange
Expand All @@ -23,7 +22,7 @@ public async Task When_class_is_abstract_then_is_abstract_CSharp_keyword_is_gene
var code = generator.GenerateFile("AbstractClass");

/// Assert
Assert.IsTrue(code.Contains("public abstract partial class AbstractClass"));
Assert.True(code.Contains("public abstract partial class AbstractClass"));
}
}
}
@@ -1,45 +1,44 @@
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NJsonSchema.CodeGeneration.CSharp;

namespace NJsonSchema.CodeGeneration.Tests.CSharp
{
[TestClass]
public class AdditionalPropertiesTests
{
[TestMethod]
public async Task When_additionalProperties_schema_is_set_for_object_then_special_property_is_rendered()
{
//// Arrange
var json =
@"{
""properties"": {
""Pet"": {
""type"": ""object"",
""properties"": {
""id"": {
""type"": ""integer"",
""format"": ""int64""
},
""category"": {
""type"": ""string""
}
},
""additionalProperties"": {
""type"": ""string""
}
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);

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

//// Assert
Assert.IsTrue(code.Contains("[Newtonsoft.Json.JsonExtensionData]"));
Assert.IsTrue(code.Contains("public System.Collections.Generic.IDictionary<string, string> AdditionalProperties"));
}
}
using System.Threading.Tasks;
using NJsonSchema.CodeGeneration.CSharp;
using Xunit;

namespace NJsonSchema.CodeGeneration.Tests.CSharp
{
public class AdditionalPropertiesTests
{
[Fact]
public async Task When_additionalProperties_schema_is_set_for_object_then_special_property_is_rendered()
{
//// Arrange
var json =
@"{
""properties"": {
""Pet"": {
""type"": ""object"",
""properties"": {
""id"": {
""type"": ""integer"",
""format"": ""int64""
},
""category"": {
""type"": ""string""
}
},
""additionalProperties"": {
""type"": ""string""
}
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);

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

//// Assert
Assert.True(code.Contains("[Newtonsoft.Json.JsonExtensionData]"));
Assert.True(code.Contains("public System.Collections.Generic.IDictionary<string, string> AdditionalProperties"));
}
}
}
245 changes: 245 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs
@@ -0,0 +1,245 @@
using System.Threading.Tasks;
using NJsonSchema.CodeGeneration.CSharp;
using Xunit;

namespace NJsonSchema.CodeGeneration.Tests.CSharp
{
public class AllOfTests
{
[Fact]
public async Task When_allOf_has_two_schemas_then_referenced_schema_is_inherited()
{
//// Arrange
var json =
@"{
""allOf"": [
{
""$ref"": ""#/definitions/B""
},
{
""type"": ""object"",
""required"": [
""prop""
],
""properties"": {
""prop"": {
""type"": ""string"",
""minLength"": 1,
""maxLength"": 30
},
""prop2"": {
""type"": ""string""
}
}
}
],
""definitions"": {
""B"": {
""properties"": {
""foo"": {
""type"": ""string""
}
}
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);

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

//// Assert
Assert.False(code.Contains("Anonymous"));
Assert.True(code.Contains("A : B"));
}

[Fact]
public async Task When_allOf_has_one_schema_then_it_is_inherited()
{
//// Arrange
var json =
@"{
""type"": ""object"",
""discriminator"": ""type"",
""required"": [
""prop"",
""type""
],
""properties"": {
""prop"": {
""type"": ""string"",
""minLength"": 1,
""maxLength"": 30
},
""prop2"": {
""type"": ""string""
},
""type"": {
""type"": ""string""
}
},
""allOf"": [
{
""$ref"": ""#/definitions/B""
}
],
""definitions"": {
""B"": {
""type"": ""object"",
""properties"": {
""foo"": {
""type"": ""string""
}
}
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);

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

//// Assert
Assert.False(code.Contains("Anonymous"));
Assert.True(code.Contains("A : B"));
}

[Fact]
public async Task When_all_of_has_multiple_refs_then_the_properties_should_expand_to_single_class()
{
//// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'id': 'http://some.domain.com/foo.json',
'type': 'object',
'additionalProperties': false,
'definitions': {
'tRef1': {
'properties': {
'val1': {
'type': 'string',
}
}
},
'tRef2': {
'properties': {
'val2': {
'type': 'string',
}
}
},
'tRef3': {
'properties': {
'val3': {
'type': 'string',
}
}
}
},
'properties' : {
'tAgg': {
'allOf': [
{'$ref': '#/definitions/tRef1'},
{'$ref': '#/definitions/tRef2'},
{'$ref': '#/definitions/tRef3'}
]
}
}
}";

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

//// Assert
Assert.True(output.Contains("public partial class TAgg"));
Assert.True(output.Contains("public string Val1 { get; set; }"));
Assert.True(output.Contains("public string Val2 { get; set; }"));
Assert.True(output.Contains("public string Val3 { get; set; }"));
}

[Fact]
public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_all_of_contains_all_properties_in_generated_code()
{
//// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties': {
'prop1' : { 'type' : 'string' }
},
'allOf': [
{
'type': 'object',
'properties': {
'baseProperty' : { 'type' : 'string' }
}
},
{
'properties': {
'prop2' : { 'type' : 'string' }
}
}
]
}";

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

//// Assert
Assert.True(code.Contains(
@" public partial class Foo : Anonymous
{
[Newtonsoft.Json.JsonProperty(""prop1"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop1 { get; set; }

[Newtonsoft.Json.JsonProperty(""prop2"", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop2 { get; set; }
".Replace("\r", string.Empty)));

Assert.True(code.Contains("class Anonymous"));
}

[Fact]
public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_class_in_generated_code()
{
//// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties': {
'prop1' : { 'type' : 'string' }
},
'allOf': [
{
'$ref': '#/definitions/Bar'
}
],
'definitions': {
'Bar': {
'type': 'object',
'properties': {
'prop2' : { 'type' : 'string' }
}
}
}
}";

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

//// Assert
Assert.True(code.Contains("class Foo : Bar"));
Assert.True(code.Contains("public string Prop1 { get; set; }"));
Assert.True(code.Contains("public string Prop2 { get; set; }"));
}
}
}