From f02161522c4dd53241d234b0e42b49ffadb90134 Mon Sep 17 00:00:00 2001 From: dchenk Date: Fri, 5 Jan 2018 12:20:21 -0800 Subject: [PATCH] Added and improved tests, fixed background format --- format_state.go | 2 +- format_state_test.go | 28 ++++++++++++++++++---------- inline_formats.go | 4 ++-- render_test.go | 41 ++++++++++++++++++++--------------------- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/format_state.go b/format_state.go index 4687610..3776ba5 100644 --- a/format_state.go +++ b/format_state.go @@ -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 { diff --git a/format_state_test.go b/format_state_test.go index 78bc760..d700581 100644 --- a/format_state_test.go +++ b/format_state_test.go @@ -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{"", ""} + want := []string{"", ""} 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() diff --git a/inline_formats.go b/inline_formats.go index 0effae7..a8a7418 100644 --- a/inline_formats.go +++ b/inline_formats.go @@ -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) diff --git a/render_test.go b/render_test.go index 15b2d48..7d339df 100644 --- a/render_test.go +++ b/render_test.go @@ -2,7 +2,6 @@ package quill import ( "bytes" - "fmt" "io/ioutil" "testing" ) @@ -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 + }