Implement ActionList::Entry#clear.
[kaya.git] / test / interaction / test_history.rb
blobdff57cb4f902cfe92962486d9729eed16257f5d9
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 'test/unit'
9 require 'interaction/history'
11 class TestHistory < Test::Unit::TestCase
12   def setup
13     @history = History.new("initial_state")
14   end
15   
16   def test_initial_state
17     assert_equal 0, @history.current
18     assert_equal "initial_state", @history.state
19     assert_nil @history.move
20     assert_equal 1, @history.size
21   end
22   
23   def test_add_move
24     @history.add_move("new_state", "first_move")
25     assert_equal 1, @history.current
26     assert_equal "new_state", @history.state
27     assert_equal "first_move", @history.move
28     assert_equal 2, @history.size
29     @history.add_move("new_new_state", "second_move")
30     assert_equal 2, @history.current
31     assert_equal "new_new_state", @history.state
32     assert_equal "second_move", @history.move
33     assert_equal 3, @history.size
34   end
35   
36   def test_back_on_first
37     assert_raise History::OutOfBound do
38       @history.back
39       assert_equal 0, @history.current
40     end
41   end
42   
43   def test_forward_on_last
44     @history.add_move("new_state", "first_move")
45     assert_raise History::OutOfBound do
46       @history.forward
47       assert_equal 1, @history.current
48     end
49   end
50   
51   def test_back
52     @history.add_move("new_state", "first_move")
53     @history.back
54     assert_equal 0, @history.current
55   end
56   
57   def test_forward
58     @history.add_move("new_state", "first_move")
59     @history.back
60     @history.forward
61     assert_equal 1, @history.current
62   end
63   
64   def test_overwrite_moves
65     @history.add_move("new_state1", "first_move1")
66     @history.add_move("new_new_state1", "second_move1")
67     @history.back
68     @history.back
69     @history.add_move("new_state2", "first_move2")
70     assert_equal 1, @history.current
71     assert_equal "new_state2", @history.state
72     assert_equal "first_move2", @history.move
73     assert_equal 2, @history.size
74   end
75   
76   def test_operations
77     @history.add_move("new_state1", "first_move1")
78     @history.add_move("new_new_state1", "second_move1")
79     assert_equal 2, @history.operations.size
80   end
81   
82   def test_undo
83     @history.add_move("new_state1", "first_move1")
84     @history.add_move("new_new_state1", "second_move1")
85     @history.undo!
86     assert_equal 1, @history.current
87     assert_equal "new_state1", @history.state
88     assert_equal "first_move1", @history.move
89   end
90   
91   def test_redo
92     @history.add_move("new_state1", "first_move1")
93     @history.add_move("new_new_state1", "second_move1")
94     @history.undo!
95     @history.redo!
96     assert_equal 2, @history.current
97     assert_equal "new_new_state1", @history.state
98     assert_equal "second_move1", @history.move
99   end
100   
101   def test_undo_overwrite
102     @history.add_move("new_state1", "first_move1")
103     @history.add_move("new_new_state1", "second_move1")
104     @history.back
105     @history.back
106     @history.add_move("new_state2", "first_move2")
107     @history.undo!
108     assert_equal 0, @history.current
109     assert_equal "initial_state", @history.state
110     assert_nil @history.move
111   end