Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first steps in expectations

  • Loading branch information...
commit bbcc3b68e02ab45f2ac1bc123516d7ecd8e4b770 1 parent 2f768f1
Josep M. Bach authored
2  Gemfile.lock
... ... @@ -1,7 +1,7 @@
1 1 PATH
2 2 remote: .
3 3 specs:
4   - stendhal (0.0.1)
  4 + stendhal (0.1.0)
5 5
6 6 GEM
7 7 remote: http://rubygems.org/
22 features/examples.feature
... ... @@ -1,10 +1,10 @@
1 1 Feature: Examples and example groups
2 2
3   - Spec-ish Domain Specific Language
  3 + RSpec-ish Domain Specific Language
4 4
5 5 Scenario: declare an example
6   - Given a directory named "rspec_project"
7   - When I cd to "rspec_project"
  6 + Given a directory named "stendhal_project"
  7 + When I cd to "stendhal_project"
8 8 Given a file named "sample_spec.rb" with:
9 9 """
10 10 describe "something" do
@@ -18,8 +18,8 @@ Feature: Examples and example groups
18 18 And the output should contain "1 example, 0 failures"
19 19
20 20 Scenario: declare a failing example
21   - Given a directory named "rspec_project"
22   - When I cd to "rspec_project"
  21 + Given a directory named "stendhal_project"
  22 + When I cd to "stendhal_project"
23 23 Given a file named "sample_spec.rb" with:
24 24 """
25 25 describe "something" do
@@ -34,8 +34,8 @@ Feature: Examples and example groups
34 34 And the output should contain "1 example, 1 failure"
35 35
36 36 Scenario: declare a pending example
37   - Given a directory named "rspec_project"
38   - When I cd to "rspec_project"
  37 + Given a directory named "stendhal_project"
  38 + When I cd to "stendhal_project"
39 39 Given a file named "sample_spec.rb" with:
40 40 """
41 41 describe "something" do
@@ -50,8 +50,8 @@ Feature: Examples and example groups
50 50 And the output should contain "1 example, 0 failures, 1 pending"
51 51
52 52 Scenario: declare an example in nested groups
53   - Given a directory named "rspec_project"
54   - When I cd to "rspec_project"
  53 + Given a directory named "stendhal_project"
  54 + When I cd to "stendhal_project"
55 55 Given a file named "sample_spec.rb" with:
56 56 """
57 57 describe "something" do
@@ -70,8 +70,8 @@ Feature: Examples and example groups
70 70 And the output should contain "1 example, 0 failures"
71 71
72 72 Scenario: many examples in different example groups
73   - Given a directory named "rspec_project"
74   - When I cd to "rspec_project"
  73 + Given a directory named "stendhal_project"
  74 + When I cd to "stendhal_project"
