diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-05 14:51:03 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-05 14:51:03 +1000 |
commit | 20b1226b4eb10e85497862bd73fe9e9a2f05191d (patch) | |
tree | cfc2cf01032515af4adba2a659f3da9734f5bb48 /src/test/java/au/id/zancanaro | |
parent | 821f5a2c711d748a95d8f5d54266069c5378b556 (diff) |
First cut of a stateful testing framework (it's pretty hacky at the moment, but all the pieces are there)
Diffstat (limited to 'src/test/java/au/id/zancanaro')
-rw-r--r-- | src/test/java/au/id/zancanaro/javacheck/statem/QueueTest.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/test/java/au/id/zancanaro/javacheck/statem/QueueTest.java b/src/test/java/au/id/zancanaro/javacheck/statem/QueueTest.java new file mode 100644 index 0000000..4cd6345 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/statem/QueueTest.java @@ -0,0 +1,88 @@ +package au.id.zancanaro.javacheck.statem; + +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.annotations.DataSource; +import au.id.zancanaro.javacheck.annotations.Property; +import au.id.zancanaro.javacheck.junit.Properties; +import org.junit.runner.RunWith; + +import java.util.*; + +import static au.id.zancanaro.javacheck.Generator.pure; +import static au.id.zancanaro.javacheck.Generators.integer; +import static au.id.zancanaro.javacheck.Generators.oneOf; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(Properties.class) +public class QueueTest { + private static final Queue<Integer> queue = new LinkedList<>(); + + @DataSource + public static Generator<CommandList<List<Integer>>> commandGenerator = + new CommandListGenerator<>(state -> oneOf( + pure(new QueuePushCommand()), + pure(new QueuePopCommand()) + )); + + @Property + public void test(CommandList<List<Integer>> commands) { + queue.clear(); + CommandResult<List<Integer>> result = commands.run(null); + assertFalse(result.isFailed()); + } + + private static class QueuePushCommand extends Command<List<Integer>, Integer, Void> { + @Override + public Generator<Integer> argsGenerator(List<Integer> integers) { + return integer(); + } + + @Override + public Void runCommand(Integer integer) { + queue.offer(integer); + return null; + } + + @Override + public List<Integer> nextState(List<Integer> integers, Integer integer, CommandValue<Void> result) { + List<Integer> nextState = new ArrayList<>(integers == null ? new ArrayList<>() : integers); + nextState.add(integer); + return nextState; + } + + @Override + public String toString() { + return "push"; + } + } + + private static class QueuePopCommand extends Command<List<Integer>, Void, Integer> { + @Override + public boolean preCondition(List<Integer> integers, Void aVoid) { + return integers != null && !integers.isEmpty(); + } + + @Override + public Integer runCommand(Void aVoid) { + return queue.poll(); + } + + @Override + public boolean postCondition(List<Integer> oldState, List<Integer> newState, Void aVoid, Integer integer) { + return Objects.equals(integer, oldState.get(0)); + } + + @Override + public List<Integer> nextState(List<Integer> integers, Void aVoid, CommandValue<Integer> result) { + List<Integer> nextState = new ArrayList<>(integers == null ? new ArrayList<>() : integers); + nextState.remove(0); + return nextState; + } + + @Override + public String toString() { + return "pop"; + } + } +} |