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 {} `, ``},
{``, ``},
{``, ``},
{``, ``},
{``, ``},
}
m := minify.New()
m.AddFunc("text/css", css.Minify)
for _, tt := range svgTests {
t.Run(tt.svg, func(t *testing.T) {
r := bytes.NewBufferString(tt.svg)
w := &bytes.Buffer{}
err := Minify(m, w, r, nil)
test.Minify(t, tt.svg, err, w.String(), tt.expected)
})
}
}
func TestSVGDecimals(t *testing.T) {
var svgTests = []struct {
svg string
expected string
}{
{``, ``},
}
m := minify.New()
o := &Minifier{Decimals: 1}
for _, tt := range svgTests {
t.Run(tt.svg, func(t *testing.T) {
r := bytes.NewBufferString(tt.svg)
w := &bytes.Buffer{}
err := o.Minify(m, w, r, nil)
test.Minify(t, tt.svg, 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 {
svg string
n []int
}{
{` ]>`, []int{0}},
{`abc`, []int{0}},
{``, []int{2}},
{``, []int{0}},
{``, []int{0}},
{``, []int{0, 1, 2, 3, 4, 5}},
{``, []int{1}},
{``, []int{1, 3}},
{``, []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)
}
}