Skip to content

Commit

Permalink
Merge a34d494 into 5630f68
Browse files Browse the repository at this point in the history
  • Loading branch information
Altai-man committed May 12, 2018
2 parents 5630f68 + a34d494 commit 86ba288
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 81 deletions.
2 changes: 1 addition & 1 deletion tap4j/src/main/java/org/tap4j/model/Patterns.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private Patterns() { // hidden as this is a final utility class
/**
* TAP Test Result Regex.
*/
static final String REGEX_TEST_RESULT = "((\\s|\\t)*)?(ok|not ok)\\s*(\\d*)\\s*([^#]*)?\\s*"
static final String REGEX_TEST_RESULT = "(\\s*)(ok|not ok)\\s*(\\d*)\\s*([^#]*)?\\s*"
+ "(#\\s*(SKIP|skip|TODO|todo)\\s*([^#]*))?\\s*(#\\s*(.*))?";

/**
Expand Down
14 changes: 7 additions & 7 deletions tap4j/src/main/java/org/tap4j/model/TapElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,22 @@ public static TapElement createTapElement(String tapLine) {

m = Patterns.TEST_RESULT_PATTERN.matcher(tapLine);
if (m.matches()) {
String testNumberText = m.group(4);
String testNumberText = m.group(3);
int testNumber = 0;
if (testNumberText != null && testNumberText.trim().equals("") == false) {
if (testNumberText != null && !testNumberText.trim().equals("")) {
testNumber = Integer.parseInt(testNumberText);
}
TestResult testResult = new TestResult(StatusValues.get(m.group(3)), testNumber);
String comment = m.group(10);
TestResult testResult = new TestResult(StatusValues.get(m.group(2)), testNumber);
String comment = m.group(9);
if (comment != null && comment.trim().length() > 0) {
final Comment c = new Comment(comment, true);
testResult.setComment(c);
testResult.addComment(c);
}
testResult.setDescription(m.group(5));
DirectiveValues directive = DirectiveValues.get(m.group(7));
testResult.setDescription(m.group(4));
DirectiveValues directive = DirectiveValues.get(m.group(6));
if (directive != null) {
testResult.setDirective(new Directive(directive, m.group(8)));
testResult.setDirective(new Directive(directive, m.group(7)));
}
testResult.indentation = m.group(1).length();
return testResult;
Expand Down
11 changes: 0 additions & 11 deletions tap4j/src/main/java/org/tap4j/parser/StreamStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ public class StreamStatus {
*/
private final TestSet testSet = new TestSet();

/**
* In case sub-tests could not have been attached to parent (as the sub-tests came first)
* we need to make sure we make the link as the parent test comes.
*/
protected boolean attachedToParent;

/**
* Stream status test set.
*/
protected TestSet looseSubtests;

/**
* Default constructor.
*/
Expand Down
73 changes: 36 additions & 37 deletions tap4j/src/main/java/org/tap4j/parser/Tap13Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,11 @@
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.tap4j.model.BailOut;
import org.tap4j.model.Comment;
import org.tap4j.model.Footer;
import org.tap4j.model.Header;
import org.tap4j.model.Plan;
import org.tap4j.model.TapElement;
import org.tap4j.model.TapElementFactory;
import org.tap4j.model.TestResult;
import org.tap4j.model.TestSet;
import org.tap4j.model.Text;
import org.tap4j.model.*;
import org.yaml.snakeyaml.Yaml;

/**
Expand All @@ -72,7 +61,7 @@ public class Tap13Parser implements Parser {
* Stack of stream status information bags. Every bag stores state of the parser
* related to certain indentation level. This is to support subtest feature.
*/
private Stack<StreamStatus> states = new Stack<StreamStatus>();
private Stack<StreamStatus> states = new Stack<>();

/**
* The current state.
Expand All @@ -97,6 +86,11 @@ public class Tap13Parser implements Parser {
*/
private boolean enableSubtests = true;

/**
* A stack that holds subtests for which we don't know exact parent yet.
*/
private Stack<StreamStatus> subStack = new Stack<>();

/**
* Parser Constructor.
*
Expand Down Expand Up @@ -280,31 +274,24 @@ public void parseLine(String tapLine) {
baseIndentation = indentation;
}

if (indentation != state.getIndentationLevel()) { // indentation changed
StreamStatus prevState = null;
if (indentation != state.getIndentationLevel() && enableSubtests) { // indentation changed

if (indentation > state.getIndentationLevel()) {
StreamStatus parentState = state;
int prevIndent = state.getIndentationLevel();
pushState(indentation); // make room for children

TapElement lastParentElement = parentState.getLastParsedElement();
if (lastParentElement instanceof TestResult) {
final TestResult lastTestResult = (TestResult) lastParentElement;
// whatever test set comes should be attached to parent
if (lastTestResult.getSubtest() == null && this.enableSubtests) {
lastTestResult.setSubtest(state.getTestSet());
state.attachedToParent = true;
}
}
if (indentation - prevIndent > 4)
subStack.push(state);
} else {
// going down
do {
StreamStatus prevState = state;
if (states.peek().getIndentationLevel() == indentation) {
prevState = state;
state = states.pop();
if (!prevState.attachedToParent && this.enableSubtests) {
state.looseSubtests = prevState.getTestSet();
}
// there could be more than one level diff
} while (indentation < state.getIndentationLevel());
} else {
state = new StreamStatus();
state.setIndentationLevel(indentation);
subStack.push(state);
}
}
}

Expand Down Expand Up @@ -343,7 +330,7 @@ public void parseLine(String tapLine) {

final TestResult testResult = (TestResult) tapElement;
if (testResult.getTestNumber() == 0) {
if (state.getTestSet().getPlan() != null && state.isPlanBeforeTestResult() == false) {
if (state.getTestSet().getPlan() != null && !state.isPlanBeforeTestResult()) {
return; // done testing mark
}
if (state.getTestSet().getPlan() != null &&
Expand All @@ -354,9 +341,21 @@ public void parseLine(String tapLine) {
}

state.getTestSet().addTestResult(testResult);
if (state.looseSubtests != null && this.enableSubtests) {
testResult.setSubtest(state.looseSubtests);
state.looseSubtests = null;

if (prevState != null && enableSubtests) {
state.getTestSet().getTestResults().get(
state.getTestSet().getNumberOfTestResults() - 1)
.setSubtest(prevState.getTestSet());
}

if (indentation == 0 && enableSubtests) {
TestResult currLast = state.getTestSet().getTestResults().get(
state.getTestSet().getNumberOfTestResults() - 1);
while (!subStack.empty()) {
StreamStatus nextLevel = subStack.pop();
currLast.setSubtest(nextLevel.getTestSet());
currLast = nextLevel.getTestSet().getTestResults().get(0);
}
}

} else if (tapElement instanceof Footer) {
Expand Down
14 changes: 7 additions & 7 deletions tap4j/src/main/java/org/tap4j/representer/Tap13Representer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ protected void printTapLine(PrintWriter pw, TapElement tapResult) {
* @param testResult TAP test result
*/
protected void printTestResult(PrintWriter pw, TestResult testResult) {
if (testResult.getSubtest() != null) {
int indent = this.options.getIndent();
int spaces = this.options.getSpaces();
this.options.setIndent(indent + spaces);
pw.append(this.representData(testResult.getSubtest()));
this.options.setIndent(indent);
}
printFiller(pw);
pw.append(testResult.getStatus().toString());
pw.append(' ' + Integer.toString(testResult.getTestNumber()));
Expand Down Expand Up @@ -155,13 +162,6 @@ protected void printTestResult(PrintWriter pw, TestResult testResult) {
}
printDiagnostic(pw, testResult);
pw.append(LINE_SEPARATOR);
if (testResult.getSubtest() != null) {
int indent = this.options.getIndent();
int spaces = this.options.getSpaces();
this.options.setIndent(indent + spaces);
pw.append(this.representData(testResult.getSubtest()));
this.options.setIndent(indent);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class TestIssue3504508 extends BaseTapTest {
@Test
public void testTapConsumer() {
final TestSet testSet = getTestSet(new Tap13Parser(/* enable subtests*/ true), "/org/tap4j/consumer/issue3504508/sample.tap");
assertTrue(testSet.getTestResult(1).getSubtest() != null);
assertTrue(testSet.getTestResult(1).getSubtest()
assertTrue(testSet.getTestResult(1).getSubtest() == null);
assertTrue(testSet.getTestResult(2).getSubtest()
.getTestResult(2).getSubtest() != null);
assertTrue(testSet.getTestResults().size() == 3);
}
Expand All @@ -59,10 +59,10 @@ public void testProducingSubtests() {
+ "ok 1 - First test\n"
+ " 1..2\n"
+ " ok 1 - This is a subtest\n"
+ " ok 2 - So is this\n"
+ " 1..2\n"
+ " 1..2\n"
+ " ok 1 - This is a subtest\n"
+ " ok 2 - So is this\n"
+ " ok 2 - So is this\n"
+ " ok 2 - So is this\n"
+ "ok 2 - An example subtest\n"
+ "ok 3 - Third test\n";
final TapConsumer consumer = TapConsumerFactory.makeTap13YamlConsumer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* The MIT License
*
* Copyright (c) 2010 tap4j team (see AUTHORS)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.tap4j.consumer.subtestOrder;

import org.junit.Test;
import org.tap4j.BaseTapTest;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.TestSet;
import org.tap4j.producer.TapProducerFactory;

import java.io.File;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;

/**
* Tests for correct subtests order.
*
* @since 0.1
*/
public class TestSubtestOrder extends BaseTapTest {
@Test
public void testProducingSubtests() {
final TapConsumer consumer = TapConsumerFactory.makeTap13YamlConsumer();
final TestSet testSet = consumer.load(new File(TestSubtestOrder.class
.getResource("/org/tap4j/consumer/subtestOrder/subtest.tap").getFile()));
String expected = "1..2\n"
+ "ok 1 - First test\n"
+ " 1..1\n"
+ " 1..1\n"
+ " ok 1 - Internal subtest subtest\n"
+ " ok 1 - Internal subtest\n"
+ "ok 2 - Some subtest\n";
assertEquals(expected, TapProducerFactory.makeTap13Producer().dump(testSet));
assertNotNull(testSet.getTestResult(2).getSubtest());
assertNotNull(testSet.getTestResult(2).getSubtest()
.getTestResult(1).getSubtest());
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
1..3
ok 1 - First test
1..2
ok 1 - This is a subtest
ok 2 - So is this
1..3
ok 1 - First test
1..2
ok 1 - This is a subtest
1..2
ok 1 - This is a subtest
ok 2 - So is this
ok 2 - An example subtest
ok 3 - Third test
ok 2 - So is this
ok 2 - An example subtest
ok 3 - Third test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ok 1 - First test
ok 1 - Internal subtest subtest
1..1
ok 1 - Internal subtest
1..1
ok 2 - Some subtest
1..2
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
TAP version 13
1..2
---
datetime: 20100101T000000
...
ok 1 - someDummyTest
---
datetime: 20100101T000002
...
Expand All @@ -13,9 +9,13 @@ ok 1 - someDummyTest
ok
ok
ok
ok 2 - anotherDummyTest
ok 1 - someDummyTest
---
datetime: 20100101T000005
...
1..1
ok
ok
ok 2 - anotherDummyTest
---
datetime: 20100101T000000
...

0 comments on commit 86ba288

Please sign in to comment.