Class: PuzzleTestHook

Inherits:
Mumukit::Templates::FileHook
  • Object
show all
Defined in:
lib/test_hook.rb

Instance Method Summary collapse

Instance Method Details

#compile(req) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/test_hook.rb', line 12

def compile(req)
  if req.client_result
    req
  else
    {
      expected: parse_positions(req),
      actual: JSON.parse(req.content)['positions']
    }.to_struct
  end
end

#parse_positions(req) ⇒ Object



23
24
25
# File 'lib/test_hook.rb', line 23

def parse_positions(req)
  (req.test =~ /Muzzle\.expect\((.+?)\)/m) && (JSON.parse($1) rescue nil)
end

#positions_relatively_equal(expected, actual) ⇒ Object



27
28
29
30
31
# File 'lib/test_hook.rb', line 27

def positions_relatively_equal(expected, actual)
  x_offset = actual.first.first - (100 * expected.first.first)
  y_offset = actual.first[1]  - (100 * expected.first[1])
  actual.flat_map { |x, y| [x - x_offset, y - y_offset] } == expected.flatten.map { |it| it * 100 }
end

#run!(req) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/test_hook.rb', line 2

def run!(req)
  if req.client_result
    ['', (req.client_result['status'].passed? ? :passed : :failed)]
  elsif !req.expected || positions_relatively_equal(req.expected, req.actual)
    ['', :passed]
  else
    ['', :failed]
  end
end