changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/examples/stories/calculator.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1$:.push File.join(File.dirname(__FILE__), *%w[.. .. lib])
2require 'spec'
3
4class AdditionMatchers < Spec::Story::StepGroup
5 steps do |add|
6 add.given("an addend of $addend") do |addend|
7 @adder ||= Adder.new
8 @adder << addend.to_i
9 end
10 end
11end
12
13steps = AdditionMatchers.new do |add|
14 add.then("the sum should be $sum") do |sum|
15 @sum.should == sum.to_i
16 end
17end
18
19steps.when("they are added") do
20 @sum = @adder.sum
21end
22
23# This Story uses steps (see above) instead of blocks
24# passed to Given, When and Then
25
26Story "addition", %{
27 As an accountant
28 I want to add numbers
29 So that I can count some beans
30}, :steps => steps do
31 Scenario "2 + 3" do
32 Given "an addend of 2"
33 And "an addend of 3"
34 When "they are added"
35 Then "the sum should be 5"
36 end
37
38 # This scenario uses GivenScenario, which silently runs
39 # all the steps in a previous scenario.
40
41 Scenario "add 4 more" do
42 GivenScenario "2 + 3"
43 Given "an addend of 4"
44 When "they are added"
45 Then "the sum should be 9"
46 end
47end
48
49# And the class that makes the story pass
50
51class Adder
52 def << addend
53 addends << addend
54 end
55
56 def sum
57 @addends.inject(0) do |result, addend|
58 result + addend.to_i
59 end
60 end
61
62 def addends
63 @addends ||= []
64 end
65end