File: watchpoint_test.go

package info (click to toggle)
golang-github-syncthing-notify 0.0~git20180806.b76b458-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 440 kB
  • sloc: makefile: 3
file content (162 lines) | stat: -rw-r--r-- 3,689 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

package notify

import (
	"fmt"
	"reflect"
	"testing"
)

func call(wp watchpoint, fn interface{}, args []interface{}) eventDiff {
	vals := []reflect.Value{reflect.ValueOf(wp)}
	for _, arg := range args {
		vals = append(vals, reflect.ValueOf(arg))
	}
	res := reflect.ValueOf(fn).Call(vals)
	if n := len(res); n != 1 {
		panic(fmt.Sprintf("unexpected len(res)=%d", n))
	}
	diff, ok := res[0].Interface().(eventDiff)
	if !ok {
		panic(fmt.Sprintf("want typeof(diff)=EventDiff; got %T", res[0].Interface()))
	}
	return diff
}

func TestWatchpoint(t *testing.T) {
	ch := NewChans(5)
	all := All | recursive
	cases := [...]struct {
		fn    interface{}
		args  []interface{}
		diff  eventDiff
		total Event
	}{
		// i=0
		{
			watchpoint.Add,
			[]interface{}{ch[0], Remove},
			eventDiff{0, Remove},
			Remove,
		},
		// i=1
		{
			watchpoint.Add,
			[]interface{}{ch[1], Create | Remove | recursive},
			eventDiff{Remove, Remove | Create},
			Create | Remove | recursive,
		},
		// i=2
		{
			watchpoint.Add,
			[]interface{}{ch[2], Create | Rename},
			eventDiff{Create | Remove, Create | Remove | Rename},
			Create | Remove | Rename | recursive,
		},
		// i=3
		{
			watchpoint.Add,
			[]interface{}{ch[0], Write | recursive},
			eventDiff{Create | Remove | Rename, Create | Remove | Rename | Write},
			Create | Remove | Rename | Write | recursive,
		},
		// i=4
		{
			watchpoint.Add,
			[]interface{}{ch[2], Remove | recursive},
			none,
			Create | Remove | Rename | Write | recursive,
		},
		// i=5
		{
			watchpoint.Del,
			[]interface{}{ch[0], all},
			eventDiff{Create | Remove | Rename | Write, Create | Remove | Rename},
			Create | Remove | Rename | recursive,
		},
		// i=6
		{
			watchpoint.Del,
			[]interface{}{ch[2], all},
			eventDiff{Create | Remove | Rename, Create | Remove},
			Create | Remove | recursive,
		},
		// i=7
		{
			watchpoint.Add,
			[]interface{}{ch[3], Create | Remove},
			none,
			Create | Remove | recursive,
		},
		// i=8
		{
			watchpoint.Del,
			[]interface{}{ch[1], all},
			none,
			Create | Remove,
		},
		// i=9
		{
			watchpoint.Add,
			[]interface{}{ch[3], recursive | Write},
			eventDiff{Create | Remove, Create | Remove | Write},
			Create | Remove | Write | recursive,
		},
		// i=10
		{
			watchpoint.Del,
			[]interface{}{ch[3], Create},
			eventDiff{Create | Remove | Write, Remove | Write},
			Remove | Write | recursive,
		},
		// i=11
		{
			watchpoint.Add,
			[]interface{}{ch[3], Create | Rename},
			eventDiff{Remove | Write, Create | Remove | Rename | Write},
			Create | Remove | Rename | Write | recursive,
		},
		// i=12
		{
			watchpoint.Add,
			[]interface{}{ch[2], Remove | Write},
			none,
			Create | Remove | Rename | Write | recursive,
		},
		// i=13
		{
			watchpoint.Del,
			[]interface{}{ch[3], Create | Remove | Write},
			eventDiff{Create | Remove | Rename | Write, Remove | Rename | Write},
			Remove | Rename | Write | recursive,
		},
		// i=14
		{
			watchpoint.Del,
			[]interface{}{ch[2], Remove},
			eventDiff{Remove | Rename | Write, Rename | Write},
			Rename | Write | recursive,
		},
		// i=15
		{
			watchpoint.Del,
			[]interface{}{ch[3], Rename | recursive},
			eventDiff{Rename | Write, Write},
			Write,
		},
	}
	wp := watchpoint{}
	for i, cas := range cases {
		if diff := call(wp, cas.fn, cas.args); diff != cas.diff {
			t.Errorf("want diff=%v; got %v (i=%d)", cas.diff, diff, i)
			continue
		}
		if total := wp[nil]; total != cas.total {
			t.Errorf("want total=%v; got %v (i=%d)", cas.total, total, i)
			continue
		}
	}
}