Skip to content
Browse files

Initial expectation and matcher concept

  • Loading branch information...
1 parent 7ee2cb4 commit 6260ba35ee84ea08b977411179e8fde4263a5526 @davedevelopment committed Sep 10, 2012
View
4 bin/dspec
@@ -41,6 +41,10 @@ if (!getenv('DSPEC_NO_GLOBALS')) {
function fail($msg = "{no message}") {
ds::fail($msg);
}
+
+ function expect($subject) {
+ return ds::expect($subject);
+ }
}
$app = new DSpec\Console\DSpecApplication(new DSpec\Container());
View
3 composer.json
@@ -16,7 +16,8 @@
"symfony/console": "2.1.*",
"symfony/finder": "2.1.*",
"symfony/event-dispatcher": "2.1.*",
- "pimple/pimple": "dev-master"
+ "pimple/pimple": "dev-master",
+ "beberlei/assert": "*"
},
"suggest": {
View
12 spec/DSpec/ExampleGroupSpec.php
@@ -17,7 +17,7 @@
$this->eg->add($innerEg);
$this->eg->add(new DSpec\Hook("beforeEach", $closure));
$this->eg->add(new DSpec\Example("one", $closure));
- assertThat($this->eg->total(), equalTo(2));
+ expect($this->eg->total())->toEqual(2);
});
});
@@ -29,7 +29,7 @@
$this->eg->add($innerEg);
$this->eg->add(new DSpec\Hook("beforeEach", $closure));
$this->eg->add(new DSpec\Example("one", $closure));
- assertThat(count($this->eg->getDescendants()), equalTo(4));
+ expect(count($this->eg->getDescendants()))->toEqual(4);
});
});
@@ -52,11 +52,11 @@
it("run examples", function() {
$obj = (object) ['count' => 0];
- $closure = function() use ($obj) { $obj++; };
+ $closure = function() use ($obj) { $obj->count++; };
$this->eg->add(new DSpec\Example("one", $closure));
$this->eg->add(new DSpec\Example("two", $closure));
$this->eg->run($this->reporter);
- assertThat($obj->count, 2);
+ expect($obj->count)->toEqual(2);
});
context("when hooks are present", function() {
@@ -73,7 +73,7 @@
$this->eg->add(new DSpec\Example("one", $this->factory->__invoke("B")));
$this->eg->add(new DSpec\Hook("afterEach", $this->factory->__invoke("C")));
$this->eg->run($this->reporter);
- assertThat($this->obj->string, equalTo("ABC"));
+ expect($this->obj->string)->toEqual("ABC");
});
it("runs them in the in the correct order", function() {
@@ -86,7 +86,7 @@
$this->eg->add(new DSpec\Hook("afterEach", $this->factory->__invoke("E")));
$this->eg->add(new DSpec\Example("one", $this->factory->__invoke("F")));
$this->eg->run($this->reporter);
- assertThat($this->obj->string, equalTo("DABCEDFE"));
+ expect($this->obj->string)->toEqual("DABCEDFE");
});
});
View
68 spec/DSpec/Expectation/SubjectSpec.php
@@ -0,0 +1,68 @@
+<?php
+
+use DSpec\Expectation\Subject;
+use DSpec\Expectation\ExpectationException;
+
+describe("DSpec\Expectation\Subject", function() {
+
+ it("checks for equality", function() {
+ $e = new Subject("dave");
+ expect($e->toEqual("dave"))->toBeTrue();
+ expect(function() use ($e) {
+ $e->toEqual("dave2");
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+
+ it("checks for non equality", function() {
+ $e = new Subject("dave");
+ expect($e->notToEqual("dave2"))->toBeTrue();
+
+ expect(function() use ($e) {
+ $e->notToEqual("dave");
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+
+ it("checks for truthiness", function() {
+ $e = new Subject(true);
+ expect($e->toBeTrue())->toBeTrue();
+
+ $e = new Subject(false);
+ expect(function() use ($e) {
+ $e->toBeTrue();
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+
+ it("checks for falsiness", function() {
+ $e = new Subject(false);
+ expect($e->toBeFalse())->toBeTrue();
+
+ $e = new Subject(true);
+ expect(function() use ($e) {
+ $e->toBeFalse();
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+
+ context("checking for exceptions", function() {
+ it("checks an exception is thrown", function() {
+ $e = new Subject(function() {});
+ expect(function() use ($e) {
+ $e->toThrow("Exception");
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+
+ it("checks for the type of exception", function() {
+ $e = new Subject(function() {throw new \RunTimeException;});
+ expect(function() use ($e) {
+ $e->toThrow("InvalidArgumentException");
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+
+ it("checks the exception message", function() {
+ $e = new Subject(function() {throw new \Exception("dave123");});
+ expect(function() use ($e) {
+ $e->toThrow("Exception", "dave1456");
+ })->toThrow("DSpec\Expectation\ExpectationException");
+ });
+ });
+
+});
View
10 src/DSpec/Context/SpecContext.php
@@ -6,6 +6,7 @@
use DSpec\ExampleGroup;
use DSpec\Example;
use DSpec\Hook;
+use DSpec\Expectation\Subject;
/**
* This file is part of dspec
@@ -112,4 +113,13 @@ public function load(array $files, ExampleGroup $eg = null)
return $this->__stack->bottom();
}
+
+ /**
+ * @param mixed subject
+ * @return Subject
+ */
+ public function expect($subject)
+ {
+ return new Subject($subject);
+ }
}
View
21 src/DSpec/Expectation/AbstractExpectation.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace DSpec\Expectation;
+
+abstract class AbstractExpectation
+{
+ protected $subject;
+
+ public function __construct($subject)
+ {
+ $this->subject = $subject;
+ }
+
+ public function getSubject()
+ {
+ return $this->subject;
+ }
+
+ abstract public function toEqual($value);
+ abstract public function notToEqual($value);
+}
View
10 src/DSpec/Expectation/Assertion.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace DSpec\Expectation;
+
+use Assert\Assertion as BaseAssertion;
+
+class Assertion extends BaseAssertion
+{
+ protected static $exceptionClass = 'DSpec\Expectation\AssertionException';
+}
View
7 src/DSpec/Expectation/AssertionException.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace DSpec\Expectation;
+
+use Exception;
+
+class AssertionException extends Exception {}
View
17 src/DSpec/Expectation/ExpectationException.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace DSpec\Expectation;
+
+use Exception;
+
+class ExpectationException extends Exception
+{
+ public function __construct($message, $actual, $expected)
+ {
+ parent::__construct($message);
+ $this->actual = $actual;
+ $this->expected = $expected;
+ }
+}
+
+
View
100 src/DSpec/Expectation/Subject.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace DSpec\Expectation;
+
+use DSpec\Expectation\AbstractExpectation;
+use DSpec\Expectation\ExpectationException;
+use DSpec\Expectation\Assertion;
+use Exception;
+
+/**
+ * These matchers need pulling out in to something standalone, have used
+ * Assert\Assertion in here as a temp fix, Hamcrest maybe more appropriate, but
+ * may have to roll my own
+ */
+class Subject
+{
+ protected $subject;
+
+ public function __construct($subject)
+ {
+ $this->subject = $subject;
+ }
+
+ public function getSubject()
+ {
+ return $this->subject;
+ }
+
+ public function toEqual($value)
+ {
+ try {
+ Assertion::eq($this->getSubject(), $value);
+ return true;
+ } catch (AssertionException $ae) {
+ throw new ExpectationException("Expected %s to equal %s", $this->getSubject(), $value);
+ }
+ }
+
+ public function notToEqual($value)
+ {
+ try {
+ $this->toEqual($value);
+ } catch (ExpectationException $e) {
+ return true;
+ }
+
+ throw new ExpectationException("Expected %s to not equal %s", $this->getSubject(), $value);
+ }
+
+ public function toBeTrue()
+ {
+ try {
+ Assertion::true($this->getSubject());
+ return true;
+ } catch (AssertionException $ae) {
+ throw new ExpectationException("Expected %s to be true", $this->getSubject(), true);
+ }
+ }
+
+ public function toBeFalse()
+ {
+ try {
+ Assertion::false($this->getSubject());
+ return true;
+ } catch (AssertionException $ae) {
+ throw new ExpectationException("Expected %s to be false", $this->getSubject(), false);
+ }
+ }
+
+ public function toThrow($exception, $message = null)
+ {
+ if (!is_string($exception)) {
+ $exception = get_class($exception);
+ }
+
+ try {
+ if (is_callable($this->getSubject())) {
+ call_user_func($this->getSubject());
+ } else {
+ eval($this->getSubject());
+ }
+ } catch (Exception $e) {
+ if ($e instanceof $exception) {
+ if ($message == null) {
+ return true;
+ }
+
+ try {
+ Assertion::eq($e->getMessage(), $message);
+ return true;
+ } catch (AssertionException $ae) {
+ throw new ExpectationException("Expected exception message to be %s", $e->getMessage(), $message);
+ }
+ }
+ throw new ExpectationException("Expected a %s to be thrown, got %s", $e, $exception);
+ }
+
+ throw new ExpectationException("Expected a %s to be thrown, but no exception thrown", null, $exception);
+ }
+}

0 comments on commit 6260ba3

Please sign in to comment.
Something went wrong with that request. Please try again.