Skip to content

Commit

Permalink
Refactor test to use graph equality check for conciseness / accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvc authored and ycombinator committed Apr 4, 2018
1 parent 2e69f1c commit ec3e8be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
3 changes: 3 additions & 0 deletions logstash-core/src/main/java/org/logstash/config/ir/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ public static PluginVertex gPlugin(SourceWithMetadata meta, PluginDefinition.Typ
return gPlugin(meta, type, pluginName, new HashMap<>());
}

public static PluginVertex gPlugin(SourceWithMetadata meta, PluginDefinition pluginDefinition) {
return gPlugin(meta, pluginDefinition.getType(), pluginDefinition.getName(), pluginDefinition.getArguments());
}

public static IfVertex gIf(SourceWithMetadata meta, BooleanExpression expression) {
return new IfVertex(meta, expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import static org.junit.Assert.assertTrue;
import java.util.stream.Stream;

import org.logstash.common.SourceWithMetadata;
import org.logstash.config.ir.DSL;
import org.logstash.config.ir.InvalidIRException;
import org.logstash.config.ir.graph.BooleanEdge;
import org.logstash.config.ir.graph.Graph;
import org.logstash.config.ir.graph.Vertex;
import org.logstash.config.ir.PluginDefinition;
import org.logstash.config.ir.expression.BooleanExpression;
import org.logstash.config.ir.expression.Expression;
import org.logstash.config.ir.graph.*;

import static org.logstash.config.ir.IRHelpers.*;

Expand All @@ -33,33 +36,27 @@ public void testEmptyIf() throws InvalidIRException {

@Test
public void testIfWithOneTrueStatement() throws InvalidIRException {
PluginDefinition pluginDef = testPluginDefinition();
Statement trueStatement = new PluginStatement(randMeta(), testPluginDefinition());
Statement falseStatement = new NoopStatement(randMeta());
BooleanExpression ifExpression = createTestExpression();
IfStatement ifStatement = new IfStatement(
randMeta(),
createTestExpression(),
ifExpression,
trueStatement,
falseStatement
);

Graph ifStatementGraph = ifStatement.toGraph();
assertFalse(ifStatementGraph.isEmpty());

Stream<Vertex> trueVertices = ifStatementGraph
.edges()
.filter(e -> e instanceof BooleanEdge)
.map(e -> (BooleanEdge) e)
.filter(e -> e.getEdgeType() == true)
.map(e -> e.getTo());
assertEquals(1, trueVertices.count());

Stream<Vertex> falseVertices = ifStatementGraph
.edges()
.filter(e -> e instanceof BooleanEdge)
.map(e -> (BooleanEdge) e)
.filter(e -> e.getEdgeType() == false)
.map(e -> e.getTo());
assertEquals(0, falseVertices.count());

Graph expected = new Graph();
IfVertex expectedIf = DSL.gIf(randMeta(), ifExpression);
expected.addVertex(expectedIf);
PluginVertex expectedT = DSL.gPlugin(randMeta(), testPluginDefinition());
expected.chainVertices(true, expectedIf, expectedT);

assertSyntaxEquals(expected, ifStatementGraph);
}


Expand Down

0 comments on commit ec3e8be

Please sign in to comment.