-
-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathAnnotationsTests.cs
More file actions
76 lines (65 loc) · 2.86 KB
/
Copy pathAnnotationsTests.cs
File metadata and controls
76 lines (65 loc) · 2.86 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.ComponentModel.DataAnnotations;
using NJsonSchema.CodeGeneration.CSharp;
using NJsonSchema.CodeGeneration.CSharp.Tests;
using NJsonSchema.NewtonsoftJson.Generation;
namespace NJsonSchema.CodeGeneration.Tests.CSharp
{
public class AnnotationsTests
{
public class MyRequiredTest
{
public string Name { get; set; }
[Required]
public List<string> Collection { get; set; }
[Required]
public Dictionary<string, object> Dictionary { get; set; }
}
[Fact]
public async Task When_array_property_is_not_nullable_then_it_does_not_have_a_setter()
{
// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<MyRequiredTest>();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
GenerateImmutableArrayProperties = true,
GenerateImmutableDictionaryProperties = true,
GenerateDefaultValues = false
});
// Act
var code = generator.GenerateFile();
// Assert
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code);
}
[Fact]
public async Task When_custom_class_and_property_annotation_templates_are_used_then_annotations_are_on_separate_lines()
{
// Arrange
var json = @"{ 'properties': { 'name': { 'type': 'string' } } }";
var schema = await JsonSchema.FromJsonAsync(json);
var tempDir = Path.Combine(Path.GetTempPath(), "NJsonSchema_AnnotationTest_" + Guid.NewGuid());
Directory.CreateDirectory(tempDir);
try
{
File.WriteAllText(Path.Combine(tempDir, "Class.Annotations.liquid"), "[System.Runtime.Serialization.DataContract]");
File.WriteAllText(Path.Combine(tempDir, "Class.Property.Annotations.liquid"), "[System.Runtime.Serialization.DataMember]");
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "TestNs",
TemplateDirectory = tempDir
});
// Act
var code = generator.GenerateFile("MyClass");
// Assert - annotations should be on their own lines, not on the same line as class/property declarations
Assert.Contains("[System.Runtime.Serialization.DataContract]\n public partial class MyClass", code);
Assert.Contains("[System.Runtime.Serialization.DataMember]\n public string Name", code);
}
finally
{
Directory.Delete(tempDir, true);
}
}
}
}