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

#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

#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



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

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(target, path, id, expected = NO_ARG) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/webspicy/tester/assertions.rb', line 62

def idFD(target, path, id, expected = NO_ARG)
  if expected == NO_ARG
    expected = id
    id, path = path, ''
  end
  target = extract_path(target, path)
  found = an_array(target).find{|t| t[:id] == id }
  expected.keys.all?{|k|
    value_equal(expected[k], found[k])
  }
end

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



44
45
46
47
48
49
50
51
# File 'lib/webspicy/tester/assertions.rb', line 44

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



53
54
55
56
57
58
59
60
# File 'lib/webspicy/tester/assertions.rb', line 53

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



81
82
83
84
# File 'lib/webspicy/tester/assertions.rb', line 81

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



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

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

#pathFD(target, path, expected) ⇒ Object



74
75
76
77
78
79
# File 'lib/webspicy/tester/assertions.rb', line 74

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
42
# File 'lib/webspicy/tester/assertions.rb', line 38

def size(target, path, expected = NO_ARG)
  path, expected = '', path if expected == NO_ARG
  target = extract_path(target, path)
  respond_to!(target, :size).size == expected
end