| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package notify |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func call(wp watchpoint, fn interface{}, args []interface{}) eventDiff { |
| 14 |
vals := []reflect.Value{reflect.ValueOf(wp)} |
| 15 |
for _, arg := range args { |
| 16 |
vals = append(vals, reflect.ValueOf(arg)) |
| 17 |
} |
| 18 |
res := reflect.ValueOf(fn).Call(vals) |
| 19 |
if n := len(res); n != 1 { |
| 20 |
panic(fmt.Sprintf("unexpected len(res)=%d", n)) |
| 21 |
} |
| 22 |
diff, ok := res[0].Interface().(eventDiff) |
| 23 |
if !ok { |
| 24 |
panic(fmt.Sprintf("want typeof(diff)=EventDiff; got %T", res[0].Interface())) |
| 25 |
} |
| 26 |
return diff |
| 27 |
} |
| 28 |
|
| 29 |
func TestWatchpoint(t *testing.T) { |
| 30 |
ch := NewChans(5) |
| 31 |
all := All | recursive |
| 32 |
cases := [...]struct { |
| 33 |
fn interface{} |
| 34 |
args []interface{} |
| 35 |
diff eventDiff |
| 36 |
total Event |
| 37 |
}{ |
| 38 |
|
| 39 |
{ |
| 40 |
watchpoint.Add, |
| 41 |
[]interface{}{ch[0], Remove}, |
| 42 |
eventDiff{0, Remove}, |
| 43 |
Remove, |
| 44 |
}, |
| 45 |
|
| 46 |
{ |
| 47 |
watchpoint.Add, |
| 48 |
[]interface{}{ch[1], Create | Remove | recursive}, |
| 49 |
eventDiff{Remove, Remove | Create}, |
| 50 |
Create | Remove | recursive, |
| 51 |
}, |
| 52 |
|
| 53 |
{ |
| 54 |
watchpoint.Add, |
| 55 |
[]interface{}{ch[2], Create | Rename}, |
| 56 |
eventDiff{Create | Remove, Create | Remove | Rename}, |
| 57 |
Create | Remove | Rename | recursive, |
| 58 |
}, |
| 59 |
|
| 60 |
{ |
| 61 |
watchpoint.Add, |
| 62 |
[]interface{}{ch[0], Write | recursive}, |
| 63 |
eventDiff{Create | Remove | Rename, Create | Remove | Rename | Write}, |
| 64 |
Create | Remove | Rename | Write | recursive, |
| 65 |
}, |
| 66 |
|
| 67 |
{ |
| 68 |
watchpoint.Add, |
| 69 |
[]interface{}{ch[2], Remove | recursive}, |
| 70 |
none, |
| 71 |
Create | Remove | Rename | Write | recursive, |
| 72 |
}, |
| 73 |
|
| 74 |
{ |
| 75 |
watchpoint.Del, |
| 76 |
[]interface{}{ch[0], all}, |
| 77 |
eventDiff{Create | Remove | Rename | Write, Create | Remove | Rename}, |
| 78 |
Create | Remove | Rename | recursive, |
| 79 |
}, |
| 80 |
|
| 81 |
{ |
| 82 |
watchpoint.Del, |
| 83 |
[]interface{}{ch[2], all}, |
| 84 |
eventDiff{Create | Remove | Rename, Create | Remove}, |
| 85 |
Create | Remove | recursive, |
| 86 |
}, |
| 87 |
|
| 88 |
{ |
| 89 |
watchpoint.Add, |
| 90 |
[]interface{}{ch[3], Create | Remove}, |
| 91 |
none, |
| 92 |
Create | Remove | recursive, |
| 93 |
}, |
| 94 |
|
| 95 |
{ |
| 96 |
watchpoint.Del, |
| 97 |
[]interface{}{ch[1], all}, |
| 98 |
none, |
| 99 |
Create | Remove, |
| 100 |
}, |
| 101 |
|
| 102 |
{ |
| 103 |
watchpoint.Add, |
| 104 |
[]interface{}{ch[3], recursive | Write}, |
| 105 |
eventDiff{Create | Remove, Create | Remove | Write}, |
| 106 |
Create | Remove | Write | recursive, |
| 107 |
}, |
| 108 |
|
| 109 |
{ |
| 110 |
watchpoint.Del, |
| 111 |
[]interface{}{ch[3], Create}, |
| 112 |
eventDiff{Create | Remove | Write, Remove | Write}, |
| 113 |
Remove | Write | recursive, |
| 114 |
}, |
| 115 |
|
| 116 |
{ |
| 117 |
watchpoint.Add, |
| 118 |
[]interface{}{ch[3], Create | Rename}, |
| 119 |
eventDiff{Remove | Write, Create | Remove | Rename | Write}, |
| 120 |
Create | Remove | Rename | Write | recursive, |
| 121 |
}, |
| 122 |
|
| 123 |
{ |
| 124 |
watchpoint.Add, |
| 125 |
[]interface{}{ch[2], Remove | Write}, |
| 126 |
none, |
| 127 |
Create | Remove | Rename | Write | recursive, |
| 128 |
}, |
| 129 |
|
| 130 |
{ |
| 131 |
watchpoint.Del, |
| 132 |
[]interface{}{ch[3], Create | Remove | Write}, |
| 133 |
eventDiff{Create | Remove | Rename | Write, Remove | Rename | Write}, |
| 134 |
Remove | Rename | Write | recursive, |
| 135 |
}, |
| 136 |
|
| 137 |
{ |
| 138 |
watchpoint.Del, |
| 139 |
[]interface{}{ch[2], Remove}, |
| 140 |
eventDiff{Remove | Rename | Write, Rename | Write}, |
| 141 |
Rename | Write | recursive, |
| 142 |
}, |
| 143 |
|
| 144 |
{ |
| 145 |
watchpoint.Del, |
| 146 |
[]interface{}{ch[3], Rename | recursive}, |
| 147 |
eventDiff{Rename | Write, Write}, |
| 148 |
Write, |
| 149 |
}, |
| 150 |
} |
| 151 |
wp := watchpoint{} |
| 152 |
for i, cas := range cases { |
| 153 |
if diff := call(wp, cas.fn, cas.args); diff != cas.diff { |
| 154 |
t.Errorf("want diff=%v; got %v (i=%d)", cas.diff, diff, i) |
| 155 |
continue |
| 156 |
} |
| 157 |
if total := wp[nil]; total != cas.total { |
| 158 |
t.Errorf("want total=%v; got %v (i=%d)", cas.total, total, i) |
| 159 |
continue |
| 160 |
} |
| 161 |
} |
| 162 |
} |