given 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,10 @@ class StackBehavior < Given::TestCase
24
24
  Invariant { expect(@stack.depth) >= 0 }
25
25
  Invariant { expect(@stack.empty?) == (@stack.depth == 0) }
26
26
 
27
+ def empty_stack
28
+ @stack = Stack.new
29
+ end
30
+
27
31
  Given(:empty_stack) do
28
32
  Then { expect(@stack.depth) == 0 }
29
33
 
@@ -36,6 +40,12 @@ class StackBehavior < Given::TestCase
36
40
  Then { expect(exception.message) =~ /empty/ }
37
41
  end
38
42
 
43
+ def stack_with_two_items
44
+ empty_stack
45
+ @stack.push(:bottom_item)
46
+ @stack.push(:top_item)
47
+ end
48
+
39
49
  Given(:stack_with_two_items) {
40
50
  Then { expect(@stack.top) == :top_item }
41
51
  Then { expect(@stack.depth) == 2}
@@ -52,16 +62,6 @@ class StackBehavior < Given::TestCase
52
62
  Then { expect(@result) == :bottom_item }
53
63
  Then { expect(@stack.depth) == 0 }
54
64
  }
55
-
56
- def empty_stack
57
- @stack = Stack.new
58
- end
59
-
60
- def stack_with_two_items
61
- empty_stack
62
- @stack.push(:bottom_item)
63
- @stack.push(:top_item)
64
- end
65
65
  end
66
66
  </pre>
67
67
 
data/Rakefile CHANGED
@@ -3,15 +3,16 @@ require 'rake/testtask'
3
3
 
4
4
  CLOBBER.include("html")
5
5
 
6
- task :default => ["test:units", "test:functionals"]
6
+ task :default => ["test:units", "test:functionals", :examples]
7
7
 
8
8
  task :tu => "test:units"
9
9
  task :tf => "test:functionals"
10
10
 
11
- EXAMPLE_FILES = FileList['examples/*_behavior.rb']
11
+ EXAMPLE_FILES = FileList['examples/**/*_test.rb']
12
12
  desc "Run Examples"
13
- task :examples do
14
- ruby "-I.:lib #{EXAMPLE_FILES}"
13
+ Rake::TestTask.new(:examples) do |t|
14
+ t.test_files = EXAMPLE_FILES
15
+ t.warning = true
15
16
  end
16
17
 
