ace_of_spades 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,196 @@
1
+ require 'spec_helper'
2
+
3
+ describe Suit do
4
+ let(:spades) { Suit.new(:Spades) }
5
+
6
+ describe '.new' do
7
+ context "when passed a valid argument" do
8
+ it "initializes" do
9
+ [:Spades, 'spades', :s, 'S'].each do |suit|
10
+ suit = Suit.new(suit)
11
+ expect(suit).to be_an_instance_of(Suit)
12
+ end
13
+ end
14
+ end
15
+ context "when passed an invalid argument" do
16
+ it "raises an error" do
17
+ [:x, :X, "X", "Spaaaaades", 42, 10].each do |suit|
18
+ expect{Suit.new(suit)}.to raise_error
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '.suitable?' do
25
+ context "when passed an argument that can be converted to a suit" do
26
+ it "returns true" do
27
+ [:Spades, "Spades", :s].each do |suit|
28
+ expect(Suit.suitable?(suit)).to be(true)
29
+ end
30
+ end
31
+ end
32
+ context "when passed an argument that cannot be converted to a suitue" do
33
+ it "returns false" do
34
+ [:x, "X", 42, 3, "AceOfSpades"].each do |suit|
35
+ expect(Suit.suitable?(suit)).to be(false)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '.wrap' do
42
+ context "when passed a Suit instance" do
43
+ it "returns the Suit instance" do
44
+ expect(Suit.wrap(spades)).to be(spades)
45
+ end
46
+ end
47
+ context "when passed an object that can be converted to a Suit" do
48
+ context "when passed a full String or Symbol" do
49
+ it "returns a Suit" do
50
+ ["Spades", "spades", :Spades, :spades].each do |suit|
51
+ suit = Suit.wrap(suit)
52
+ expect(suit).to be_an_instance_of(Suit)
53
+ expect(suit.instance_variable_get(:@symbol)).to eq(:Spades)
54
+ end
55
+ end
56
+ end
57
+ context "when passed a short String or Symbol" do
58
+ it "returns a Suit" do
59
+ ["S", "s", :S, :s].each do |suit|
60
+ suit = Suit.wrap(suit)
61
+ expect(suit).to be_an_instance_of(Suit)
62
+ expect(suit.instance_variable_get(:@symbol)).to eq(:Spades)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ context "when passed an object that cannot be coerced to a Suit" do
68
+ it "returns nil" do
69
+ ["AceOfSpades", :X, 42, 42.0, 1, true].each do |suit|
70
+ suit = Suit.wrap(suit)
71
+ expect(suit).to be_nil
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '.parse' do
78
+ context "when passed an object that can be converted to a Suit Symbol" do
79
+ context "when passed a full String or Symbol" do
80
+ it "returns a Suit Symbol" do
81
+ ["Spades", "spades", :Spades, :spades].each do |suit|
82
+ suit = Suit.parse(suit)
83
+ expect(suit).to eq(:Spades)
84
+ end
85
+ end
86
+ end
87
+ context "when passed a full String or Symbol in singular form" do
88
+ it "returns a Suit Symbol" do
89
+ ["Spade", "spade", :Spade, :spade].each do |suit|
90
+ suit = Suit.parse(suit)
91
+ expect(suit).to eq(:Spades)
92
+ end
93
+ end
94
+ end
95
+ context "when passed a short String or Symbol" do
96
+ it "returns a Suit Symbol" do
97
+ ["S", "s", :S, :s].each do |suit|
98
+ suit = Suit.parse(suit)
99
+ expect(suit).to eq(:Spades)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ context "when passed an object that cannot be coerced to a Suit Symbol" do
105
+ it "returns nil" do
106
+ ["AceOfSpades", :X, 42, 42.0, 1, true].each do |suit|
107
+ suit = Suit.parse(suit)
108
+ expect(suit).to be_nil
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ describe '#<=>' do
115
+ let(:spades) { Suit.new(:Spades) }
116
+ let(:clubs) { Suit.new(:Clubs) }
117
+ context "when passed an argument that cannot be compared to a Suit" do
118
+ it "returns nil" do
119
+ expect(spades <=> 5).to be_nil
120
+ end
121
+ end
122
+ context "when passed an argument that is a Suit" do
123
+ context "when passed a Suit that is greater than receiver" do
124
+ it "returns -1" do
125
+ expect(clubs <=> spades).to be(-1)
126
+ end
127
+ end
128
+ context "when passed a Suit that is less than receiver" do
129
+ it "returns 1" do
130
+ expect(spades <=> clubs).to be(1)
131
+ end
132
+ end
133
+ context "when passed a Suit that is equal to receiver" do
134
+ it "returns 0" do
135
+ expect(spades <=> spades).to be(0)
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ describe '#to_sym' do
142
+ it "returns the Suit as a Symbol" do
143
+ expect(spades.to_sym).to eq(:Spades)
144
+ end
145
+ end
146
+
147
+ describe '#to_s' do
148
+ it "returns the Suit as a String" do
149
+ expect(spades.to_s).to eq('Spades')
150
+ end
151
+ end
152
+
153
+ describe '#to_str' do
154
+ it "returns the Suit as a String" do
155
+ expect(spades.to_str).to eq('Spades')
156
+ end
157
+ end
158
+
159
+ describe '#card & #card=' do
160
+ it "assigns/retrieves the card" do
161
+ expect(spades.card).to be_nil
162
+ spades.card = :Foo
163
+ expect(spades.card).to be(:Foo)
164
+ end
165
+ end
166
+
167
+ describe '#method_missing' do
168
+ context "when a valid predicate method is called" do
169
+ context "when the method called should return true" do
170
+ it "returns true" do
171
+ expect(spades.spades?).to be(true)
172
+ end
173
+ context "when passed a singular suit" do
174
+ it "returns true" do
175
+ expect(spades.spade?).to be(true)
176
+ end
177
+ end
178
+ end
179
+ context "when the method called should return false" do
180
+ it "returns false" do
181
+ expect(spades.clubs?).to be(false)
182
+ end
183
+ end
184
+ end
185
+ context "when an invalid predicate method is called" do
186
+ it "raises NoMethodError" do
187
+ expect{spades.not_a_method?}.to raise_error(NoMethodError)
188
+ end
189
+ end
190
+ context "when an invalid method is called" do
191
+ it "raises NoMethodError" do
192
+ expect{spades.not_a_method}.to raise_error(NoMethodError)
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,234 @@
1
+ require 'spec_helper'
2
+
3
+ describe Value do
4
+ let(:ace) { Value.new(:Ace) }
5
+
6
+ describe '.new' do
7
+ context "when passed a valid argument" do
8
+ it "initializes" do
9
+ [:Ace, :a, 'Ace', 'A', 1].each do |val|
10
+ value = Value.new(val)
11
+ expect(value).to be_an_instance_of(Value)
12
+ end
13
+ end
14
+ end
15
+ context "when passed an invalid argument" do
16
+ it "raises an error" do
17
+ [:x, :X, "X", "AceOfSpades", 42].each do |val|
18
+ expect{Value.new(val)}.to raise_error
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '.valuable?' do
25
+ context "when passed an argument that can be converted to a Value" do
26
+ it "returns true" do
27
+ [:Ace, "Ace", "a", :A, 1, 14].each do |val|
28
+ expect(Value.valuable?(val)).to be(true)
29
+ end
30
+ end
31
+ end
32
+ context "when passed an argument that cannot be converted to a Value" do
33
+ it "returns false" do
34
+ [:x, "X", 42, "AceOfSpades"].each do |val|
35
+ expect(Value.valuable?(val)).to be(false)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '.wrap' do
42
+ context "when passed a Value instance" do
43
+ it "returns the Value instance" do
44
+ expect(Value.wrap(ace)).to be(ace)
45
+ end
46
+ end
47
+ context "when passed an object that can be converted to a Value" do
48
+ context "when passed a full String or Symbol" do
49
+ it "returns a Value" do
50
+ ["Ace", "ace", :Ace, :ace].each do |val|
51
+ val = Value.wrap(val)
52
+ expect(val).to be_an_instance_of(Value)
53
+ expect(val.to_sym).to be(:Ace)
54
+ end
55
+ end
56
+ end
57
+ context "when passed a short String or Symbol" do
58
+ it "returns a Value" do
59
+ ["j", "J", :j, :J].each do |val|
60
+ val = Value.wrap(val)
61
+ expect(val).to be_an_instance_of(Value)
62
+ expect(val.to_sym).to be(:Jack)
63
+ end
64
+ end
65
+ end
66
+ context "when passed an Integer or Float" do
67
+ it "returns a Value" do
68
+ [1, 1.0].each do |val|
69
+ val = Value.wrap(val)
70
+ expect(val).to be_an_instance_of(Value)
71
+ expect(val.to_sym).to be(:Ace)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ context "when passed an object that cannot be coerced to a Value" do
77
+ it "returns nil" do
78
+ ["AceOfSpades", :X, 42, 42.0, true].each do |val|
79
+ val = Value.wrap(val)
80
+ expect(val).to be_nil
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '.parse' do
87
+ context "when passed an object that can be converted to a Value Symbol" do
88
+ context "when passed a full String or Symbol" do
89
+ it "returns a Value" do
90
+ ["Ace", "ace", :Ace, :ace].each do |val|
91
+ val = Value.parse(val)
92
+ expect(val).to be(:Ace)
93
+ end
94
+ end
95
+ context "when not a face card" do
96
+ it "returns a value" do
97
+ expect(Value.parse("three")).to be(:Three)
98
+ end
99
+ end
100
+ end
101
+ context "when passed a short String or Symbol" do
102
+ it "returns a Value" do
103
+ ["J", "j", :J, :j].each do |val|
104
+ val = Value.parse(val)
105
+ expect(val).to be(:Jack)
106
+ end
107
+ end
108
+ end
109
+ context "when passed an Integer" do
110
+ it "returns a Value" do
111
+ [1, 1.0, 14.0].each do |val|
112
+ val = Value.parse(val)
113
+ expect(val).to be(:Ace)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ context "when passed an object that cannot be coerced to a Value Symbol" do
119
+ it "returns nil" do
120
+ ["AceOfSpades", "T", 42, true, :X].each do |val|
121
+ val = Value.parse(val)
122
+ expect(val).to be_nil
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '#<=>' do
129
+ let(:ace) { Value.new(:Ace) }
130
+ let(:king) { Value.new(:King) }
131
+ context "when passed an argument that cannot be compared to a Value" do
132
+ it "returns nil" do
133
+ expect(ace <=> 5).to be_nil
134
+ end
135
+ end
136
+ context "when passed an argument that is a Value" do
137
+ context "when passed a Value that is greater than receiver" do
138
+ it "returns -1" do
139
+ expect(king <=> ace).to be(-1)
140
+ end
141
+ end
142
+ context "when passed a Value that is less than receiver" do
143
+ it "returns 1" do
144
+ expect(ace <=> king).to be(1)
145
+ end
146
+ end
147
+ context "when passed a Value that is equal to receiver" do
148
+ it "returns 0" do
149
+ expect(ace <=> ace).to be(0)
150
+ end
151
+ end
152
+ context "when aces are low" do
153
+ it "compares them appropriately" do
154
+ deck = Deck.new(aces_high: false)
155
+ card = Card.new(:ace, :spades)
156
+ card.deck = deck
157
+ ace.card = card
158
+ expect(ace <=> king).to be(-1)
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ describe '#card & #card=' do
165
+ it "assigns/retrieves the card" do
166
+ expect(ace.card).to be_nil
167
+ ace.card = :Foo
168
+ expect(ace.card).to be(:Foo)
169
+ end
170
+ end
171
+
172
+ describe '#aces_high?' do
173
+ it "returns the #aces_high? value of its card" do
174
+ card = Card.new(:ace, :spades)
175
+ ace.card = card
176
+ expect(ace.aces_high?).to be(card.aces_high?)
177
+ end
178
+ end
179
+
180
+ describe '#to_sym' do
181
+ it "returns the Value as a Symbol" do
182
+ expect(ace.to_sym).to eq(:Ace)
183
+ end
184
+ end
185
+
186
+ describe '#to_s' do
187
+ it "returns the Value as a String" do
188
+ expect(ace.to_s).to eq("Ace")
189
+ end
190
+ end
191
+
192
+ describe '#to_str' do
193
+ it "returns the Value as a String" do
194
+ expect(ace.to_str).to eq("Ace")
195
+ end
196
+ end
197
+
198
+ describe '#to_i' do
199
+ it "returns the Value as an Integer" do
200
+ expect(ace.to_i).to be(14)
201
+ end
202
+ end
203
+
204
+ describe '#to_int' do
205
+ it "returns the Value as an Integer" do
206
+ expect(ace.to_int).to be(14)
207
+ end
208
+ end
209
+
210
+ describe '#method_missing' do
211
+ context "when a valid predicate method is called" do
212
+ context "when the method called should return true" do
213
+ it "returns true" do
214
+ expect(ace.ace?).to be(true)
215
+ end
216
+ end
217
+ context "when the method called should return false" do
218
+ it "returns false" do
219
+ expect(ace.king?).to be(false)
220
+ end
221
+ end
222
+ end
223
+ context "when an invalid predicate method is called" do
224
+ it "raises NoMethodError" do
225
+ expect{ace.no_method?}.to raise_error(NoMethodError)
226
+ end
227
+ end
228
+ context "when an invalid method is called" do
229
+ it "raises NoMethodError" do
230
+ expect{ace.not_a_method}.to raise_error(NoMethodError)
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,14 @@
1
+ require 'ace_of_spades'
2
+ include AceOfSpades
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = 'random'
14
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ace_of_spades
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Dom Kiva-Meyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: guard
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.5.3
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.1.1
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.1
62
+ description: A simple and flexible playing cards library.
63
+ email:
64
+ - hello@domkm.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rbenv-version
71
+ - .rspec
72
+ - Gemfile
73
+ - Guardfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - ace_of_spades.gemspec
78
+ - lib/ace_of_spades.rb
79
+ - lib/ace_of_spades/card.rb
80
+ - lib/ace_of_spades/deck.rb
81
+ - lib/ace_of_spades/suit.rb
82
+ - lib/ace_of_spades/value.rb
83
+ - spec/lib/card_spec.rb
84
+ - spec/lib/deck_spec.rb
85
+ - spec/lib/suit_spec.rb
86
+ - spec/lib/value_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/domkm/ace_of_spades
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>'
104
+ - !ruby/object:Gem::Version
105
+ version: 1.3.1
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.23
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: A simple and flexible playing cards library.
112
+ test_files:
113
+ - spec/lib/card_spec.rb
114
+ - spec/lib/deck_spec.rb
115
+ - spec/lib/suit_spec.rb
116
+ - spec/lib/value_spec.rb
117
+ - spec/spec_helper.rb