diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-19 17:23:14 +0100 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-19 17:23:14 +0100 |
commit | 9cc3d67c577377af1af1b9de594f99d9dabc05b3 (patch) | |
tree | 8647eedfbe618556a40bc0e1ede6c305a6be59f9 | |
parent | 1e8e5d4e7a5ca3e1b43e2782c5fee52986042f81 (diff) |
Pull Data Files tests into its own file
-rw-r--r-- | hugolib/datafiles_test.go | 92 | ||||
-rw-r--r-- | hugolib/site_test.go | 96 |
2 files changed, 97 insertions, 91 deletions
diff --git a/hugolib/datafiles_test.go b/hugolib/datafiles_test.go new file mode 100644 index 000000000..00e1b2d77 --- /dev/null +++ b/hugolib/datafiles_test.go @@ -0,0 +1,92 @@ +package hugolib + +import ( + "github.com/spf13/hugo/parser" + "github.com/spf13/hugo/source" + "path/filepath" + "reflect" + "testing" +) + +func TestDataDirJSON(t *testing.T) { + sources := []source.ByteSource{ + {filepath.FromSlash("test/foo.json"), []byte(`{ "bar": "foofoo" }`)}, + {filepath.FromSlash("test.json"), []byte(`{ "hello": [ { "world": "foo" } ] }`)}, + } + + expected, err := parser.HandleJSONMetaData([]byte(`{ "test": { "hello": [{ "world": "foo" }] , "foo": { "bar":"foofoo" } } }`)) + + if err != nil { + t.Fatalf("Error %s", err) + } + + doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}}) +} + +func TestDataDirToml(t *testing.T) { + sources := []source.ByteSource{ + {filepath.FromSlash("test/kung.toml"), []byte("[foo]\nbar = 1")}, + } + + expected, err := parser.HandleTOMLMetaData([]byte("[test]\n[test.kung]\n[test.kung.foo]\nbar = 1")) + + if err != nil { + t.Fatalf("Error %s", err) + } + + doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}}) +} + +func TestDataDirYAMLWithOverridenValue(t *testing.T) { + sources := []source.ByteSource{ + // filepath.Walk walks the files in lexical order, '/' comes before '.'. Simulate this: + {filepath.FromSlash("a.yaml"), []byte("a: 1")}, + {filepath.FromSlash("test/v1.yaml"), []byte("v1-2: 2")}, + {filepath.FromSlash("test/v2.yaml"), []byte("v2:\n- 2\n- 3")}, + {filepath.FromSlash("test.yaml"), []byte("v1: 1")}, + } + + expected := map[string]interface{}{"a": map[string]interface{}{"a": 1}, + "test": map[string]interface{}{"v1": map[string]interface{}{"v1-2": 2}, "v2": map[string]interface{}{"v2": []interface{}{2, 3}}}} + + doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}}) +} + +// issue 892 +func TestDataDirMultipleSources(t *testing.T) { + s1 := []source.ByteSource{ + {filepath.FromSlash("test/first.toml"), []byte("bar = 1")}, + } + + s2 := []source.ByteSource{ + {filepath.FromSlash("test/first.toml"), []byte("bar = 2")}, + {filepath.FromSlash("test/second.toml"), []byte("tender = 2")}, + } + + expected, _ := parser.HandleTOMLMetaData([]byte("[test.first]\nbar = 1\n[test.second]\ntender=2")) + + doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: s1}, &source.InMemorySource{ByteSource: s2}}) + +} + +func TestDataDirUnknownFormat(t *testing.T) { + sources := []source.ByteSource{ + {filepath.FromSlash("test.roml"), []byte("boo")}, + } + s := &Site{} + err := s.loadData([]source.Input{&source.InMemorySource{ByteSource: sources}}) + if err == nil { + t.Fatalf("Should return an error") + } +} + +func doTestDataDir(t *testing.T, expected interface{}, sources []source.Input) { + s := &Site{} + err := s.loadData(sources) + if err != nil { + t.Fatalf("Error loading data: %s", err) + } + if !reflect.DeepEqual(expected, s.Data) { + t.Errorf("Expected structure\n%#v got\n%#v", expected, s.Data) + } +} diff --git a/hugolib/site_test.go b/hugolib/site_test.go index ca75eac73..711487278 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -3,13 +3,6 @@ package hugolib import ( "bytes" "fmt" - "github.com/spf13/hugo/parser" - "html/template" - "io" - "path/filepath" - "strings" - "testing" - "github.com/spf13/afero" "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/hugofs" @@ -17,7 +10,11 @@ import ( "github.com/spf13/hugo/target" "github.com/spf13/hugo/tpl" "github.com/spf13/viper" - "reflect" + "html/template" + "io" + "path/filepath" + "strings" + "testing" ) const ( @@ -906,86 +903,3 @@ func TestWeightedTaxonomies(t *testing.T) { t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Taxonomies["categories"]["e"][0].Page.Title) } } - -func TestDataDirJSON(t *testing.T) { - sources := []source.ByteSource{ - {filepath.FromSlash("test/foo.json"), []byte(`{ "bar": "foofoo" }`)}, - {filepath.FromSlash("test.json"), []byte(`{ "hello": [ { "world": "foo" } ] }`)}, - } - - expected, err := parser.HandleJSONMetaData([]byte(`{ "test": { "hello": [{ "world": "foo" }] , "foo": { "bar":"foofoo" } } }`)) - - if err != nil { - t.Fatalf("Error %s", err) - } - - doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}}) -} - -func TestDataDirToml(t *testing.T) { - sources := []source.ByteSource{ - {filepath.FromSlash("test/kung.toml"), []byte("[foo]\nbar = 1")}, - } - - expected, err := parser.HandleTOMLMetaData([]byte("[test]\n[test.kung]\n[test.kung.foo]\nbar = 1")) - - if err != nil { - t.Fatalf("Error %s", err) - } - - doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}}) -} - -func TestDataDirYAMLWithOverridenValue(t *testing.T) { - sources := []source.ByteSource{ - // filepath.Walk walks the files in lexical order, '/' comes before '.'. Simulate this: - {filepath.FromSlash("a.yaml"), []byte("a: 1")}, - {filepath.FromSlash("test/v1.yaml"), []byte("v1-2: 2")}, - {filepath.FromSlash("test/v2.yaml"), []byte("v2:\n- 2\n- 3")}, - {filepath.FromSlash("test.yaml"), []byte("v1: 1")}, - } - - expected := map[string]interface{}{"a": map[string]interface{}{"a": 1}, - "test": map[string]interface{}{"v1": map[string]interface{}{"v1-2": 2}, "v2": map[string]interface{}{"v2": []interface{}{2, 3}}}} - - doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}}) -} - -// issue 892 -func TestDataDirMultipleSources(t *testing.T) { - s1 := []source.ByteSource{ - {filepath.FromSlash("test/first.toml"), []byte("bar = 1")}, - } - - s2 := []source.ByteSource{ - {filepath.FromSlash("test/first.toml"), []byte("bar = 2")}, - {filepath.FromSlash("test/second.toml"), []byte("tender = 2")}, - } - - expected, _ := parser.HandleTOMLMetaData([]byte("[test.first]\nbar = 1\n[test.second]\ntender=2")) - - doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: s1}, &source.InMemorySource{ByteSource: s2}}) - -} - -func TestDataDirUnknownFormat(t *testing.T) { - sources := []source.ByteSource{ - {filepath.FromSlash("test.roml"), []byte("boo")}, - } - s := &Site{} - err := s.loadData([]source.Input{&source.InMemorySource{ByteSource: sources}}) - if err == nil { - t.Fatalf("Should return an error") - } -} - -func doTestDataDir(t *testing.T, expected interface{}, sources []source.Input) { - s := &Site{} - err := s.loadData(sources) - if err != nil { - t.Fatalf("Error loading data: %s", err) - } - if !reflect.DeepEqual(expected, s.Data) { - t.Errorf("Expected structure\n%#v got\n%#v", expected, s.Data) - } -} |