-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStructuralFeatureHelperTestFixture.cs
More file actions
223 lines (158 loc) · 8.65 KB
/
Copy pathStructuralFeatureHelperTestFixture.cs
File metadata and controls
223 lines (158 loc) · 8.65 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// -------------------------------------------------------------------------------------------------
// <copyright file="StructuralFeatureHelperTestFixture.cs" company="Starion Group S.A.">
//
// Copyright 2017-2025 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------
namespace ECoreNetto.HandleBars.Tests
{
using System.Globalization;
using System.IO;
using System.Linq;
using ECoreNetto;
using HandlebarsDotNet;
using NUnit.Framework;
/// <summary>
/// Suite of tests for the <see cref="GeneralizationHelper"/> class
/// </summary>
[TestFixture]
public class StructuralFeatureHelperTestFixture
{
private IHandlebars handlebarsContenxt;
private EPackage root;
[SetUp]
public void Setup()
{
this.handlebarsContenxt = Handlebars.Create();
this.handlebarsContenxt.Configuration.FormatProvider = CultureInfo.InvariantCulture;
StructuralFeatureHelper.RegisterStructuralFeatureHelper(this.handlebarsContenxt);
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", "recipe.ecore");
this.root = ModelLoader.Load(path);
}
[Test]
public void Verify_that_StructuralFeature_QueryIsEnumerable_returns_expected_result()
{
var template = "{{ #StructuralFeature.QueryIsEnumerable this }}";
var action = this.handlebarsContenxt.Compile(template);
var eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "ingredients");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("True"));
}
[Test]
public void Verify_that_StructuralFeature_QueryIsAttribute_returns_expected_result()
{
var template = "{{ #StructuralFeature.QueryIsAttribute this }}";
var action = this.handlebarsContenxt.Compile(template);
var eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "ingredients");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("False"));
eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "TimeTrigger");
eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "minutes");
result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("True"));
}
[Test]
public void Verify_that_StructuralFeature_QueryIsReference_returns_expected_result()
{
var template = "{{ #StructuralFeature.QueryIsReference this }}";
var action = this.handlebarsContenxt.Compile(template);
var eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "ingredients");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("True"));
eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "TimeTrigger");
eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "minutes");
result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("False"));
}
[Test]
public void Verify_that_StructuralFeature_QueryIsEnum_returns_expected_result()
{
var template = "{{ #StructuralFeature.QueryIsEnum this }}";
var action = this.handlebarsContenxt.Compile(template);
var eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "ingredients");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("False"));
eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Amount");
eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "unit");
result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("True"));
}
[Test]
public void Verify_that_StructuralFeature_QueryHasDefaultValue_returns_expected_result()
{
var template = "{{ #StructuralFeature.QueryHasDefaultValue this }}";
var action = this.handlebarsContenxt.Compile(template);
var eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "ingredients");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("False"));
eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Amount");
eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "unit");
result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("True"));
}
[Test]
public void Verify_that_StructuralFeature_QueryIsContainment_returns_expected_result()
{
var template = "{{ #StructuralFeature.QueryIsContainment this }}";
var action = this.handlebarsContenxt.Compile(template);
var eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Ingredient");
var eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "amount");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("True"));
eClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "TimeTrigger");
eStructuralFeature = eClass.EStructuralFeatures.Single(x => x.Name == "minutes");
result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("False"));
}
[Test]
public void Verify_that_StructuralFeature_IsEnumerable_block_helper_writes_content_only_when_true()
{
var template = "{{#StructuralFeature.IsEnumerable this}}Enumerable{{/StructuralFeature.IsEnumerable}}";
var action = this.handlebarsContenxt.Compile(template);
var recipeClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var enumerableFeature = recipeClass.EStructuralFeatures.Single(x => x.Name == "ingredients");
var timeTriggerClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "TimeTrigger");
var nonEnumerableFeature = timeTriggerClass.EStructuralFeatures.Single(x => x.Name == "minutes");
Assert.Multiple(() =>
{
Assert.That(action(enumerableFeature), Is.EqualTo("Enumerable"));
Assert.That(action(nonEnumerableFeature), Is.EqualTo(string.Empty));
});
}
[Test]
public void Verify_that_StructuralFeature_QueryTypeName_returns_expected_result()
{
var template = "{{#with this}}{{StructuralFeature.QueryTypeName}}{{/with}}";
var action = this.handlebarsContenxt.Compile(template);
var recipeClass = this.root.EClassifiers.OfType<EClass>().Single(x => x.Name == "Recipe");
var eStructuralFeature = recipeClass.EStructuralFeatures.Single(x => x.Name == "name");
var result = action(eStructuralFeature);
Assert.That(result, Is.EqualTo("string"));
}
[Test]
public void Verify_that_StructuralFeature_QueryTypeName_throws_when_context_is_not_a_structural_feature()
{
var template = "{{StructuralFeature.QueryTypeName}}";
var action = this.handlebarsContenxt.Compile(template);
Assert.That(() => action(new object()), Throws.TypeOf<ArgumentException>());
}
}
}