17
18
  # README Formatting --------------------------------------------------
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ require 'greed/score'
4
+
5
+ class GreedScoreTest < Given::TestCase
6
+
7
+ Invariant { @score.bust? == (@score.points == 0) }
8
+ Invariant {
9
+ if @score.unused == 0
10
+ expect(@score.remaining) == 5
11
+ else
12
+ expect(@score.remaining) == @score.unused
13
+ end
14
+ }
15
+
16
+ def points(dice)
17
+ @score = Greed::Score.for(dice)
18
+ expect(@score.points)
19
+ end
20
+
21
+ Given do
22
+ Then { points([]) == 0 }
23
+ Then { points([5]) == 50 }
24
+ Then { points([5,5]) == 100 }
25
+ Then { points([1]) == 100 }
26
+ Then { points([1,1]) == 200 }
27
+ Then { points([1,5,2,3,6]) == 150 }
28
+ Then { points([1,1,1]) == 1000 }
29
+ Then { points([2,2,2]) == 200 }
30
+ Then { points([3,3,3]) == 300 }
31
+ Then { points([1,1,1,1]) == 1100 }
32
+ Then { points([5,5,5,5]) == 550 }
33
+ end
34
+
35
+ def unused(dice)
36
+ @score = Greed::Score.for(dice)
37
+ expect(@score.unused)
38
+ end
39
+
40
+ Given do
41
+ Then { unused([]) == 0 }
42
+ Then { unused([1,5,2,2,2]) == 0 }
43
+ Then { unused([1,5,2,2]) == 2 }
44
+ end
45
+
46
+ def a_zero_score
47
+ @score = Greed::Score.new
48
+ end
49
+
50
+ Given(:a_zero_score) do
51
+ Then { expect(@score.points) == 0 }
52
+ Then { expect(@score.unused) == 0 }
53
+ Then { expect(@score) == Greed::Score.new(0, 0) }
54
+ Then { expect(@score).not == Greed::Score.new(1, 0) }
55
+ Then { expect(@score).not == Greed::Score.new(0, 1) }
56
+ Then { expect(@score).not == :non_score }
57
+ end
58
+
59
+ Given(:a_zero_score) do
60
+ Then { expect(@score + 0) == 0 }
61
+ Then { expect(@score + 100) == 0 }
62
+ end
63
+
64
+ def a_100_point_score
65
+ @score = Greed::Score.new(100, 4)
66
+ end
67
+
68
+ Given(:a_100_point_score) do
69
+ Then { expect(@score + 0) == 100 }
70
+ Then { expect(@score + 200) == 300 }
71
+ end
72
+
73
+ end
@@ -0,0 +1,107 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ require 'rubygems'
4
+ require 'flexmock/test_unit'
5
+ require 'greed/turner'
6
+ require 'greed/score'
7
+
8
+ class TurnerTest < Given::TestCase
9
+ Score = Greed::Score
10
+
11
+ def a_turner_with_a_player
12
+ @rolls = []
13
+ @dice = flexmock("dice")
14
+ @dice.should_receive(:roll).with(Integer).
15
+ and_return { |n| @rolls.shift[0,n] }
16
+ @player = flexmock("player")
17
+ @player.should_receive(:start_turn => nil,
18
+ :roll_again? => false,
19
+ :end_turn => nil).by_default
20
+ @turner = Greed::Turner.new(@player, @dice)
21
+ end
22
+
23
+ def a_bust_roll
24
+ @rolls << [2, 3, 4, 4, 6]
25
+ end
26
+
27
+ def a_good_roll
28
+ @rolls << [1, 2, 2, 3, 3]
29
+ end
30
+
31
+ Given(:a_turner_with_a_player, :a_bust_roll) do
32
+ When { @turner.take_turn(400) }
33
+ Expecting do
34
+ @player.should_receive(:start_turn).once.with(0, 400)
35
+ @player.should_receive(:roll_again?).never
36
+ @player.should_receive(:end_turn).once.with(0)
37
+ end
38
+ end
39
+
40
+ Given(:a_turner_with_a_player, :a_good_roll, :a_bust_roll) do
41
+ When { @turner.take_turn(400) }
42
+ Expecting do
43
+ @player.should_receive(:start_turn).once.with(0, 400)
44
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 4)).and_return(true)
45
+ @player.should_receive(:end_turn).once.with(0)
46
+ end
47
+ end
48
+
49
+ Given(:a_turner_with_a_player, :a_good_roll, :a_bust_roll) do
50
+ When { @turner.take_turn(400) }
51
+ Expecting do
52
+ @player.should_receive(:start_turn).once.with(0, 400)
53
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 4)).and_return(true)
54
+ @player.should_receive(:end_turn).once.with(0)
55
+ end
56
+ end
57
+
58
+ # Goal "Show that a player who stops after a good roll gets the score"
59
+ Given(:a_turner_with_a_player, :a_good_roll, :a_bust_roll) do
60
+ When { @turner.take_turn(400) }
61
+ Expecting do
62
+ @player.should_receive(:start_turn).once.with(0, 400)
63
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 4)).and_return(false)
64
+ @player.should_receive(:end_turn).once.with(100)
65
+ end
66
+ end
67
+
68
+ Given(:a_turner_with_a_player, :a_good_roll, :a_good_roll, :a_bust_roll) do
69
+ When { @turner.take_turn(400) }
70
+ Expecting do
71
+ @player.should_receive(:start_turn).once.with(0, 400)
72
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 4)).and_return(true)
73
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 3)).and_return(false)
74
+ @player.should_receive(:end_turn).once.with(200)
75
+ end
76
+ end
77
+
78
+ def two_good_rolls
79
+ 6.times { a_good_roll }
80
+ end
81
+
82
+ #Goal "Show that a turn ends when roll_again? returns false."
83
+ Given(:a_turner_with_a_player, :two_good_rolls, :a_bust_roll) do
84
+ When { @turner.take_turn(400) }
85
+ Expecting do
86
+ @player.should_receive(:start_turn).once.with(0, 400)
87
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 4)).and_return(true)
88
+ @player.should_receive(:roll_again?).once.with(Score.new(100, 3)).and_return(false)
89
+ @player.should_receive(:end_turn).once.with(200)
90
+ end
91
+ end
92
+
93
+ def six_good_rolls
94
+ 6.times { a_good_roll }
95
+ end
96
+
97
+ # Goal "Show that scoring all the dice resets number rolled to 5.
98
+ Given(:a_turner_with_a_player, :six_good_rolls, :a_bust_roll) do
99
+ When { @turner.take_turn(400) }
100
+ Expecting do
101
+ @player.should_receive(:start_turn).once.with(0, 400)
102
+ @player.should_receive(:roll_again?).and_return(true).times(5)
103
+ @player.should_receive(:roll_again?).and_return(false)
104
+ @player.should_receive(:end_turn).once.with(600)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,101 @@
1
+ # Greed is a dice game. The rules to Greed varies, depending on who
2
+ # you talk to, but the rules we are using are:
3
+ #
4
+ # * You can roll up to 5 dice
5
+ #
6
+ # * Any triplet of ones is worth 1000
7
+ #
8
+ # * Any triplet of any other face value is worth 100 times the face
9
+ # value (e.g. 3 fours is worth 400 points)
10
+ #
11
+ # * Any ones not used in a triplet are worth 100 points
12
+ #
13
+ # * Any fives not used in a triplet are worth 50 points.
14
+ #
15
+ # * Any 2, 3, 4, or 6 faces not used in a triplet are worth 0 points
16
+ # are are counted as unused dice.
17
+ #
18
+ module Greed
19
+ # Score:
20
+ # * Tracks the points for a roll of dice
21
+ # * Reports the points for a roll
22
+ # * Reports the number of unused faces in a roll.
23
+ class Score
24
+ attr_reader :points, :unused
25
+
26
+ # Return the score object for a roll. A roll is a list of
27
+ # integers representing the faces showing after a roll of dice.
28
+ def self.for(roll)
29
+ @score = new
30
+ @score.score(roll)
31
+ @score
32
+ end
33
+
34
+ def initialize(points=0, unused=0)
35
+ @points = points
36
+ @unused = unused
37
+ end
38
+
39
+ def to_s
40
+ "score<#{points},#{unused}>"
41
+ end
42
+
43
+ def inspect
44
+ to_s
45
+ end
46
+
47
+ def ==(other)
48
+ other.kind_of?(Greed::Score) &&
49
+ points == other.points &&
50
+ unused == other.unused
51
+ end
52
+
53
+ def remaining
54
+ unused == 0 ? 5 : unused
55
+ end
56
+
57
+ def bust?
58
+ points == 0
59
+ end
60
+
61
+ def +(previous_turn_score)
62
+ bust? ? 0 : previous_turn_score + points
63
+ end
64
+
65
+ def score(roll)
66
+ (1..6).each do |face|
67
+ score_face(count_faces(face, roll), face)
68
+ end
69
+ end
70
+
71
+ def score_face(count, face)
72
+ if count >= 3 && face == 1
73
+ count -= 3
74
+ @points += 1000
75
+ elsif count >= 3
76
+ count -= 3
77
+ @points += face * 100
78
+ end
79
+ if face == 1 || face == 5
80
+ @points += count * score_single(face)
81
+ else
82
+ @unused += count
83
+ end
84
+ end
85
+
86
+ def score_single(die)
87
+ if die == 1
88
+ 100
89
+ elsif die == 5
90
+ 50
91
+ else
92
+ 0
93
+ end
94
+ end
95
+
96
+ def count_faces(face, dice)
97
+ dice.select { |f| f == face }.size
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,2 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../test_helper'))
2
+
@@ -0,0 +1,21 @@
1
+ require 'greed/score'
2
+
3
+ module Greed
4
+ class Turner
5
+ def initialize(player, dice=d)
6
+ @dice = dice
7
+ @player = player
8
+ end
9
+
10
+ def take_turn(highest_score)
11
+ @player.start_turn(0, highest_score)
12
+ turn_score = 0
13
+ score = Score.new(0,0)
14
+ begin
15
+ score = Score.for(@dice.roll(score.unused.nonzero? || 5))
16
+ turn_score = score + turn_score
17
+ end while ! score.bust? && @player.roll_again?(score)
18
+ @player.end_turn(turn_score)
19
+ end
20
+ end
21
+ end
@@ -5,6 +5,10 @@ class StackBehavior < Given::TestCase
5
5
  Invariant { expect(@stack.depth) >= 0 }
