Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return type of substring method #1504

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<Type> getArguments() {

@Override
public Type getReturnType() {
return Type.string();
return Type.optional(Type.string());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Type typeCheck(Scope<Type> scope) {
*/
public Type type() {
if (cachedType == null) {
throw new RuntimeException("you must call typeCheck first");
throw new RuntimeException("typechecking was never invoked on this expression.");
}
return cachedType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package software.amazon.smithy.rulesengine;

import java.io.InputStream;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.rulesengine.language.EndpointRuleSet;

public class RulesetTestUtil {
public static EndpointRuleSet loadRuleSet(String resourceId) {
InputStream is = RulesetTestUtil.class.getClassLoader().getResourceAsStream(resourceId);
assert is != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use the try-with-resources (try (InputStream is...) {) pattern to not need this assertion and also safely close the stream.

Node node = ObjectNode.parse(is);
return EndpointRuleSet.fromNode(node);
}

public static EndpointRuleSet minimalRuleSet() {
return loadRuleSet("software/amazon/smithy/rulesengine/testutil/valid-rules/minimal-ruleset.json");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.rulesengine.RulesetTestUtil;
import software.amazon.smithy.rulesengine.language.eval.RuleEvaluator;
import software.amazon.smithy.rulesengine.language.eval.Scope;
import software.amazon.smithy.rulesengine.language.eval.Value;
Expand All @@ -35,17 +36,9 @@
import software.amazon.smithy.utils.MapUtils;

class EndpointRuleSetTest {
private EndpointRuleSet parse(String resource) {
InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
assert is != null;
Node node = ObjectNode.parse(is);
return EndpointRuleSet.fromNode(node);
}

@Test
void testRuleEval() {
EndpointRuleSet actual = parse(
"software/amazon/smithy/rulesengine/testutil/valid-rules/minimal-ruleset.json");
EndpointRuleSet actual = RulesetTestUtil.minimalRuleSet();
Value result = RuleEvaluator.evaluate(actual, MapUtils.of(Identifier.of("Region"),
Value.string("us-east-1")));
Value.Endpoint expected = new Value.Endpoint.Builder(SourceLocation.none())
Expand All @@ -63,9 +56,7 @@ void testRuleEval() {

@Test
void testMinimalRuleset() {
EndpointRuleSet actual = parse(
"software/amazon/smithy/rulesengine/testutil/valid-rules/minimal-ruleset.json");
actual.typeCheck(new Scope<>());
EndpointRuleSet actual = RulesetTestUtil.minimalRuleSet();
assertEquals(EndpointRuleSet.builder()
.version("1.3")
.parameters(Parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package software.amazon.smithy.rulesengine.language;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.InputStream;
import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.rulesengine.RulesetTestUtil;
import software.amazon.smithy.rulesengine.language.EndpointRuleSet;
import software.amazon.smithy.rulesengine.language.eval.Type;
import software.amazon.smithy.rulesengine.language.syntax.rule.Condition;

public class TypeIntrospectionTest {
@Test
void introspectCorrectTypesForFunctions() {
EndpointRuleSet actual = RulesetTestUtil.loadRuleSet(
"software/amazon/smithy/rulesengine/testutil/valid-rules/substring.json");
List<Condition> conditions = actual.getRules().get(0).getConditions();
// stringEquals({TestCaseId}, 1)
assertEquals(conditions.get(0).getFn().type(), Type.bool());

// output = substring({Input}, ...);
assertEquals(conditions.get(1).getFn().type(), Type.optional(Type.string()));
}

@Test
void introspectCorrectTypesForGetAttr() {
EndpointRuleSet actual = RulesetTestUtil.loadRuleSet(
"software/amazon/smithy/rulesengine/testutil/valid-rules/get-attr-type-inference.json");
// bucketArn.resourceId[2]
assertEquals(actual.getRules().get(0).getConditions().get(2).getFn().type(), Type.optional(Type.string()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,16 @@
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.rulesengine.RulesetTestUtil;
import software.amazon.smithy.rulesengine.language.EndpointRuleSet;
import software.amazon.smithy.rulesengine.language.syntax.Identifier;
import software.amazon.smithy.utils.MapUtils;

class RuleEngineTest {

private EndpointRuleSet parse(String resource) {
InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
assert is != null;
Node node = ObjectNode.parse(is);
return EndpointRuleSet.fromNode(node);
}

@Test
void testRuleEval() {
EndpointRuleSet actual = parse(
"software/amazon/smithy/rulesengine/testutil/valid-rules/minimal-ruleset.json");
EndpointRuleSet actual = RulesetTestUtil.minimalRuleSet();
Value result = RuleEvaluator.evaluate(actual, MapUtils.of(Identifier.of("Region"),
Value.string("us-east-1")));
Value.Endpoint expected = new Value.Endpoint.Builder(SourceLocation.none())
Expand Down