Increase refLinker test coverage
authorVas Sudanagunta <vas@commonkarma.org>
Thu, 19 Jul 2018 23:04:45 +0000 (19:04 -0400)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 20 Jul 2018 05:02:41 +0000 (07:02 +0200)
hugolib/site_test.go

index 7787ea2a4bef80bf1763b278a5d2c86e8f15355e..2315af20919199b47f30fc1471e61d020984e3da 100644 (file)
@@ -874,6 +874,8 @@ func TestWeightedTaxonomies(t *testing.T) {
 func setupLinkingMockSite(t *testing.T) *Site {
        sources := [][2]string{
                {filepath.FromSlash("level2/unique.md"), ""},
+               {filepath.FromSlash("_index.md"), ""},
+               {filepath.FromSlash("common.md"), ""},
                {filepath.FromSlash("rootfile.md"), ""},
                {filepath.FromSlash("root-image.png"), ""},
 
@@ -884,10 +886,13 @@ func setupLinkingMockSite(t *testing.T) *Site {
                {filepath.FromSlash("level2/common.png"), ""},
 
                {filepath.FromSlash("level2/level3/start.md"), ""},
+               {filepath.FromSlash("level2/level3/_index.md"), ""},
                {filepath.FromSlash("level2/level3/3-root.md"), ""},
                {filepath.FromSlash("level2/level3/common.md"), ""},
                {filepath.FromSlash("level2/level3/3-image.png"), ""},
                {filepath.FromSlash("level2/level3/common.png"), ""},
+
+               {filepath.FromSlash("level2/level3/embedded.dot.md"), ""},
        }
 
        cfg, fs := newTestCfg()
@@ -921,14 +926,52 @@ func TestRefLinking(t *testing.T) {
                relative     bool
                expected     string
        }{
+               // different refs resolving to the same unique filename:
+               {"/level2/unique.md", "", true, "/level2/unique/"},
+               {"../unique.md", "", true, "/level2/unique/"},
                {"unique.md", "", true, "/level2/unique/"},
+
                {"level2/common.md", "", true, "/level2/common/"},
                {"3-root.md", "", true, "/level2/level3/3-root/"},
+               {"../..", "", true, "/"},
+
+               // different refs resolving to the same ambiguous top-level filename:
+               {"../../common.md", "", true, "/common/"},
+               {"/common.md", "", true, "/common/"},
+
+               // different refs resolving to the same ambiguous level-2 filename:
+               {"/level2/common.md", "", true, "/level2/common/"},
+               {"../common.md", "", true, "/level2/common/"},
+               {"common.md", "", true, "/level2/level3/common/"},
+
+               // different refs resolving to the same section:
+               {"/level2", "", true, "/level2/"},
+               {"..", "", true, "/level2/"},
+               {"../", "", true, "/level2/"},
+
+               // different refs resolving to the same subsection:
+               {"/level2/level3", "", true, "/level2/level3/"},
+               {"/level2/level3/_index.md", "", true, "/level2/level3/"},
+               {".", "", true, "/level2/level3/"},
+               {"./", "", true, "/level2/level3/"},
+
+               // try to confuse parsing
+               {"embedded.dot.md", "", true, "/level2/level3/embedded.dot/"},
+
+               //test empty link, as well as fragment only link
+               {"", "", true, ""},
        } {
-               if out, err := site.refLink(test.link, currentPage, test.relative, test.outputFormat); err != nil || out != test.expected {
-                       t.Errorf("[%d] Expected %s to resolve to (%s), got (%s) - error: %s", i, test.link, test.expected, out, err)
-               }
+               checkLinkCase(site, test.link, currentPage, test.relative, test.outputFormat, test.expected, t, i)
+
+               //make sure fragment links are also handled
+               checkLinkCase(site, test.link+"#intro", currentPage, test.relative, test.outputFormat, test.expected+"#intro", t, i)
        }
 
        // TODO: and then the failure cases.
 }
+
+func checkLinkCase(site *Site, link string, currentPage *Page, relative bool, outputFormat string, expected string, t *testing.T, i int) {
+       if out, err := site.refLink(link, currentPage, relative, outputFormat); err != nil || out != expected {
+               t.Errorf("[%d] Expected %q from %q to resolve to %q, got %q - error: %s", i, link, currentPage.absoluteSourceRef(), expected, out, err)
+       }
+}