package cnm import ( "bytes" "testing" ) var writeTests = map[string]*Document{ "": &Document{}, "title\n\tfoo bar\n": &Document{ Title: "foo bar", }, "title\n\tfoo bar baz\n" + "links\n\tqwe asd\n\tzxc 123\n\tfoo\n" + "site\n\tfoo\n\t\tbar\n\t\tbaz/quux\n\t\t\t123\n\ttest\n": &Document{ Title: "foo bar baz", Links: []Link{ Link{"qwe", "asd", ""}, Link{"zxc", "123", ""}, Link{"foo", "", ""}, }, Site: Site{ Children: []Site{ Site{ Path: "foo", Children: []Site{ Site{Path: "bar"}, Site{ Path: "baz/quux", Children: []Site{ Site{Path: "123"}, }, }, }, }, Site{Path: "test"}, }, }, }, `title Test document content section Test section text This is \n just a text pre t e s t raw text/plain of various \n features section of the table header text Column 1 text Column 2 row text CNM text document format row section list text ipsum list ordered list unordered text dolor sit amet embed text/cnm cnp://example.com/ thing whatever `: &Document{ Title: "Test document", Content: &ContentBlock{ name: "content", args: nil, children: []Block{ &SectionBlock{ContentBlock{ name: "section", args: []string{"Test", "section"}, children: []Block{ &TextBlock{ Contents: TextPlainContents{[]string{ "This is \n just a", }}, }, &TextBlock{ Format: "pre", Contents: TextPreContents{ " t e \n s t \n\t", }, }, &RawBlock{ Syntax: "text/plain", Contents: "of various \\n features", }, &SectionBlock{ContentBlock{ name: "section", args: []string{"of the"}, children: []Block{ &TableBlock{[]Block{ &HeaderBlock{ContentBlock{ name: "header", args: []string{}, children: []Block{ &TextBlock{ Contents: TextPlainContents{[]string{ "Column 1", }}, }, &TextBlock{ Contents: TextPlainContents{[]string{ "Column 2", }}, }, }, }}, &RowBlock{ContentBlock{ name: "row", args: []string{}, children: []Block{ &TextBlock{ Contents: TextPlainContents{[]string{ "CNM", }}, }, &TextBlock{ Contents: TextPlainContents{[]string{ "document", "format", }}, }, }, }}, &RowBlock{ContentBlock{ name: "row", args: []string{""}, children: []Block{ &SectionBlock{ContentBlock{ name: "section", args: []string{"", "", ""}, }}, &ListBlock{ContentBlock{ name: "list", args: nil, children: []Block{ &TextBlock{ Contents: TextPlainContents{[]string{ "ipsum", }}, }, &ListBlock{ContentBlock{ name: "list", args: []string{"ordered"}, children: []Block{ &ListBlock{ContentBlock{ name: "list", args: []string{"unordered"}, children: []Block{ &TextBlock{ Contents: TextPlainContents{[]string{ "dolor", "sit amet", }}, }, }, }}, }, }}, }, }}, }, }}, }}, }, }}, }, }}, &EmbedBlock{ Type: "text/cnm", URL: "cnp://example.com/", Description: "thing whatever", }, }, }, }, } func TestWrite(t *testing.T) { for k, v := range writeTests { t.Run(k, func(t *testing.T) { var buf bytes.Buffer err := v.Write(&buf) if err != nil { t.Fatalf("Write error: %v", err) } w := buf.String() t.Log("====================") t.Log("expected:\n" + k) t.Log("--------------------") t.Log(" got:\n" + w) t.Log("====================") if k != w { t.Fatal("Write: output did not match expected document") } }) } }