Module: Webspicy::Tester::Assertions

Included in:
Webspicy::Tester::Asserter::AssertionsClass
Defined in:
lib/webspicy/tester/assertions.rb

Defined Under Namespace

Classes: InvalidArgError

Constant Summary collapse

NO_ARG =
Object.new

Instance Method Summary collapse

Instance Method Details

#actual_size(target, path) ⇒ Object



43
44
45
46
# File 'lib/webspicy/tester/assertions.rb', line 43

def actual_size(target, path)
  target = extract_path(target, path)
  respond_to!(target, :size).size
end

#element_with_id(target, path, id) ⇒ Object



66
67
68
69
# File 'lib/webspicy/tester/assertions.rb', line 66

def element_with_id(target, path, id)
  target = extract_path(target, path)
  an_array(target).find { |t| t[:id] == id }
end

#empty(target, path = NO_ARG) ⇒ Object



29
30
31
32
# File 'lib/webspicy/tester/assertions.rb', line 29

def empty(target, path = NO_ARG)
  target = extract_path(target, path)
  respond_to!(target, :empty?).empty?
end

#eq(target, path, expected) ⇒ Object



93
94
95
96
# File 'lib/webspicy/tester/assertions.rb', line 93

def eq(target, path, expected)
  target = extract_path(target, path)
  target == expected
end

#eql(target, path, expected) ⇒ Object



98
99
100
101
# File 'lib/webspicy/tester/assertions.rb', line 98

def eql(target, path, expected)
  target = extract_path(target, path)
  value_equal(target, expected)
end

#exists(target, path = NO_ARG) ⇒ Object



19
20
21
22
# File 'lib/webspicy/tester/assertions.rb', line 19

def exists(target, path = NO_ARG)
  target = extract_path(target, path)
  not target.nil?
end

#extract_path(target, path = NO_ARG) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/webspicy/tester/assertions.rb', line 105

def extract_path(target, path = NO_ARG)
  return target if path.nil? or path==NO_ARG or path.empty?
  return nil unless target.respond_to?(:[])
  path.split('/').inject(target) do |memo,key|
    memo && (memo.is_a?(Array) ? memo[key.to_i] : memo[key.to_sym])
  end
end

#idFD(element, expected) ⇒ Object



71
72
73
74
75
# File 'lib/webspicy/tester/assertions.rb', line 71

def idFD(element, expected)
  expected.keys.all? do |k|
    value_equal(expected[k], element[k])
  end
end

#idIn(target, path, expected = NO_ARG) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/webspicy/tester/assertions.rb', line 48

def idIn(target, path, expected = NO_ARG)
  path, expected = '', path if expected == NO_ARG
  target = extract_path(target, path)
  ids = an_array(target).map do |tuple|
    respond_to!(tuple, :[])[:id]
  end
  ids.to_set == expected.to_set
end

#idNotIn(target, path, expected = NO_ARG) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/webspicy/tester/assertions.rb', line 57

def idNotIn(target, path, expected = NO_ARG)
  path, expected = '', path if expected == NO_ARG
  target = extract_path(target, path)
  ids = an_array(target).map do |tuple|
    respond_to!(tuple, :[])[:id]
  end
  (ids.to_set & expected.to_set).empty?
end

#includes(target, path, expected = NO_ARG) ⇒ Object



9
10
11
12
13
# File 'lib/webspicy/tester/assertions.rb', line 9

def includes(target, path, expected = NO_ARG)
  path, expected = '', path if expected == NO_ARG
  target = extract_path(target, path)
  an_array(target).include?(expected)
end

#match(target, path, rx) ⇒ Object



84
85
86
87
# File 'lib/webspicy/tester/assertions.rb', line 84

def match(target, path, rx)
  target = extract_path(target, path)
  !(target.to_s =~ rx).nil?
end

#notEmpty(target, path = NO_ARG) ⇒ Object



34
35
36
# File 'lib/webspicy/tester/assertions.rb', line 34

def notEmpty(target, path = NO_ARG)
  not empty(target, path)
end

#notExists(target, path = NO_ARG) ⇒ Object



24
25
26
27
# File 'lib/webspicy/tester/assertions.rb', line 24

def notExists(target, path = NO_ARG)
  target = extract_path(target, path)
  target.nil?
end

#notIncludes(target, path, expected = NO_ARG) ⇒ Object



15
16
17
# File 'lib/webspicy/tester/assertions.rb', line 15

def notIncludes(target, path, expected = NO_ARG)
  not self.includes(target, path, expected)
end

#notMatch(target, path, rx) ⇒ Object



89
90
91
# File 'lib/webspicy/tester/assertions.rb', line 89

def notMatch(target, path, rx)
  !match(target, path, rx)
end

#pathFD(target, path, expected) ⇒ Object



77
78
79
80
81
82
# File 'lib/webspicy/tester/assertions.rb', line 77

def pathFD(target, path, expected)
  target = extract_path(target, path)
  expected.keys.all?{|k|
    value_equal(expected[k], target[k])
  }
end

#size(target, path, expected = NO_ARG) ⇒ Object



38
39
40
41
# File 'lib/webspicy/tester/assertions.rb', line 38

def size(target, path, expected = NO_ARG)
  path, expected = '', path if expected == NO_ARG
  actual_size(target, path) == expected
end