Module: RWebSpec::Utils

Included in:
LoadTestHelper, RSpecHelper, TestScript, WebTestCase
Defined in:
lib/rwebspec/test_utils.rb

Constant Summary collapse

WORDS =
%w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat)

Instance Method Summary collapse

Instance Method Details

#days_before(days, format = nil) ⇒ Object



63
64
65
66
# File 'lib/rwebspec/test_utils.rb', line 63

def days_before(days, format = nil)        
  return nil if !(days.instance_of?(Fixnum))
  format_date(Time.now - days * 24 * 3600, date_format(format))
end

#days_from_now(days, format = nil) ⇒ Object Also known as: days_after



72
73
74
75
# File 'lib/rwebspec/test_utils.rb', line 72

def days_from_now(days, format = nil)
  return nil if !(days.instance_of?(Fixnum))
  format_date(Time.now + days * 24 * 3600, date_format(format))
end

#interpret_value(value) ⇒ Object

If an array or range is passed, a random value will be selected to match. All other values are simply returned.



157
158
159
160
161
162
163
# File 'lib/rwebspec/test_utils.rb', line 157

def interpret_value(value)
  case value
  when Array then value.rand
  when Range then value_in_range(value)
  else value
  end
end

#paragraphs(total) ⇒ Object

Generate a given number of paragraphs. If a range is passed, it will generate a random number of paragraphs within that range.



149
150
151
152
153
# File 'lib/rwebspec/test_utils.rb', line 149

def paragraphs(total)
  (1..interpret_value(total)).map do
    sentences(3..8).capitalize
  end.join("\n\n")
end

#random_booleanObject



88
89
90
# File 'lib/rwebspec/test_utils.rb', line 88

def random_boolean
  return random_number(0, 1) == 1
end

#random_char(lowercase = true) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/rwebspec/test_utils.rb', line 92

def random_char(lowercase = true)
  if lowercase
    sprintf("%c", random_number(97, 122))
  else
    sprintf("%c", random_number(65, 90))
  end
end

#random_digitObject



100
101
102
# File 'lib/rwebspec/test_utils.rb', line 100

def random_digit()
  sprintf("%c", random_number(48, 57))
end

#random_number(min, max) ⇒ Object

return a random number >= min, but <= max



84
85
86
# File 'lib/rwebspec/test_utils.rb', line 84

def random_number(min, max)
  rand(max-min+1)+min
end

#random_str(length, lowercase = true) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/rwebspec/test_utils.rb', line 104

def random_str(length, lowercase = true)
  randomStr = ""
  length.times {
    randomStr += random_char(lowercase)
  }
  randomStr
end

#random_string_in(arr) ⇒ Object Also known as: random_string_in_collection

Return a random string in a rangeof pre-defined strings



113
114
115
116
117
# File 'lib/rwebspec/test_utils.rb', line 113

def random_string_in(arr)
  return nil if arr.empty?
  index = random_number(0, arr.length-1)
  arr[index]
end

#sentences(total) ⇒ Object

Generate a given number of sentences. If a range is passed, it will generate a random number of sentences within that range.



141
142
143
144
145
# File 'lib/rwebspec/test_utils.rb', line 141

def sentences(total)
  (1..interpret_value(total)).map do
    words(5..20).capitalize
  end.join('. ')
end

#today(format = nil) ⇒ Object Also known as: getToday_AU, getToday_US, getToday

default date format returned is 29/12/2007. if supplied parameter is not ‘%m/%d/%Y’ -> 12/29/2007 Otherwise, “2007-12-29”, which is most approiate date format

%a - The abbreviated weekday name (``Sun'')
%A - The  full  weekday  name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The  full  month  name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM''  or  ``PM'')
%S - Second of the minute (00..60)
%U - Week  number  of the current year,
        starting with the first Sunday as the first
        day of the first week (00..53)
%W - Week  number  of the current year,
        starting with the first Monday as the first
        day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character


55
56
57
# File 'lib/rwebspec/test_utils.rb', line 55

def today(format = nil)                
  format_date(Time.now, date_format(format))
end

#tomorrow(format = nil) ⇒ Object



78
79
80
# File 'lib/rwebspec/test_utils.rb', line 78

def tomorrow(format = nil)
  days_from_now(1, date_format(format))
end

#value_in_range(range) ⇒ Object

Pick a random value out of a given range.



124
125
126
127
128
129
130
131
# File 'lib/rwebspec/test_utils.rb', line 124

def value_in_range(range)
  case range.first
  when Integer then number_in_range(range)
  when Time then time_in_range(range)
  when Date then date_in_range(range)
  else range.to_a.rand
  end
end

#words(total) ⇒ Object

Generate a given number of words. If a range is passed, it will generate a random number of words within that range.



135
136
137
# File 'lib/rwebspec/test_utils.rb', line 135

def words(total)
  (1..interpret_value(total)).map { WORDS[random_number(0, total)] }.join(' ')
end

#yesterday(format = nil) ⇒ Object



68
69
70
# File 'lib/rwebspec/test_utils.rb', line 68

def yesterday(format = nil)
  days_before(1, date_format(format))
end