Browse Source

making tests a little easier to read

Stephen Searles 1 year ago
parent
commit
561b4feee1
2 changed files with 122 additions and 100 deletions
  1. 26
    100
      doc_test.go
  2. 96
    0
      helpers_test.go

+ 26
- 100
doc_test.go View File

@@ -1,10 +1,6 @@
1 1
 package caddyhugo
2 2
 
3 3
 import (
4
-	"encoding/json"
5
-	"io/ioutil"
6
-	"os"
7
-	"os/exec"
8 4
 	"path"
9 5
 	"sync"
10 6
 	"testing"
@@ -13,135 +9,65 @@ import (
13 9
 	"git.stephensearles.com/stephen/acedoc"
14 10
 )
15 11
 
16
-type World struct {
17
-	CH         *CaddyHugo
18
-	BlogFolder string
19
-}
20
-
21
-func (w *World) Clean() {
22
-	if w.BlogFolder != "" {
23
-		os.RemoveAll(w.BlogFolder)
24
-	}
25
-}
26
-
27
-func NewWorld(t *testing.T) *World {
28
-	dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
29
-	if err != nil {
30
-		t.Fatalf("error initializing test environment: %v", err)
31
-	}
32
-
33
-	w := &World{BlogFolder: dir}
34
-
35
-	cmd := exec.Command("hugo", "new", "site", dir)
36
-	cmd.Dir = dir
37
-	out, err := cmd.CombinedOutput()
38
-	if err != nil {
39
-		t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
40
-	}
41
-
42
-	w.CH = &CaddyHugo{}
43
-	w.CH.Setup(dir)
44
-
45
-	return w
46
-}
47
-
48 12
 func TestEdits(t *testing.T) {
49 13
 	w := NewWorld(t)
50 14
 	defer w.Clean()
51 15
 
52 16
 	const title = "sometitle"
53
-	var contentPath = path.Join("content", title+".md")
54 17
 
55
-	w.CH.NewContent(title, "")
18
+	var (
19
+		mtx sync.Mutex
56 20
 
57
-	send := []acedoc.Delta{
58
-		acedoc.Insert(0, 0, "hello"),
59
-		acedoc.Insert(0, 5, " world"),
60
-		acedoc.Insert(0, 11, " world"),
61
-	}
62
-	var mtx sync.Mutex
63
-	received := []acedoc.Delta{}
21
+		contentPath = path.Join("content", title+".md")
22
+
23
+		// we will send these to the doc
24
+		send = []acedoc.Delta{
25
+			acedoc.Insert(0, 0, "hello"),
26
+			acedoc.Insert(0, 5, " world"),
27
+			acedoc.Insert(0, 11, " world"),
28
+		}
29
+
30
+		// we will use this to track what we get out of the doc
31
+		received = []acedoc.Delta{}
32
+	)
33
+
34
+	// prepare a new post
35
+	w.CH.NewContent(title, "")
64 36
 
37
+	// start an edit session
65 38
 	doc, err := w.CH.editSession(contentPath)
66 39
 	if err != nil {
67 40
 		t.Fatal("error creating document client:", err)
68 41
 	}
69 42
 
70
-	doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error {
71
-		// receive some deltas...
43
+	// register an *extra* client that just adds to the received delta slice we're tracking
44
+	c := doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error {
72 45
 		mtx.Lock()
73 46
 		defer mtx.Unlock()
74 47
 		received = append(received, ds...)
75 48
 		return nil
76 49
 	}))
77 50
 
51
+	// make sure we have an edit session
78 52
 	_, ok := w.CH.hasEditSession(contentPath)
79 53
 	if !ok {
80 54
 		t.Fatal("expected there to be an established client")
81 55
 	}
82 56
 
83
-	doc.doc.Apply(send...)
57
+	// push the deltas
58
+	c.PushDeltas(send...)
84 59
 
60
+	// wait...
85 61
 	<-time.After(5 * time.Second)
86 62
 
63
+	// be sure we got the correct number of deltas back
87 64
 	mtx.Lock()
88 65
 	defer mtx.Unlock()
89 66
 	if len(received) != len(send) {
90 67
 		t.Errorf("expected %d deltas, received %d; expected: %v, received: %v", len(send), len(received), send, received)
91 68
 	}
92 69
 
93
-}
94
-
95
-type WebsocketTester struct {
96
-	receivedPointer int
97
-	received        [][]byte
98
-	wroteMessages   []Message
99
-	wroteDeltas     []acedoc.Delta
100
-	mtx             sync.Mutex
101
-}
102
-
103
-// ReadJSON reads the next pending message from the "client" into v
104
-func (ws *WebsocketTester) ReadJSON(v interface{}) error {
105
-	ws.mtx.Lock()
106
-	defer ws.mtx.Unlock()
107
-
108
-	if len(ws.received) <= ws.receivedPointer {
109
-		return nil
110
-	}
111
-
112
-	err := json.Unmarshal(ws.received[ws.receivedPointer], v)
113
-	ws.receivedPointer++
114
-	return err
115
-}
116
-
117
-// WriteJSON "sends" a message, v, to the "client"
118
-func (ws *WebsocketTester) WriteJSON(v interface{}) error {
119
-	ws.mtx.Lock()
120
-	defer ws.mtx.Unlock()
121
-
122
-	m, ok := v.(Message)
123
-	if !ok {
124
-		panic("wrong type written to WebsocketTester")
125
-	}
126
-
127
-	ws.wroteMessages = append(ws.wroteMessages, m)
128
-	ws.wroteDeltas = append(ws.wroteDeltas, m.Deltas...)
129
-
130
-	return nil
131
-}
132
-
133
-// ReceiveJSON queues a message to be sent to the client
134
-func (ws *WebsocketTester) ReceiveJSON(v interface{}) error {
135
-	ws.mtx.Lock()
136
-	defer ws.mtx.Unlock()
137
-
138
-	out, err := json.Marshal(v)
139
-	if err != nil {
140
-		return err
141
-	}
142
-
143
-	ws.received = append(ws.received, out)
144
-	return nil
70
+	t.Log(w.CH.Contents())
145 71
 }
