-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
EnumTypeDescriptorTests.cs
165 lines (142 loc) · 4.88 KB
/
EnumTypeDescriptorTests.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using Xunit;
namespace HotChocolate.Types
{
public class EnumTypeDescriptorTests
: DescriptorTestBase
{
[Fact]
public void InferNameFromType()
{
// act
var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));
// assert
Assert.Equal("FooEnum", descriptor.CreateDefinition().Name);
}
[Fact]
public void NoTypeProvided()
{
// act
Action a = () => EnumTypeDescriptor.New(Context, (Type)null);
// assert
Assert.Throws<ArgumentNullException>(a);
}
[Fact]
public void InferValuesFromType()
{
// act
var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));
// assert
EnumTypeDefinition description = descriptor.CreateDefinition();
Assert.Collection(description.Values,
t =>
{
Assert.Equal("BAR1", t.Name);
Assert.Equal(FooEnum.Bar1, t.Value);
},
t =>
{
Assert.Equal("BAR2", t.Name);
Assert.Equal(FooEnum.Bar2, t.Value);
});
}
[Fact]
public void SpecifyOneValueInferTheOthers()
{
// arrange
var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));
// act
IEnumTypeDescriptor desc = descriptor;
desc.Item(FooEnum.Bar1).Name("FOOBAR");
// assert
EnumTypeDefinition description = descriptor.CreateDefinition();
Assert.Collection(description.Values,
t =>
{
Assert.Equal("FOOBAR", t.Name);
Assert.Equal(FooEnum.Bar1, t.Value);
},
t =>
{
Assert.Equal("BAR2", t.Name);
Assert.Equal(FooEnum.Bar2, t.Value);
});
}
[Fact]
public void ExplicitValueBinding()
{
// arrange
var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));
// act
IEnumTypeDescriptor desc = descriptor;
desc.Item(FooEnum.Bar1).Name("FOOBAR");
desc.BindValues(BindingBehavior.Explicit);
// assert
EnumTypeDefinition description = descriptor.CreateDefinition();
Assert.Collection(description.Values,
t =>
{
Assert.Equal("FOOBAR", t.Name);
Assert.Equal(FooEnum.Bar1, t.Value);
});
}
[Fact]
public void AddDirective()
{
// arrange
var descriptor = EnumTypeDescriptor.New(Context);
// act
IEnumTypeDescriptor desc = descriptor;
desc.Directive(new NameString("Bar"));
// assert
EnumTypeDefinition description = descriptor.CreateDefinition();
Assert.Collection(description.Directives,
t => Assert.Equal("Bar", t.ParsedDirective.Name.Value));
}
[Fact]
public void AddDirectiveWithDirectiveNode()
{
// arrange
var descriptor = EnumTypeDescriptor.New(Context);
// act
IEnumTypeDescriptor desc = descriptor;
desc.Directive(new DirectiveNode("Bar"));
// assert
EnumTypeDefinition description = descriptor.CreateDefinition();
Assert.Collection(description.Directives,
t => Assert.Equal("Bar", t.ParsedDirective.Name.Value));
}
[Fact]
public void AddDirectiveWithArgument()
{
// arrange
var descriptor = EnumTypeDescriptor.New(Context);
// act
IEnumTypeDescriptor desc = descriptor;
desc.Directive("Bar",
new ArgumentNode("a", new StringValueNode("b")));
// assert
EnumTypeDefinition description = descriptor.CreateDefinition();
Assert.Collection(description.Directives,
t =>
{
Assert.Equal("Bar", t.ParsedDirective.Name.Value);
Assert.Collection(t.ParsedDirective.Arguments,
x =>
{
Assert.Equal("a", x.Name.Value);
Assert.IsType<StringValueNode>(x.Value);
Assert.Equal("b", ((StringValueNode)x.Value).Value);
});
});
}
private enum FooEnum
{
Bar1,
Bar2
}
}
}