6
6
  Invariant { expect(@stack.empty?) == (@stack.depth == 0) }
7
7
 
8
+ def empty_stack
9
+ @stack = Stack.new
10
+ end
11
+
8
12
  Given(:empty_stack) do
9
13
  Then { expect(@stack.depth) == 0 }
10
14
 
@@ -17,6 +21,12 @@ class StackBehavior < Given::TestCase
17
21
  Then { expect(exception.message) =~ /empty/ }
18
22
  end
19
23
 
24
+ def stack_with_two_items
25
+ empty_stack
26
+ @stack.push(:bottom_item)
27
+ @stack.push(:top_item)
28
+ end
29
+
20
30
  Given(:stack_with_two_items) {
21
31
  Then { expect(@stack.top) == :top_item }
22
32
  Then { expect(@stack.depth) == 2}
@@ -33,14 +43,4 @@ class StackBehavior < Given::TestCase
33
43
  Then { expect(@result) == :bottom_item }
34
44
  Then { expect(@stack.depth) == 0 }
35
45
  }
36
-
37
- def empty_stack
38
- @stack = Stack.new
39
- end
40
-
41
- def stack_with_two_items
42
- empty_stack
43
- @stack.push(:bottom_item)
44
- @stack.push(:top_item)
45
- end
46
46
  end
