Skip to content

Commit

Permalink
fixed an issue where a single decimal property as a condition after a…
Browse files Browse the repository at this point in the history
…n AND/OR was parsed as an ASTOperandStaticString
  • Loading branch information
lionelpa authored and Bischoff, Silas committed Oct 2, 2019
1 parent c9bd8b7 commit 36997f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
Expand Up @@ -116,13 +116,9 @@ public ASTCondition transformImplicitCondition(ASTItem item, ParseTree source) t
builder.withSource(source.getText());

if (item != null && item instanceof ASTOperandBase) {
if (((ASTOperandBase) item).getDataType() != DataPropertyType.Boolean) {
builder.withLeftOperand(new ASTOperandStaticString(item.getPreprocessedSource()));
} else {
builder
.withLeftOperand((ASTOperandBase) item)
.withOperator(ASTComparisonOperator.EQUALS)
.withRightOperandAsBoolean(true);
builder.withLeftOperand((ASTOperandBase) item);
if (((ASTOperandBase) item).getDataType() == DataPropertyType.Boolean) {
builder.withOperator(ASTComparisonOperator.EQUALS);
}
}

Expand Down
Expand Up @@ -195,4 +195,44 @@ void should_create_implicit_equals_with_2_number_operand_on_the_left_side() thro
.hasValue(500.0);
});
}

@Test
void condition_group_with_incomplete_condition_and_decimal_property() throws Exception {
// assemble
//IF year SMALLER THAN another_int AND age THEN error
String input =
GrammarBuilder.createRule()
.with("year")
.LESS_THAN()
.with("another_int")
.AND()
.with("age")
.THEN("error")
.getText();

String schema = "{year: 1234, another_int: 1234, age: 12}";

// assert
ANTLRRunner.run(
input,
schema,
r -> {
r.rules()
.hasSizeOf(1)
.first()
.conditionGroup()
.hasSize(2)
.first()
.hasOperator(ASTComparisonOperator.LESS_THAN)
.leftProperty()
.parentCondition()
.rightProperty()
.parentConditionGroup()
.second()
.hasNoOperator()
.hasNoRightOperand()
.leftProperty("age")
.hasType(DataPropertyType.Decimal);
});
}
}
Expand Up @@ -42,6 +42,12 @@ public ConditionAssertion hasOperator() {
return this;
}

public ConditionAssertion hasNoOperator() {
shouldBeNull(this.model.getOperator(), "OPERATOR");

return this;
}

public ConditionAssertion hasIndentationLevel(int indentationLevel) {
shouldNotBeEmpty(this.model.getIndentationLevel(), "INDENTATION LEVEL");
shouldEquals(this.model.getIndentationLevel(), indentationLevel, "INDENTATION LEVEL");
Expand Down
Expand Up @@ -77,6 +77,8 @@ public ConditionAssertion parentCondition() {
return parentOperand().parentCondition();
}

public ConditionGroupAssertion parentConditionGroup() {return parentCondition().parentConditionGroup();}

public ModelRootAssertion parentModel() {
if (this.parent() instanceof VariableAssertion)
return ((VariableAssertion) this.parent()).parentModel();
Expand Down
10 changes: 10 additions & 0 deletions openvalidation-core/src/test/java/exceptionhandling/RuleTest.java
Expand Up @@ -216,4 +216,14 @@ public void incomplete_condition() throws Exception {
r.containsValidationMessage("missing right operand in condition.");
});
}

@Test
public void condition_group_with_incomplete_condition_and_decimal_property() throws Exception {
runner.run(
"IF year SMALLER THAN another_int AND age THEN error",
"{year: 1234, another_int: 1234, age: 12}",
r -> {
r.containsValidationMessage("invalid condition. missing comparison operator and operand.");
});
}
}

0 comments on commit 36997f5

Please sign in to comment.