Update connect/disconnect action states.
[kaya.git] / lib / helpers / validation_helper.rb
blob65b033feaf7cb85a0b04192f7c159fdf83071d9b
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 module ValidationHelper
9   def unpack_move(*args)
10     opts = {}
11     if args.last.instance_of? Hash
12       opts = args.last
13       args = args[0...-1]
14     end
15     
16     case args.size
17     when 1
18       args.first
19     when 2
20       @state.move_factory.new(*(args + [opts]))
21     when 4
22       @state.move_factory.new(*(args.to_enum(:each_slice, 2).map{|x| Point.new(*x) } + [opts]))
23     else
24       raise ArgumentError.new("Could not unpack move using #{args.size} parameters")
25     end
26   end
27   
28   def unpack_point(*args)
29     case args.size
30     when 1
31       args.first
32     when 2
33       Point.new(*args)
34     end
35   end
36   
37   def assert_valid(*args)
38     move = unpack_move(*args)
39     assert @validate[move]
40     yield move if block_given?
41   end
42   
43   def assert_not_valid(*args)
44     assert ! @validate[unpack_move(*args)]
45   end
46   
47   def assert_piece(color, type, *point)
48     p = unpack_point(*point)
49     piece = @board[p]
50     exp = @state.piece_factory.new(color, type)
51     assert_not_nil piece, "#{exp} expected on #{p}, nothing found"
52     assert_equal exp, piece, "#{exp} expected on #{p}, #{piece} found"
53     yield piece if block_given?
54   end
55   
56   def assert_no_piece(*point)
57     assert_nil @board[unpack_point(*point)]
58   end
59   
60   def execute(*args)
61     move = unpack_move(*args)
62     assert @validate[move]
63     @state.perform! move
64   end
65   
66   def assert_pool(color, type, number)
67     piece = @state.piece_factory.new(color, type)
68     assert_equal number, @state.pool(color).pieces[piece]
69   end
70   
71   def serialize(serializer, *args)
72     move = unpack_move(*args)
73     serializer.serialize(move, @state)
74   end
75 end