146 72
 
147 73
 func TestDeltasSingle(t *testing.T) {

+ 96
- 0
helpers_test.go View File

@@ -0,0 +1,96 @@
1
+package caddyhugo
2
+
3
+import (
4
+	"encoding/json"
5
+	"io/ioutil"
6
+	"os"
7
+	"os/exec"
8
+	"sync"
9
+	"testing"
10
+
11
+	"git.stephensearles.com/stephen/acedoc"
12
+)
13
+
14
+type World struct {
15
+	CH         *CaddyHugo
16
+	BlogFolder string
17
+}
18
+
19
+func (w *World) Clean() {
20
+	if w.BlogFolder != "" {
21
+		os.RemoveAll(w.BlogFolder)
22
+	}
23
+}
24
+
25
+func NewWorld(t *testing.T) *World {
26
+	dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
27
+	if err != nil {
28
+		t.Fatalf("error initializing test environment: %v", err)
29
+	}
30
+
31
+	w := &World{BlogFolder: dir}
32
+
33
+	cmd := exec.Command("hugo", "new", "site", dir)
34
+	cmd.Dir = dir
35
+	out, err := cmd.CombinedOutput()
36
+	if err != nil {
37
+		t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
38
+	}
39
+
40
+	w.CH = &CaddyHugo{}
41
+	w.CH.Setup(dir)
42
+
43
+	return w
44
+}
45
+
46
+type WebsocketTester struct {
47
+	receivedPointer int
48
+	received        [][]byte
49
+	wroteMessages   []Message
50
+	wroteDeltas     []acedoc.Delta
51
+	mtx             sync.Mutex
52
+}
53
+
54
+// ReadJSON reads the next pending message from the "client" into v
55
+func (ws *WebsocketTester) ReadJSON(v interface{}) error {
56
+	ws.mtx.Lock()
57
+	defer ws.mtx.Unlock()
58
+
59
+	if len(ws.received) <= ws.receivedPointer {
60
+		return nil
61
+	}
62
+
63
+	err := json.Unmarshal(ws.received[ws.receivedPointer], v)
64
+	ws.receivedPointer++
65
+	return err
66
+}
67
+
68
+// WriteJSON "sends" a message, v, to the "client"
69
+func (ws *WebsocketTester) WriteJSON(v interface{}) error {
70
+	ws.mtx.Lock()
71
+	defer ws.mtx.Unlock()
72
+
73
+	m, ok := v.(Message)
74
+	if !ok {
75
+		panic("wrong type written to WebsocketTester")
76
+	}
77
+
78
+	ws.wroteMessages = append(ws.wroteMessages, m)
79
+	ws.wroteDeltas = append(ws.wroteDeltas, m.Deltas...)
80
+
81
+	return nil
82
+}
83
+
84
+// ReceiveJSON queues a message to be sent to the client
85
+func (ws *WebsocketTester) ReceiveJSON(v interface{}) error {
86
+	ws.mtx.Lock()
87
+	defer ws.mtx.Unlock()
88
+
89
+	out, err := json.Marshal(v)
90
+	if err != nil {
91
+		return err
92
+	}
93
+
94
+	ws.received = append(ws.received, out)
95
+	return nil
96
+}

Loading…
Cancel
Save