Skip to content
Permalink
Browse files

improved documentation and error messages

  • Loading branch information...
maciejmikosik committed Aug 31, 2018
2 parents 2c45f54 + d803289 commit c7477fe49f29173ecbc619c28d394e1faeea97db
@@ -59,4 +59,4 @@ public class ArrayListTest {

![ArrayListTest.png](main/doc/ArrayListTest.png "ArrayListTest.png")

See [tutorial](main/doc/tutorial.md) for all features.
See [documentation](main/doc/documentation.md) for all features.
@@ -44,7 +44,7 @@ Example usage looks like this.
.withFactory("copyOf");
.test(com.google.common.collect.ImmutableList.class);

Generated tests can be run using junit runner.
Generated tests can be run using [junit runner](#junit).

@RunWith(QuackeryRunner.class)
public class ArrayListTest {
@@ -3,6 +3,8 @@
import static java.lang.String.format;
import static java.util.Objects.deepEquals;

import java.util.Arrays;

public class AssertException extends ReportException {
public AssertException() {}

@@ -26,17 +28,24 @@ public static void assertTrue(boolean condition) {

public static void assertEquals(Object actual, Object expected) {
if (!deepEquals(actual, expected)) {
throw new AssertException(format(""
+ "\n"
+ " expected that\n"
throw new AssertException(format("\n"
+ " expected equal to\n"
+ " %s\n"
+ " is equal to\n"
+ " but was\n"
+ " %s\n",
actual,
expected));
print(expected),
print(actual)));
}
}

private static String print(Object object) {
return object == null
? "null"
: object.getClass().isArray()
? Arrays.deepToString((Object[]) object)
: object.toString();
}

public static void fail() {
throw new AssertException();
}
@@ -6,6 +6,8 @@
import static org.quackery.TestSuite.test_suite;
import static org.quackery.contract.collection.TestCollectionContract.test_collection_contract;
import static org.quackery.junit.TestQuackeryRunner.test_quackery_runner;
import static org.quackery.report.TestAssertException.test_assert_exception;
import static org.quackery.report.TestAssumeException.test_assume_exception;
import static org.quackery.report.TestReportsCountThrowables.test_reports_count_throwables;
import static org.quackery.report.TestReportsFormat.test_reports_format;
import static org.quackery.run.TestRunnersClassLoaderScoped.test_runners_class_loader_scoped;
@@ -33,6 +35,8 @@ public static void main(String[] args) throws Throwable {
test_runners_thread_scoped();
test_runners_class_loader_scoped();

test_assert_exception();
test_assume_exception();
test_reports_count_throwables();
test_reports_format();

@@ -0,0 +1,111 @@
package org.quackery.report;

import static java.lang.String.format;
import static org.quackery.report.AssertException.assertEquals;
import static org.quackery.report.AssertException.assertTrue;
import static org.quackery.report.AssertException.fail;
import static org.quackery.testing.Testing.mockObject;

import java.util.Arrays;

import org.quackery.testing.Testing;

public class TestAssertException {
public static void test_assert_exception() {
fail_fails();
assert_true_handles_booleans();
assert_equals_handles_objects();
assert_equals_handles_deep_arrays();
assert_equals_handles_null();
}

private static void fail_fails() {
try {
fail();
Testing.fail();
} catch (AssertException e) {}
}

private static void assert_true_handles_booleans() {
assertTrue(true);

try {
assertTrue(false);
Testing.fail();
} catch (AssertException e) {}
}

private static void assert_equals_handles_objects() {
Object expected = mockObject("expected");
Object equal = mockObject("expected");
Object notEqual = mockObject("notEqual");

assertEquals(expected, expected);
assertEquals(equal, expected);

try {
assertEquals(notEqual, expected);
Testing.fail();
} catch (AssertException e) {
Testing.assertEquals(
e.getMessage(),
format("\n"
+ " expected equal to\n"
+ " %s\n"
+ " but was\n"
+ " %s\n",
expected,
notEqual));
}
}

private static void assert_equals_handles_deep_arrays() {
String[][] expected = new String[][] { { "expected" } };
String[][] equal = new String[][] { { "expected" } };
String[][] notEqual = new String[][] { { "notEqual" } };

assertEquals(expected, expected);
assertEquals(equal, expected);

try {
assertEquals(notEqual, expected);
Testing.fail();
} catch (AssertException e) {
Testing.assertEquals(
e.getMessage(),
format("\n"
+ " expected equal to\n"
+ " %s\n"
+ " but was\n"
+ " %s\n",
Arrays.deepToString(expected),
Arrays.deepToString(notEqual)));
}
}

private static void assert_equals_handles_null() {
assertEquals(null, null);

try {
assertEquals(null, "string");
Testing.fail();
} catch (AssertException e) {
Testing.assertEquals(e.getMessage(), "\n"
+ " expected equal to\n"
+ " string\n"
+ " but was\n"
+ " null\n");
}

try {
assertEquals("string", null);
Testing.fail();
} catch (AssertException e) {
Testing.assertEquals(e.getMessage(), "\n"
+ " expected equal to\n"
+ " null\n"
+ " but was\n"
+ " string\n");
}
}
}
@@ -0,0 +1,16 @@
package org.quackery.report;

import static org.quackery.report.AssumeException.assume;

import org.quackery.testing.Testing;

public class TestAssumeException {
public static void test_assume_exception() {
assume(true);

try {
assume(false);
Testing.fail();
} catch (AssumeException e) {}
}
}
@@ -48,6 +48,16 @@ public static void fail() {

public static Object mockObject(final String name) {
return new Object() {
public boolean equals(Object object) {
return object != null
&& getClass() == object.getClass()
&& toString().equals(object.toString());
}

public int hashCode() {
return name.hashCode();
}

public String toString() {
return name;
}

0 comments on commit c7477fe

Please sign in to comment.
You can’t perform that action at this time.