Skip to content

Commit

Permalink
Adding more unit tests for IfStatement class
Browse files Browse the repository at this point in the history
Fixes #9314
  • Loading branch information
ycombinator committed Apr 4, 2018
1 parent d5c9d33 commit def8742
Showing 1 changed file with 107 additions and 5 deletions.
@@ -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());
}
}

0 comments on commit def8742

Please sign in to comment.