// TestSML2XML checks the conversion from SML to XML. func TestSML2XML(t *testing.T) { assert := audit.NewTestingAssertion(t, true) in := `{html {head {title A test document}} {body {h1:title A test document} {p:intro:preface The is a simple sentence with an {em emphasized} and a {strong strong} text. We'll see how it renders.} {ul {li:1 It should be nice.} {li:2 It should be error free.} {li:3 It should be fast.} } {! for foo := 0; foo < 42; foo++ { println(foo) } !} }}` builder := sml.NewNodeBuilder() err := sml.ReadSML(strings.NewReader(in), builder) assert.Nil(err) root, err := builder.Root() assert.Nil(err) buf := bytes.NewBufferString("") ctx := sml.NewWriterContext(sml.NewXMLWriter("pre"), buf, true, " ") ctx.Register("li", newLIWriter()) sml.WriteSML(root, ctx) assert.Logf("===== XML =====") assert.Logf(buf.String()) assert.Logf("===== DONE =====") }
// Create a node structure. func createNodeStructure(assert audit.Assertion) sml.Node { builder := sml.NewNodeBuilder() builder.BeginTagNode("root") builder.TextNode("Text A") builder.TextNode("Text B") builder.CommentNode("A first comment.") builder.BeginTagNode("sub-a:1st:important") builder.TextNode("Text A.A") builder.CommentNode("A second comment.") builder.EndTagNode() builder.BeginTagNode("sub-b:2nd") builder.TextNode("Text B.A") builder.BeginTagNode("text") builder.TextNode("Any text with the special characters {, }, and ^.") builder.EndTagNode() builder.EndTagNode() builder.BeginTagNode("sub-c") builder.TextNode("Before raw.") builder.RawNode("func Test(i int) { println(i) }") builder.TextNode("After raw.") builder.EndTagNode() builder.EndTagNode() root, err := builder.Root() assert.Nil(err) return root }
// TestNegativeNodeReading checks the failing reading of nodes. func TestNegativeNodeReading(t *testing.T) { assert := audit.NewTestingAssertion(t, true) text := "{Foo {bar:1 Yadda {test} {} 1} {bar:2 Yadda 2}}" builder := sml.NewNodeBuilder() err := sml.ReadSML(strings.NewReader(text), builder) assert.ErrorMatch(err, `.* cannot read SML document: invalid character after opening at index .*`) }
// TestPositiveNodeReading checks the successful reading of nodes. func TestPositiveNodeReading(t *testing.T) { assert := audit.NewTestingAssertion(t, true) text := "Before! {foo:main {bar:1:first Yadda ^{Test^} 1} {! Raw: }} { ! ^^^ !} {inbetween} {bar:2:last Yadda {Test ^^} 2}} After!" builder := sml.NewNodeBuilder() err := sml.ReadSML(strings.NewReader(text), builder) assert.Nil(err) root, err := builder.Root() assert.Nil(err) assert.Equal(root.Tag(), []string{"foo", "main"}) assert.NotEmpty(root) buf := bytes.NewBufferString("") ctx := sml.NewWriterContext(sml.NewStandardSMLWriter(), buf, true, " ") sml.WriteSML(root, ctx) assert.Logf("===== PARSED SML =====") assert.Logf(buf.String()) assert.Logf("===== DONE =====") }