-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathExpressionTestCaseRoot.cs
More file actions
170 lines (130 loc) · 5.01 KB
/
ExpressionTestCaseRoot.cs
File metadata and controls
170 lines (130 loc) · 5.01 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
using System.Text.Json;
using System.Text.Json.Serialization;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Internal.Expressions;
using Altinn.App.Core.Models.Expressions;
using Altinn.App.Core.Models.Layout.Components;
using Altinn.Platform.Storage.Interface.Models;
namespace Altinn.App.Core.Tests.LayoutExpressions.CommonTests;
public class ExpressionTestCaseRoot
{
public ExpressionTestCaseRoot(TestCaseItem testCaseItem)
{
Name = testCaseItem.Name;
Expression = testCaseItem.Expression;
Expects = testCaseItem.Expects;
ExpectsFailure = testCaseItem.ExpectsFailure;
}
public ExpressionTestCaseRoot() { }
[JsonIgnore]
public string? Filename { get; set; }
[JsonIgnore]
public string? FullPath { get; set; }
[JsonIgnore]
public string? Folder { get; set; }
[JsonIgnore]
public string? RawJson { get; set; }
[JsonIgnore]
public Exception? ParsingException { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("expression")]
public Expression Expression { get; set; }
[JsonPropertyName("context")]
public ComponentContextForTestSpec? Context { get; set; }
[JsonPropertyName("expects")]
public JsonElement Expects { get; set; }
[JsonPropertyName("expectsFailure")]
public string? ExpectsFailure { get; set; }
public class TestCaseItem
{
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("expression")]
public required Expression Expression { get; set; }
[JsonPropertyName("expects")]
public JsonElement Expects { get; set; }
[JsonPropertyName("expectsFailure")]
public string? ExpectsFailure { get; set; }
}
[JsonPropertyName("testCases")]
public List<TestCaseItem>? TestCases { get; set; }
[JsonPropertyName("layouts")]
public IReadOnlyDictionary<string, JsonElement>? Layouts { get; set; }
[JsonPropertyName("dataModel")]
public JsonElement? DataModel { get; set; }
[JsonPropertyName("dataModels")]
public List<DataModelAndElement>? DataModels { get; set; }
[JsonPropertyName("frontendSettings")]
public FrontEndSettings? FrontEndSettings { get; set; }
[JsonPropertyName("textResources")]
public List<TextResourceElement>? TextResources { get; set; }
[JsonPropertyName("instance")]
public Instance Instance { get; set; } = new Instance();
[JsonPropertyName("gatewayAction")]
public string? GatewayAction { get; set; }
[JsonPropertyName("profileSettings")]
public ProfileSettings? ProfileSettings { get; set; }
[JsonPropertyName("positionalArguments")]
public List<JsonElement>? PositionalArguments { get; set; }
public override string ToString()
{
return $"{Filename}: {Name}";
}
public async Task<ComponentContext> GetContextOrNull(LayoutEvaluatorState state)
{
ComponentContext? context = null;
if (Context is not null)
{
//! Some tests do not need context, but it is not nullable in expression evaluator
context = await state.GetComponentContext(
Context.CurrentPageName,
Context.ComponentId,
Context.RowIndices
)!;
}
//! Some tests do not need context, but it is not nullable in expression evaluator
return context!;
}
}
public class DataModelAndElement
{
[JsonPropertyName("dataElement")]
public required DataElement DataElement { get; set; }
[JsonPropertyName("data")]
public required JsonElement Data { get; set; }
}
public class ProfileSettings
{
[JsonPropertyName("language")]
public string? Language { get; set; }
}
public class ComponentContextForTestSpec
{
[JsonPropertyName("component")]
public required string ComponentId { get; init; }
[JsonPropertyName("rowIndices")]
public int[]? RowIndices { get; init; }
[JsonPropertyName("currentLayout")]
public required string CurrentPageName { get; init; }
[JsonPropertyName("children")]
public List<ComponentContextForTestSpec> ChildContexts { get; init; } = [];
public static ComponentContextForTestSpec FromContext(ComponentContext context)
{
ArgumentNullException.ThrowIfNull(context.Component);
ArgumentNullException.ThrowIfNull(context.Component.Id);
ArgumentNullException.ThrowIfNull(context.Component.PageId);
var id = context.Component.Id;
List<ComponentContextForTestSpec> childContexts = context
.ChildContexts.SelectMany(c => c.Component is RepeatingGroupRowComponent ? c.ChildContexts : [c]) // Flatten out the synthetic Row components that are not used in test spec
.Select(FromContext)
.ToList();
return new ComponentContextForTestSpec
{
ComponentId = id,
CurrentPageName = context.Component.PageId,
ChildContexts = childContexts,
RowIndices = context.RowIndices,
};
}
}