-
-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathArrayTests.cs
More file actions
61 lines (53 loc) · 1.92 KB
/
ArrayTests.cs
File metadata and controls
61 lines (53 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using NJsonSchema.Annotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Xunit;
namespace NJsonSchema.CodeGeneration.CSharp.Tests
{
public class ArrayTests
{
public class ArrayTest
{
[Required]
public List<string> ArrayProperty { get; set; }
}
[Fact]
public async Task When_array_property_is_required_then_array_instance_can_be_changed()
{
//// Arrange
var schema = JsonSchema.FromType<ArrayTest>();
var data = schema.ToJson();
//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
ArrayType = "Foo",
ArrayInstanceType = "Bar",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");
//// Assert
Assert.Contains("public Foo<string> ArrayProperty { get; set; } = new Bar<string>();", code);
}
public class ClassWithNullableArrayItems
{
[NotNull]
[ItemsCanBeNull]
public List<int?> Items { get; set; }
}
[Fact]
public async Task When_array_item_is_nullable_then_generated_CSharp_is_correct()
{
// Arrange
var schema = JsonSchema.FromType<ClassWithNullableArrayItems>();
var json = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
// Act
var output = generator.GenerateFile("MyClass");
// Assert
Assert.True(schema.Properties["Items"].Item.IsNullable(SchemaType.JsonSchema));
Assert.Contains("System.Collections.Generic.ICollection<int?> Items", output);
}
}
}