clean_test 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ Gemfile.lock
4
4
  pkg/*
5
5
  coverage
6
6
  html
7
+ .*.sw?
data/README.rdoc CHANGED
@@ -14,8 +14,6 @@ The main problems this library solves are:
14
14
  * Understanding what elements of a test are relevant to the test, and which are arbitrary placeholders
15
15
  * Removing the requirement that your tests are method names
16
16
 
17
- <b>Note: this gem is still called <tt>test_unit-given</tt> in RubyGems; I'm in the middle of changing the name</b>
18
-
19
17
  == Install
20
18
 
21
19
  gem install clean_test
@@ -1,7 +1,7 @@
1
1
  require 'faker'
2
2
 
3
- module Clean # :nodoc:
4
- module Test # :nodoc:
3
+ module Clean #:nodoc:
4
+ module Test #:nodoc:
5
5
  # Public: Provides the ability to vend arbitrary values without using literals, or
6
6
  # long calls to Faker. This has two levels of utility:
7
7
  #
@@ -46,7 +46,7 @@ module Clean # :nodoc:
46
46
  # }
47
47
  # }
48
48
  module Any
49
- MAX_RAND = 50000 # :nodoc:
49
+ MAX_RAND = 50000 #:nodoc:
50
50
 
51
51
  # Public: Get any number; one that doesn't matter
52
52
  #
@@ -62,11 +62,11 @@ module Clean # :nodoc:
62
62
  any :int,options
63
63
  end
64
64
 
65
- # Public: Get an arbitrary string of any potential length (the real max is 2048 characters if you don't override it)
65
+ # Public: Get an arbitrary string of any potential positive length
66
66
  #
67
67
  # options - options to control the returned string:
68
- # :max - the max size of the string you want
69
- # :min - the minimum size we want to come back
68
+ # :max - the max size of the string you want, must be positive and greater than :min
69
+ # :min - the minimum size we want to come back, must be positive and less than :max
70
70
  #
71
71
  # Example
72
72
  #
@@ -77,6 +77,23 @@ module Clean # :nodoc:
77
77
  any :string,options
78
78
  end
79
79
 
80
+ # Public: Get an arbitrary sentence of arbitrary words of any potential length. Currently,
81
+ # this returns a sentence between 10 and 21 words, though you can control that with options
82
+ #
83
+ # options - options to control the returned sentence
84
+ # :max - the maximum number of words you want returned
85
+ # :min - the minimum number of words you want returned; the sentence will be between
86
+ # :min and (:min + 10) words
87
+ #
88
+ # Example
89
+ #
90
+ # any_sentence :min => 20 # at least a 20-word sentence
91
+ # any_sentence :max => 4 # no more than four words
92
+ #
93
+ def any_sentence(options = {})
94
+ any :sentence,options
95
+ end
96
+
80
97
  # Public: Get a predefined, arbitrary any.
81
98
  #
82
99
  # sym - the any that has been defined already. By default, the following are defined:
@@ -120,24 +137,36 @@ module Clean # :nodoc:
120
137
  @anies ||= default_anies
121
138
  end
122
139
 
123
- ANY_STRING = Proc.new do |options| # :nodoc:
124
- max_size = options[:max] || rand(2048)
125
- min_size = options[:min] || rand(1024)
126
- min_size = max_size if min_size > max_size
140
+ ANY_STRING = Proc.new do |options| #:nodoc:
141
+ if options[:min] && options[:max]
142
+ raise ":min must be less than :max" if options[:min] > options[:max]
143
+ end
144
+ if options[:min]
145
+ raise ":min must be positive" if options[:min] < 1
146
+ end
127
147
 
128
- size = rand(max_size)
148
+ min_size = options[:min]
149
+ max_size = options[:max]
129
150
 
130
- size = min_size if size < min_size
151
+ if min_size.nil? && max_size.nil?
152
+ min_size = rand(1024) + 1
153
+ max_size = min_size + rand(1024)
154
+ elsif min_size.nil?
155
+ min_size = max_size - rand(max_size)
156
+ min_size = 1 if min_size < 1
157
+ else
158
+ max_size = min_size + rand(min_size) + 1
159
+ end
131
160
 
132
161
  string = Faker::Lorem.words(1).join('')
133
- while string.length < size
162
+ while string.length < min_size
134
163
  string += Faker::Lorem.words(1).join('')
135
164
  end
136
165
 
137
166
  string[0..(max_size-1)]
138
167
  end
139
168
 
140
- ANY_NUMBER = Proc.new do |options| # :nodoc:
169
+ ANY_NUMBER = Proc.new do |options| #:nodoc:
141
170
  number = (rand(2 * MAX_RAND) - MAX_RAND).to_f/100.0
142
171
  if options.include? :positive
143
172
  number + MAX_RAND
@@ -148,7 +177,22 @@ module Clean # :nodoc:
148
177
  end
149
178
  end
150
179
 
151
- ANY_INT = Proc.new do |options| # :nodoc:
180
+ ANY_SENTENCE = Proc.new do |options| #:nodoc:
181
+ min = 11
182
+ max = 21
183
+
184
+ if options[:max]
185
+ min = 1
186
+ max = options[:max]
187
+ elsif options[:min]
188
+ min = options[:min]
189
+ max = min + 10
190
+ end
191
+
192
+ Faker::Lorem.words(rand(max - min) + min).join(' ')
193
+ end
194
+
195
+ ANY_INT = Proc.new do |options| #:nodoc:
152
196
  (ANY_NUMBER.call(options)).to_i
153
197
  end
154
198
 
@@ -161,6 +205,7 @@ module Clean # :nodoc:
161
205
  :int => ANY_INT,
162
206
  Fixnum => ANY_INT,
163
207
  Integer => ANY_INT,
208
+ :sentence => ANY_SENTENCE,
164
209
  }
165
210
  end
166
211
  end
@@ -1,5 +1,5 @@
1
- module Clean # :nodoc:
2
- module Test # :nodoc:
1
+ module Clean #:nodoc:
2
+ module Test #:nodoc:
3
3
  # A means of documenting the parts of your test code according
4
4
  # to the class "Given/When/Then" system.
5
5
  # This does no enforcement of any kind and is merely documentation to make
@@ -160,7 +160,7 @@ module Clean # :nodoc:
160
160
  lambda {}
161
161
  end
162
162
 
163
- # Public: Similar to #test_runs, this is used to make clear what
163
+ # Public: Similar to #the_test_runs, this is used to make clear what
164
164
  # you are testing and what the assertions are. Since many Ruby mock
165
165
  # frameworks do not require an explicit "verify" step, you often have tests
166
166
  # that have no explicit asserts, the assertions being simply that the mocks were called
@@ -3,8 +3,8 @@ require 'clean_test/given_when_then'
3
3
  require 'clean_test/any'
4
4
  require 'clean_test/test_that'
5
5
 
6
- module Clean # :nodoc:
7
- module Test # :nodoc:
6
+ module Clean #:nodoc:
7
+ module Test #:nodoc:
8
8
  # Public: A Base class brings in all modules that are part
9
9
  # of Clean::Test.
10
10
  #
@@ -1,5 +1,5 @@
1
- module Clean # :nodoc:
2
- module Test # :nodoc:
1
+ module Clean #:nodoc:
2
+ module Test #:nodoc:
3
3
  # Public: Module that, when included, makes a class method, +#test_that+ available
4
4
  # to create test methods in a more fluent way. See ClassMethods.
5
5
  module TestThat
@@ -48,14 +48,32 @@ module Clean # :nodoc:
48
48
  define_method(test_name, &block)
49
49
  end
50
50
 
51
+ # Public: Create a test that you don't want to actually run.
52
+ # This can be handy if you want to temporarily keep a test from
53
+ # running, but don't want to deal with comments or if (false) blocks.
54
+ # Tests skipped this way will generate a warning to the standard error.
55
+ # Arguments are indentical to #test_that
56
+ def skip_a_test_that(description=nil,&block)
57
+ description = make_up_name(block) if description.nil?
58
+ STDERR.puts "warning: test 'test that #{description}' is being skipped" unless $FOR_TESTING_ONLY_SKIP_STDERR
59
+ end
60
+
61
+ # Public: Create a test that is pending or that you intend to implement soon.
62
+ # This can be handy for sketching out some tests that you want, as this will
63
+ # print the pending tests to the standard error
64
+ def someday_test_that(description=nil,&block)
65
+ description = make_up_name(block) if description.nil?
66
+ STDERR.puts "warning: test 'test that #{description}' is pending" unless $FOR_TESTING_ONLY_SKIP_STDERR
67
+ end
68
+
51
69
  private
52
70
 
53
71
  def make_up_name(some_proc)
54
72
  if some_proc.respond_to? :source_location
55
73
  name,location = some_proc.source_location
56
- "anonymous test at #{name}, line #{location}"
74
+ "anonymous test at #{name}, line #{location} passes"
57
75
  else
58
- "anonymous test for proc #{some_proc.object_id}"
76
+ "anonymous test for proc #{some_proc.object_id} passes"
59
77
  end
60
78
  end
61
79
  end
@@ -1,5 +1,5 @@
1
- module Clean # :nodoc:
2
- module Test # :nodoc:
3
- VERSION = "0.10.0"
1
+ module Clean #:nodoc:
2
+ module Test #:nodoc:
3
+ VERSION = "0.10.1"
4
4
  end
5
5
  end
data/test/test_any.rb CHANGED
@@ -2,80 +2,110 @@ require 'test/unit'
2
2
  require 'clean_test/test_case'
3
3
 
4
4
  class TestAny < Clean::Test::TestCase
5
+
6
+ def setup
7
+ # Fix the seed to be the same all day so our tests are repeatable
8
+ srand((Time.now.to_i / (1000 * 60 * 60)))#.tap { |_| puts "seed: #{_}" })
9
+ end
10
+
5
11
  test_that {
6
- Given { random_seeded_for_negative_float }
7
- When { @number = any_number }
8
- Then {
9
- assert @number < 0,"any_number should be negative, but we got #{@number}"
10
- assert @number.to_i != @number,"Expected a float"
11
- }
12
- }
13
- test_that {
14
- Given { random_seeded_for_positive }
15
12
  When { @number = any_number }
16
- Then { assert @number > 0,"any_number should be positive, but we got #{@number}" }
13
+ Then { assert_not_nil @number }
17
14
  }
18
15
 
19
16
  test_that {
20
- Given { random_seeded_for_negative_float }
21
17
  When { @number = any_number :positive }
22
18
  Then { assert @number > 0,"We specified :positive, but got a negative" }
23
19
  }
24
20
 
25
21
  test_that {
26
- Given { random_seeded_for_positive }
27
22
  When { @number = any_number :negative }
28
23
  Then { assert @number < 0,"We specified :negative, but got a positive" }
29
24
  }
30
25
 
31
26
  test_that {
32
- Given { random_seeded_for_negative_float }
33
27
  When { @number = any_int }
34
28
  Then {
35
- assert @number < 0,"Expected int to be negative"
29
+ assert_not_nil @number
36
30
  assert_equal @number.to_i,@number,"Expected an int, not a #{@number.class}"
37
31
  }
38
32
  }
39
33
 
40
34
  test_that {
41
- Given { random_seeded_for_negative_float }
42
35
  When { @int = any_int :positive }
43
36
  Then { assert @int > 0,"We specified :positive, but got a negative" }
44
37
  }
45
38
 
46
39
  test_that {
47
- Given { random_seeded_for_positive }
48
40
  When { @int = any_int :negative }
49
41
  Then { assert @int < 0,"We specified :negative, but got a positive" }
50
42
  }
51
43
 
52
44
  test_that {
53
- Given { random_seeded_for_long_string }
54
45
  When { @string = any_string }
55
46
  Then {
56
- assert @string.length < 1441,"expected less than our rand value, but got #{@string.length}"
47
+ assert_not_nil @string
57
48
  assert_equal String,@string.class
58
49
  }
59
50
  }
60
51
 
61
52
  test_that {
62
- Given { random_seeded_for_long_string }
63
53
  When { @string = any_string :max => 255 }
64
54
  Then {
65
- assert @string.length <= 255,"Expected a string of less than 256 characters, got #{@string.length}"
66
55
  assert_equal String,@string.class
56
+ assert @string.length <= 255,"Expected a string of less than 256 characters, got #{@string.length}"
67
57
  }
68
58
  }
69
59
 
70
60
  test_that {
71
- Given { random_seeded_for_long_string }
72
61
  When { @string = any_string :min => 1000 }
73
62
  Then {
74
- assert @string.length > 1000,"Expected a string of at least 1500 characters, got one of #{@string.length} characters"
75
63
  assert_equal String,@string.class
64
+ assert @string.length >= 1000,"Expected a string of at least 1000 characters, got one of #{@string.length} characters"
65
+ }
66
+ }
67
+
68
+ test_that "min and max must agree" do
69
+ When {
70
+ @code = lambda { @string = any_string :min => 10, :max => 3 }
71
+ }
72
+ Then {
73
+ assert_raises(RuntimeError,&@code)
76
74
  }
75
+ end
76
+
77
+ [
78
+ [-10, 1],
79
+ [ 0, 1],
80
+ [ 0, 0],
81
+ [ 0,-10],
82
+ ].each do |(min,max)|
83
+ test_that "both min and max must be positive (#{min},#{max})" do
84
+ When {
85
+ @code = lambda { @string = any_string :min => min, :max => max }
86
+ }
87
+ Then {
88
+ assert_raises(RuntimeError,&@code)
89
+ }
90
+ end
91
+ end
92
+
93
+ test_that {
94
+ When { @sentence = any_sentence }
95
+ Then { assert @sentence.split(/\s/).size > 10,@sentence }
96
+ }
97
+
98
+ test_that {
99
+ When { @sentence = any_sentence :max => 5 }
100
+ Then { assert @sentence.split(/\s/).size <= 5,@sentence }
101
+ }
102
+
103
+ test_that {
104
+ When { @sentence = any_sentence :min => 20 }
105
+ Then { assert @sentence.split(/\s/).size >= 20,@sentence }
77
106
  }
78
107
 
108
+
79
109
  test_that "we can register custom anys" do
80
110
  Given {
81
111
  new_any :foo do
@@ -126,19 +156,4 @@ class TestAny < Clean::Test::TestCase
126
156
  }
127
157
  end
128
158
  end
129
-
130
- private
131
-
132
- def random_seeded_for_long_string
133
- Random.srand(34)
134
- end
135
-
136
- def random_seeded_for_negative_float
137
- Random.srand(45)
138
- end
139
-
140
- def random_seeded_for_positive
141
- Random.srand(2)
142
- end
143
-
144
159
  end
@@ -3,6 +3,7 @@ require 'clean_test/test_case'
3
3
 
4
4
  class TestSimpleGiven < Clean::Test::TestCase
5
5
 
6
+
6
7
  test_that "when assigning @x to 4, it is 4" do
7
8
  Given {
8
9
  @x = nil
@@ -27,6 +28,16 @@ class TestSimpleGiven < Clean::Test::TestCase
27
28
  }
28
29
  }
29
30
 
31
+ $FOR_TESTING_ONLY_SKIP_STDERR = true
32
+ skip_a_test_that "a skipped test isn't called" do
33
+ raise "This shouldn't happen!"
34
+ end
35
+
36
+ someday_test_that "a pending test isn't called" do
37
+ raise "This shouldn't happen, either!"
38
+ end
39
+ $FOR_TESTING_ONLY_SKIP_STDERR = false
40
+
30
41
  def test_that_test_that_barfs_with_no_block
31
42
  assert_raises RuntimeError do
32
43
  self.class.test_that "foo"
metadata CHANGED
@@ -1,69 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: clean_test
3
- version: !ruby/object:Gem::Version
4
- version: 0.10.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 53
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 10
9
+ - 1
10
+ version: 0.10.1
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - David Copeland
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-01-18 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-01-19 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
15
22
  name: faker
16
- requirement: &70203919977400 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
17
25
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
22
33
  type: :runtime
23
- prerelease: false
24
- version_requirements: *70203919977400
25
- - !ruby/object:Gem::Dependency
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
26
36
  name: rdoc
27
- requirement: &70203919976980 !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
28
39
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
33
47
  type: :development
34
- prerelease: false
35
- version_requirements: *70203919976980
36
- - !ruby/object:Gem::Dependency
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
37
50
  name: rake
38
- requirement: &70203919976560 !ruby/object:Gem::Requirement
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
39
53
  none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
44
61
  type: :development
45
- prerelease: false
46
- version_requirements: *70203919976560
47
- - !ruby/object:Gem::Dependency
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
48
64
  name: simplecov
49
- requirement: &70203919976140 !ruby/object:Gem::Requirement
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
50
67
  none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
55
75
  type: :development
56
- prerelease: false
57
- version_requirements: *70203919976140
58
- description: You can easily make your plain Ruby Test::Unit test cases clean and clear
59
- with Given/When/Then, placeholder values, and textual descriptions without resorting
60
- to metaprogramming or complex frameworks. Use as much or as little as you like
61
- email:
76
+ version_requirements: *id004
77
+ description: You can easily make your plain Ruby Test::Unit test cases clean and clear with Given/When/Then, placeholder values, and textual descriptions without resorting to metaprogramming or complex frameworks. Use as much or as little as you like
78
+ email:
62
79
  - davetron5000@gmail.com
63
80
  executables: []
81
+
64
82
  extensions: []
83
+
65
84
  extra_rdoc_files: []
66
- files:
85
+
86
+ files:
67
87
  - .gitignore
68
88
  - .rvmrc
69
89
  - .travis.yml
@@ -82,31 +102,41 @@ files:
82
102
  - test/test_circle.rb
83
103
  - test/test_simple_given.rb
84
104
  - test/test_test_that.rb
85
- homepage: ''
105
+ has_rdoc: true
106
+ homepage: ""
86
107
  licenses: []
108
+
87
109
  post_install_message:
88
110
  rdoc_options: []
89
- require_paths:
111
+
112
+ require_paths:
90
113
  - lib
91
- required_ruby_version: !ruby/object:Gem::Requirement
114
+ required_ruby_version: !ruby/object:Gem::Requirement
92
115
  none: false
93
- requirements:
94
- - - ! '>='
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
124
  none: false
99
- requirements:
100
- - - ! '>='
101
- - !ruby/object:Gem::Version
102
- version: '0'
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
103
132
  requirements: []
133
+
104
134
  rubyforge_project: clean_test
105
- rubygems_version: 1.8.10
135
+ rubygems_version: 1.5.2
106
136
  signing_key:
107
137
  specification_version: 3
108
138
  summary: Clean up your Test::Unit tests
109
- test_files:
139
+ test_files:
110
140
  - test/bootstrap.rb
111
141
  - test/test_any.rb
112
142
  - test/test_circle.rb