Support handling of style12 event with delta > 1.
[kaya.git] / test / plugins / ics / test_match_helper.rb
blob5abd539547609f83b3198ea8ec5b26016683f815
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 require 'require_bundle'
9 require_bundle 'ics', 'match_helper'
10 require 'interaction/history'
11 require 'rubygems'
12 require 'mocha'
13 require 'games/all'
14 require 'ostruct'
16 class TestMatchHelper < Test::Unit::TestCase
17   def setup
18     chess = Game.get(:chess)
19     connection = stub_everything("connection")
20     
21     @protocol = stub_everything("protocol") do
22       stubs(:connection).returns(connection)
23     end
24     @user = stub_everything("user")
25     @match = stub_everything("match") do
26       history = History.new(chess.state.new.tap{|s| s.setup })
27       
28       stubs(:game).returns(chess)
29       stubs(:history).returns(history)
30       stubs(:started?).returns(true)
31     end
32     
33     @match_info = {
34       :game => chess,
35       :number => 37,
36       :white => { :name => 'Karpov' },
37       :black => { :name => 'Fisher' },
38       :icsapi => stub_everything("icsapi"),
39       :match => @match
40     }
41     @style12 = OpenStruct.new(
42       :game_number => 37,
43       :relation => ICS::Style12::Relation::MY_MOVE,
44       :state => chess.state.new.tap {|s| s.setup },
45       :move_index => 0)
47   end
48   
49   def test_default_start
50     helper = ICS::DefaultMatchHelper.instance
51     @user.expects(:name=).with("Karpov")
52     @match.expects(:start).with(@user)
53     @match.expects(:start).with{|opp| opp.name == "Fisher" }
54     helper.start(@protocol, @user, @match_info, @style12)
55   end
56   
57   def test_default_get_match
58     helper = ICS::DefaultMatchHelper.instance
59     info = helper.get_match(@protocol, @match_info, @style12)
60     assert_same @match, info[:match]
61     
62     info = helper.get_match(@protocol, nil, @style12)
63     assert_nil info
64   end
65   
66   def test_examination_start
67     helper = ICS::ExaminingMatchHelper.instance
68     @match.expects(:start).with{|player| player.name == "Karpov" }
69     @match.expects(:start).with{|player| player.name == "Fisher" }
70     helper.start(@protocol, @user, @match_info, @style12)
71   end
72   
73   def test_examination_get_match
74     helper = ICS::ExaminingMatchHelper.instance
75     info = helper.get_match(@protocol, @match_info, @style12)
76     assert_same @match, info[:match]
77     
78     @style12.match_info = @match_info
79     info = helper.get_match(@protocol, nil, @style12)
80     assert_not_nil info
81     assert_not_nil info[:match]
82     assert_equal 37, info[:number]
83   end
84   
85   def test_observation_start
86     helper = ICS::ObservingMatchHelper.instance
87     @match.expects(:start).with{|player| player.name == "Karpov" }
88     @match.expects(:start).with{|player| player.name == "Fisher" }
89     helper.start(@protocol, @user, @match_info, @style12)
90   end
91 end