RWebSpec::Utils

Constants

WORDS

Public Instance Methods

days_after(days, format = nil) click to toggle source
Alias for: days_from_now
days_before(days, format = nil) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: days_after
getToday(format = nil) click to toggle source
Alias for: today
getToday_AU(format = nil) click to toggle source
Alias for: today
getToday_US(format = nil) click to toggle source
Alias for: today
interpret_value(value) click to toggle source

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

# File lib/rwebspec/test_utils.rb, line 161
def interpret_value(value)
  case value
  when Array then value.rand
  when Range then value_in_range(value)
  else value
  end
end
paragraphs(total) click to toggle source

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

# File lib/rwebspec/test_utils.rb, line 153
def paragraphs(total)
  (1..interpret_value(total)).map do
    sentences(3..8).capitalize
  end.join("\n\n")
end
random_boolean() click to toggle source
# File lib/rwebspec/test_utils.rb, line 88
def random_boolean
  return random_number(0, 1) == 1
end
random_char(lowercase = true) click to toggle source
# 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_digit() click to toggle source
# File lib/rwebspec/test_utils.rb, line 100
def random_digit()
  sprintf("%c", random_number(48, 57))
end
random_number(min, max) click to toggle source

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

# File lib/rwebspec/test_utils.rb, line 84
def random_number(min, max)
  rand(max-min+1)+min
end
random_str(length, lowercase = true) click to toggle source
# 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) click to toggle source

Return a random string in a rangeof pre-defined strings

# 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
Also aliased as: random_string_in_collection
random_string_in_collection(arr) click to toggle source
Alias for: random_string_in
sentences(total) click to toggle source

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

# File lib/rwebspec/test_utils.rb, line 145
def sentences(total)
  (1..interpret_value(total)).map do
    words(5..20).capitalize
  end.join('. ')
end
today(format = nil) click to toggle source

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
# File lib/rwebspec/test_utils.rb, line 55
def today(format = nil)                
  format_date(Time.now, date_format(format))
end
Also aliased as: getToday_AU, getToday_US, getToday
tomorrow(format = nil) click to toggle source
# File lib/rwebspec/test_utils.rb, line 78
def tomorrow(format = nil)
  days_from_now(1, date_format(format))
end
value_in_range(range) click to toggle source

Pick a random value out of a given range.

# 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) click to toggle source

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

# File lib/rwebspec/test_utils.rb, line 135
def words(total)
  if total.class == Range
    (1..interpret_value(total)).map { WORDS[random_number(total.min, total.max)] }.join(' ')
  else
    (1..interpret_value(total)).map { WORDS[random_number(0, total)] }.join(' ')
  end
end
yesterday(format = nil) click to toggle source
# File lib/rwebspec/test_utils.rb, line 68
def yesterday(format = nil)
  days_before(1, date_format(format))
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.