Module: RWebUnit::Utils

Included in:
RSpecHelper, TestScript, WebTestCase
Defined in:
lib/rwebunit/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 = '%d/%m/%Y') ⇒ Object



50
51
52
53
# File 'lib/rwebunit/test_utils.rb', line 50

def days_before(days, format = '%d/%m/%Y')
  nil if !(days.instance_of?(Fixnum))
  format_date(Time.now - days * 24 * 3600, format)
end

#days_from_now(days, format = '%d/%m/%Y') ⇒ Object Also known as: days_after



59
60
61
62
# File 'lib/rwebunit/test_utils.rb', line 59

def days_from_now(days, format = '%d/%m/%Y')
  nil if !(days.instance_of?(Fixnum))
  format_date(Time.now + days * 24 * 3600, 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.



140
141
142
143
144
145
146
# File 'lib/rwebunit/test_utils.rb', line 140

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.



132
133
134
135
136
# File 'lib/rwebunit/test_utils.rb', line 132

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

#random_booleanObject



74
75
76
# File 'lib/rwebunit/test_utils.rb', line 74

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

#random_char(lowercase = true) ⇒ Object



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

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

#random_digitObject



83
84
85
# File 'lib/rwebunit/test_utils.rb', line 83

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

#random_number(min, max) ⇒ Object

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



70
71
72
# File 'lib/rwebunit/test_utils.rb', line 70

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

#random_str(length, lowercase = true) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rwebunit/test_utils.rb', line 87

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



96
97
98
99
100
# File 'lib/rwebunit/test_utils.rb', line 96

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.



124
125
126
127
128
# File 'lib/rwebunit/test_utils.rb', line 124

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

#today(format = '%d/%m/%Y') ⇒ 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


42
43
44
# File 'lib/rwebunit/test_utils.rb', line 42

def today(format = '%d/%m/%Y')
  format_date(Time.now, format)
end

#tomorrowObject



65
66
67
# File 'lib/rwebunit/test_utils.rb', line 65

def tomorrow
  days_from_now(1)
end

#value_in_range(range) ⇒ Object

Pick a random value out of a given range.



107
108
109
110
111
112
113
114
# File 'lib/rwebunit/test_utils.rb', line 107

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.



118
119
120
# File 'lib/rwebunit/test_utils.rb', line 118

def words(total)
  (1..interpret_value(total)).map { WORDS.rand }.join(' ')
end

#yesterdayObject



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

def yesterday
  days_before(1)
end