From b75c11b5ea63b1c4d3db1032f82094acf106401f Mon Sep 17 00:00:00 2001 From: Arnas Udovicius Date: Wed, 12 May 2021 07:56:36 +0300 Subject: [PATCH] better check in tests --- tree_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/tree_test.go b/tree_test.go index b3e7c83..1c7c3fc 100644 --- a/tree_test.go +++ b/tree_test.go @@ -2,7 +2,9 @@ package zord_tree import ( cp "github.com/otiai10/copy" + "io/ioutil" "os" + "strings" "testing" ) @@ -81,7 +83,28 @@ func TestId(t *testing.T) { t.Fatal("Couldn't prepare data for testing") } - PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}) + err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}) + if err != nil { + t.Fatal("Population ended in err:", err) + } + + b, err := ioutil.ReadFile("./testdata/sunny2/file1.md") + if err != nil { + t.Fatal("No destination file 'file1.md'.") + } + str := string(b) + if !strings.Contains(str, "\nid: 1\n") { + t.Fatal("No id in file 'file1.md'.") + } + + b, err = ioutil.ReadFile("./testdata/sunny2/subcategory/file3.md") + if err != nil { + t.Fatal("No destination file 'substring/file3.md'.") + } + str = string(b) + if !strings.Contains(str, "\nid: 3\n") { + t.Fatal("No id in file 'file3.md'.") + } } func TestMissingOptions(t *testing.T) { @@ -96,5 +119,44 @@ func TestMissingOptions(t *testing.T) { t.Fatal("Couldn't prepare data for testing") } - PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{"option1", "option2"}) + err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{"option1", "option2"}) + if err != nil { + t.Fatal("Population ended in err:", err) + } + + b, err := ioutil.ReadFile("./testdata/sunny2/file1.md") + if err != nil { + t.Fatal("No destination file 'file1.md'.") + } + str := string(b) + if !strings.Contains(str, "\noption1: test option\n") { + t.Fatal("Changed old value for 'option1' in file 'file1.md'.") + } + if !strings.Contains(str, "\noption2: \n") { + t.Fatal("'option2' has not been added to file 'file1.md'.") + } + + b, err = ioutil.ReadFile("./testdata/sunny2/file2.md") + if err != nil { + t.Fatal("No destination file 'file2.md'.") + } + str = string(b) + if !strings.Contains(str, "\noption1: \n") { + t.Fatal("'option1' has not been added to file 'file2.md'.") + } + if !strings.Contains(str, "\noption2: \n") { + t.Fatal("'option2' has not been added to file 'file2.md'.") + } + + b, err = ioutil.ReadFile("./testdata/sunny2/subcategory/file3.md") + if err != nil { + t.Fatal("No destination file 'substring/file3.md'.") + } + str = string(b) + if !strings.Contains(str, "\noption1: \n") { + t.Fatal("'option1' has not been added to file 'file3.md'.") + } + if !strings.Contains(str, "\noption2: it exists\n") { + t.Fatal("Changed old value for 'option2' in file 'file3.md'.") + } }