faetr 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/faetr.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{faetr}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bodaniel Jeanes", "Jack Chen"]
@@ -30,8 +30,11 @@ Gem::Specification.new do |s|
30
30
  "lib/faetr/range_ext.rb",
31
31
  "lib/faetr/symbol_ext.rb",
32
32
  "lib/faetr/warn_define.rb",
33
- "spec/faetr_spec.rb",
34
- "spec/spec_helper.rb"
33
+ "spec/array_ext_spec.rb",
34
+ "spec/range_ext_spec.rb",
35
+ "spec/spec_helper.rb",
36
+ "spec/symbol_ext_spec.rb",
37
+ "spec/warn_define_spec.rb"
35
38
  ]
36
39
  s.homepage = %q{http://github.com/bjeanes/faetr}
37
40
  s.rdoc_options = ["--charset=UTF-8"]
@@ -40,8 +43,11 @@ Gem::Specification.new do |s|
40
43
  s.rubygems_version = %q{1.3.5}
41
44
  s.summary = %q{Collection of nice Ruby object extensions}
42
45
  s.test_files = [
43
- "spec/faetr_spec.rb",
44
- "spec/spec_helper.rb"
46
+ "spec/array_ext_spec.rb",
47
+ "spec/range_ext_spec.rb",
48
+ "spec/spec_helper.rb",
49
+ "spec/symbol_ext_spec.rb",
50
+ "spec/warn_define_spec.rb"
45
51
  ]
46
52
 
47
53
  if s.respond_to? :specification_version then
@@ -1,6 +1,23 @@
1
+ require 'date'
2
+ require 'time'
3
+
1
4
  class Range
2
5
  # TODO: make this be a no-op for Time/Dates
3
6
  warn_define :rand do
4
- to_a.rand # not optimized, but will work regardless of range time
7
+ case first
8
+ when Integer
9
+ Kernel.rand((exclude_end? ? last - 1 : last) - first + 1) + first
10
+ when Date
11
+ first + Kernel.rand((exclude_end? ? last - 1 : last) - first + 1)
12
+ when Time
13
+ first + Kernel.rand((exclude_end? ? last - 1 : last) - first + 1)
14
+ else
15
+ @@rand_warnings ||= {}
16
+ if !@@rand_warnings[first.class.to_s.to_sym]
17
+ $stderr.puts "Falling back to non-optimised Range#rand for unimplemented class #{first.class.to_s}"
18
+ @@rand_warnings[first.class.to_s.to_sym] = true
19
+ end
20
+ to_a.rand
21
+ end
5
22
  end
6
23
  end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.dirname(__FILE__) + '/../lib/faetr/array_ext'
3
+
4
+ describe Array do
5
+ describe "#rand" do
6
+ subject { [*(1..10000)] }
7
+
8
+ it "returns a random array element" do
9
+ subject.should include(subject.rand)
10
+ end
11
+
12
+ it "should return a random value" do
13
+ subject.rand.should_not == subject.rand
14
+ end
15
+ end
16
+
17
+ describe "#to_proc" do
18
+ # ["ID", "TITLE"].collect(&[:downcase, :to_sym]) # => [:id, :title]
19
+ it "should create a proc object" do
20
+ def test_proc(&proc); proc.class.should == Proc; end
21
+ [:symbol].to_proc.class.should == Proc
22
+ test_proc(&[:symbol])
23
+ end
24
+
25
+ it "should create a proc that calls the method named after the symbol on the caller" do
26
+ array = [stub, stub, stub]
27
+ array.each do|s|
28
+ s.should_receive(:foo).and_return(foo = stub)
29
+ foo.should_receive(:bar)
30
+ end
31
+ array.map(&[:foo, :bar])
32
+ end
33
+
34
+ it "enables Array#map(&[:list, :of, :symbols])" do
35
+ ['ID', 'NAME', 'EMAIL'].map(&[:downcase, :to_sym]).should == [:id, :name, :email]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,309 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.dirname(__FILE__) + '/../lib/faetr/range_ext'
3
+
4
+ RANDOM_TEST_COUNT = 100
5
+
6
+ describe Range do
7
+ describe "rand" do
8
+
9
+ describe "on String" do
10
+ describe "inclusive" do
11
+ subject do
12
+ 'a'..'z'
13
+ end
14
+
15
+ it "return a random character within the range" do
16
+ (RANDOM_TEST_COUNT * 3).times do
17
+ subject.should include(subject.rand)
18
+ end
19
+ end
20
+
21
+ it "include the start character" do
22
+ f = false
23
+ (RANDOM_TEST_COUNT * 3).times do
24
+ if subject.rand == subject.first
25
+ f = true
26
+ break
27
+ end
28
+ end
29
+
30
+ f.should == true
31
+ end
32
+
33
+ it "include the end character" do
34
+ f = false
35
+ (RANDOM_TEST_COUNT * 3).times do
36
+ if subject.rand == subject.last
37
+ f = true
38
+ break
39
+ end
40
+ end
41
+
42
+ f.should == true
43
+ end
44
+ end
45
+
46
+ describe "exclusive" do
47
+ subject do
48
+ 'a'...'z'
49
+ end
50
+
51
+ it "return a random character within the range" do
52
+ (RANDOM_TEST_COUNT * 3).times do
53
+ subject.should include(subject.rand)
54
+ end
55
+ end
56
+
57
+ it "include the start character" do
58
+ f = false
59
+ (RANDOM_TEST_COUNT * 3).times do
60
+ if subject.rand == subject.first
61
+ f = true
62
+ break
63
+ end
64
+ end
65
+
66
+ f.should == true
67
+ end
68
+
69
+ it "doesn't include the end character" do
70
+ f = false
71
+ (RANDOM_TEST_COUNT * 3).times do
72
+ if subject.rand == subject.last
73
+ f = true
74
+ break
75
+ end
76
+ end
77
+
78
+ f.should == false
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+
85
+ describe "on Integers" do
86
+ describe "inclusive" do
87
+ subject do
88
+ (5..10)
89
+ end
90
+
91
+ it "return a random number within the range" do
92
+ RANDOM_TEST_COUNT.times do
93
+ subject.should include(subject.rand)
94
+ end
95
+ end
96
+
97
+ it "include the start number" do
98
+ f = false
99
+ RANDOM_TEST_COUNT.times do
100
+ if subject.rand == subject.first
101
+ f = true
102
+ break
103
+ end
104
+ end
105
+
106
+ f.should == true
107
+ end
108
+
109
+ it "include the end number" do
110
+ f = false
111
+ RANDOM_TEST_COUNT.times do
112
+ if subject.rand == subject.last
113
+ f = true
114
+ break
115
+ end
116
+ end
117
+
118
+ f.should == true
119
+ end
120
+ end
121
+
122
+ describe "exclusive" do
123
+ subject do
124
+ (5...10)
125
+ end
126
+
127
+ it "return a random number within the range" do
128
+ RANDOM_TEST_COUNT.times do
129
+ subject.should include(subject.rand)
130
+ end
131
+ end
132
+
133
+ it "include the start number" do
134
+ f = false
135
+ RANDOM_TEST_COUNT.times do
136
+ if subject.rand == subject.first
137
+ f = true
138
+ break
139
+ end
140
+ end
141
+
142
+ f.should == true
143
+ end
144
+
145
+ it "doesn't include the end number" do
146
+ f = false
147
+ RANDOM_TEST_COUNT.times do
148
+ if subject.rand == subject.last
149
+ f = true
150
+ break
151
+ end
152
+ end
153
+
154
+ f.should_not == true
155
+ end
156
+ end
157
+
158
+ end
159
+
160
+ describe "on Date" do
161
+ describe "inclusive" do
162
+ subject do
163
+ (Date.new(2009, 1, 1)..Date.new(2009, 1, 10))
164
+ end
165
+
166
+ it "return a random Date within the range" do
167
+ RANDOM_TEST_COUNT.times do
168
+ subject.should include(subject.rand)
169
+ end
170
+ end
171
+
172
+ it "include the start Date" do
173
+ f = false
174
+ RANDOM_TEST_COUNT.times do
175
+ if subject.rand == subject.first
176
+ f = true
177
+ break
178
+ end
179
+ end
180
+
181
+ f.should == true
182
+ end
183
+
184
+ it "include the end Date" do
185
+ f = false
186
+ RANDOM_TEST_COUNT.times do
187
+ if subject.rand == subject.last
188
+ f = true
189
+ break
190
+ end
191
+ end
192
+
193
+ f.should == true
194
+ end
195
+ end
196
+
197
+ describe "exclusive" do
198
+ subject do
199
+ (Date.new(2009, 1, 1)...Date.new(2009, 1, 30))
200
+ end
201
+
202
+ it "return a random number within the range" do
203
+ RANDOM_TEST_COUNT.times do
204
+ subject.should include(subject.rand)
205
+ end
206
+ end
207
+
208
+ it "include the start Date" do
209
+ f = false
210
+ RANDOM_TEST_COUNT.times do
211
+ if subject.rand == subject.first
212
+ f = true
213
+ break
214
+ end
215
+ end
216
+
217
+ f.should == true
218
+ end
219
+
220
+ it "doesn't include the end Date" do
221
+ f = false
222
+ RANDOM_TEST_COUNT.times do
223
+ if subject.rand == subject.last
224
+ f = true
225
+ break
226
+ end
227
+ end
228
+
229
+ f.should_not == true
230
+ end
231
+ end
232
+ end
233
+
234
+ describe "on Time" do
235
+ describe "inclusive" do
236
+ subject do
237
+ (Time.local(2009, 1, 1, 0, 0, 0)..Time.local(2009, 1, 1, 0, 2, 0))
238
+ end
239
+
240
+ it "return a random Date within the range" do
241
+ (RANDOM_TEST_COUNT * 5).times do
242
+ subject.should include(subject.rand)
243
+ end
244
+ end
245
+
246
+ it "include the start Time" do
247
+ f = false
248
+ (RANDOM_TEST_COUNT * 5).times do
249
+ if subject.rand == subject.first
250
+ f = true
251
+ break
252
+ end
253
+ end
254
+
255
+ f.should == true
256
+ end
257
+
258
+ it "include the end Time" do
259
+ f = false
260
+ (RANDOM_TEST_COUNT * 5).times do
261
+ if subject.rand == subject.last
262
+ f = true
263
+ break
264
+ end
265
+ end
266
+
267
+ f.should == true
268
+ end
269
+ end
270
+
271
+ describe "exclusive" do
272
+ subject do
273
+ (Time.local(2009, 1, 1, 0, 0, 0)...Time.local(2009, 1, 1, 0, 2, 0))
274
+ end
275
+
276
+ it "return a random Date within the range" do
277
+ (RANDOM_TEST_COUNT * 5).times do
278
+ subject.should include(subject.rand)
279
+ end
280
+ end
281
+
282
+ it "include the start Time" do
283
+ f = false
284
+ (RANDOM_TEST_COUNT * 5).times do
285
+ if subject.rand == subject.first
286
+ f = true
287
+ break
288
+ end
289
+ end
290
+
291
+ f.should == true
292
+ end
293
+
294
+ it "doesn't include the end Time" do
295
+ f = false
296
+ (RANDOM_TEST_COUNT * 5).times do
297
+ if subject.rand == subject.last
298
+ f = true
299
+ break
300
+ end
301
+ end
302
+
303
+ f.should == false
304
+ end
305
+ end
306
+
307
+ end
308
+ end
309
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.dirname(__FILE__) + '/../lib/faetr/symbol_ext'
3
+
4
+ describe Symbol do
5
+ describe "#to_proc" do
6
+ it "should create a proc object" do
7
+ def test_proc(&proc); proc.class.should == Proc; end
8
+ :symbol.to_proc.class.should == Proc
9
+ test_proc(&:symbol)
10
+ end
11
+
12
+ it "should create a proc that calls the method named after the symbol on the caller" do
13
+ array = [stub, stub, stub]
14
+ array.each {|s| s.should_receive(:foo)}
15
+ array.map(&:foo)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.dirname(__FILE__) + '/../lib/faetr/warn_define'
3
+
4
+ describe Class do
5
+ describe "warn_define" do
6
+ before(:all) do
7
+ @err, @out = $stderr, $stdout
8
+ @define = lambda { |m| Object.warn_define(m.to_sym) { p m } }
9
+ end
10
+
11
+ before { $stderr, $stdout = StringIO.new, StringIO.new }
12
+ after { $stderr, $stdout = @err, @out}
13
+
14
+ context "when the method already exists" do
15
+ before do
16
+ @define[:nil?]
17
+ $stderr.rewind
18
+ end
19
+
20
+ it "should print out a warning" do
21
+ $stderr.read.should_not == ""
22
+ end
23
+
24
+ it "should leave the original method intact" do
25
+ Object.nil?.should == false
26
+ end
27
+ end
28
+
29
+ context "when the method does not exist" do
30
+ before do
31
+ @define[:foobaz]
32
+ Object.foobaz
33
+ $stdout.rewind
34
+ end
35
+
36
+ it "should define the method" do
37
+ $stdout.read.chomp.should == ":foobaz"
38
+ end
39
+
40
+ it "should not print out a warning" do
41
+ $stderr.read.chomp.should == ""
42
+ end
43
+ end
44
+ end
45
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faetr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bodaniel Jeanes
@@ -46,8 +46,11 @@ files:
46
46
  - lib/faetr/range_ext.rb
47
47
  - lib/faetr/symbol_ext.rb
48
48
  - lib/faetr/warn_define.rb
49
- - spec/faetr_spec.rb
49
+ - spec/array_ext_spec.rb
50
+ - spec/range_ext_spec.rb
50
51
  - spec/spec_helper.rb
52
+ - spec/symbol_ext_spec.rb
53
+ - spec/warn_define_spec.rb
51
54
  has_rdoc: true
52
55
  homepage: http://github.com/bjeanes/faetr
53
56
  licenses: []
@@ -77,5 +80,8 @@ signing_key:
77
80
  specification_version: 3
78
81
  summary: Collection of nice Ruby object extensions
79
82
  test_files:
80
- - spec/faetr_spec.rb
83
+ - spec/array_ext_spec.rb
84
+ - spec/range_ext_spec.rb
81
85
  - spec/spec_helper.rb
86
+ - spec/symbol_ext_spec.rb
87
+ - spec/warn_define_spec.rb
data/spec/faetr_spec.rb DELETED
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Faetr" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end