Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding more unit tests for IfStatement class
Fixes #9314
- Loading branch information
1 parent
d5c9d33
commit def8742
Showing
1 changed file
with
107 additions
and
5 deletions.
There are no files selected for viewing
112 changes: 107 additions & 5 deletions
112
logstash-core/src/test/java/org/logstash/config/ir/imperative/IfStatementTest.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 |
---|---|---|
@@ -1,25 +1,127 @@ | ||
package org.logstash.config.ir.imperative; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import java.util.stream.Stream; | ||
|
||
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 static org.logstash.config.ir.IRHelpers.*; | ||
|
||
public class IfStatementTest { | ||
|
||
@Test | ||
public void testEmptyIfStatement() throws InvalidIRException { | ||
public void testEmptyIf() throws InvalidIRException { | ||
Statement trueStatement = new NoopStatement(randMeta()); | ||
Statement falseStatement = new NoopStatement(randMeta()); | ||
IfStatement ifStatement = new IfStatement( | ||
randMeta(), | ||
createTestExpression(), | ||
new NoopStatement(randMeta()), | ||
new NoopStatement(randMeta()) | ||
randMeta(), | ||
createTestExpression(), | ||
trueStatement, | ||
falseStatement | ||
); | ||
|
||
Graph ifStatementGraph = ifStatement.toGraph(); | ||
assertTrue(ifStatementGraph.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testIfWithOneTrueStatement() throws InvalidIRException { | ||
Statement trueStatement = new PluginStatement(randMeta(), testPluginDefinition()); | ||
Statement falseStatement = new NoopStatement(randMeta()); | ||
IfStatement ifStatement = new IfStatement( | ||
randMeta(), | ||
createTestExpression(), | ||
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()); | ||
} | ||
|
||
|
||
@Test | ||
public void testIfWithOneFalseStatement() throws InvalidIRException { | ||
Statement trueStatement = new NoopStatement(randMeta()); | ||
Statement falseStatement = new PluginStatement(randMeta(), testPluginDefinition()); | ||
IfStatement ifStatement = new IfStatement( | ||
randMeta(), | ||
createTestExpression(), | ||
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(0, 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(1, falseVertices.count()); | ||
} | ||
|
||
@Test | ||
public void testIfWithOneTrueOneFalseStatement() throws InvalidIRException { | ||
Statement trueStatement = new PluginStatement(randMeta(), testPluginDefinition()); | ||
Statement falseStatement = new PluginStatement(randMeta(), testPluginDefinition()); | ||
IfStatement ifStatement = new IfStatement( | ||
randMeta(), | ||
createTestExpression(), | ||
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(1, falseVertices.count()); | ||
} | ||
} |