pathdata_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package svg // import "github.com/tdewolff/minify/svg"
  2. import (
  3. "testing"
  4. "github.com/tdewolff/test"
  5. )
  6. func TestPathData(t *testing.T) {
  7. var pathDataTests = []struct {
  8. pathData string
  9. expected string
  10. }{
  11. {"M10 10 20 10", "M10 10H20"},
  12. {"M10 10 10 20", "M10 10V20"},
  13. {"M50 50 100 100", "M50 50l50 50"},
  14. {"m50 50 40 40m50 50", "m50 50 40 40m50 50"},
  15. {"M10 10zM15 15", "M10 10zm5 5"},
  16. {"M50 50H55V55", "M50 50h5v5"},
  17. {"M10 10L11 10 11 11", "M10 10h1v1"},
  18. {"M10 10l1 0 0 1", "M10 10h1v1"},
  19. {"M10 10L11 11 0 0", "M10 10l1 1L0 0"},
  20. {"M246.614 51.028L246.614-5.665 189.922-5.665", "M246.614 51.028V-5.665H189.922"},
  21. {"M100,200 C100,100 250,100 250,200 S400,300 400,200", "M1e2 2e2c0-1e2 150-1e2 150 0s150 1e2 150 0"},
  22. {"M200,300 Q400,50 600,300 T1000,300", "M2e2 3e2q2e2-250 4e2.0t4e2.0"},
  23. {"M300,200 h-150 a150,150 0 1,0 150,-150 z", "M3e2 2e2H150A150 150 0 1 0 3e2 50z"},
  24. {"x5 5L10 10", "L10 10"},
  25. {"M.0.1", "M0 .1"},
  26. {"M200.0.1", "M2e2.1"},
  27. {"M0 0a3.28 3.28.0.0.0 3.279 3.28", "M0 0a3.28 3.28.0 0 0 3.279 3.28"}, // #114
  28. {"A1.1.0.0.0.0.2.3", "A1.1.0.0 0 0 .2."}, // bad input (sweep and large-arc are not booleans) gives bad output
  29. // fuzz
  30. {"", ""},
  31. {"ML", ""},
  32. {".8.00c0", ""},
  33. {".1.04h0e6.0e6.0e0.0", "h0 0 0 0"},
  34. {"M.1.0.0.2Z", "M.1.0.0.2z"},
  35. {"A.0.0.0.0.3.2e3.7.0.0.0.0.0.1.3.0.0.0.0.2.3.2.0.0.0.0.20.2e-10.0.0.0.0.0.0.0.0", "A0 0 0 0 .3 2e2.7.0.0.0 0 0 .1.3 30 0 0 0 .2.3.2 3 20 0 0 .2 2e-1100 11 0 0 0 "}, // bad input (sweep and large-arc are not booleans) gives bad output
  36. }
  37. p := NewPathData(&Minifier{Decimals: -1})
  38. for _, tt := range pathDataTests {
  39. t.Run(tt.pathData, func(t *testing.T) {
  40. path := p.ShortenPathData([]byte(tt.pathData))
  41. test.Minify(t, tt.pathData, nil, string(path), tt.expected)
  42. })
  43. }
  44. }
  45. ////////////////////////////////////////////////////////////////
  46. func BenchmarkShortenPathData(b *testing.B) {
  47. p := NewPathData(&Minifier{})
  48. r := []byte("M8.64,223.948c0,0,143.468,3.431,185.777-181.808c2.673-11.702-1.23-20.154,1.316-33.146h16.287c0,0-3.14,17.248,1.095,30.848c21.392,68.692-4.179,242.343-204.227,196.59L8.64,223.948z")
  49. for i := 0; i < b.N; i++ {
  50. p.ShortenPathData(r)
  51. }
  52. }