Skip to content

Commit

Permalink
Simplify tests for splits in negative test data (#36)
Browse files Browse the repository at this point in the history
We propagate the split of negative samples in
[aas-core3.0-testgen 08d3ec37c] into unserializable and invalid negative
examples. This makes the generation and future maintenance of the test
code easier in this SDK as well, since we now do not have to track the
individual negative cases.

In addition, we improve the error messages if the recorded test data is
missing to make it clearer for the programmer what needs to be fixed in
that case.

[aas-core3.0-testgen 08d3ec37c]: aas-core-works/aas-core3.0-testgen@08d3ec37c
  • Loading branch information
mristin committed May 31, 2024
1 parent b798c90 commit b7968a5
Show file tree
Hide file tree
Showing 7,689 changed files with 2,879 additions and 1,699 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ class TracingVisitorThrough : Aas.Visitation.VisitorThrough
if (!System.IO.File.Exists(expectedPath))
{
throw new System.IO.FileNotFoundException(
$"The file with the recorded trace does not exist: {expectedPath}");
"The file with the recorded trace does not " +
$"exist: {expectedPath}; maybe you want to set the environment " +
$"variable {Aas.Tests.Common.RecordModeEnvironmentVariableName}?");
}
string expected = System.IO.File.ReadAllText(expectedPath);
Expand Down
4 changes: 3 additions & 1 deletion dev_scripts/test_codegen/generate_test_for_descend_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def main() -> int:
if (!System.IO.File.Exists(expectedPath))
{
throw new System.IO.FileNotFoundException(
$"The file with the recorded trace does not exist: {expectedPath}");
$"The file with the recorded trace does not exist: {expectedPath}; " +
"maybe you want to set the environment " +
$"variable {Aas.Tests.Common.RecordModeEnvironmentVariableName}?");
}
string expected = System.IO.File.ReadAllText(expectedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ def main() -> int:
if (!System.IO.File.Exists(expectedPath))
{
throw new System.IO.FileNotFoundException(
$"The file with the recorded value does not exist: {expectedPath}");
$"The file with the recorded value does not exist: {expectedPath}; " +
"maybe you want to set the environment " +
$"variable {Aas.Tests.Common.RecordModeEnvironmentVariableName}?");
}
Nodes.JsonNode expected = Aas.Tests.CommonJson.ReadFromFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _generate_for_self_contained(
var paths = Directory.GetFiles(
Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"SelfContained",
"Json",
"SelfContained",
"Expected",
{csharp_common.string_literal(cls_name_json)}
),
Expand Down Expand Up @@ -71,25 +71,31 @@ def _generate_for_self_contained(
[Test]
public void Test_{cls_name_csharp}_deserialization_fail()
{{
foreach (string cause in CausesForDeserializationFailure)
{{
string baseDir = Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"SelfContained",
"Unexpected",
cause,
foreach (
string causeDir in
Directory.GetDirectories(
Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"SelfContained",
"Unexpected",
"Unserializable"
)
)
) {{
string clsDir = Path.Combine(
causeDir,
{csharp_common.string_literal(cls_name_json)}
);
if (!Directory.Exists(baseDir))
if (!Directory.Exists(clsDir))
{{
// No examples of {cls_name_csharp} for the failure cause.
continue;
}}
}}
var paths = Directory.GetFiles(
baseDir,
clsDir,
"*.json",
System.IO.SearchOption.AllDirectories).ToList();
paths.Sort();
Expand Down Expand Up @@ -123,25 +129,31 @@ def _generate_for_self_contained(
[Test]
public void Test_{cls_name_csharp}_verification_fail()
{{
foreach (string cause in Aas.Tests.Common.CausesForVerificationFailure)
{{
string baseDir = Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"SelfContained",
"Unexpected",
cause,
foreach (
string causeDir in
Directory.GetDirectories(
Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"SelfContained",
"Unexpected",
"Invalid"
)
)
) {{
string clsDir = Path.Combine(
causeDir,
{csharp_common.string_literal(cls_name_json)}
);
if (!Directory.Exists(baseDir))
if (!Directory.Exists(clsDir))
{{
// No examples of {cls_name_csharp} for the failure cause.
continue;
}}
var paths = Directory.GetFiles(
baseDir,
clsDir,
"*.json",
System.IO.SearchOption.AllDirectories).ToList();
paths.Sort();
Expand All @@ -152,7 +164,7 @@ def _generate_for_self_contained(
var instance = Aas.Jsonization.Deserialize.{cls_name_csharp}From(
node);
var errors = Aas.Verification.Verify(instance).ToList();
Aas.Tests.Common.AssertEqualsExpectedOrRerecordVerificationErrors(
errors, path);
Expand Down Expand Up @@ -184,8 +196,8 @@ def _generate_for_contained_in_environment(
var paths = Directory.GetFiles(
Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"ContainedInEnvironment",
"Json",
"ContainedInEnvironment",
"Expected",
{csharp_common.string_literal(cls_name_json)}
),
Expand Down Expand Up @@ -216,7 +228,7 @@ def _generate_for_contained_in_environment(
[Test]
public void Test_{cls_name_csharp}_deserialization_from_non_object_fail()
{{
var node = Nodes.JsonValue.Create("INVALID")
var node = Nodes.JsonValue.Create("INVALID")
?? throw new System.InvalidOperationException(
"Unexpected failure of the node creation");
Expand Down Expand Up @@ -251,24 +263,31 @@ def _generate_for_contained_in_environment(
[Test]
public void Test_{cls_name_csharp}_deserialization_fail()
{{
foreach (string cause in CausesForDeserializationFailure)
{{
string baseDir = Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"ContainedInEnvironment",
"Unexpected",
cause,
{csharp_common.string_literal(cls_name_json)});
if (!Directory.Exists(baseDir))
foreach (
string causeDir in
Directory.GetDirectories(
Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"ContainedInEnvironment",
"Unexpected",
"Unserializable"
)
)
) {{
string clsDir = Path.Combine(
causeDir,
{csharp_common.string_literal(cls_name_json)}
);
if (!Directory.Exists(clsDir))
{{
// No examples of {cls_name_csharp} for the failure cause.
continue;
}}
var paths = Directory.GetFiles(
baseDir,
clsDir,
"*.json",
System.IO.SearchOption.AllDirectories).ToList();
paths.Sort();
Expand Down Expand Up @@ -302,25 +321,31 @@ def _generate_for_contained_in_environment(
[Test]
public void Test_{cls_name_csharp}_verification_fail()
{{
foreach (string cause in Aas.Tests.Common.CausesForVerificationFailure)
{{
string baseDir = Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"ContainedInEnvironment",
"Unexpected",
cause,
foreach (
string causeDir in
Directory.GetDirectories(
Path.Combine(
Aas.Tests.Common.TestDataDir,
"Json",
"ContainedInEnvironment",
"Unexpected",
"Invalid"
)
)
) {{
string clsDir = Path.Combine(
causeDir,
{csharp_common.string_literal(cls_name_json)}
);
if (!Directory.Exists(baseDir))
if (!Directory.Exists(clsDir))
{{
// No examples of {cls_name_csharp} for the failure cause.
continue;
}}
var paths = Directory.GetFiles(
baseDir,
clsDir,
"*.json",
System.IO.SearchOption.AllDirectories).ToList();
paths.Sort();
Expand Down Expand Up @@ -398,16 +423,6 @@ def main() -> int:
}
}
private static readonly List<string> CausesForDeserializationFailure = (
new List<string>()
{
"TypeViolation",
"RequiredViolation",
"EnumViolation",
"NullViolation",
"UnexpectedAdditionalProperty"
});
private static void AssertEqualsExpectedOrRerecordDeserializationException(
Aas.Jsonization.Exception? exception,
string path)
Expand All @@ -431,7 +446,9 @@ def main() -> int:
if (!System.IO.File.Exists(exceptionPath))
{
throw new System.IO.FileNotFoundException(
$"The file with the recorded exception does not exist: {exceptionPath}");
$"The file with the recorded exception does not exist: {exceptionPath}; " +
"maybe you want to set the environment " +
$"variable {Aas.Tests.Common.RecordModeEnvironmentVariableName}?");
}
string expected = System.IO.File.ReadAllText(exceptionPath);
Expand Down Expand Up @@ -491,7 +508,6 @@ def main() -> int:
using Nodes = System.Text.Json.Nodes;
using Path = System.IO.Path;
using System.Collections.Generic; // can't alias
using System.Linq; // can't alias
using NUnit.Framework; // can't alias
Expand Down
Loading

0 comments on commit b7968a5

Please sign in to comment.