alpha/model_test.go

211 lines
4.4 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"testing"
)
func TestPage(t *testing.T) {
i := &Index{
Children: map[string]Index{
"foo": {
Children: map[string]Index{},
Pages: map[string]Page{
"bar": {
Contents: []byte("bar"),
},
"index": {
Contents: []byte("quuz"),
},
},
},
"bar": {
Children: map[string]Index{},
Pages: map[string]Page{
"index": {
Contents: []byte("quuz"),
},
"goof": {
Contents: []byte("quuz2"),
},
},
},
},
Pages: map[string]Page{
"index": {
Contents: []byte("indextest"),
},
"foo": {
Contents: []byte("rootfoo"),
},
},
}
p, err := i.Page("")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("indextest")) {
t.Logf("expected contents to be 'indextest', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("/")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("indextest")) {
t.Logf("expected contents to be 'indextest', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("foo/bar")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("bar")) {
t.Logf("expected contents to be 'bar', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("/foo/bar")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("bar")) {
t.Logf("expected contents to be 'bar', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("foo")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("rootfoo")) {
t.Logf("expected contents to be 'rootfoo', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("foo/")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("rootfoo")) {
t.Logf("expected contents to be 'rootfoo', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("bar/")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("quuz")) {
t.Logf("expected contents to be 'quuz', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("bar/goof")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("quuz2")) {
t.Logf("expected contents to be 'quuz2', received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("foo/quuz")
if err == nil {
t.Logf("expected err, received nil err")
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("")) {
t.Logf("expected no content, received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("quuz/bar")
if err == nil {
t.Logf("expected err, received nil err")
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("")) {
t.Logf("expected no content, received '%v'", p.Contents)
t.Fail()
}
p, err = i.Page("//////////")
if err == nil {
t.Logf("expected err, received nil err")
t.Fail()
}
if !bytes.Equal(p.Contents, []byte("")) {
t.Logf("expected no content, received '%v'", p.Contents)
t.Fail()
}
}
func TestSave(t *testing.T) {
i := &Index{}
2022-05-23 01:31:23 +00:00
err := i.Save("/", &Page{Title: "invalid"})
if err == nil {
t.Logf("expected err, received nil")
}
_, err = i.Page("/")
if err == nil {
t.Logf("expected err, received nil")
t.Fail()
}
err = i.Save("foo", &Page{Title: "fooroot"})
if err != nil {
t.Logf("expected no err, received %v", err)
}
p, err := i.Page("foo")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if p.Title != "fooroot" {
t.Logf("expected title to be 'fooroot', received '%v'", p.Title)
t.Fail()
}
2022-05-23 01:31:23 +00:00
err = i.Save("/foo/", &Page{Title: "fooroot2"})
if err != nil {
t.Logf("expected no err, received %v", err)
}
p, err = i.Page("foo")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Fail()
}
if p.Title != "fooroot2" {
t.Logf("expected title to be 'fooroot2', received '%v'", p.Title)
t.Fail()
}
err = i.Save("bar/baz", &Page{Title: "quuz"})
if err != nil {
t.Logf("expected no err, received %v", err)
}
p, err = i.Page("bar/baz")
if err != nil {
t.Logf("expected no err, received err: %v", err)
t.Logf("%+v", i)
t.Fail()
}
if p.Title != "quuz" {
t.Logf("expected title to be 'quuz', received '%v'", p.Title)
t.Fail()
}
}