- Revision
- 721
- Author
- sirenian
- Date
- 2007-04-17 07:35:59 -0500 (Tue, 17 Apr 2007)
Log Message
[EK] Finished splitting UsingMatchers into composed classes
Modified Paths
- trunk/core/src/behaviour/org/jbehave/core/mock/UsingMatchersBehaviour.java
- trunk/core/src/java/org/jbehave/core/Ensure.java
- trunk/core/src/java/org/jbehave/core/matchers/CustomMatcher.java
- trunk/core/src/java/org/jbehave/core/matchers/UsingEqualityMatchers.java
- trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java
Diff
Modified: trunk/core/src/behaviour/org/jbehave/core/mock/UsingMatchersBehaviour.java (720 => 721)
--- trunk/core/src/behaviour/org/jbehave/core/mock/UsingMatchersBehaviour.java 2007-04-13 17:24:43 UTC (rev 720) +++ trunk/core/src/behaviour/org/jbehave/core/mock/UsingMatchersBehaviour.java 2007-04-17 12:35:59 UTC (rev 721) @@ -1,11 +1,43 @@ package org.jbehave.core.mock; -import org.jbehave.core.matchers.UsingExceptions; +import org.jbehave.core.Block; +import org.jbehave.core.exception.PendingException; +import org.jbehave.core.exception.VerificationException; + public class UsingMatchersBehaviour { - public void shouldDelegateCorrectlyAndNotThrowAnyStackOverflowErrors() { - UsingExceptions.todo(); - } - + public void shouldDelegateCorrectlyAndNotThrowAnyStackOverflowErrors() throws Exception { + final UsingMatchers m = new UsingMatchers(){}; + Object obj = new Object(); + + m.ensureThat(true); + m.ensureThat(true, "works with a message"); + m.ensureThat(false, m.eq(false)); + m.ensureThat('a', m.eq('a')); + m.ensureThat(1.0, m.eq(1.0)); + m.ensureThat(1L, m.eq(1L)); + m.ensureThat(obj, m.is(obj)); + m.ensureThat(false, m.not(m.eq(true)), "works with a message"); + m.ensureThat('c', m.eq('a').or(m.eq('c')), "works with a message"); + m.ensureThat(1.5, m.eq(1.5), "works with a message"); + m.ensureThat(3L, m.and(m.eq(3L), m.isA(Long.class)), "works with a message"); + m.ensureThat(new Double(4.3), m.isA(Double.class), "works with a message"); + m.ensureThat(null, m.isNull()); + m.ensureThat(obj, m.not(m.nothing())); + + Exception e = m.runAndCatch(VerificationException.class, new Block() { + public void run() throws Exception { + m.fail("Oops!"); + } + }); + m.ensureThat(e, m.isNotNull()); + + e = m.runAndCatch(PendingException.class, new Block() { + public void run() throws Exception { + m.todo(); + } + }); + m.ensureThat(e, m.isNotNull()); + } }
Modified: trunk/core/src/java/org/jbehave/core/Ensure.java (720 => 721)
--- trunk/core/src/java/org/jbehave/core/Ensure.java 2007-04-13 17:24:43 UTC (rev 720) +++ trunk/core/src/java/org/jbehave/core/Ensure.java 2007-04-17 12:35:59 UTC (rev 721) @@ -9,8 +9,11 @@ import org.jbehave.core.exception.PendingException; import org.jbehave.core.exception.VerificationException; +import org.jbehave.core.matchers.UsingEqualityMatchers; +import org.jbehave.core.matchers.UsingExceptions; import org.jbehave.core.mock.Matcher; import org.jbehave.core.mock.UsingMatchers; +import org.jbehave.core.mock.UsingMatchers.CustomMatcher; /** @@ -20,186 +23,184 @@ * @author <a href="" PROTECTED]">Steve Freeman</a> */ public class Ensure { - private static final UsingMatchers matchers = new UsingMatchers() {}; + private static final String NL = System.getProperty("line.separator"); /** should not be subclassed for behaviour classes but can be extended to add methods to namespace */ protected Ensure() {} /** Ensure.that(something, isBlah()) */ public static void that(Object arg, Matcher matcher) { - matchers.ensureThat(arg, matcher); + that(arg, matcher, null); } - public static void that(Object arg, Matcher matcher, String message) { - matchers.ensureThat(arg, matcher, message); + + public static void that(Object arg, Matcher matcher, String message) { + if (!matcher.matches(arg)) { + UsingExceptions.fail("Expected: " + + (message != null ? "[" + message + "] " : "") + NL + + matcher + NL + + "but got: " + NL + arg); + } } + + public static void that(Object arg, CustomMatcher matcher, String message) { + if (!matcher.matches(arg)) { + UsingExceptions.fail("Expected: " + + (message != null ? "[" + message + "] ": "") + NL + + matcher + NL + + "but got: " + NL + matcher.describe(arg)); + } + } public static void that(long arg, Matcher matcher) { - matchers.ensureThat(arg, matcher, null); + that(arg, matcher, null); } public static void that(long arg, Matcher matcher, String message) { - matchers.ensureThat(arg, matcher, message); + that(new Long(arg), matcher, message); } public static void that(double arg, Matcher matcher) { - matchers.ensureThat(arg, matcher, null); + that(arg, matcher, null); } public static void that(double arg, Matcher matcher, String message) { - matchers.ensureThat(arg, matcher, message); + that(new Double(arg), matcher, message); } public static void that(char arg, Matcher matcher) { - matchers.ensureThat(arg, matcher, null); + that(arg, matcher, null); } public static void that(char arg, Matcher matcher, String message) { - matchers.ensureThat(arg, matcher, message); + that(new Character(arg), matcher, message); } public static void that(boolean arg, Matcher matcher) { - matchers.ensureThat(arg, matcher); + that(arg, matcher, null); } public static void that(boolean arg, Matcher matcher, String message) { - matchers.ensureThat(arg, matcher, message); + that(Boolean.valueOf(arg), matcher, message); } - /** Ensure.that(...) without matchers */ public static void that(String message, boolean condition) { - matchers.ensureThat(condition, message); + that(condition, message); } /** Ensure.that(...) without matchers */ public static void that(boolean condition) { - matchers.ensureThat(condition, (String)null); + that(condition, "Expected condition was not met"); } + public static void that(boolean condition, String message) { + if (!condition) { + UsingExceptions.fail(message); + } + } + public static void not(String message, boolean condition) { - Ensure.that(message, !condition); + that(message, !condition); } public static void not(boolean condition) { - Ensure.that(null, !condition); + that(!condition); } /** like junit fail() */ public static void impossible(String message) { - matchers.fail(("\"Impossible\" behaviour: " + message)); + UsingExceptions.fail(("\"Impossible\" behaviour: " + message)); } - // Verify.pending("...") public static void pending(String message) { - matchers.todo(message); + UsingExceptions.todo(message); } public static void pending() { - throw new PendingException(); + UsingExceptions.pending(); } // throws exception public static void throwsException(Class exceptionType, Block block) throws Exception { - try { - block.run(); - matchers.fail("should have thrown " + exceptionType.getName()); - } - catch (Exception e) { - if (!exceptionType.isAssignableFrom(e.getClass())) { - throw e; - } - } + Exception exception = UsingExceptions.runAndCatch(exceptionType, block); + that(exception, UsingEqualityMatchers.isNotNull()); } public static void doesNotThrowException(Block block) throws Exception { - try { - block.run(); - } catch (Exception e) { - throw new VerificationException("Expected no exception", e); - } + Exception exception = UsingExceptions.runAndCatch(Exception.class, block); + that(exception, UsingEqualityMatchers.isNull()); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.isA */ public static void instanceOf(Class type, Object instance) { - String message = "should be instance of " + type.getName() - + " but it is " + (instance == null ? "null" : instance.getClass().getName()); - that(message, type.isInstance(instance)); + that(instance, UsingEqualityMatchers.isA(type)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(String message, boolean expected, boolean actual) { - equal(message, Boolean.valueOf(expected), Boolean.valueOf(actual)); + that(actual, UsingEqualityMatchers.eq(expected), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(boolean expected, boolean actual) { - equal(null, expected, actual); + that(actual, UsingEqualityMatchers.eq(expected)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(String message, long expected, long actual) { - if (expected != actual) { - matchers.fail(message, new Long(expected), new Long(actual)); - } + that(actual, UsingEqualityMatchers.eq(expected), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(long expected, long actual) { - equal(null, expected, actual); + that(actual, UsingEqualityMatchers.eq(expected)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(String message, double expected, double actual, double delta) { - if (Math.abs(expected - actual) > delta) { - matchers.fail(message, new Double(expected), new Double(actual)); - } + that(actual, UsingEqualityMatchers.eq(expected, delta), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(double expected, double actual, double delta) { - equal(null, expected, actual, delta); + that(actual, UsingEqualityMatchers.eq(expected)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(String message, Object expected, Object actual) { - if (expected == null) { - if (actual != null) { - matchers.fail(message, expected, actual); - } - } - else if (!expected.equals(actual)) { - matchers.fail(message, expected, actual); - } + that(actual, UsingEqualityMatchers.eq(expected), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.eq */ public static void equal(Object expected, Object actual) { - equal(null, expected, actual); + that(actual, UsingEqualityMatchers.eq(expected)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.is */ public static void sameInstance(String message, Object expected, Object actual) { - if (expected != actual) matchers.fail(message, expected, actual); + that(actual, UsingEqualityMatchers.is(expected), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.is */ public static void sameInstance(Object expected, Object actual) { - sameInstance(null, expected, actual); + that(actual, UsingEqualityMatchers.is(expected)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.is */ public static void identical(String message, Object expected, Object actual) { - sameInstance(message, expected, actual); + that(actual, UsingEqualityMatchers.is(expected), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.is */ public static void identical(Object expected, Object actual) { - sameInstance(null, expected, actual); + that(actual, UsingEqualityMatchers.is(expected)); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.isNotNull */ public static void notNull(String message, Object actual) { - not(message, actual == null); + that(actual, UsingEqualityMatchers.isNotNull(), message); } - /** @deprecated use matchers */ + /** @deprecated use UsingMatchers or UsingEqualityMatchers.isNotNull */ public static void notNull(Object actual) { - notNull(null, actual); + that(actual, UsingEqualityMatchers.isNotNull()); } + + }
Modified: trunk/core/src/java/org/jbehave/core/matchers/CustomMatcher.java (720 => 721)
--- trunk/core/src/java/org/jbehave/core/matchers/CustomMatcher.java 2007-04-13 17:24:43 UTC (rev 720) +++ trunk/core/src/java/org/jbehave/core/matchers/CustomMatcher.java 2007-04-17 12:35:59 UTC (rev 721) @@ -3,26 +3,10 @@ import org.jbehave.core.mock.Matcher; import org.jbehave.core.mock.UsingMatchers; -public abstract class CustomMatcher extends UsingMatchers implements Matcher { - private final String description; +public abstract class CustomMatcher extends UsingMatchers.CustomMatcher implements Matcher { public CustomMatcher(String description) { - this.description = description; + super(description); } - public String toString() { - return description; - } - - public CustomMatcher and(Matcher that) { - return and(this, that); - } - - public CustomMatcher or(Matcher that) { - return or(this, that); - } - - public String describe(Object arg) { - return "" + arg; - } }
Modified: trunk/core/src/java/org/jbehave/core/matchers/UsingEqualityMatchers.java (720 => 721)
--- trunk/core/src/java/org/jbehave/core/matchers/UsingEqualityMatchers.java 2007-04-13 17:24:43 UTC (rev 720) +++ trunk/core/src/java/org/jbehave/core/matchers/UsingEqualityMatchers.java 2007-04-17 12:35:59 UTC (rev 721) @@ -1,6 +1,6 @@ package org.jbehave.core.matchers; -import org.jbehave.core.mock.UsingMatchers.CustomMatcher; +import org.jbehave.core.matchers.CustomMatcher; public class UsingEqualityMatchers {
Modified: trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java (720 => 721)
--- trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java 2007-04-13 17:24:43 UTC (rev 720) +++ trunk/core/src/java/org/jbehave/core/mock/UsingMatchers.java 2007-04-17 12:35:59 UTC (rev 721) @@ -3,6 +3,7 @@ import java.util.Collection; import org.jbehave.core.Block; +import org.jbehave.core.Ensure; import org.jbehave.core.matchers.UsingCollectionMatchers; import org.jbehave.core.matchers.UsingEqualityMatchers; import org.jbehave.core.matchers.UsingExceptions; @@ -35,15 +36,33 @@ private static final String NL = System.getProperty("line.separator"); - - public static abstract class CustomMatcher extends org.jbehave.core.matchers.CustomMatcher { - public CustomMatcher(String description) { - super(description); + /** @deprecated Use org.jbehave.core.matchers.CustomMatcher instead */ + public static abstract class CustomMatcher extends UsingMatchers implements Matcher { + private final String description; + + public CustomMatcher(String description) { + this.description = description; } + + public String toString() { + return description; + } + + public CustomMatcher and(Matcher that) { + return UsingLogicalMatchers.and(this, that); + } + + public CustomMatcher or(Matcher that) { + return UsingLogicalMatchers.or(this, that); + } + + public String describe(Object arg) { + return "" + arg; + } } /** ensures object is not null */ - public CustomMatcher isNotNull() { + public org.jbehave.core.matchers.CustomMatcher isNotNull() { return UsingEqualityMatchers.isNotNull(); } @@ -158,53 +177,45 @@ } public void ensureThat(Object arg, CustomMatcher matcher, String message) { - if (!matcher.matches(arg)) { - fail("Expected: " + - (message != null ? "[" + message + "] ": "") + NL + - matcher + NL + - "but got: " + NL + matcher.describe(arg)); - } + Ensure.that(arg, matcher, message); } public void ensureThat(Object arg, Matcher matcher, String message) { - if (!matcher.matches(arg)) { - fail("Expected: " + - (message != null ? "[" + message + "] " : "") + NL + - matcher + NL + - "but got: " + NL + arg); - } + Ensure.that(arg, matcher, message); } public void ensureThat(Object arg, Matcher matcher) { - ensureThat(arg, matcher, null); + Ensure.that(arg, matcher); } public void ensureThat(long arg, Matcher matcher, String message) { - ensureThat(new Long(arg), matcher, message); + Ensure.that(arg, matcher, message); } public void ensureThat(long arg, Matcher matcher) { - ensureThat(new Long(arg), matcher, null); + Ensure.that(arg, matcher); } public void ensureThat(double arg, Matcher matcher, String message) { - ensureThat(new Double(arg), matcher, message); + Ensure.that(arg, matcher, message); } + public void ensureThat(double arg, Matcher matcher) { - ensureThat(arg, matcher, null); + Ensure.that(arg, matcher); } public void ensureThat(char arg, Matcher matcher, String message) { - ensureThat(new Character(arg), matcher, message); + Ensure.that(arg, matcher, message); } public void ensureThat(char arg, Matcher matcher) { - ensureThat(arg, matcher, null); + Ensure.that(arg, matcher); } public void ensureThat(boolean arg, Matcher matcher, String message) { - ensureThat(Boolean.valueOf(arg), matcher, message); + Ensure.that(arg, matcher, message); } + public void ensureThat(boolean arg, Matcher matcher) { - ensureThat(arg, matcher, null); + Ensure.that(arg, matcher); } /** @@ -214,18 +225,17 @@ return UsingExceptions.runAndCatch(exceptionType, block); } - /** ensure(...) without matchers */ public void ensureThat(boolean condition, String message) { - if (!condition) { - fail(message + ": expected condition was not met"); - } + Ensure.that(condition, message); } + + public void that(String message, boolean condition) { + Ensure.that(message, condition); + } /** ensure(...) without matchers */ public void ensureThat(boolean condition) { - if (!condition) { - fail("Expected condition was not met"); - } + Ensure.that(condition); } public void fail(String message) {
To unsubscribe from this list please visit:
