From 49f76505125e266d1170e6847c13ee982e489c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Bochniak?= Date: Tue, 20 Mar 2018 14:17:13 +0100 Subject: [PATCH 1/2] Updated GTStructure to use pointers to items instead of value receivers. (#1) This is a breaking change as the public method signatures and main type has been modified. - allows creation of the structure and flexible mutating it before actual printing - pointer receiver should be more efficient for large structures - added simple test + test resources --- gotree.go | 21 +++++------ gotree_test.go | 57 ++++++++++++++++++++++++++++++ test-resources/dir1/dir3/file2.txt | 0 test-resources/dir1/file1.txt | 0 test-resources/dir2/file3.txt | 0 test-resources/file4.txt | 0 6 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 gotree_test.go create mode 100644 test-resources/dir1/dir3/file2.txt create mode 100644 test-resources/dir1/file1.txt create mode 100644 test-resources/dir2/file3.txt create mode 100644 test-resources/file4.txt diff --git a/gotree.go b/gotree.go index 53a49de..89ef554 100644 --- a/gotree.go +++ b/gotree.go @@ -9,10 +9,10 @@ import ( /*GTStructure Structure to output print */ type GTStructure struct { Name string - Items []GTStructure + Items []*GTStructure } -func StringTree(object GTStructure) (result string) { +func StringTree(object *GTStructure) (result string) { result += object.Name + "\n" var spaces []bool result += stringObjItems(object.Items, spaces) @@ -37,7 +37,7 @@ func stringLine(name string, spaces []bool, last bool) (result string) { return } -func stringObjItems(items []GTStructure, spaces []bool) (result string) { +func stringObjItems(items []*GTStructure, spaces []bool) (result string) { for i, f := range items { last := (i >= len(items)-1) result += stringLine(f.Name, spaces, last) @@ -50,14 +50,13 @@ func stringObjItems(items []GTStructure, spaces []bool) (result string) { } /*PrintTree - Print the tree in console */ -func PrintTree(object GTStructure) { +func PrintTree(object *GTStructure) { fmt.Println(StringTree(object)) } /*ReadFolder - Read a folder and return the generated object */ -func ReadFolder(directory string) GTStructure { - - var parent GTStructure +func ReadFolder(directory string) *GTStructure { + parent := >Structure{} parent.Name = directory parent.Items = createGTReadFolder(directory) @@ -65,14 +64,12 @@ func ReadFolder(directory string) GTStructure { return parent } -func createGTReadFolder(directory string) []GTStructure { - - var items []GTStructure +func createGTReadFolder(directory string) []*GTStructure { + var items []*GTStructure files, _ := ioutil.ReadDir(directory) for _, f := range files { - - var child GTStructure + child := >Structure{} child.Name = f.Name() if f.IsDir() { diff --git a/gotree_test.go b/gotree_test.go new file mode 100644 index 0000000..dadd27e --- /dev/null +++ b/gotree_test.go @@ -0,0 +1,57 @@ +package gotree + +import ( + "testing" +) + +// TestFolderTree should test if reading a folder returns rendered structure as expected +func TestFolderTree(t *testing.T) { + tree := ReadFolder("test-resources") + actual := StringTree(tree) + expected := "test-resources\n" + + "├── dir1\n" + + "│ ├── dir3\n" + + "│ │ └── file2.txt\n" + + "│ └── file1.txt\n" + + "├── dir2\n" + + "│ └── file3.txt\n" + + "└── file4.txt\n" + + if actual != expected { + t.Fatalf("Actual tree::\n[%s]\nis not the same as expected:\n[%s]", actual, expected) + } +} + +// TestUpdatingItemsStructure should test whenever item updates in the tree structure are +// reflected correctly in the rendered structure +func TestUpdatingItemsStructure(t *testing.T) { + expected := "Pantera\n" + + "└── Far Beyond Driven\n" + + " └── 5 minutes Alone\n" + + var artist GTStructure + artist.Name = "Pantera Typo0" + + var album GTStructure + album.Name = "Far Beyond Driven Typo1" + + var music GTStructure + music.Name = "5 Minutes Alone Typo2" + + // Add Music to the Album + album.Items = append(album.Items, &music) + + // Add Album to the Artist + artist.Items = append(artist.Items, &album) + + // apply updates to the items that are already in the tree structure + music.Name = "5 minutes Alone" + album.Name = "Far Beyond Driven" + artist.Name = "Pantera" + + actual := StringTree(&artist) + + if actual != expected { + t.Fatalf("Actual tree::\n[%s]\nis not the same as expected:\n[%s]", actual, expected) + } +} diff --git a/test-resources/dir1/dir3/file2.txt b/test-resources/dir1/dir3/file2.txt new file mode 100644 index 0000000..e69de29 diff --git a/test-resources/dir1/file1.txt b/test-resources/dir1/file1.txt new file mode 100644 index 0000000..e69de29 diff --git a/test-resources/dir2/file3.txt b/test-resources/dir2/file3.txt new file mode 100644 index 0000000..e69de29 diff --git a/test-resources/file4.txt b/test-resources/file4.txt new file mode 100644 index 0000000..e69de29 From 397906871d4fb7a7c4d6b215e99df82b54c2b9e1 Mon Sep 17 00:00:00 2001 From: Jaroslaw Bochniak Date: Wed, 21 Mar 2018 09:22:47 +0100 Subject: [PATCH 2/2] After review improvements. - removed test folder we should find a better solution for testing `ReadFolder` method - updated readme to reflect signature changes --- README.md | 6 +++--- gotree_test.go | 18 ------------------ test-resources/dir1/dir3/file2.txt | 0 test-resources/dir1/file1.txt | 0 test-resources/dir2/file3.txt | 0 test-resources/file4.txt | 0 6 files changed, 3 insertions(+), 21 deletions(-) delete mode 100644 test-resources/dir1/dir3/file2.txt delete mode 100644 test-resources/dir1/file1.txt delete mode 100644 test-resources/dir2/file3.txt delete mode 100644 test-resources/file4.txt diff --git a/README.md b/README.md index 86df81d..b6cb205 100644 --- a/README.md +++ b/README.md @@ -49,12 +49,12 @@ var music gotree.GTStructure music.Name = "5 Minutes Alone" //Add Music to the Album -album.Items = append(album.Items, music) +album.Items = append(album.Items, &music) //Add Album to the Artist -artist.Items = append(artist.Items, album) +artist.Items = append(artist.Items, &album) -gotree.PrintTree(artist) +gotree.PrintTree(&artist) ``` ### Read folder and print tree diff --git a/gotree_test.go b/gotree_test.go index dadd27e..8df39b8 100644 --- a/gotree_test.go +++ b/gotree_test.go @@ -4,24 +4,6 @@ import ( "testing" ) -// TestFolderTree should test if reading a folder returns rendered structure as expected -func TestFolderTree(t *testing.T) { - tree := ReadFolder("test-resources") - actual := StringTree(tree) - expected := "test-resources\n" + - "├── dir1\n" + - "│ ├── dir3\n" + - "│ │ └── file2.txt\n" + - "│ └── file1.txt\n" + - "├── dir2\n" + - "│ └── file3.txt\n" + - "└── file4.txt\n" - - if actual != expected { - t.Fatalf("Actual tree::\n[%s]\nis not the same as expected:\n[%s]", actual, expected) - } -} - // TestUpdatingItemsStructure should test whenever item updates in the tree structure are // reflected correctly in the rendered structure func TestUpdatingItemsStructure(t *testing.T) { diff --git a/test-resources/dir1/dir3/file2.txt b/test-resources/dir1/dir3/file2.txt deleted file mode 100644 index e69de29..0000000 diff --git a/test-resources/dir1/file1.txt b/test-resources/dir1/file1.txt deleted file mode 100644 index e69de29..0000000 diff --git a/test-resources/dir2/file3.txt b/test-resources/dir2/file3.txt deleted file mode 100644 index e69de29..0000000 diff --git a/test-resources/file4.txt b/test-resources/file4.txt deleted file mode 100644 index e69de29..0000000