Add more tests to helper
authorbep <bjorn.erik.pedersen@gmail.com>
Tue, 27 Jan 2015 09:15:57 +0000 (10:15 +0100)
committerbep <bjorn.erik.pedersen@gmail.com>
Tue, 27 Jan 2015 09:15:57 +0000 (10:15 +0100)
helpers/content_test.go
helpers/path.go
helpers/path_test.go

index 18cdfcc5db1dc2878df0829ee5d3d9237d56d839..63ef82f2f6585b2d8e52509ffa7a6b20f7993f27 100644 (file)
@@ -1,6 +1,8 @@
 package helpers
 
 import (
+       "github.com/stretchr/testify/assert"
+       "html/template"
        "testing"
 )
 
@@ -20,3 +22,12 @@ func TestStripHTML(t *testing.T) {
                }
        }
 }
+
+func TestStripEmptyNav(t *testing.T) {
+       cleaned := StripEmptyNav([]byte("do<nav>\n</nav>\n\nbedobedo"))
+       assert.Equal(t, []byte("dobedobedo"), cleaned)
+}
+
+func TestBytesToHTML(t *testing.T) {
+       assert.Equal(t, template.HTML("dobedobedo"), BytesToHTML([]byte("dobedobedo")))
+}
index 165ce2314afc2f2cc466367b6dfb893e6db90745..f236f30aab9cca10b3ba302c165a5c282f0652fa 100644 (file)
@@ -131,6 +131,7 @@ func AbsPathify(inPath string) string {
                return filepath.Clean(inPath)
        }
 
+       // todo consider move workingDir to argument list
        return filepath.Clean(filepath.Join(viper.GetString("WorkingDir"), inPath))
 }
 
index 603723c27c56f47ddbcef26f9afeae59d0ccf724..0da8835a2eaefb6f3e861243056c26ae770e48f7 100644 (file)
@@ -2,6 +2,7 @@ package helpers
 
 import (
        "fmt"
+       "github.com/spf13/viper"
        "io/ioutil"
        "os"
        "path/filepath"
@@ -55,6 +56,37 @@ func TestMakePathToLower(t *testing.T) {
        }
 }
 
+func TestGetRelativePath(t *testing.T) {
+       tests := []struct {
+               path   string
+               base   string
+               expect interface{}
+       }{
+               {filepath.FromSlash("/a/b"), filepath.FromSlash("/a"), filepath.FromSlash("b")},
+               {filepath.FromSlash("/c"), filepath.FromSlash("/a/b"), filepath.FromSlash("../../c")},
+               {filepath.FromSlash("/c"), "", false},
+       }
+       for i, this := range tests {
+               // ultimately a fancy wrapper around filepath.Rel
+               result, err := GetRelativePath(this.path, this.base)
+
+               if b, ok := this.expect.(bool); ok && !b {
+                       if err == nil {
+                               t.Errorf("[%d] GetRelativePath didn't return an expected error", i)
+                       }
+               } else {
+                       if err != nil {
+                               t.Errorf("[%d] GetRelativePath failed: %s", i, err)
+                               continue
+                       }
+                       if result != this.expect {
+                               t.Errorf("[%d] GetRelativePath got %v but expected %v", i, result, this.expect)
+                       }
+               }
+
+       }
+}
+
 func TestMakePathRelative(t *testing.T) {
        type test struct {
                inPath, path1, path2, output string
@@ -357,21 +389,21 @@ func TestExists(t *testing.T) {
 
 }
 
-// TestAbsPathify cannot be tested further because it relies on the
-// viper.GetString("WorkingDir") which the test cannot know.
-// viper.GetString("WorkingDir") should be passed to AbsPathify as a
-// parameter.
 func TestAbsPathify(t *testing.T) {
        type test struct {
-               input, expected string
+               inPath, workingDir, expected string
        }
        data := []test{
-               {os.TempDir(), filepath.Clean(os.TempDir())}, // TempDir has trailing slash
-               {filepath.FromSlash("/banana/../dir/"), filepath.FromSlash("/dir")},
+               {os.TempDir(), filepath.FromSlash("/work"), filepath.Clean(os.TempDir())}, // TempDir has trailing slash
+               {filepath.FromSlash("/banana/../dir/"), filepath.FromSlash("/work"), filepath.FromSlash("/dir")},
+               {"dir", filepath.FromSlash("/work"), filepath.FromSlash("/work/dir")},
        }
 
        for i, d := range data {
-               expected := AbsPathify(d.input)
+               // todo see comment in AbsPathify
+               viper.Set("WorkingDir", d.workingDir)
+
+               expected := AbsPathify(d.inPath)
                if d.expected != expected {
                        t.Errorf("Test %d failed. Expected %q but go %q", i, d.expected, expected)
                }