75 75 Given a file named "sample_spec.rb" with:
76 76 """
77 77 describe "something" do
56 features/expectations.feature
... ... @@ -0,0 +1,56 @@
  1 +Feature: Expectations
  2 +
  3 + Stendhal comes with some built-in expectations.
  4 +
  5 + Scenario: equality expectations
  6 + Given a directory named "stendhal_project"
  7 + When I cd to "stendhal_project"
  8 + Given a file named "sample_spec.rb" with:
  9 + """
  10 + describe "something" do
  11 + it "does something" do
  12 +
  13 + (3 + 4).should == 7
  14 + 6.should_not == 7
  15 +
  16 + end
  17 + end
  18 + """
  19 + When I run "stendhal sample_spec.rb"
  20 + Then the exit status should be 0
  21 + And the output should contain "1 example, 0 failures"
  22 +
  23 + Scenario: identity expectations
  24 + Given a directory named "stendhal_project"
  25 + When I cd to "stendhal_project"
  26 + Given a file named "sample_spec.rb" with:
  27 + """
  28 + describe "something" do
  29 + it "does something" do
  30 +
  31 + 6.should be_a(Fixnum)
  32 +
  33 + end
  34 + end
  35 + """
  36 + When I run "stendhal sample_spec.rb"
  37 + Then the exit status should be 0
  38 + And the output should contain "1 example, 0 failures"
  39 +
  40 + Scenario: predicate expectations
  41 + Given a directory named "stendhal_project"
  42 + When I cd to "stendhal_project"
  43 + Given a file named "sample_spec.rb" with:
  44 + """
  45 + describe "something" do
  46 + it "does something" do
  47 +
  48 + str = "String".freeze
  49 + str.should be_frozen
  50 +
  51 + end
  52 + end
  53 + """
  54 + When I run "stendhal sample_spec.rb"
  55 + Then the exit status should be 0
  56 + And the output should contain "1 example, 0 failures"
4 lib/stendhal.rb
... ... @@ -1,4 +1,6 @@
1   -require 'stendhal/assertions'
  1 +require 'stendhal/exceptions'
  2 +require 'stendhal/expectations'
  3 +require 'stendhal/matchers'
2 4 require 'stendhal/example'
3 5 require 'stendhal/example_group'
4 6
12 lib/stendhal/assertions.rb
... ... @@ -1,12 +0,0 @@
1   -module Stendhal
2   - module Assertions
3   - def assert test, msg = nil
4   - msg ||= "Failed assertion, no message given."
5   - unless test then
6   - raise AssertionFailed, msg
7   - end
8   - true
9   - end
10   - end
11   - class AssertionFailed < Exception; end
12   -end
2  lib/stendhal/dsl.rb
... ... @@ -1,5 +1,6 @@
1 1 module Stendhal
2 2 module DSL
  3 +
3 4 module Example
4 5 def it(*args,&blk)
5 6 self.add_example Stendhal::Example.new(*args,&blk)
@@ -14,6 +15,7 @@ def pending(*args,&blk)
14 15 self.add_example Stendhal::Example.new(*args,options,&blk)
15 16 end
16 17 end
  18 +
17 19 end
18 20 end
19 21
2  lib/stendhal/example.rb
... ... @@ -1,6 +1,6 @@
1 1 module Stendhal
2 2 class Example
3   - include Assertions
  3 + include Matchers
4 4
5 5 @@examples = []
6 6
5 lib/stendhal/exceptions.rb
... ... @@ -0,0 +1,5 @@
  1 +module Stendhal
  2 + module Exceptions
  3 + class ExpectationNotMet < StandardError; end
  4 + end
  5 +end
7 lib/stendhal/expectations.rb
... ... @@ -0,0 +1,7 @@
  1 +module Stendhal
  2 + module Expectations
  3 + class EqualityExpectation; end
  4 + class KindExpectation; end
  5 + class PredicateExpectation; end
  6 + end
  7 +end
18 lib/stendhal/matchers.rb
... ... @@ -0,0 +1,18 @@
  1 +module Stendhal
  2 + module Matchers
  3 + def ==(other)
  4 + Stendhal::Expectations::EqualityExpectation.new(other)
  5 + end
  6 + def eq(other)
  7 + Stendhal::Expectations::EqualityExpectation.new(other)
  8 + end
  9 + def be_a(kind)
  10 + Stendhal::Expectations::KindExpectation.new(kind)
  11 + end
  12 + def method_missing(m,*args)
  13 + if m.to_s =~ /be_(\w+)/
  14 + Stendhal::Expectations::PredicateExpectation.new(($1 + '?').to_sym)
  15 + end
  16 + end
  17 + end
  18 +end
29 spec/stendhal/assertions_spec.rb
... ... @@ -1,29 +0,0 @@
1   -require 'spec_helper'
2   -
3   -class MyClass
4   - include Stendhal::Assertions
5   -end
6   -
7   -module Stendhal
8   - describe Assertions do
9   -
10   - subject { MyClass.new }
11   -
12   - describe "#assert" do
13   -
14   - it "does nothing if asserted expression returns true" do
15   - subject.assert true
16   - end
17   -
18   - it "raises an AssertionFailed exception otherwise" do
19   - expect { subject.assert false }.to raise_error(AssertionFailed)
20   - end
21   -
22   - it "yields a given message for the error" do
23   - expect { subject.assert false, "softwared!" }.to raise_error(AssertionFailed, "softwared!")
24   - end
25   -
26   - end
27   -
28   - end
29   -end
34 spec/stendhal/matchers_spec.rb
... ... @@ -0,0 +1,34 @@
  1 +require 'spec_helper'
  2 +
  3 +class MyClass
  4 + include Stendhal::Matchers
  5 +end
  6 +
  7 +module Stendhal
  8 + describe "a class with included Matchers" do
  9 +
  10 + let(:object) { MyClass.new }
  11 +
  12 + describe "#==" do
  13 + it 'creates a new equality expectation' do
  14 + Expectations::EqualityExpectation.should_receive(:new).with(7)
  15 + object == 7
  16 + end
  17 + end
  18 +
  19 + describe "#be_a" do
  20 + it 'creates a new kind expectation' do
  21 + Expectations::KindExpectation.should_receive(:new).with(Fixnum)
  22 + object.be_a(Fixnum)
  23 + end
  24 + end
  25 +
  26 + describe "#be_whatever" do
  27 + it 'creates a new predicate expectation' do
  28 + Expectations::PredicateExpectation.should_receive(:new).with(:frozen?)
  29 + object.be_frozen
  30 + end
  31 + end
  32 +
  33 + end
  34 +end

0 comments on commit bbcc3b6

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