Module: Clean::Test::Any
- Included in:
- TestCase
- Defined in:
- lib/clean_test/any.rb
Overview
Public: Provides the ability to vend arbitrary values without using literals, or long calls to Faker. This has two levels of utility:
helper methods - #any_number, #any_int, #any_string provide arbitrary primitives
to make it clear what numbers, ints, and strings in your tests are
relevant. Arbitrary values should use one of these any_ helpers.
any sort of any - you can define your own “any” values by using #new_any, which allows you to
extend things if you like. Of course, you could just make your own any_method as well.
Example:
class Person
def initialize(first_name,last_name,age)
# ...
end
end
test_that "someone under 18 is a minor" {
Given {
# First name and last name aren't relevant to the test
@person = Person.new(any_string,any_string,17)
}
When {
@minor = @person.minor?
}
Then {
assert @minor
}
}
test_that "full_name gives the full name" {
Given {
# Age isn't relevant; it just needs to be positive
@person = Person.new("Dave","Copeland",any_int :positive)
}
When {
@full_name = @person.full_name
}
Then {
assert_equal "Dave Copeland",@full_namej
}
}
Constant Summary collapse
- MAX_RAND =
:nodoc:
50000
Instance Method Summary collapse
-
#any(sym, options = {}) ⇒ Object
Public: Get a predefined, arbitrary any.
-
#any_int(*options) ⇒ Object
Public: Returns an integer.
-
#any_number(*options) ⇒ Object
Public: Get any number; one that doesn’t matter.
-
#any_sentence(options = {}) ⇒ Object
Public: Get an arbitrary sentence of arbitrary words of any potential length.
-
#any_string(options = {}) ⇒ Object
Public: Get an arbitrary string of any potential positive length.
-
#any_symbol ⇒ Object
Public: Get an arbitrary symbol, for example to use as a Hash key.
-
#new_any(any, &block) ⇒ Object
Public: Create a new any that can be retrieved via #any.
Instance Method Details
#any(sym, options = {}) ⇒ Object
Public: Get a predefined, arbitrary any.
sym - the any that has been defined already. By default, the following are defined:
:string - does any_string
String - does any_string
:number - does any_number
Numeric - does any_number
Float - does any_number
:int - does any_int
Fixnum - does any_int
Integer - does any_int
options - whatever options are relevant to the user-defined any
Example
new_any(:foo) do ||
if [:bar]
'bar'
else
'quux'
end
end
some_foo = any :foo
some_other_foo = any :foo, :bar => true
129 130 131 |
# File 'lib/clean_test/any.rb', line 129 def any(sym, = {}) anies[sym].call() end |
#any_int(*options) ⇒ Object
Public: Returns an integer. options is the same as for #any_number
61 62 63 |
# File 'lib/clean_test/any.rb', line 61 def any_int(*) any :int, end |
#any_number(*options) ⇒ Object
Public: Get any number; one that doesn’t matter
options - options to control what sort of number comes back:
:positive - make sure that the number is greater than zero
:negative - make sure that the number is less than zero
56 57 58 |
# File 'lib/clean_test/any.rb', line 56 def any_number(*) any :number, end |
#any_sentence(options = {}) ⇒ Object
Public: Get an arbitrary sentence of arbitrary words of any potential length. Currently, this returns a sentence between 10 and 21 words, though you can control that with options
options - options to control the returned sentence
:max - the maximum number of words you want returned
:min - the minimum number of words you want returned; the sentence will be between
:min and (:min + 10) words
Example
any_sentence :min => 20 # at least a 20-word sentence
any_sentence :max => 4 # no more than four words
100 101 102 |
# File 'lib/clean_test/any.rb', line 100 def any_sentence( = {}) any :sentence, end |
#any_string(options = {}) ⇒ Object
Public: Get an arbitrary string of any potential positive length
options - options to control the returned string:
:max - the max size of the string you want, must be positive and greater than :min
:min - the minimum size we want to come back, must be positive and less than :max
Example
any_string :max => 255 # => ensure it'll fit into a varchar(255)
any_string :min => 1024 # at least 1024 characters
76 77 78 |
# File 'lib/clean_test/any.rb', line 76 def any_string( = {}) any :string, end |
#any_symbol ⇒ Object
Public: Get an arbitrary symbol, for example to use as a Hash key. The symbol will be between 2 and 20 characters long. If you need super-long symbols for some reason, use any_string.to_sym
.
83 84 85 |
# File 'lib/clean_test/any.rb', line 83 def any_symbol (any_string :min => 2, :max => 20).to_sym end |
#new_any(any, &block) ⇒ Object
Public: Create a new any that can be retrieved via #any
any - the identifer of your new any. Can be anything though a Symbol or classname would be appropriate block - the block that will be called whenever you #any your any.
137 138 139 |
# File 'lib/clean_test/any.rb', line 137 def new_any(any,&block) anies[any] = block end |