package xml // import "github.com/tdewolff/minify/xml" import ( "bytes" "fmt" "os" "regexp" "testing" "github.com/tdewolff/minify" "github.com/tdewolff/test" ) func TestXML(t *testing.T) { xmlTests := []struct { xml string expected string }{ {"", ""}, {"x", "x"}, {"x", "x"}, {"x\ny", "x\ny"}, {" ", "a"}, {"a", "a"}, {"", ""}, {"", ""}, {" ", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"text text", "text text"}, {"text\n\ntext", "text\ntext"}, {"", ""}, // bad formatted, doctype must be uppercase and html must have attribute value {"\n\n", ""}, {"", ""}, {`cats and dogs `, `cats and dogs`}, {` test test `, `
test test
`}, {"text\n\ntext", "text\ntext"}, {"text\ntext text", "text\ntext text"}, {"\n\n", "\n"}, {"", ""}, {" ", " "}, {" ", " x "}, {" ", ""}, } m := minify.New() xmlMinifier := &Minifier{KeepWhitespace: true} for _, tt := range xmlTests { t.Run(tt.xml, func(t *testing.T) { r := bytes.NewBufferString(tt.xml) w := &bytes.Buffer{} err := xmlMinifier.Minify(m, w, r, nil) test.Minify(t, tt.xml, err, w.String(), tt.expected) }) } } func TestReaderErrors(t *testing.T) { r := test.NewErrorReader(0) w := &bytes.Buffer{} m := minify.New() err := Minify(m, w, r, nil) test.T(t, err, test.ErrPlain, "return error at first read") } func TestWriterErrors(t *testing.T) { errorTests := []struct { xml string n []int }{ {``, []int{0}}, {``, []int{0, 1}}, {``, []int{0, 1, 2, 3, 4, 8, 9}}, {``, []int{1}}, {``, []int{0}}, {``, []int{1}}, {``, []int{0}}, {`text`, []int{0}}, } m := minify.New() for _, tt := range errorTests { for _, n := range tt.n { t.Run(fmt.Sprint(tt.xml, " ", tt.n), func(t *testing.T) { r := bytes.NewBufferString(tt.xml) w := test.NewErrorWriter(n) err := Minify(m, w, r, nil) test.T(t, err, test.ErrPlain) }) } } } //////////////////////////////////////////////////////////////// func ExampleMinify() { m := minify.New() m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), Minify) if err := m.Minify("text/xml", os.Stdout, os.Stdin); err != nil { panic(err) } }