class String

Public Instance Methods

as_date() click to toggle source
# File lib/spectre/helpers.rb, line 12
def as_date
  DateTime.parse(self)
end
as_json() click to toggle source
# File lib/spectre/helpers.rb, line 8
def as_json
  JSON.parse(self, object_class: OpenStruct)
end
as_timestamp() click to toggle source
# File lib/spectre/helpers.rb, line 16
def as_timestamp
  DateTime.parse(self).to_time.to_i
end
content(with: nil) click to toggle source

File helpers

# File lib/spectre/helpers.rb, line 53
def content with: nil
  fail "'#{self}' is not a file path, or the file does not exist." if !File.exists? self
  file_content = File.read(self)

  if with
    file_content.with(with)
  else
    file_content
  end
end
exists?() click to toggle source
# File lib/spectre/helpers.rb, line 69
def exists?
  File.exists? self
end
file_size() click to toggle source
# File lib/spectre/helpers.rb, line 64
def file_size
  fail "'#{self}' is not a file path, or the file does not exist." if !File.exists? self
  File.size(self)
end
pick(path) click to toggle source
# File lib/spectre/helpers.rb, line 40
def pick path
  raise ArgumentError.new("`path' must not be nil or empty") if path.nil? or path.empty?

  begin
    JsonPath.on(self, path)

  rescue MultiJson::ParseError
    # do nothing and return nil
  end
end
remove!() click to toggle source
# File lib/spectre/helpers.rb, line 73
def remove!
  fail "'#{self}' is not a file path, or the file does not exist." if !File.exists? self
  File.delete self
end
should_be(val) click to toggle source
# File lib/spectre/assertion.rb, line 104
def should_be(val)
  raise AssertionFailure.new("The text '#{self.trim}' should be '#{val.to_s.trim}'", val, self) unless self == val
end
should_be_empty() click to toggle source
# File lib/spectre/assertion.rb, line 108
def should_be_empty
  raise AssertionFailure.new("The text '#{self.trim}' should be empty", nil, self) unless self.empty?
end
should_contain(value) click to toggle source
# File lib/spectre/assertion.rb, line 120
def should_contain(value)
  raise AssertionFailure.new("`value' must not be nil") if value.nil?

  predicate = proc { |x| self.include? x.to_s }
  evaluation = SingleEvaluation.new(value)
  success = evaluation.call(predicate)

  return if success

  raise AssertionFailure.new("The text '#{self.to_s.trim}' should contain #{evaluation.to_s}", evaluation, self)
end
should_match(regex) click to toggle source
# File lib/spectre/assertion.rb, line 136
def should_match(regex)
  raise AssertionFailure.new("The text '#{self.trim}' should match '#{val}'", regex, self) unless self.match(regex)
end
should_not_be(val) click to toggle source
# File lib/spectre/assertion.rb, line 112
def should_not_be(val)
  raise AssertionFailure.new("The text '#{self.trim}' should not be '#{val.to_s.trim}'", val, self) unless self != val
end
should_not_be_empty() click to toggle source
# File lib/spectre/assertion.rb, line 116
def should_not_be_empty
  raise AssertionFailure.new('The text should not be empty', 'nothing', self) unless not self.empty?
end
should_not_contain(val) click to toggle source
# File lib/spectre/assertion.rb, line 132
def should_not_contain(val)
  raise AssertionFailure.new("The text '#{self.trim}' should not contain '#{val.trim}'", val, self) if self.include? val
end
should_not_match(regex) click to toggle source
# File lib/spectre/assertion.rb, line 140
def should_not_match(regex)
  raise AssertionFailure.new("The text '#{self.trim}' should not match '#{val}'", regex, self) if self.match(regex)
end
trim(size = 50) click to toggle source
# File lib/spectre/helpers.rb, line 32
def trim size = 50
  if (self.length + 3) > size
    return self[0..size-4] + '...'
  end

  self
end
with(mapping) click to toggle source
# File lib/spectre/helpers.rb, line 20
def with mapping
  return self unless mapping and mapping.is_a? Hash

  new_string = self

  mapping.each do |key, value|
    new_string = new_string.gsub('#{' + key.to_s + '}', value.to_s)
  end

  new_string
end