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 produced random values, so you must be sure that your code truly should work with any value. The random seed used will be output, and you can use the environment variable RANDOM_SEED to recreate the conditions of a particular test.

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_random_seedObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/clean_test/any.rb', line 44

def self.setup_random_seed
  seed = if ENV['RANDOM_SEED']
           ENV['RANDOM_SEED'].to_i
         else
           srand() # generate random seed
           seed = srand() # save it (but we've now generated another one)
         end
  srand(seed) # set it explicitly
  puts "Random seed was #{seed}; re-use it via environment variable RANDOM_SEED"
end

Instance Method Details

#any_int(*options) ⇒ Object

Public: Returns an integer. options is the same as for #any_number



74
75
76
# File 'lib/clean_test/any.rb', line 74

def any_int(*options)
  any_number(*options).to_i
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


62
63
64
65
66
67
68
69
70
71
# File 'lib/clean_test/any.rb', line 62

def any_number(*options)
  number = (rand(2 * MAX_RAND) - MAX_RAND).to_f/100.0
  if options.include? :positive
    number + MAX_RAND
  elsif options.include? :negative
    number - MAX_RAND
  else
    number
  end
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


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/clean_test/any.rb', line 138

def any_sentence(options = {})
  min = 11
  max = 21

  if options[:max]
    min = 1
    max = options[:max]
  elsif options[:min]
    min = options[:min]
    max = min + 10
  end

  Faker::Lorem.words(number: rand(max - min) + min).join(' ')
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


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/clean_test/any.rb', line 89

def any_string(options = {})
  if options[:min] && options[:max]
    raise ":min must be less than :max" if options[:min] > options[:max]
  end
  if options[:min]
    raise ":min must be positive" if options[:min] < 1
  end

  min_size = options[:min]
  max_size = options[:max]

  if min_size.nil? && max_size.nil?
    min_size = rand(80) + 1
    max_size = min_size + rand(80)
  elsif min_size.nil?
    min_size = max_size - rand(max_size)
    min_size = 1 if min_size < 1
  else
    max_size = min_size + rand(min_size) + 1
  end

  string = Faker::Lorem.words(number: 1).join(' ')
  while string.length < min_size
    string += Faker::Lorem.words(number: 1).join(' ') 
  end

  string[0..(max_size-1)]
end

#any_symbolObject

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.



121
122
123
# File 'lib/clean_test/any.rb', line 121

def any_symbol
  (any_string :min => 2, :max => 20).to_sym
end