-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
144 lines (112 loc) · 2.97 KB
/
example_test.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2016 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package do_test
import (
"fmt"
"github.com/GoLangsam/container/oneway/list"
"github.com/GoLangsam/do"
)
// Value returns a function which
// sets Value to v
// and returns it's undo Opt.
func Value(v interface{}) do.Option {
return func(any interface{}) do.Opt {
a := any.(*list.Element)
prev := (*a).Value
(*a).Value = v
return func() do.Opt {
return Value(prev)(a)
}
}
}
func ExampleOpt() {
e := list.NewList("TestList", "Element One").Front()
fmt.Println(e.Value) // Element One
undo := Value(3)(e)
fmt.Println(e.Value) // 3 (temporarily)
redo := undo()
fmt.Println(e.Value) // Element One (temporary setting undone)
rere := redo()
fmt.Println(e.Value) // 3 (undo undone)
_ = rere()
fmt.Println(e.Value) // Element One (temporary setting undone)
// Output:
// Element One
// 3
// Element One
// 3
// Element One
}
func ExampleOptionJoin() {
e := list.NewList("TestList", "Element One").Front()
fmt.Println(e.Value) // Element One
undo := do.OptionJoin(Value(3), Value("5"), Value(7))(e)
fmt.Println(e.Value) // 7 (temporarily)
redo := undo()
fmt.Println(e.Value) // Element One (temporary setting undone)
_ = redo()
fmt.Println(e.Value) // 7 (undo undone)
// Output:
// Element One
// 7
// Element One
// 7
}
func ExampleOptions() {
e := list.NewList("TestList", "Element One").Front()
fmt.Println(e.Value) // Element One
undo := do.Options(e, Value(3), Value("5"), Value(7))
fmt.Println(e.Value) // 7 (temporarily)
redo := undo()
fmt.Println(e.Value) // Element One (temporary setting undone)
_ = redo()
fmt.Println(e.Value) // 7 (undo undone)
// Output:
// Element One
// 7
// Element One
// 7
}
func Example_value() {
// Value returns a function which
// sets Value to v
// and returns it's undo Opt.
Value := func(v interface{}) do.Option {
return func(any interface{}) do.Opt {
a := any.(*list.Element)
prev := a.Value
a.Value = v
return func() do.Opt {
return Value(prev)(a)
}
}
}
e := list.NewList("TestList", "Element One").Front()
setValue := func(e *list.Element, v interface{}) {
// upon exit apply undo to restore original value while setting to new value v now via
defer Value(v)(e)() // Note the triple evaluation.
// ... do some stuff with Value being temporarily set to v.
fmt.Println(e.Value) // Changed Value
}
fmt.Println(e.Value) // Original Value
setValue(e, 5)
fmt.Println(e.Value)
// Output:
// Element One
// 5
// Element One
}
func ExampleOptions_nochange() {
e := list.NewList("TestList", "Element One").Front()
undo := do.Options(e)
fmt.Println(e.Value) // Element One (unchanged)
redo := undo()
fmt.Println(e.Value) // Element One (temporary no-change undone)
_ = redo()
fmt.Println(e.Value) // Element One (temporary no-change undo redone)
// Output:
// Element One
// Element One
// Element One
}