Skip to content

Commit

Permalink
Synchronizing shared expression tests with frontend (#1040)
Browse files Browse the repository at this point in the history
* Updating shared tests to match frontend

* Removing shared tests for layout-preprocessor (which does not exist on backend, and no longer exists in this form on frontend)

* Removing tests for duplicate IDs in component lookups. This is no longer supported on frontend, as duplicate component IDs are now a hard error.

* Adding shared tests for the argv function, and making positional arguments nullable

---------

Co-authored-by: Ole Martin Handeland <[email protected]>
  • Loading branch information
olemartinorg and Ole Martin Handeland authored Jan 16, 2025
1 parent 48ef962 commit 2965048
Show file tree
Hide file tree
Showing 39 changed files with 191 additions and 614 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool defaultReturn
LayoutEvaluatorState state,
Expression expr,
ComponentContext context,
object[]? positionalArguments = null
object?[]? positionalArguments = null
)
{
if (!expr.IsFunctionExpression)
Expand Down Expand Up @@ -534,7 +534,7 @@ private static (double?, double?) PrepareNumericArgs(object?[] args)
return string.Equals(ToStringForEquals(args[0]), ToStringForEquals(args[1]), StringComparison.Ordinal);
}

private static object Argv(object?[] args, object[]? positionalArguments)
private static object? Argv(object?[] args, object?[]? positionalArguments)
{
if (args.Length != 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class ExpressionTestCaseRoot
[JsonPropertyName("profileSettings")]
public ProfileSettings? ProfileSettings { get; set; }

[JsonPropertyName("positionalArguments")]
public List<JsonElement>? PositionalArguments { get; set; }

public override string ToString()
{
return $"{Filename}: {Name}";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Text.Json;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Features;
Expand Down Expand Up @@ -27,6 +28,10 @@ public TestFunctions(ITestOutputHelper output)
_output = output;
}

[Theory]
[SharedTest("argv")]
public async Task Argv_Theory(string testName, string folder) => await RunTestCase(testName, folder);

[Theory]
[SharedTest("and")]
public async Task And_Theory(string testName, string folder) => await RunTestCase(testName, folder);
Expand Down Expand Up @@ -194,6 +199,20 @@ private async Task RunTestCase(string testName, string folder)
dataAccessor = DynamicClassBuilder.DataAccessorFromJsonDocument(test.Instance, test.DataModels);
}

var positionalArguments = test
.PositionalArguments?.Select<JsonElement, object?>(e =>
e.ValueKind switch
{
JsonValueKind.String => e.GetString(),
JsonValueKind.Number => e.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null => null,
_ => throw new NotImplementedException($"JsonElement value kind {e.ValueKind} not implemented"),
}
)
.ToArray();

LayoutModel? componentModel = null;
if (test.Layouts is not null)
{
Expand Down Expand Up @@ -221,7 +240,8 @@ private async Task RunTestCase(string testName, string folder)
await ExpressionEvaluator.EvaluateExpression(
state,
test.Expression,
test.Context?.ToContext(componentModel, state)!
test.Context?.ToContext(componentModel, state)!,
positionalArguments
);
};
(await act.Should().ThrowAsync<Exception>()).WithMessage($"*{test.ExpectsFailure}*");
Expand All @@ -235,7 +255,8 @@ await ExpressionEvaluator.EvaluateExpression(
var result = await ExpressionEvaluator.EvaluateExpression(
state,
test.Expression,
test.Context?.ToContext(componentModel, state)!
test.Context?.ToContext(componentModel, state)!,
positionalArguments
);

switch (test.Expects.ValueKind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@
}
],
"dataModel": {
"gruppe1": [{ "comp3binding": "123", "comp4binding": "dadda" }]
"gruppe1": [{ "altinnRowId": "row0", "comp3binding": "123", "comp4binding": "dadda" }]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
],
"dataModel": {
"gruppe1": [
{ "comp3binding": "123", "comp4binding": "dadda" },
{ "comp3binding": "456", "comp4binding": "dodo" }
{ "altinnRowId": "row1", "comp3binding": "123", "comp4binding": "dadda" },
{ "altinnRowId": "row2", "comp3binding": "456", "comp4binding": "dodo" }
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@
"dataModel": {
"group": [
{
"altinnRowId": "row0",
"subgroup": [
{
"altinnRowId": "row0-child0",
"asdf": "123"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@
"dataModel": {
"group": [
{
"altinnRowId": "row0",
"subgroup": [
{
"altinnRowId": "row0-child0",
"asdf": "123"
},
{
"altinnRowId": "row0-child1",
"asdf": "456"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,19 @@
"dataModel": {
"group": [
{
"altinnRowId": "row0",
"subgroup": [
{
"altinnRowId": "row0-child0",
"asdf": "123"
}
]
},
{
"altinnRowId": "row1",
"subgroup": [
{
"altinnRowId": "row1-child0",
"asdf": "456"
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Positional argument can also be null",
"expression": ["argv", 1],
"expects": null,
"positionalArguments": ["foo", null, 5]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Errors when trying to read argument when none exist",
"expression": ["argv", 0],
"expectsFailure": "No positional arguments available"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Errors when trying to read argument that does not exist",
"expression": ["argv", 2],
"expectsFailure": "Index 2 out of range",
"positionalArguments": ["foo", 1234]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Simple: Read arg 0",
"expression": ["argv", 0],
"expects": "foo",
"positionalArguments": ["foo", "bar"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Simple: Read arg 1",
"expression": ["argv", 1],
"expects": 1234,
"positionalArguments": ["foo", 1234]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Lookup across pages (when target page is hidden)",
"expression": ["component", "page1-input"],
"context": {
"component": "art",
"component": "art2",
"rowIndices": [0, 1],
"currentLayout": "Mennesker"
},
Expand All @@ -26,17 +26,17 @@
"dataModelBindings": {
"group": "Dyr"
},
"children": ["art", "navn"]
"children": ["art1", "dyr-navn"]
},
{
"id": "art",
"id": "art1",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Dyr.Art"
}
},
{
"id": "navn",
"id": "dyr-navn",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Dyr.Navn"
Expand All @@ -55,10 +55,10 @@
"dataModelBindings": {
"group": "Mennesker"
},
"children": ["navn", "alder", "favoritt-dyr"]
"children": ["person-navn", "alder", "favoritt-dyr"]
},
{
"id": "navn",
"id": "person-navn",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Mennesker.Navn"
Expand All @@ -77,10 +77,10 @@
"dataModelBindings": {
"group": "Mennesker.FavorittDyr"
},
"children": ["art"]
"children": ["art2"]
},
{
"id": "art",
"id": "art2",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Mennesker.FavorittDyr.Art"
Expand All @@ -94,28 +94,36 @@
"Side1": "Hei verden",
"Dyr": [
{
"altinnRowId": "animal0",
"Navn": "Pia Potet",
"Art": "Kanin"
},
{
"altinnRowId": "animal1",
"Navn": "Erling Ert",
"Art": "Dovendyr"
},
{
"altinnRowId": "animal2",
"Navn": "Mona Melon",
"Art": "Hamster"
}
],
"Mennesker": [
{
"altinnRowId": "person0",
"Navn": "Kåre",
"Alder": 28,
"FavorittDyr": [{ "Art": "Kanin" }, { "Art": "Hamster" }]
"FavorittDyr": [
{ "altinnRowId": "person0-fav0", "Art": "Kanin" },
{ "altinnRowId": "person0-fav1", "Art": "Hamster" }
]
},
{
"altinnRowId": "person1",
"Navn": "Arild",
"Alder": 14,
"FavorittDyr": [{ "Art": "Dovendyr" }]
"FavorittDyr": [{ "altinnRowId": "person1-fav0", "Art": "Dovendyr" }]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Lookup across pages",
"expression": ["component", "page1-input"],
"context": {
"component": "art",
"component": "art2",
"rowIndices": [0, 1],
"currentLayout": "Mennesker"
},
Expand All @@ -25,17 +25,17 @@
"dataModelBindings": {
"group": "Dyr"
},
"children": ["art", "navn"]
"children": ["art1", "dyr-navn"]
},
{
"id": "art",
"id": "art1",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Dyr.Art"
}
},
{
"id": "navn",
"id": "dyr-navn",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Dyr.Navn"
Expand All @@ -54,10 +54,10 @@
"dataModelBindings": {
"group": "Mennesker"
},
"children": ["navn", "alder", "favoritt-dyr"]
"children": ["person-navn", "alder", "favoritt-dyr"]
},
{
"id": "navn",
"id": "person-navn",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Mennesker.Navn"
Expand All @@ -76,10 +76,10 @@
"dataModelBindings": {
"group": "Mennesker.FavorittDyr"
},
"children": ["art"]
"children": ["art2"]
},
{
"id": "art",
"id": "art2",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Mennesker.FavorittDyr.Art"
Expand All @@ -93,28 +93,36 @@
"Side1": "Hei verden",
"Dyr": [
{
"altinnRowId": "animal0",
"Navn": "Pia Potet",
"Art": "Kanin"
},
{
"altinnRowId": "animal1",
"Navn": "Erling Ert",
"Art": "Dovendyr"
},
{
"altinnRowId": "animal2",
"Navn": "Mona Melon",
"Art": "Hamster"
}
],
"Mennesker": [
{
"altinnRowId": "person0",
"Navn": "Kåre",
"Alder": 28,
"FavorittDyr": [{ "Art": "Kanin" }, { "Art": "Hamster" }]
"FavorittDyr": [
{ "altinnRowId": "person0-fav0", "Art": "Kanin" },
{ "altinnRowId": "person0-fav1", "Art": "Hamster" }
]
},
{
"altinnRowId": "person1",
"Navn": "Arild",
"Alder": 14,
"FavorittDyr": [{ "Art": "Dovendyr" }]
"FavorittDyr": [{ "altinnRowId": "person1-fav0", "Art": "Dovendyr" }]
}
]
}
Expand Down
Loading

0 comments on commit 2965048

Please sign in to comment.