// 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 =====") }
// TestNegativeTreeReading checks the failing reading of trees. func TestNegativeTreeReading(t *testing.T) { assert := audit.NewTestingAssertion(t, true) text := "{foo {bar 1}{bar 2}}" builder := sml.NewKeyStringValueTreeBuilder() err := sml.ReadSML(strings.NewReader(text), builder) assert.ErrorMatch(err, `.* node has multiple values`) }
// 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 .*`) }
// TestPositiveTreeReading checks the successful reading of trees. func TestPositiveTreeReading(t *testing.T) { assert := audit.NewTestingAssertion(t, true) text := "{config {foo 1}{bar 2}{yadda {up down}{down up}}}" builder := sml.NewKeyStringValueTreeBuilder() err := sml.ReadSML(strings.NewReader(text), builder) assert.Nil(err) tree, err := builder.Tree() assert.Nil(err) assert.Logf("%v", tree) }
// Read reads the SML source of the configuration from a // reader, parses it, and returns the configuration instance. func Read(source io.Reader) (Configuration, error) { builder := sml.NewKeyStringValueTreeBuilder() err := sml.ReadSML(source, builder) if err != nil { return nil, errors.Annotate(err, ErrIllegalSourceFormat, errorMessages) } tree, err := builder.Tree() if err != nil { return nil, errors.Annotate(err, ErrIllegalSourceFormat, errorMessages) } if err := tree.At("config").Error(); err != nil { return nil, errors.Annotate(err, ErrIllegalSourceFormat, errorMessages) } return &configuration{tree}, nil }
// 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 =====") }