Skip to content

Instantly share code, notes, and snippets.

@yuanmai
Last active September 26, 2016 08:41
Show Gist options
  • Save yuanmai/ba41f00a5b7925aae3d62fcf918905ec to your computer and use it in GitHub Desktop.
Save yuanmai/ba41f00a5b7925aae3d62fcf918905ec to your computer and use it in GitHub Desktop.
回归测试框架雏形:JRegress.recording() 记录运行结果, JRegress.matching() 比较实际和上次记录的结果
package jregress;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public class JRegressTest {
//All tests can share the the sam regress instance
public static JRegress regress = JRegress.recording("");
//public static JRegress regress = JRegress.matching();
public int square(int i) {
return i * i;
}
@Test
public void should_record_expected_result_and_compare_with_actual() {
assertRegress(square(2), JRegress.recording("sq").matching(2));
assertRegress(square(2), JRegress.matching("sq").matching(2));
assertRegress(5, JRegress.matching("sq").matching(2));
}
public static void assertRegress(Object actual, Matcher<String> matcher) {
org.junit.Assert.assertThat(actual.toString(), matcher);
}
interface Storage {
String get(String key);
void put(String key, String value);
}
public static class FileStorage implements Storage {
private final File path;
public FileStorage(File path) {
this.path = path;
}
public FileStorage() {
this(new File("."));
}
@Override
public String get(String key) {
try {
return new String(Files.readAllBytes(new File(path, key).toPath()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void put(String key, String value) {
try {
Files.write(new File(path, key).toPath(), value.getBytes(), StandardOpenOption.CREATE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
static class JRegress {
private final String id;
private final Storage storage;
public JRegress(String id, Storage storage) {
this.id = id;
this.storage = storage;
}
public JRegress(String id) {
this(id, defaultStorage());
}
public Matcher<String> recording(Object input) {
return new RecordingMatcher(hash(input), storage);
}
public Matcher<String> matching(Object input) {
return new StringMatcher(storage.get(hash(input)));
}
public static JRegress recording(String id, Storage storage) {
return new JRegress(id, storage) {
@Override
public Matcher<String> matching(Object input) {
return recording(input);
}
};
}
public static JRegress matching(String id) {
return new JRegress(id, defaultStorage());
}
public static JRegress recording(String id) {
return recording(id, defaultStorage());
}
protected String hash(Object input) {
return id + input.hashCode();
}
private static Storage defaultStorage() {
return new FileStorage();
}
}
private static class RecordingMatcher extends BaseMatcher<String> {
private final String key;
private final Storage storage;
private RecordingMatcher(String key, Storage storage) {
this.key = key;
this.storage = storage;
}
@Override
public void describeTo(Description description) {
}
@Override
public boolean matches(Object o) {
storage.put(key, o.toString());
return true;
}
}
private static class StringMatcher extends BaseMatcher<String> {
private final String expected;
private StringMatcher(String expected) {
this.expected = expected;
}
@Override
public void describeTo(Description description) {
}
@Override
public boolean matches(Object o) {
return expected.equals(o);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment