making tests a little easier to read

pull/16/head
Stephen Searles 7 years ago
parent 3d57e589e6
commit 561b4feee1
  1. 126
      doc_test.go
  2. 96
      helpers_test.go

@ -1,10 +1,6 @@
package caddyhugo package caddyhugo
import ( import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path" "path"
"sync" "sync"
"testing" "testing"
@ -13,135 +9,65 @@ import (
"git.stephensearles.com/stephen/acedoc" "git.stephensearles.com/stephen/acedoc"
) )
type World struct {
CH *CaddyHugo
BlogFolder string
}
func (w *World) Clean() {
if w.BlogFolder != "" {
os.RemoveAll(w.BlogFolder)
}
}
func NewWorld(t *testing.T) *World {
dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
if err != nil {
t.Fatalf("error initializing test environment: %v", err)
}
w := &World{BlogFolder: dir}
cmd := exec.Command("hugo", "new", "site", dir)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
}
w.CH = &CaddyHugo{}
w.CH.Setup(dir)
return w
}
func TestEdits(t *testing.T) { func TestEdits(t *testing.T) {
w := NewWorld(t) w := NewWorld(t)
defer w.Clean() defer w.Clean()
const title = "sometitle" const title = "sometitle"
var contentPath = path.Join("content", title+".md")
w.CH.NewContent(title, "") var (
mtx sync.Mutex
send := []acedoc.Delta{ contentPath = path.Join("content", title+".md")
acedoc.Insert(0, 0, "hello"),
acedoc.Insert(0, 5, " world"), // we will send these to the doc
acedoc.Insert(0, 11, " world"), send = []acedoc.Delta{
} acedoc.Insert(0, 0, "hello"),
var mtx sync.Mutex acedoc.Insert(0, 5, " world"),
received := []acedoc.Delta{} acedoc.Insert(0, 11, " world"),
}
// we will use this to track what we get out of the doc
received = []acedoc.Delta{}
)
// prepare a new post
w.CH.NewContent(title, "")
// start an edit session
doc, err := w.CH.editSession(contentPath) doc, err := w.CH.editSession(contentPath)
if err != nil { if err != nil {
t.Fatal("error creating document client:", err) t.Fatal("error creating document client:", err)
} }
doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error { // register an *extra* client that just adds to the received delta slice we're tracking
// receive some deltas... c := doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error {
mtx.Lock() mtx.Lock()
defer mtx.Unlock() defer mtx.Unlock()
received = append(received, ds...) received = append(received, ds...)
return nil return nil
})) }))
// make sure we have an edit session
_, ok := w.CH.hasEditSession(contentPath) _, ok := w.CH.hasEditSession(contentPath)
if !ok { if !ok {
t.Fatal("expected there to be an established client") t.Fatal("expected there to be an established client")
} }
doc.doc.Apply(send...) // push the deltas
c.PushDeltas(send...)
// wait...
<-time.After(5 * time.Second) <-time.After(5 * time.Second)
// be sure we got the correct number of deltas back
mtx.Lock() mtx.Lock()
defer mtx.Unlock() defer mtx.Unlock()
if len(received) != len(send) { if len(received) != len(send) {
t.Errorf("expected %d deltas, received %d; expected: %v, received: %v", len(send), len(received), send, received) t.Errorf("expected %d deltas, received %d; expected: %v, received: %v", len(send), len(received), send, received)
} }
} t.Log(w.CH.Contents())
type WebsocketTester struct {
receivedPointer int
received [][]byte
wroteMessages []Message
wroteDeltas []acedoc.Delta
mtx sync.Mutex
}
// ReadJSON reads the next pending message from the "client" into v
func (ws *WebsocketTester) ReadJSON(v interface{}) error {
ws.mtx.Lock()
defer ws.mtx.Unlock()
if len(ws.received) <= ws.receivedPointer {
return nil
}
err := json.Unmarshal(ws.received[ws.receivedPointer], v)
ws.receivedPointer++
return err
}
// WriteJSON "sends" a message, v, to the "client"
func (ws *WebsocketTester) WriteJSON(v interface{}) error {
ws.mtx.Lock()
defer ws.mtx.Unlock()
m, ok := v.(Message)
if !ok {
panic("wrong type written to WebsocketTester")
}
ws.wroteMessages = append(ws.wroteMessages, m)
ws.wroteDeltas = append(ws.wroteDeltas, m.Deltas...)
return nil
}
// ReceiveJSON queues a message to be sent to the client
func (ws *WebsocketTester) ReceiveJSON(v interface{}) error {
ws.mtx.Lock()
defer ws.mtx.Unlock()
out, err := json.Marshal(v)
if err != nil {
return err
}
ws.received = append(ws.received, out)
return nil
} }
func TestDeltasSingle(t *testing.T) { func TestDeltasSingle(t *testing.T) {

@ -0,0 +1,96 @@
package caddyhugo
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"sync"
"testing"
"git.stephensearles.com/stephen/acedoc"
)
type World struct {
CH *CaddyHugo
BlogFolder string
}
func (w *World) Clean() {
if w.BlogFolder != "" {
os.RemoveAll(w.BlogFolder)
}
}
func NewWorld(t *testing.T) *World {
dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
if err != nil {
t.Fatalf("error initializing test environment: %v", err)
}
w := &World{BlogFolder: dir}
cmd := exec.Command("hugo", "new", "site", dir)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
}
w.CH = &CaddyHugo{}
w.CH.Setup(dir)
return w
}
type WebsocketTester struct {
receivedPointer int
received [][]byte
wroteMessages []Message
wroteDeltas []acedoc.Delta
mtx sync.Mutex
}
// ReadJSON reads the next pending message from the "client" into v
func (ws *WebsocketTester) ReadJSON(v interface{}) error {
ws.mtx.Lock()
defer ws.mtx.Unlock()
if len(ws.received) <= ws.receivedPointer {
return nil
}
err := json.Unmarshal(ws.received[ws.receivedPointer], v)
ws.receivedPointer++
return err
}
// WriteJSON "sends" a message, v, to the "client"
func (ws *WebsocketTester) WriteJSON(v interface{}) error {
ws.mtx.Lock()
defer ws.mtx.Unlock()
m, ok := v.(Message)
if !ok {
panic("wrong type written to WebsocketTester")
}
ws.wroteMessages = append(ws.wroteMessages, m)
ws.wroteDeltas = append(ws.wroteDeltas, m.Deltas...)
return nil
}
// ReceiveJSON queues a message to be sent to the client
func (ws *WebsocketTester) ReceiveJSON(v interface{}) error {
ws.mtx.Lock()
defer ws.mtx.Unlock()
out, err := json.Marshal(v)
if err != nil {
return err
}
ws.received = append(ws.received, out)
return nil
}
Loading…
Cancel
Save