Class: String

Inherits:
Object show all
Defined in:
lib/spectre/helpers.rb,
lib/spectre/assertion.rb

Instance Method Summary collapse

Instance Method Details

#as_dateObject



14
15
16
# File 'lib/spectre/helpers.rb', line 14

def as_date
  DateTime.parse(self)
end

#as_jsonObject



10
11
12
# File 'lib/spectre/helpers.rb', line 10

def as_json
  JSON.parse(self, object_class: OpenStruct)
end

#as_timestampObject



18
19
20
# File 'lib/spectre/helpers.rb', line 18

def as_timestamp
  DateTime.parse(self).to_time.to_i
end

#content(with: nil) ⇒ Object

File helpers



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spectre/helpers.rb', line 54

def content with: nil
  fail "'#{self}' is not a file path, or the file does not exist." unless File.exist? self

  file_content = File.read(self)

  if with
    file_content.with(with)
  else
    file_content
  end
end

#exists?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/spectre/helpers.rb', line 72

def exists?
  File.exist? self
end

#file_sizeObject



66
67
68
69
70
# File 'lib/spectre/helpers.rb', line 66

def file_size
  fail "'#{self}' is not a file path, or the file does not exist." unless File.exist? self

  File.size(self)
end

#pick(path) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
# File 'lib/spectre/helpers.rb', line 42

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!Object



76
77
78
79
80
# File 'lib/spectre/helpers.rb', line 76

def remove!
  fail "'#{self}' is not a file path, or the file does not exist." unless File.exist? self

  File.delete self
end

#should_be(val) ⇒ Object



130
131
132
133
134
# File 'lib/spectre/assertion.rb', line 130

def should_be(val)
  evaluate(val, "'#{self}' should be '#{val}'") do |x|
    self.to_s == x.to_s
  end
end

#should_be_emptyObject

Raises:

  • (AssertionFailure)


136
137
138
# File 'lib/spectre/assertion.rb', line 136

def should_be_empty
  raise AssertionFailure.new("'#{self.trim}' should be empty", nil, self) unless self.empty?
end

#should_contain(val) ⇒ Object



150
151
152
153
154
# File 'lib/spectre/assertion.rb', line 150

def should_contain(val)
  evaluate(val, "'#{self.trim}' should contain '#{val.to_s}'") do |x|
    self.include? x.to_s
  end
end

#should_match(regex) ⇒ Object



162
163
164
165
166
# File 'lib/spectre/assertion.rb', line 162

def should_match(regex)
  evaluate(regex, "'#{self.trim}' should match /#{regex}/") do |x|
    self.match(x)
  end
end

#should_not_be(val) ⇒ Object



140
141
142
143
144
# File 'lib/spectre/assertion.rb', line 140

def should_not_be(val)
  evaluate(val, "'#{self}' should not be '#{val}'") do |x|
    self.to_s != x.to_s
  end
end

#should_not_be_emptyObject

Raises:

  • (AssertionFailure)


146
147
148
# File 'lib/spectre/assertion.rb', line 146

def should_not_be_empty
  raise AssertionFailure.new('Text should not be empty', 'nothing', self) unless not self.empty?
end

#should_not_contain(val) ⇒ Object



156
157
158
159
160
# File 'lib/spectre/assertion.rb', line 156

def should_not_contain(val)
  evaluate(val, "'#{self}' should not contain '#{val}'") do |x|
    not self.include? x.to_s
  end
end

#should_not_match(regex) ⇒ Object



168
169
170
171
172
# File 'lib/spectre/assertion.rb', line 168

def should_not_match(regex)
  evaluate(regex, "'#{self.trim}' should not match '#{regex}'") do |x|
    not self.match(x)
  end
end

#trim(size = 50) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/spectre/helpers.rb', line 34

def trim size = 50
  if (self.length + 3) > size
    return self[0..size-4] + '...'
  end

  self
end

#with(mapping) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spectre/helpers.rb', line 22

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