@@ -0,0 +1,5 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'test/unit'
4
+ require 'given/test_unit'
5
+
@@ -25,6 +25,7 @@ module Given
25
25
  @_given_setup_codes ||= []
26
26
  @_given_invariant_codes ||= []
27
27
  @_given_when_code = DO_NOTHING
28
+ @_given_mock_codes = []
28
29
  @_given_exception_class = nil
29
30
  old_setups = @_given_setup_codes
30
31
  old_invariants = @_given_invariant_codes
@@ -40,9 +41,17 @@ module Given
40
41
  def When(&when_code)
41
42
  _given_must_have_context("When")
42
43
  @_given_when_code = Code.new('W', when_code)
44
+ @_given_mock_codes = []
45
+
43
46
  @_given_exception_class = nil
44
47
  end
45
48
 
49
+ def Expecting(&mock_code)
50
+ _given_must_have_context("Mock")
51
+ @_given_mock_codes << AnonymousCode.new(mock_code)
52
+ Then { true }
53
+ end
54
+
46
55
  def Then(&then_code)
47
56
  _given_must_have_context("Then")
48
57
  _given_make_test_method("Then", Code.new('T', then_code), @_given_exception_class)
@@ -86,9 +95,11 @@ module Given
86
95
  def _given_make_test_method(clause, then_code, exception_class)
87
96
  setup_codes = @_given_setup_codes
88
97
  when_code = @_given_when_code
98
+ mock_codes = @_given_mock_codes
89
99
  invariant_codes = @_given_invariant_codes
90
100
  define_method _given_test_name(setup_codes, when_code, then_code) do
