Skip to content

Commit

Permalink
Added and improved tests, fixed background format
Browse files Browse the repository at this point in the history
  • Loading branch information
dchenk committed Jan 5, 2018
1 parent 7cb33b5 commit f021615
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion format_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (fs *formatState) closePrevious(buf *bytes.Buffer, o *Op, doingBlock bool)

}

// pop removes the last format state from the list of open states.
// pop removes the last format from the state of currently open formats.
func (fs *formatState) pop(buf *bytes.Buffer) {
indx := len(fs.open) - 1
if fs.open[indx].wrap {
Expand Down
28 changes: 18 additions & 10 deletions format_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,37 @@ func TestFormatState_add(t *testing.T) {

func TestFormatState_closePrevious(t *testing.T) {

o := &Op{
Data: "stuff",
Type: "text",
// no attributes set
}
o1 := blankOp()
o1.Attrs["italic"] = "y"
o1.Attrs["bold"] = "y"

o2 := blankOp()
o2.Attrs["background"] = "#e0e0e0"
o2.Attrs["italic"] = "y"

cases := []formatState{
{[]*Format{
{"em", Tag, false, false, "", "", o.getFormatter("italic", nil)},
{"strong", Tag, false, false, "", "", o.getFormatter("bold", nil)},
{"em", Tag, false, false, "", "", o1.getFormatter("italic", nil)},
{"strong", Tag, false, false, "", "", o1.getFormatter("bold", nil)},
}},
{[]*Format{
{"background-color:#e0e0e0;", Style, false, false, "", "", o2.getFormatter("background", nil)},
{"em", Tag, false, false, "", "", o2.getFormatter("italic", nil)},
}},
}

want := []string{"</strong></em>", "</strong></li></ul>"}
want := []string{"</strong></em>", "</em></span>"}

buf := new(bytes.Buffer)

for i := range cases {

o := blankOp()

cases[i].closePrevious(buf, o, false)
got := buf.String()
if got != want[i] {
t.Errorf("closed formats wrong (index %d); wanted %q; got %q\n", i, want[i], got)
if got != want[i] || len(cases[i].open) != 0 {
t.Errorf("closed formats wrong (index %d); wanted %q; got %q", i, want[i], got)
}

buf.Reset()
Expand Down
4 changes: 2 additions & 2 deletions inline_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func (bf *bkgFormat) Fmt() *Format {
}
}

func (*bkgFormat) HasFormat(o *Op) bool {
return o.HasAttr("background")
func (bf *bkgFormat) HasFormat(o *Op) bool {
return o.Attrs["background"] == bf.c
}

// script (sup and sub)
Expand Down
41 changes: 20 additions & 21 deletions render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package quill

import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)
Expand Down Expand Up @@ -61,28 +60,28 @@ func TestRender(t *testing.T) {
pairNames := []string{"ops1", "nested", "ordering", "list1", "list2", "list3", "list4", "indent"}

for _, n := range pairNames {
if err := testPair(n+".json", n+".html"); err != nil {
t.Errorf("(name: %s) %s", n, err)

ops, err := ioutil.ReadFile("./tests/" + n + ".json")
if err != nil {
t.Errorf("could not read %s.json; %s", n, err)
t.FailNow()
}
}

}
html, err := ioutil.ReadFile("./tests/" + n + ".html")
if err != nil {
t.Errorf("could not read %s.html; %s", n, err)
t.FailNow()
}

got, err := Render(ops)
if err != nil {
t.Errorf("error rendering; %s", err)
}

if !bytes.Equal(html, got) {
t.Errorf("bad rendering (name: %s):\nwanted: \n%s\ngot: \n%s", n, html, got)
}

func testPair(opsFile, htmlFile string) error {
ops, err := ioutil.ReadFile("./tests/" + opsFile)
if err != nil {
return fmt.Errorf("could not read %s; %s", opsFile, err)
}
html, err := ioutil.ReadFile("./tests/" + htmlFile)
if err != nil {
return fmt.Errorf("could not read %s; %s", htmlFile, err)
}
got, err := Render(ops)
if err != nil {
return fmt.Errorf("error rendering; %s", err)
}
if !bytes.Equal(html, got) {
return fmt.Errorf("bad rendering; \nwanted: \n%s\ngot: \n%s", html, got)
}
return nil

}

0 comments on commit f021615

Please sign in to comment.