github.com/golazy/golazy@v0.0.7-0.20221012133820-968fe65a0b65/lazyview/nodes/lazyview_test.go (about)

     1  package nodes
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func ExampleNewElement() {
    11  	Beautify = false
    12  	content := NewElement("html", NewAttr("lang", "en"),
    13  		NewElement("head",
    14  			NewElement("title", "Mi pagina")),
    15  		NewElement("body",
    16  			NewElement("h1", "This is my page")),
    17  	)
    18  
    19  	content.WriteTo(os.Stdout)
    20  
    21  	// Output: <!DOCTYPE html><html lang=en><head><title>Mi pagina</title><body><h1>This is my page</h1>
    22  }
    23  
    24  // ttr test render tag
    25  func trt(t *testing.T, what io.WriterTo, expectation string) {
    26  	t.Helper()
    27  	buf := &bytes.Buffer{}
    28  	n, err := what.WriteTo(buf)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  	if n != int64(len(buf.Bytes())) {
    33  		t.Errorf("Size missmatch. Got %d but was %d", n, len(buf.Bytes()))
    34  	}
    35  	if expectation != buf.String() {
    36  		t.Errorf("Expecting %q Got %q", expectation, buf.String())
    37  	}
    38  }
    39  
    40  func TestRendererRenderTo(t *testing.T) {
    41  	trt(t, NewElement("html"), "<!DOCTYPE html><html>")
    42  	trt(t, NewElement("html", NewAttr("lang", "en")), `<!DOCTYPE html><html lang=en>`)
    43  	trt(t, NewElement("meta"), `<meta>`)
    44  }