Skip to content

Commit 813afac

Browse files
committed
Truth first checkin
0 parents  commit 813afac

File tree

17 files changed

+394
-0
lines changed

17 files changed

+394
-0
lines changed

.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/main/java"/>
4+
<classpathentry kind="src" path="src/test/java"/>
5+
<classpathentry kind="lib" path="truth0.1/truth-0.1-tests.jar"/>
6+
<classpathentry kind="lib" path="truth0.1/truth-0.1.jar"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
8+
<classpathentry combineaccessrules="false" kind="src" path="/junit"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin
2+
target
3+
truth*

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>truth</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

build.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<project>
2+
<property name="target" location="target" />
3+
<property name="version-base" value="0.1" />
4+
<property name="version-status" value="" />
5+
<property name="version" value="${version-base}${version-status}" />
6+
<property name="dist" value="truth${version}" />
7+
<property name="src" value="src/main/java" />
8+
<property name="binjar" value="truth-${version}.jar" />
9+
<property name="testjar" value="truth-${version}-tests.jar" />
10+
<property name="testsrc" location="src/test/java" />
11+
<property name="testbin" location="${target}/test/java" />
12+
<property name="bin" location="${target}/main" />
13+
14+
<macrodef name="truth_compilation">
15+
<attribute name="srcdir"/>
16+
<attribute name="destdir"/>
17+
<attribute name="classpath"/>
18+
<sequential>
19+
<mkdir dir="@{destdir}"/>
20+
<javac
21+
srcdir="@{srcdir}"
22+
destdir="@{destdir}"
23+
debug="on"
24+
classpath="@{classpath}"
25+
includeantruntime="false"
26+
target="1.5"
27+
>
28+
<compilerarg value="-Xlint:unchecked" />
29+
<classpath>
30+
<pathelement location="../junit/junit4.9b2/junit-4.9b2.jar" />
31+
</classpath>
32+
</javac>
33+
</sequential>
34+
</macrodef>
35+
36+
<macrodef name="run-tests">
37+
<element name="extra-args" implicit="yes" />
38+
<sequential>
39+
<java classname="org.junit.runner.JUnitCore" fork="yes" failonerror="true">
40+
<extra-args />
41+
<arg value="test.org.junit.contrib.truth.AllTests"/>
42+
<classpath>
43+
<pathelement location="${dist}" />
44+
<pathelement location="${dist}/${binjar}" />
45+
<pathelement location="${dist}/${testjar}" />
46+
<!-- TODO: don't hard-code JUnit version -->
47+
<pathelement location="../junit/junit4.9b2/junit-4.9b2.jar" />
48+
</classpath>
49+
</java>
50+
</sequential>
51+
</macrodef>
52+
53+
<target name="build">
54+
<truth_compilation srcdir="${src}" destdir="${bin}" classpath=""/>
55+
<truth_compilation srcdir="${testsrc}" destdir="${testbin}" classpath="${bin}"/>
56+
</target>
57+
58+
<target name="jars" depends="build">
59+
<mkdir dir="${dist}" />
60+
<jar
61+
jarfile="${dist}/${binjar}"
62+
basedir="${bin}"
63+
/>
64+
<jar
65+
jarfile="${dist}/${testjar}"
66+
basedir="${testbin}"
67+
/>
68+
</target>
69+
70+
<target name="populate-dist"
71+
depends="build, jars"
72+
>
73+
</target>
74+
75+
<target name="dist" depends="populate-dist">
76+
<run-tests>
77+
<jvmarg value="-Dignore.this=ignored" />
78+
</run-tests>
79+
</target>
80+
</project>
81+
<!-- TODO: cheating -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.junit.contrib.truth;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.rules.TestRule;
7+
import org.junit.runner.Description;
8+
import org.junit.runners.model.Statement;
9+
10+
public class Expect extends TestVerb implements TestRule {
11+
private static class ExpectationGatherer implements FailureStrategy {
12+
List<String> messages = new ArrayList<String>();
13+
14+
@Override
15+
public void fail(String message) {
16+
messages.add(message);
17+
}
18+
}
19+
20+
private final ExpectationGatherer gatherer;
21+
private boolean inRuleContext = false;
22+
23+
public static Expect create() {
24+
return new Expect(new ExpectationGatherer());
25+
}
26+
27+
private Expect(ExpectationGatherer gatherer) {
28+
super(gatherer);
29+
this.gatherer = gatherer;
30+
}
31+
32+
@Override
33+
public Statement apply(final Statement base, Description description) {
34+
inRuleContext = true;
35+
return new Statement() {
36+
37+
@Override
38+
public void evaluate() throws Throwable {
39+
base.evaluate();
40+
if (! gatherer.messages.isEmpty()) {
41+
String message = "All failed expectations:\n";
42+
for (int i = 0; i < gatherer.messages.size(); i++) {
43+
message += " " + (i+1) + ". " + gatherer.messages.get(i) + "\n";
44+
}
45+
throw new AssertionError(message);
46+
}
47+
}
48+
};
49+
}
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.junit.contrib.truth;
2+
3+
public interface FailureStrategy {
4+
void fail(String message);
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.junit.contrib.truth;
2+
3+
public class IntSubject extends Subject<Integer> {
4+
public IntSubject(FailureStrategy failureStrategy, int i) {
5+
super(failureStrategy, i);
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.junit.contrib.truth;
2+
3+
public class StringSubject extends Subject<String> {
4+
public StringSubject(FailureStrategy failureStrategy, String string) {
5+
super(failureStrategy, string);
6+
}
7+
8+
public StringSubject contains(String string) {
9+
if (!getSubject().contains(string)) {
10+
fail("contains", string);
11+
}
12+
return this;
13+
}
14+
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.junit.contrib.truth;
2+
3+
public class Subject<T> {
4+
private final FailureStrategy failureStrategy;
5+
private final T subject;
6+
7+
public Subject(FailureStrategy failureStrategy, T subject) {
8+
this.failureStrategy = failureStrategy;
9+
this.subject = subject;
10+
}
11+
12+
public Subject<T> is(Object other) {
13+
if (!getSubject().equals(other)) {
14+
fail("is", other);
15+
}
16+
return this;
17+
}
18+
19+
protected T getSubject() {
20+
return subject;
21+
}
22+
23+
protected void fail(String verb, Object... messageParts) {
24+
String message = "Not true: ";
25+
message += "<" + getSubject() + "> " + verb;
26+
for (Object part : messageParts) {
27+
message += " <" + part + ">";
28+
}
29+
failureStrategy.fail(message);
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.junit.contrib.truth;
2+
3+
public class TestVerb {
4+
private final FailureStrategy failureStrategy;
5+
6+
public TestVerb(FailureStrategy failureStrategy) {
7+
this.failureStrategy = failureStrategy;
8+
}
9+
10+
public IntSubject that(int i) {
11+
return new IntSubject(getFailureStrategy(), i);
12+
}
13+
14+
public StringSubject that(String string) {
15+
return new StringSubject(getFailureStrategy(), string);
16+
}
17+
18+
protected FailureStrategy getFailureStrategy() {
19+
return failureStrategy;
20+
}
21+
}

0 commit comments

Comments
 (0)