From 07daa5e160fd856c2f49b01b665a216579fd0535 Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Sun, 6 Aug 2017 13:18:16 -0700 Subject: [PATCH] adding a test for parsing size strings --- media_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 media_test.go diff --git a/media_test.go b/media_test.go new file mode 100644 index 0000000..6a91fb0 --- /dev/null +++ b/media_test.go @@ -0,0 +1,49 @@ +package caddyhugo + +import ( + "fmt" + "image" + "testing" +) + +// TestThumbSizeStrings tests parsing various size strings, which doesn't include +// scaling zeros, but does use the actual image size to infer single-dimension +// strings +func TestThumbSizeStrings(t *testing.T) { + type testcase struct { + input string + actualSize image.Rectangle + expect image.Rectangle + } + + i50x100 := image.Rect(0, 0, 50, 100) + i100x100 := image.Rect(0, 0, 100, 100) + + var cases = []testcase{ + {"100x100", i100x100, image.Rect(0, 0, 100, 100)}, + {"x100", i100x100, image.Rect(0, 0, 0, 100)}, + {"100x", i100x100, image.Rect(0, 0, 100, 0)}, + {"100", i100x100, image.Rect(0, 0, 100, 0)}, + + {"100x100", i50x100, image.Rect(0, 0, 100, 100)}, + {"x100", i50x100, image.Rect(0, 0, 0, 100)}, + {"100x", i50x100, image.Rect(0, 0, 100, 0)}, + {"100", i50x100, image.Rect(0, 0, 0, 100)}, + } + + for _, c := range cases { + t.Run(fmt.Sprint(c.input, "=>", c.actualSize), func(t *testing.T) { + got, err := parseSizeString(c.input, image.Rect(0, 0, 100, 100)) + if err != nil { + t.Errorf("error parsing size string: %v", err) + return + } + + if !got.Eq(c.expect) { + t.Errorf("expected %v, got %v", c.expect, got) + return + } + + }) + } +}