summaryrefslogtreecommitdiffstats
path: root/minifiers/minifiers_test.go
diff options
context:
space:
mode:
authorJames Kiefer <zinefer@gmail.com>2018-09-12 11:33:14 -0600
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-09-12 19:33:14 +0200
commit3dafe206e31bb92f27802a04bf9159cbc20af234 (patch)
tree6b2aabcaa3b98087ece4e2a85e1d7944366abc07 /minifiers/minifiers_test.go
parent37d6463479952f7dfba59d899eed38b41e223283 (diff)
minifiers: Make JSON minification more generic
Add a regex matcher for json types. Specifically support LD+JSON which allows for google seo minification out of the box. Expanded JS/JSON minification testing.
Diffstat (limited to 'minifiers/minifiers_test.go')
-rw-r--r--minifiers/minifiers_test.go46
1 files changed, 39 insertions, 7 deletions
diff --git a/minifiers/minifiers_test.go b/minifiers/minifiers_test.go
index a0f0f97b4..6500aa2c2 100644
--- a/minifiers/minifiers_test.go
+++ b/minifiers/minifiers_test.go
@@ -28,14 +28,46 @@ func TestNew(t *testing.T) {
assert := require.New(t)
m := New(media.DefaultTypes, output.DefaultFormats)
- var b bytes.Buffer
+ var rawJS string
+ var minJS string
+ rawJS = " var foo =1 ; foo ++ ; "
+ minJS = "var foo=1;foo++;"
- assert.NoError(m.Minify(media.CSSType, &b, strings.NewReader("body { color: blue; }")))
- assert.Equal("body{color:blue}", b.String())
+ var rawJSON string
+ var minJSON string
+ rawJSON = " { \"a\" : 123 , \"b\":2, \"c\": 5 } "
+ minJSON = "{\"a\":123,\"b\":2,\"c\":5}"
- b.Reset()
+ for _, test := range []struct {
+ tp media.Type
+ rawString string
+ expectedMinString string
+ }{
+ {media.CSSType, " body { color: blue; } ", "body{color:blue}"},
+ {media.RSSType, " <hello> Hugo! </hello> ", "<hello>Hugo!</hello>"}, // RSS should be handled as XML
+ {media.JSONType, rawJSON, minJSON},
+ {media.JavascriptType, rawJS, minJS},
+ // JS Regex minifiers
+ {media.Type{MainType: "application", SubType: "ecmascript"}, rawJS, minJS},
+ {media.Type{MainType: "application", SubType: "javascript"}, rawJS, minJS},
+ {media.Type{MainType: "application", SubType: "x-javascript"}, rawJS, minJS},
+ {media.Type{MainType: "application", SubType: "x-ecmascript"}, rawJS, minJS},
+ {media.Type{MainType: "text", SubType: "ecmascript"}, rawJS, minJS},
+ {media.Type{MainType: "text", SubType: "javascript"}, rawJS, minJS},
+ {media.Type{MainType: "text", SubType: "x-javascript"}, rawJS, minJS},
+ {media.Type{MainType: "text", SubType: "x-ecmascript"}, rawJS, minJS},
+ // JSON Regex minifiers
+ {media.Type{MainType: "application", SubType: "json"}, rawJSON, minJSON},
+ {media.Type{MainType: "application", SubType: "x-json"}, rawJSON, minJSON},
+ {media.Type{MainType: "application", SubType: "ld+json"}, rawJSON, minJSON},
+ {media.Type{MainType: "text", SubType: "json"}, rawJSON, minJSON},
+ {media.Type{MainType: "text", SubType: "x-json"}, rawJSON, minJSON},
+ {media.Type{MainType: "text", SubType: "ld+json"}, rawJSON, minJSON},
+ } {
+ var b bytes.Buffer
+
+ assert.NoError(m.Minify(test.tp, &b, strings.NewReader(test.rawString)))
+ assert.Equal(test.expectedMinString, b.String())
+ }
- // RSS should be handled as XML
- assert.NoError(m.Minify(media.RSSType, &b, strings.NewReader("<hello> Hugo! </hello> ")))
- assert.Equal("<hello>Hugo!</hello>", b.String())
}