github.com/blugnu/test-report@v0.1.3/writer_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/blugnu/test"
     9  )
    10  
    11  func TestWriter(t *testing.T) {
    12  	// ARRANGE
    13  	testcases := []struct {
    14  		scenario string
    15  		exec     func(t *testing.T)
    16  	}{
    17  		{scenario: "in error state",
    18  			exec: func(t *testing.T) {
    19  				// ARRANGE
    20  				buf := bytes.NewBuffer(nil)
    21  				w := &IndentWriter{
    22  					output: buf,
    23  					error:  errors.New("writer error "),
    24  				}
    25  
    26  				// ACT
    27  				w.Write("this should not be written")
    28  
    29  				// ASSERT
    30  				test.That(t, buf.Bytes()).IsNil()
    31  			},
    32  		},
    33  		{scenario: "WriteXMLElement bare",
    34  			exec: func(t *testing.T) {
    35  				// ARRANGE
    36  				buf := bytes.NewBuffer(nil)
    37  				w := &IndentWriter{output: buf}
    38  
    39  				// ACT
    40  				w.WriteXMLElement(func() { w.WriteLn("content") }, "tag")
    41  
    42  				// ASSERT
    43  				test.That(t, buf.String()).Equals("<tag>\n  content\n</tag>\n")
    44  			},
    45  		},
    46  		{scenario: "WriteXMLElement with attributes",
    47  			exec: func(t *testing.T) {
    48  				// ARRANGE
    49  				buf := bytes.NewBuffer(nil)
    50  				w := &IndentWriter{output: buf}
    51  
    52  				// ACT
    53  				w.WriteXMLElement(func() { w.WriteLn("content") }, "tag", "attr1='1'", "attr2")
    54  
    55  				// ASSERT
    56  				test.That(t, buf.String()).Equals("<tag attr1='1' attr2>\n  content\n</tag>\n")
    57  			},
    58  		},
    59  	}
    60  	for _, tc := range testcases {
    61  		t.Run(tc.scenario, func(t *testing.T) {
    62  			tc.exec(t)
    63  		})
    64  	}
    65  }