57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package notes
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"local/notes-server/config"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestComment(t *testing.T) {
|
|
d, err := ioutil.TempDir(os.TempDir(), "testComment.*")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(d)
|
|
f, err := ioutil.TempFile(d, "testFile.*")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.Write([]byte(`
|
|
hello
|
|
world
|
|
# i have a heading
|
|
## i have a subber heading
|
|
### i have a[heading with](http://google.com) a hyperlink
|
|
## *I think this heading is in italics*
|
|
`))
|
|
f.Close()
|
|
fpath := path.Join("/comment", strings.TrimPrefix(f.Name(), d))
|
|
config.Root = d
|
|
n := &Notes{}
|
|
t.Logf("d=%s, fpath=%s", d, fpath)
|
|
|
|
if err := n.Comment("/comment/a", 5, "a"); err == nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if err := n.Comment(fpath, -1, "illegal line no"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if err := n.Comment(fpath, 10000, "big line no"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if err := n.Comment(fpath, 0, "first_line_OK"); err != nil {
|
|
t.Error(err)
|
|
} else if b, err := ioutil.ReadFile(f.Name()); err != nil {
|
|
t.Error(err)
|
|
} else if !bytes.Contains(b, []byte("> *first_line_OK*\n")) {
|
|
t.Errorf("%s", b)
|
|
}
|
|
}
|