a(lineSeparator).shouldNotBeNull(); int[] base64 = Decoder.fromBase64; for (byte b : lineSeparator) {
public static Expectation<Object> a(Object o1){ return new Expectation<>(o1); }
checkNull(); String methodName1 = "is" + capitalize(booleanMethod); String methodName2 = "has" + capitalize(booleanMethod); Method m = booleanMethodNamed(booleanMethod); if (m == null) { m = booleanMethodNamed(methodName1); m = booleanMethodNamed(methodName2);
/** * Tested value is equal expected. * * @param expected expected value. */ public void shouldBeEqual(T expected) { checkNull(); if (actual == null) { if (expected != null) { throw newShouldBeEqualException(expected); } } else { if (expected == null) { throw newShouldBeEqualException(expected); } //TODO: improve Number comparison, see http://stackoverflow.com/questions/2683202/comparing-the-values-of-two-generic-numbers if (actual instanceof Number && expected instanceof Number) { if (((Number) actual).doubleValue() != ((Number) expected).doubleValue()) { throw newShouldBeEqualException(expected); } } else if (!actual.equals(expected)) { throw newShouldBeEqualException(expected); } } }
private boolean contains(Object expected) { checkNull(); if (actual instanceof Collection) { return ((Collection) actual).contains(expected); } else if (actual instanceof Map) { return ((Map) actual).containsKey(expected); } else { return actual.toString().contains(expected.toString()); } }
/** * Invokes a boolean method and uses return value in comparison. * * @param booleanMethod name of boolean method as specified in Java Beans specification. Example: if method name * is <code>isValid()</code>, then the string "valid" needs to be passed. This results in readable code * such as: * <pre> * a(bean).shouldBe("valid"); * </pre> */ public void shouldBe(String booleanMethod) { invokeBoolean(booleanMethod, Boolean.TRUE); }
/** * This method is exactly opposite (negated) of {@link #shouldContain(Object)}. * * @param expected value that is expected to be NOT contained in a tested object. */ public void shouldNotContain(Object expected) { if(contains(expected)) throw new TestException("tested object contains the value: " + expected + ", but it should not"); }
/** * Alias to {@link #shouldBeEqual(Object)}. * * @param expected expected value. */ public void shouldEqual(T expected){ shouldBeEqual(expected); }
/** * This is for cases suh as: "hasErrors()": <code>a(p).shouldHave("errors")</code>. * Invokes a boolean method and uses return value in comparison. * @param booleanMethod name of boolean method as specified in Java Beans specification. Example: if method name * is <code>hasChildren()</code>, then the string "children" needs to be passed. This results in readable code * such as: * <pre> * a(bean).shouldHave("children"); * </pre> */ public void shouldHave(String booleanMethod) { shouldBe(booleanMethod); }
/** * Tests that the Tested value is a specific type. * * @param clazz type the the expected value should have (or super type). Lets say the super type is Car, and sub type is * Toyota, then this test will pass: * <pre> * a(new Toyota()).shouldBeA(Car.class). * </pre> * Think of this not in terms of direct typing but from a point of view of inheritance. * <p> * Synonym for {@link #shouldBeType(Class)}. */ public void shouldBeA(Class clazz) { shouldBeType(clazz); }
/** * Tested value is the same reference value as expected. * * @param expected expected reference. */ public void shouldBeTheSameAs(T expected) { checkNull(); if (actual != expected) throw new TestException("references are not the same, but they should be"); }
/** * Tested value is equal expected. * * @param expected expected value. */ public void shouldBeEqual(T expected) { checkNull(); if (actual == null) { if (expected != null) { throw newShouldBeEqualException(expected); } } else { if (expected == null) { throw newShouldBeEqualException(expected); } //TODO: improve Number comparison, see http://stackoverflow.com/questions/2683202/comparing-the-values-of-two-generic-numbers if (actual instanceof Number && expected instanceof Number) { if (((Number) actual).doubleValue() != ((Number) expected).doubleValue()) { throw newShouldBeEqualException(expected); } } else if (!actual.equals(expected)) { throw newShouldBeEqualException(expected); } } }
/** * Invokes a boolean method and uses return value in comparison. * * @param booleanMethod name of boolean method as specified in Java Beans specification. Example: if method name * is <code>isValid()</code>, then the string "valid" needs to be passed. This results in readable code * such as: * <pre> * a(bean).shouldNotBe("valid"); * </pre> */ public void shouldNotBe(String booleanMethod) { invokeBoolean(booleanMethod, Boolean.FALSE); }
if(!contains(expected)) throw new TestException("tested value does not contain expected value: " + expected);
/** * Alias to {@link #shouldBeEqual(Object)}. * * @param expected expected value. */ public void shouldEqual(T expected){ shouldBeEqual(expected); }
/** * This is for cases suh as: "hasErrors()": <code>a(p).shouldHave("errors")</code>. * Invokes a boolean method and uses return value in comparison. * @param booleanMethod name of boolean method as specified in Java Beans specification. Example: if method name * is <code>hasChildren()</code>, then the string "children" needs to be passed. This results in readable code * such as: * <pre> * a(bean).shouldHave("children"); * </pre> */ public void shouldHave(String booleanMethod) { shouldBe(booleanMethod); }
/** * Tests that the Tested value is a specific type. * * @param clazz type the the expected value should have (or super type). Lets say the super type is Car, and sub type is * Toyota, then this test will pass: * <pre> * a(new Toyota()).shouldBeA(Car.class). * </pre> * Think of this not in terms of direct typing but from a point of view of inheritance. * <p> * Synonym for {@link #shouldBeType(Class)}. */ public void shouldBeA(Class clazz) { shouldBeType(clazz); }
/** * Wraps an output stream for encoding byte data using the {@link Base64} * encoding scheme. * * <p> It is recommended to promptly close the returned output stream after * use, during which it will flush all possible leftover bytes to the underlying * output stream. Closing the returned output stream will close the underlying * output stream. * * @param os * the output stream. * @return the output stream for encoding the byte data into the * specified Base64 encoded format */ public OutputStream wrap(OutputStream os) { the(os).shouldNotBeNull(); return new EncOutputStream(os, isURL ? toBase64URL : toBase64, newline, linemax, doPadding); }
/** * Tested value should be false. */ public void shouldBeFalse() { checkNull(); if ((Boolean) actual) throw new TestException("should not be true, but it is"); }
/** * Tested value is equal expected. * * @param expected expected value. */ public void shouldBeEqual(T expected) { checkNull(); if (actual == null) { if (expected != null) { throw newShouldBeEqualException(expected); } } else { if (expected == null) { throw newShouldBeEqualException(expected); } //TODO: improve Number comparison, see http://stackoverflow.com/questions/2683202/comparing-the-values-of-two-generic-numbers if (actual instanceof Number && expected instanceof Number) { if (((Number) actual).doubleValue() != ((Number) expected).doubleValue()) { throw newShouldBeEqualException(expected); } } else if (!actual.equals(expected)) { throw newShouldBeEqualException(expected); } } }