91
101
  setup_codes.each do |s| send s end
102
+ mock_codes.each do |m| m.run(self) end
92
103
  if exception_class.nil?
93
104
  when_code.run(self)
94
105
  else
@@ -1,7 +1,7 @@
1
1
  module Given
2
2
  VERSION_MAJOR = 0
3
3
  VERSION_MINOR = 0
4
- VERSION_BUILD = 1
4
+ VERSION_BUILD = 2
5
5
  VERSION_NUMBERS = [VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD]
6
6
  VERSION = VERSION_NUMBERS.join(".")
7
7
  end
@@ -0,0 +1,54 @@
1
+ require 'test/test_helper'
2
+
3
+ require 'given'
4
+
5
+ class ExpectingTest < GivenTestCase
6
+ def test_mocks_are_called_before_when
7
+ assert_all_pass do
8
+ def trace
9
+ @trace ||= []
10
+ end
11
+ Given do
12
+ When { trace << :when }
13
+ Expecting { trace << :mock }
14
+ Then { expect(trace) == [:mock, :when] }
15
+ end
16
+ end
17
+ end
18
+
19
+ def test_mocks_without_when_still_runs_a_test
20
+ assert_all_pass do
21
+ Given do
22
+ Expecting { }
23
+ end
24
+ end
25
+ end
26
+
27
+ def test_mocks_dont_leak_across_whens
28
+ assert_all_pass do
29
+ def trace
30
+ @trace ||= []
31
+ end
32
+ Given do
33
+ Expecting { trace << :m1 }
34
+
35
+ When { }
36
+ Then { expect(trace) == [] }
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_mocks_will_stack
42
+ assert_all_pass do
43
+ def trace
44
+ @trace ||= []
45
+ end
46
+ Given do
47
+ When { }
48
+ Expecting { trace << :m1 }
49
+ Expecting { trace << :m2 }
50
+ Then { expect(trace) == [:m1, :m2] }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -39,6 +39,25 @@ class InvalidUseTest < GivenTestCase
39
39
  assert_equal "A When clause must be inside a given block", ex.message
40
40
  end
41
41
 
42
+ def test_mock_must_be_inside_given
43
+ ex = assert_raise(Given::UsageError) do
44
+ run_tests do
45
+ Expecting { }
46
+ end
47
+ end
48
+ assert_equal "A Mock clause must be inside a given block", ex.message
49
+ end
50
+
51
+ def test_mock_must_be_inside_given_part_2
52
+ ex = assert_raise(Given::UsageError) do
53
+ run_tests do
54
+ Given do end
55
+ Expecting { }
56
+ end
57
+ end
58
+ assert_equal "A Mock clause must be inside a given block", ex.message
59
+ end
60
+
42
61
  def test_fails_with_must_be_inside_given
43
62
  ex = assert_raise(Given::UsageError) do
44
63
  run_tests do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: given
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Weirich
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-17 00:00:00 -04:00
12
+ date: 2009-10-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -38,6 +38,7 @@ files:
38
38
  - lib/given/test_unit.rb
39
39
  - lib/given/version.rb
40
40
  - lib/given.rb
41
+ - test/functional/expecting_test.rb
41
42
  - test/functional/fails_with_test.rb
42
43
  - test/functional/invalid_use_test.rb
43
44
  - test/functional/invariant_test.rb
@@ -48,8 +49,14 @@ files:
48
49
  - test/given/expectation_test.rb
49
50
  - test/given/test_unit/adapter_contract.rb
50
51
  - test/test_helper.rb
52
+ - examples/greed/greed_score_test.rb
53
+ - examples/greed/greed_turner_test.rb
54
+ - examples/greed/score.rb
55
+ - examples/greed/test_helper.rb
56
+ - examples/greed/turner.rb
51
57
  - examples/stack.rb
52
58
  - examples/stack_test.rb
59
+ - examples/test_helper.rb
53
60
  has_rdoc: true
54
61
  homepage: http://github.com/jimweirich/Given
55
62
  licenses: []