package svg // import "github.com/tdewolff/minify/svg" import ( "bytes" "fmt" "io" "os" "testing" "github.com/tdewolff/minify" "github.com/tdewolff/minify/css" "github.com/tdewolff/test" ) func TestSVG(t *testing.T) { svgTests := []struct { svg string expected string }{ {``, ``}, {``, ``}, {` ]>`, ` ]>`}, {``, ``}, {``, ``}, {``, ``}, {``, ``}, {``, ``}, {``, ``}, {``, ``}, // from SVGO {``, ``}, {``, ``}, // #45 {``, ``}, // #117 // go fuzz {`<0 d=09e9.6e-9e0`, `<0 d=""`}, {` a > b {} `, ``}, {``, ``}, {``, ``}, {``, []int{2}}, {``, []int{0}}, {``, []int{0}}, {``, []int{0, 1, 2, 3, 4, 5}}, {``, []int{1}}, {`x`, []int{1, 3}}, {`x`, []int{3}}, } m := minify.New() for _, tt := range errorTests { for _, n := range tt.n { t.Run(fmt.Sprint(tt.svg, " ", tt.n), func(t *testing.T) { r := bytes.NewBufferString(tt.svg) w := test.NewErrorWriter(n) err := Minify(m, w, r, nil) test.T(t, err, test.ErrPlain) }) } } } func TestMinifyErrors(t *testing.T) { errorTests := []struct { svg string err error }{ {``, test.ErrPlain}, {``, test.ErrPlain}, {``, test.ErrPlain}, } m := minify.New() m.AddFunc("text/css", func(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error { return test.ErrPlain }) for _, tt := range errorTests { t.Run(tt.svg, func(t *testing.T) { r := bytes.NewBufferString(tt.svg) w := &bytes.Buffer{} err := Minify(m, w, r, nil) test.T(t, err, tt.err) }) } } //////////////////////////////////////////////////////////////// func ExampleMinify() { m := minify.New() m.AddFunc("image/svg+xml", Minify) m.AddFunc("text/css", css.Minify) if err := m.Minify("image/svg+xml", os.Stdout, os.Stdin); err != nil { panic(err) } }