public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_undo.nu
100644 93 lines (72 sloc) 2.967 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;; test_undo.nu
;; tests for Nu support for invocation-based undo.
;;
;; Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
 
(if (eq (uname) "Darwin")
    
    (class Toddler is NSObject
         (ivar (id) shoes (int) giggles)
         (ivar-accessors)
         
         (- init is
            (super init)
            (set @shoes "untied")
            (set @giggles 0)
            self)
         
         (- tieShoes is
            (set @shoes "tied"))
         
         (- untieShoes is
            (set @shoes "untied"))
         
         (- (void) tickle:(int) times is
            (set @giggles (+ @giggles times))
            (if (>= @giggles 5) (self untieShoes))))
    
    (class TestUndo is NuTestCase
         
         (- testUndo is
            (set u (NSUndoManager new))
            (u setGroupsByEvent:NO) ;; we will manage our own undo groups
            (u setLevelsOfUndo:0) ;; we will use an unlimited undo stack
            
            ;; create our test subject
            (set shannon (Toddler new))
            
            ;; verify initial conditions
            (assert_equal "untied" (shannon shoes))
            (assert_equal 0 (shannon giggles))
            (assert_equal 0 (u canUndo))
            
            ;; now let's push some "undo" actions on the stack.
            ;; these actions are last-in-first-out.
            
            ;; tie Shannon's shoes.
            (u beginUndoGrouping)
            (u prepareWithInvocationTarget:shannon)
            (u tieShoes)
            (u endUndoGrouping)
            
            ;; tickle her again.
            (u beginUndoGrouping)
            (u prepareWithInvocationTarget:shannon)
            (u tickle:3)
            (u endUndoGrouping)
            
            ;; tickle her.
            (u beginUndoGrouping)
            (u prepareWithInvocationTarget:shannon)
            (set one 1)
            (u tickle:(+ one one one))
            (u endUndoGrouping)
            
            ;; first, tie Shannon's shoes.
            (u beginUndoGrouping)
            (u prepareWithInvocationTarget:shannon)
            (u tieShoes)
            (u endUndoGrouping)
            
            (assert_equal 1 (u canUndo))
            
            ;; now let's "undo" everything.
            
            (u undo)
            (assert_equal "tied" (shannon shoes))
            (assert_equal 0 (shannon giggles))
            (assert_equal 1 (u canUndo))
            
            (u undo)
            (assert_equal "tied" (shannon shoes))
            (assert_equal 3 (shannon giggles))
            (assert_equal 1 (u canUndo))
            
            (u undo)
            (assert_equal "untied" (shannon shoes))
            (assert_equal 6 (shannon giggles))
            (assert_equal 1 (u canUndo))
            
            (u undo)
            (assert_equal "tied" (shannon shoes))
            (assert_equal 6 (shannon giggles))
            (assert_equal 0 (u canUndo)))))