Skip to content

Commit

Permalink
test: migrate to cucumber v7.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeltour committed Nov 14, 2022
1 parent 8c04746 commit 39f97a4
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 207 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,19 @@
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.5.4</version>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.5.4</version>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.7.1</version>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/w3c/epubcheck/DebugCucumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@CucumberOptions(
tags = "@debug",
features = "classpath:",
strict = true,
monochrome = true,
plugin = { "pretty" })
public class DebugCucumberTest
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/org/w3c/epubcheck/RunCucumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "classpath:",
strict = true)
@CucumberOptions(features = "classpath:")
public class RunCucumberTest
{

Expand Down
32 changes: 24 additions & 8 deletions src/test/java/org/w3c/epubcheck/test/AssertionSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,44 @@ public void assertNoErrorsOrWarning()
assertThat("Unexpected warning", report.getAll(Severity.WARNING), is(emptyIterable()));
}

@Then("(the ){severity} {messageId} is reported( \\(.*)")
/*
* Common step definition for "is reported" and "is reported {int} times" see
* https://github.com/cucumber/cucumber-expressions/issues/166
*/
@Then("(the ){severity} {messageId} is reported{messageQuantity}( \\(){}")
public void assertMessage(Severity severity, MessageId id, int quantity, String ignore)
{
if (quantity == 1)
{
assertMessageOnce(severity, id);
}
else
{
assertMessageNTimes(severity, id, quantity);
}
}

// @Then("(the ){severity} {messageId} is reported")
public void assertMessageOnce(Severity severity, MessageId id)
{
lastAssertedMessage = report.consume(id);
assertThat("No message found with ID " + id, lastAssertedMessage, is(notNullValue()));
assertThat(lastAssertedMessage.getSeverity(), equalTo(severity));
}

@Then("(the ){severity} {messageId} is reported {int} time(s)( \\(.*)")
public void assertMessageNTimes(Severity severity, MessageId id, int times)
// @Then("(the ){severity} {messageId} is reported {int} time(s)")
public void assertMessageNTimes(Severity severity, MessageId id, int quantity)
{
List<MessageInfo> actual = report.consumeAll(id);
lastAssertedMessage = Iterables.getLast(actual, null);
assertThat(actual, hasSize(times));
assertThat(actual, hasSize(quantity));
assertThat(actual, everyItem(hasProperty("severity", equalTo(severity))));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Then("(the )following {severity}( ID)(s) are/is reported( \\(.*)")
public void assertMessageList(Severity severity, List<Matcher> matchers)
@Then("(the )following {severity}( ID)(s) are/is reported( \\(){}")
public void assertMessageList(Severity severity, String ignore,
List<Matcher<? super MessageInfo>> expected)
{
List<Matcher<? super MessageInfo>> expected = (List<Matcher<? super MessageInfo>>) (Object) matchers;
List<MessageInfo> actual = report.consumeAll(severity);
assertThat(actual, contains(expected));
}
Expand Down
126 changes: 126 additions & 0 deletions src/test/java/org/w3c/epubcheck/test/ParameterTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.w3c.epubcheck.test;

import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;

import java.util.List;
import java.util.Locale;

import org.hamcrest.Matcher;
import org.w3c.epubcheck.test.ExecutionSteps.CheckerMode;

import com.adobe.epubcheck.api.EPUBProfile;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.messages.Severity;
import com.adobe.epubcheck.util.EPUBVersion;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;

import io.cucumber.java.DataTableType;
import io.cucumber.java.ParameterType;

public class ParameterTypes
{

private static Function<String, MessageId> TO_ID = new Function<String, MessageId>()
{

@Override
public MessageId apply(String input)
{
Preconditions.checkArgument(input != null);
return MessageId.valueOf(input.replace('-', '_'));
}

};

@ParameterType("?i:(full publication|((Media Overlays|Navigation|Package|SVG Content|XHTML Content) Document))")
public CheckerMode checkerMode(String mode)
{
switch (mode.toLowerCase(Locale.ENGLISH))
{
case "full publication":
return ExecutionSteps.CheckerMode.EPUB;
case "media overlays document":
return ExecutionSteps.CheckerMode.MEDIA_OVERLAYS_DOC;
case "navigation document":
return ExecutionSteps.CheckerMode.NAVIGATION_DOC;
case "package document":
return ExecutionSteps.CheckerMode.PACKAGE_DOC;
case "svg content document":
return ExecutionSteps.CheckerMode.SVG_CONTENT_DOC;
case "xhtml content document":
return ExecutionSteps.CheckerMode.XHTML_CONTENT_DOC;
default:
throw new IllegalArgumentException("Unknown file type: " + mode);
}
}

@ParameterType(".*?")
public Locale locale(String locale)
{
try
{
return Locale.forLanguageTag(locale);
} catch (NullPointerException e)
{
throw new IllegalArgumentException("Couldn’t set locale: " + locale, e);
}
}

@ParameterType("[A-Z]{3}-[0-9]{3}[a-z]?")
public MessageId messageId(String id)
{
return TO_ID.apply(id);
}

@ParameterType("(?: )?(\\d*)(?: time(?:s)?)?")
public Integer messageQuantity(String times)
{
return (times.isEmpty()) ? 1 : Integer.parseInt(times);
}

@DataTableType
public Matcher<? super MessageInfo> messageRow(List<String> row)
{
if (row.size() == 1)
{
return hasProperty("id", equalTo(MessageId.valueOf(row.get(0).replace('-', '_'))));
}
else
{
return both(hasProperty("id", equalTo(TO_ID.apply(row.get(0)))))
.and(hasProperty("message", containsString(row.get(1))));
}
}

@ParameterType(".*?")
public EPUBProfile profile(String profile)
{
try
{
return EPUBProfile.valueOf(profile.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e)
{
throw new IllegalArgumentException("Unknown EPUBCheck profile: " + profile, e);
}
}

@ParameterType("?i:(fatal error|error|warning|usage|info)")
public Severity severity(String severity)
{
if ("fatal error".equals(severity)) severity = "fatal";
return Severity.valueOf(severity.toUpperCase(Locale.ENGLISH));
}

@ParameterType("\\d(?:\\.\\d)?(?:\\.\\d)?")
public EPUBVersion version(String version)
{
if (version.equals("3") || version.startsWith("3.")) return EPUBVersion.VERSION_3;
if (version.equals("2") || version.startsWith("2.")) return EPUBVersion.VERSION_2;
return EPUBVersion.Unknown;
}

}
Loading

0 comments on commit 39f97a4

Please sign in to comment.