Commit ed7deaa8 authored by Ferdie Reijnen's avatar Ferdie Reijnen
Browse files

Merge branch '200-improve-assert-class-messages' into 'develop'

#200 Improved Assert class messages.

Closes #200

See merge request !158
parents b3487636 70d9bf44
Pipeline #911 passed with stage
in 0 seconds
......@@ -143,8 +143,8 @@ class AsciiDocHtmlAnalyzer {
}
}
});
Assert.check(pageStack.size() == 1, String.valueOf(pageStack.size()));
Assert.check(tocStack.size() == 1, String.valueOf(tocStack.size()));
Assert.check(pageStack.size() == 1, pageStack.size());
Assert.check(tocStack.size() == 1, tocStack.size());
// Ensure content for each page.
for (AsciiDocHtmlPage page: htmlPages.pages) {
......@@ -152,7 +152,7 @@ class AsciiDocHtmlAnalyzer {
}
// Return the TOC.
Assert.check(tocStack.peek().right == 0, String.valueOf(tocStack.peek().right));
Assert.check(tocStack.peek().right == 0, tocStack.peek().right);
htmlPages.toc = tocStack.pop().left;
}
}
......@@ -345,9 +345,9 @@ class AsciiDocHtmlModifier {
// Move copyright/version.
Element elemBodyCopyrightVersion = single(doc.select("#header div.details"));
elemBodyCopyrightVersion.remove();
Assert.check(elemBodyCopyrightVersion.children().size() == 3, elemBodyCopyrightVersion.toString());
Assert.check(elemBodyCopyrightVersion.children().size() == 3, elemBodyCopyrightVersion);
Elements elemBodyCopyrightVersionSpans = elemBodyCopyrightVersion.children().select("span");
Assert.check(elemBodyCopyrightVersionSpans.size() == 2, elemBodyCopyrightVersionSpans.toString());
Assert.check(elemBodyCopyrightVersionSpans.size() == 2, elemBodyCopyrightVersionSpans);
elemBodyCopyrightVersionSpans.removeAttr("id");
elemBodyCopyrightVersionSpans.removeAttr("class");
for (Element elem: Lists.reverse(elemBodyCopyrightVersionSpans)) {
......@@ -357,7 +357,7 @@ class AsciiDocHtmlModifier {
// Move title.
Element elemBodyTitle = single(doc.select("#header h1"));
Assert.check(elemBodyTitle.children().isEmpty(), elemBodyTitle.toString());
Assert.check(elemBodyTitle.children().isEmpty(), elemBodyTitle);
elemBodyTitle.tagName("span");
elemBodyFooterText.prependChild(elemBodyTitle);
}
......@@ -468,8 +468,8 @@ class AsciiDocHtmlModifier {
minHeaderNr = Math.min(headerNr, minHeaderNr);
}
}
Assert.check(minHeaderNr > 0, String.valueOf(minHeaderNr));
Assert.check(minHeaderNr < Integer.MAX_VALUE, String.valueOf(minHeaderNr));
Assert.check(minHeaderNr > 0, minHeaderNr);
Assert.check(minHeaderNr < Integer.MAX_VALUE, minHeaderNr);
// Normalize header numbers to ensure minimum header number is '2'.
for (Element elem: elemContent.getAllElements()) {
......@@ -477,7 +477,7 @@ class AsciiDocHtmlModifier {
if (matcher.matches()) {
int headerNr = Integer.parseInt(matcher.group(1), 10);
int newHeaderNr = headerNr - minHeaderNr + 2;
Assert.check(newHeaderNr <= 6, String.valueOf(newHeaderNr)); // Only h1-h6 are defined in HTML.
Assert.check(newHeaderNr <= 6, newHeaderNr); // Only h1-h6 are defined in HTML.
elem.tagName("h" + newHeaderNr);
}
}
......@@ -503,11 +503,11 @@ class AsciiDocHtmlModifier {
// Sanity checks.
if (htmlPages.homePage == page) {
Assert.check(tocLinkCurPageCount == 0, String.valueOf(tocLinkCurPageCount));
Assert.check(tocLinkCurPageCount == 0, tocLinkCurPageCount);
} else {
// If the TOC level setting used to generate the single page HTML file is too limited, the page will not be
// in the TOC, and this will fail (count is zero).
Assert.check(tocLinkCurPageCount == 1, String.valueOf(tocLinkCurPageCount));
Assert.check(tocLinkCurPageCount == 1, tocLinkCurPageCount);
}
}
......@@ -599,7 +599,7 @@ class AsciiDocHtmlModifier {
if (allowEmptyRefIfNoChildren) {
// Occurs for 'a.href' for bibliography entries.
// But then they have no child nodes, and are thus not clickable.
Assert.check(elem.childNodeSize() == 0, String.valueOf(elem.childNodeSize()));
Assert.check(elem.childNodeSize() == 0, elem.childNodeSize());
continue;
} else {
throw new RuntimeException(
......@@ -631,7 +631,7 @@ class AsciiDocHtmlModifier {
Assert.check(uriScheme == null, uriScheme);
Assert.check(uri.getUserInfo() == null, uri.getUserInfo());
Assert.check(uri.getHost() == null, uri.getHost());
Assert.check(uri.getPort() == -1, String.valueOf(uri.getPort()));
Assert.check(uri.getPort() == -1, uri.getPort());
Assert.check(uri.getAuthority() == null, uri.getAuthority());
Assert.check(uri.getQuery() == null, uri.getQuery());
Assert.check(uri.getFragment() == null, uri.getFragment());
......
......@@ -69,7 +69,7 @@ public class AsciiDocMultiPageHtmlSplitter {
*/
public static void main(String[] args) throws IOException {
System.out.println("Command line arguments: " + Arrays.toString(args));
Assert.check(args.length == 4, args.toString());
Assert.check(args.length == 4, args);
Path sourceRootPath = Paths.get(args[0]);
Path singleHtmlPagePath = Paths.get(args[1]);
......@@ -98,8 +98,8 @@ public class AsciiDocMultiPageHtmlSplitter {
throws IOException
{
// Check inputs exist.
Assert.check(Files.isDirectory(sourceRootPath), sourceRootPath.toString());
Assert.check(Files.isRegularFile(singleHtmlPagePath), singleHtmlPagePath.toString());
Assert.check(Files.isDirectory(sourceRootPath), sourceRootPath);
Assert.check(Files.isRegularFile(singleHtmlPagePath), singleHtmlPagePath);
// Ensure empty directory for output.
if (Files.isDirectory(outputRootPath)) {
......@@ -146,7 +146,7 @@ public class AsciiDocMultiPageHtmlSplitter {
if (htmlType == HtmlType.WEBSITE) {
System.out.println("Copying single-page HTML file to: " + outputRootPath.toString());
Path singlePathOutputPath = outputRootPath.resolve("index-single-page.html");
Assert.check(!Files.exists(singlePathOutputPath), singlePathOutputPath.toString());
Assert.check(!Files.exists(singlePathOutputPath), singlePathOutputPath);
Files.copy(singleHtmlPagePath, singlePathOutputPath);
}
......
......@@ -13,6 +13,8 @@
package org.eclipse.escet.common.java;
import static org.eclipse.escet.common.java.Strings.fmt;
/** A class for performing assertion checks. Unlike the Java assert statement, these are always checked. */
public class Assert {
/** Constructor for the {@link Assert} class. */
......@@ -38,15 +40,16 @@ public class Assert {
* Checks a condition and throws an {@link AssertionError}, with the given message, if the condition doesn't hold.
*
* @param condition The condition that should hold.
* @param msg The message to use for the exception.
* @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a
* message.
*
* @throws AssertionError If the condition doesn't hold.
*/
public static void check(boolean condition, String msg) {
public static void check(boolean condition, Object msg) {
if (condition) {
return;
}
throw new AssertionError(msg);
throw new AssertionError(String.valueOf(msg));
}
/**
......@@ -61,7 +64,7 @@ public class Assert {
if (!left || right) {
return;
}
throw new AssertionError();
throw new AssertionError(fmt("'%s => %s' doesn't hold", left, right));
}
/**
......@@ -70,15 +73,16 @@ public class Assert {
*
* @param left The left side of the implication.
* @param right The right side of the implication.
* @param msg The message to use for the exception.
* @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a
* message.
*
* @throws AssertionError If the condition doesn't hold.
*/
public static void implies(boolean left, boolean right, String msg) {
public static void implies(boolean left, boolean right, Object msg) {
if (!left || right) {
return;
}
throw new AssertionError(msg);
throw new AssertionError(String.valueOf(msg), new AssertionError(fmt("'%s => %s' doesn't hold", left, right)));
}
/**
......@@ -93,7 +97,7 @@ public class Assert {
if (left == right) {
return;
}
throw new AssertionError();
throw new AssertionError(fmt("'%s <=> %s' doesn't hold", left, right));
}
/**
......@@ -102,15 +106,16 @@ public class Assert {
*
* @param left The left side of the biimplication.
* @param right The right side of the biimplication.
* @param msg The message to use for the exception.
* @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a
* message.
*
* @throws AssertionError If the biimplication doesn't hold.
*/
public static void ifAndOnlyIf(boolean left, boolean right, String msg) {
public static void ifAndOnlyIf(boolean left, boolean right, Object msg) {
if (left == right) {
return;
}
throw new AssertionError(msg);
throw new AssertionError(String.valueOf(msg), new AssertionError(fmt("'%s <=> %s' doesn't hold", left, right)));
}
/**
......@@ -125,12 +130,13 @@ public class Assert {
/**
* Unconditionally throws an {@link AssertionError}, with the given message.
*
* @param msg The message to use for the exception.
* @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a
* message.
*
* @throws AssertionError Always thrown.
*/
public static void fail(String msg) {
throw new AssertionError(msg);
public static void fail(Object msg) {
throw new AssertionError(String.valueOf(msg));
}
/**
......@@ -153,14 +159,15 @@ public class Assert {
* message, if it is {@code null}.
*
* @param value The value to check.
* @param msg The message to use for the exception.
* @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a
* message.
*
* @throws AssertionError If the value is {@code null}.
*/
public static void notNull(Object value, String msg) {
public static void notNull(Object value, Object msg) {
if (value != null) {
return;
}
throw new AssertionError(msg);
throw new AssertionError(String.valueOf(msg));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment