-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e2b310
commit 2dc880a
Showing
15 changed files
with
1,008 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
smithy-model/src/test/java/software/amazon/smithy/model/selector/SelectorRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package software.amazon.smithy.model.selector; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.loader.Prelude; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
public final class SelectorRunnerTest { | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("source") | ||
public void selectorTest(Path filename) { | ||
Model model = Model.assembler().addImport(filename).assemble().unwrap(); | ||
List<ObjectNode> tests = findTestCases(model); | ||
|
||
for (ObjectNode test : tests) { | ||
Selector selector = Selector.parse(test.expectStringMember("selector").getValue()); | ||
boolean skipPrelude = test.getBooleanMemberOrDefault("skipPreludeShapes", false); | ||
|
||
Set<ShapeId> expectedMatches = new HashSet<>(new HashSet<>(test.expectArrayMember("matches") | ||
.getElementsAs(n -> n.expectStringNode("Each element of matches must be an ID").expectShapeId()))) | ||
.stream() | ||
.filter(shapeId -> { | ||
String namespace = shapeId.getNamespace(); | ||
return !namespace.contains(Prelude.NAMESPACE) | ||
|| (!skipPrelude && namespace.contains(Prelude.NAMESPACE)); | ||
}) | ||
.collect(Collectors.toSet()); | ||
|
||
Set<ShapeId> actualMatches = selector.shapes(model) | ||
.map(Shape::getId) | ||
.filter(shapeId -> { | ||
String namespace = shapeId.getNamespace(); | ||
return !namespace.contains(Prelude.NAMESPACE) | ||
|| (!skipPrelude && namespace.contains(Prelude.NAMESPACE)); | ||
}) | ||
.collect(Collectors.toSet()); | ||
|
||
if (!expectedMatches.equals(actualMatches)) { | ||
failTest(filename, test, expectedMatches, actualMatches); | ||
} | ||
} | ||
} | ||
|
||
private List<ObjectNode> findTestCases(Model model) { | ||
return model.getMetadataProperty("selectorTests") | ||
.orElseThrow(() -> new IllegalArgumentException("Missing selectorTests metadata key")) | ||
.expectArrayNode("selectorTests must be an array") | ||
.getElementsAs(ObjectNode.class); | ||
} | ||
|
||
private void failTest(Path filename, ObjectNode test, Set<ShapeId> expectedMatches, Set<ShapeId> actualMatches) { | ||
String selector = test.expectStringMember("selector").getValue(); | ||
Set<ShapeId> missing = new TreeSet<>(expectedMatches); | ||
missing.removeAll(actualMatches); | ||
|
||
Set<ShapeId> extra = new TreeSet<>(actualMatches); | ||
extra.removeAll(expectedMatches); | ||
|
||
StringBuilder error = new StringBuilder("Selector ") | ||
.append(selector) | ||
.append(" test case failed.\n"); | ||
|
||
if (!missing.isEmpty()) { | ||
error.append("The following shapes were not matched: ").append(missing).append(".\n"); | ||
} | ||
|
||
if (!extra.isEmpty()) { | ||
error.append("The following shapes were matched unexpectedly: ").append(extra).append(".\n"); | ||
} | ||
|
||
test.getStringMember("documentation") | ||
.ifPresent(docs -> error.append('(').append(docs.getValue()).append(")")); | ||
|
||
Assertions.fail(error.toString()); | ||
} | ||
|
||
public static List<Path> source() throws Exception { | ||
List<Path> paths = new ArrayList<>(); | ||
try (Stream<Path> files = Files.walk(Paths.get(SelectorRunnerTest.class.getResource("cases").toURI()))) { | ||
files | ||
.filter(Files::isRegularFile) | ||
.filter(file -> { | ||
String filename = file.toString(); | ||
return filename.endsWith(".smithy") || filename.endsWith(".json"); | ||
}) | ||
.forEach(paths::add); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error loading models for selector runner", e); | ||
} | ||
|
||
return paths; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...src/test/resources/software/amazon/smithy/model/selector/cases/attribute-existence.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
$version: "2.0" | ||
|
||
metadata selectorTests = [ | ||
{ | ||
selector: "[trait|enum]" | ||
matches: [ | ||
smithy.example#SimpleEnum | ||
smithy.example#EnumWithTags | ||
] | ||
}, | ||
{ | ||
selector: "[trait|enum|(values)|tags|(values)]" | ||
matches: [ | ||
smithy.example#EnumWithTags | ||
] | ||
} | ||
] | ||
|
||
namespace smithy.example | ||
|
||
@deprecated | ||
string NoMatch | ||
|
||
@enum([ | ||
{name: "foo", value: "foo"} | ||
{name: "baz", value: "baz"} | ||
]) | ||
string SimpleEnum | ||
|
||
@enum([ | ||
{name: "foo", value: "foo", tags: ["a"]} | ||
{name: "baz", value: "baz"} | ||
{name: "spam", value: "spam", tags: []} | ||
]) | ||
string EnumWithTags |
Oops, something went wrong.