Skip to content

Commit

Permalink
Merge pull request #79 from parsingdata/improve-coverage
Browse files Browse the repository at this point in the history
Improve coverage
  • Loading branch information
rdvdijk authored Jul 26, 2016
2 parents 6858faf + 6fe1b04 commit 550c38e
Show file tree
Hide file tree
Showing 36 changed files with 824 additions and 142 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/io/parsingdata/metal/Shorthand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import io.parsingdata.metal.expression.value.reference.*;
import io.parsingdata.metal.token.*;

public class Shorthand {
public final class Shorthand {

private Shorthand() {}

public static Token def(final String name, final ValueExpression size, final Expression predicate, final Encoding encoding) { return new Def(name, size, predicate, encoding); }
public static Token def(final String name, final ValueExpression size, final Expression predicate) { return def(name, size, predicate, null); }
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/io/parsingdata/metal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import io.parsingdata.metal.token.Token;

public class Util {
public final class Util {

private Util() {}

final private static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

public static <T>T checkNotNull(final T argument, final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private ParseGraph(final Token definition) {

private ParseGraph(final ParseItem head, final ParseGraph tail, final Token definition, final boolean branched) {
this.head = checkNotNull(head, "head");
if (head.isValue() && branched) { throw new IllegalArgumentException("Argument branch cannot be true when head contains a ParseValue."); }
this.tail = checkNotNull(tail, "tail");
this.branched = branched;
this.definition = checkNotNull(definition, "definition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.data.ParseGraph.EMPTY;

public class ByItem {
public final class ByItem {

private ByItem() {}

/**
* @param graph The graph to search
* @param lastHead The first item (bottom-up) to be excluded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import static io.parsingdata.metal.Util.checkNotNull;

public class ByName {
public final class ByName {

private ByName() {}

/**
* @param graph The graph to search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import static io.parsingdata.metal.Util.checkNotNull;

public class ByOffset {
public final class ByOffset {

private ByOffset() {}

public static boolean hasGraphAtRef(final ParseGraph graph, final long ref) {
return findRef(ByType.getGraphs(graph), ref) != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import static io.parsingdata.metal.Util.checkNotNull;

public class ByToken {
public final class ByToken {

private ByToken() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

import static io.parsingdata.metal.Util.checkNotNull;

public class ByType {
public final class ByType {

private ByType() {}

public static ParseGraphList getRefs(final ParseGraph graph) {
checkNotNull(graph, "graph");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import io.parsingdata.metal.data.ParseGraph;
import io.parsingdata.metal.data.ParseItem;

public class Reversal {
public final class Reversal {

private Reversal() {}

public static ParseGraph reverse(ParseGraph oldGraph, ParseGraph newGraph) {
if (oldGraph.isEmpty()) { return newGraph; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package io.parsingdata.metal.encoding;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Encoding {

private static final Sign DEFAULT_SIGNED = Sign.UNSIGNED;
private static final Charset DEFAULT_CHARSET = Charset.forName("ISO646-US");
private static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
private static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN;

private final Sign _sign;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.math.BigInteger;
import java.util.BitSet;

public class ConstantFactory {
public final class ConstantFactory {

private ConstantFactory() {}

public static Value createFromNumeric(final BigInteger value, final Encoding enc) {
return new Value(compact(value.toByteArray(), enc.isSigned()), setToBE(enc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,22 @@

package io.parsingdata.metal;

import static io.parsingdata.metal.Shorthand.con;
import static io.parsingdata.metal.Shorthand.def;
import static io.parsingdata.metal.Shorthand.eq;
import static io.parsingdata.metal.Shorthand.eqNum;
import static io.parsingdata.metal.Shorthand.eqStr;
import static io.parsingdata.metal.Shorthand.expTrue;
import static io.parsingdata.metal.Shorthand.gtNum;
import static io.parsingdata.metal.Shorthand.ltNum;
import static io.parsingdata.metal.Shorthand.ref;
import static io.parsingdata.metal.Shorthand.self;
import static io.parsingdata.metal.Shorthand.seq;
import static io.parsingdata.metal.util.TokenDefinitions.any;
import static io.parsingdata.metal.util.EncodingFactory.enc;
import static io.parsingdata.metal.util.EnvironmentFactory.stream;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;

import io.parsingdata.metal.expression.Expression;
import org.junit.runners.Parameterized.Parameters;

import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.Expression;
import io.parsingdata.metal.expression.comparison.ComparisonExpression;
import io.parsingdata.metal.token.Token;
import io.parsingdata.metal.util.ParameterizedParse;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;
import java.util.Collection;

import static io.parsingdata.metal.Shorthand.*;
import static io.parsingdata.metal.util.EncodingFactory.enc;
import static io.parsingdata.metal.util.EnvironmentFactory.stream;
import static io.parsingdata.metal.util.TokenDefinitions.any;
import static java.nio.charset.StandardCharsets.US_ASCII;

public class ComparisonExpressionSemanticsTest extends ParameterizedParse {

Expand All @@ -57,16 +46,18 @@ public static Collection<Object[]> data() {
{ "1 < 1", numCom(1, ltNum(ref("a"))), stream(1, 1), enc(), false },
{ "2 < 1", numCom(1, ltNum(ref("a"))), stream(1, 2), enc(), false },
{ "1 < 2", numCom(1, ltNum(ref("a"))), stream(2, 1), enc(), true },
{ "\"abc\" == \"abc\"", strCom(3, eqStr(ref("a"))), stream("abcabc", Charset.forName("ISO646-US")), enc(), true },
{ "\"abd\" == \"abc\"", strCom(3, eqStr(ref("a"))), stream("abcabd", Charset.forName("ISO646-US")), enc(), false },
{ "\"abc\" == \"abc\"", strCom(3, eqStr(ref("a"))), stream("abcabc", US_ASCII), new Encoding(US_ASCII), true },
{ "\"abd\" == \"abc\"", strCom(3, eqStr(ref("a"))), stream("abcabd", US_ASCII), new Encoding(US_ASCII), false },
{ "1 == 1(eq)", valCom(1, eq(ref("a"))), stream(1, 1), enc(), true },
{ "2 == 1(eq)", valCom(1, eq(ref("a"))), stream(1, 2), enc(), false },
{ "1 == 1 with self", valCom(1, eq(self, ref("a"))), stream(1, 1), enc(), true },
{ "1 == 2 with self", valCom(1, eq(self, ref("a"))), stream(1, 2), enc(), false },
{ "1, 2 == 1", listCom(eq(ref("a"), ref("b")), expTrue()), stream(1, 2, 1, 2), enc(), false },
{ "1, 2 == 1, 2", listCom(expTrue(), eq(ref("a"), ref("b"))), stream(1, 2, 1, 2), enc(), true },
{ "1, 2 == 2, 2", listCom(expTrue(), eq(ref("a"), ref("b"))), stream(1, 2, 2, 2), enc(), false },
{ "1, 2 == 1, 3", listCom(expTrue(), eq(ref("a"), ref("b"))), stream(1, 2, 1, 3), enc(), false }
{ "1, 2 == 1, 3", listCom(expTrue(), eq(ref("a"), ref("b"))), stream(1, 2, 1, 3), enc(), false },
{ "1, 2, 1 != 1/0", valCom(1, eqNum(con(1), div(con(1), con(0)))), stream(1, 2), enc(), false },
{ "1, 2, 1/0 != 1", valCom(1, eqNum(div(con(1), con(0)), con(1))), stream(1, 2), enc(), false }
});
}

Expand Down
29 changes: 19 additions & 10 deletions core/src/test/java/io/parsingdata/metal/DefSizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@

package io.parsingdata.metal;

import static io.parsingdata.metal.Shorthand.con;
import static io.parsingdata.metal.Shorthand.def;
import static io.parsingdata.metal.Shorthand.ref;
import static io.parsingdata.metal.Shorthand.seq;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import io.parsingdata.metal.data.ByteStream;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ParseGraph;
import io.parsingdata.metal.data.ParseResult;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.token.Token;
import io.parsingdata.metal.util.InMemoryByteStream;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

import static io.parsingdata.metal.Shorthand.*;
import static io.parsingdata.metal.util.EncodingFactory.enc;
import static io.parsingdata.metal.util.EnvironmentFactory.stream;
import static io.parsingdata.metal.util.TokenDefinitions.EMPTY_VE;
import static io.parsingdata.metal.util.TokenDefinitions.any;
import static org.junit.Assert.assertFalse;

public class DefSizeTest {
public static final Token FORMAT =
Expand Down Expand Up @@ -68,4 +69,12 @@ public void testInvalidLength() throws IOException {
// The top-level Token (Seq) has failed, so no values are recorded in the ParseGraph.
Assert.assertEquals(ParseGraph.EMPTY, result.environment.order);
}

@Test
public void testEmptyLengthInList() throws IOException {
assertFalse(def("a", EMPTY_VE).parse(stream(1, 2, 3, 4), enc()).succeeded);
final Token aList = seq(any("a"), any("a"));
assertFalse(seq(aList, def("b", ref("a"))).parse(stream(1, 2, 3, 4), enc()).succeeded);
}

}
39 changes: 21 additions & 18 deletions core/src/test/java/io/parsingdata/metal/ErrorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,23 @@

package io.parsingdata.metal;

import static org.junit.Assert.assertFalse;

import static io.parsingdata.metal.Shorthand.con;
import static io.parsingdata.metal.Shorthand.def;
import static io.parsingdata.metal.Shorthand.div;
import static io.parsingdata.metal.Shorthand.ref;
import static io.parsingdata.metal.Shorthand.repn;
import static io.parsingdata.metal.Shorthand.seq;
import static io.parsingdata.metal.util.TokenDefinitions.any;
import static io.parsingdata.metal.util.EncodingFactory.enc;
import static io.parsingdata.metal.util.EnvironmentFactory.stream;

import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import io.parsingdata.metal.data.ByteStream;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.OptionalValueList;
import io.parsingdata.metal.data.ParseResult;
import io.parsingdata.metal.token.Token;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;

import static io.parsingdata.metal.Shorthand.*;
import static io.parsingdata.metal.util.EncodingFactory.enc;
import static io.parsingdata.metal.util.EnvironmentFactory.stream;
import static io.parsingdata.metal.util.TokenDefinitions.any;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class ErrorsTest {

Expand Down Expand Up @@ -75,4 +70,12 @@ public void multiValueInRepN() throws IOException {
assertFalse(parseResult.succeeded);
}

@Test
public void definedValueHasNoOffset() {
final OptionalValueList offsetCon = offset(con(1)).eval(stream(), enc());
assertFalse(offsetCon.isEmpty());
assertEquals(1, offsetCon.size);
assertFalse(offsetCon.head.isPresent());
}

}
Loading

0 comments on commit 550c38e

Please sign in to comment.