Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26daec2

Browse files
author
Tomas Vik
committed
Exporting TstFile and TstFolder as public interface of core package
- rewriting rmaining tests
1 parent ba42ee4 commit 26daec2

4 files changed

Lines changed: 87 additions & 69 deletions

File tree

core/test_utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package core
2+
3+
// TstFolder is providing easy interface to create foders for automated tests
4+
// Never use in production code!
5+
func TstFolder(name string, files ...*File) *File {
6+
folder := &File{name, nil, 0, true, []*File{}}
7+
if files == nil {
8+
return folder
9+
}
10+
var size int64
11+
for _, file := range files {
12+
size += file.Size
13+
file.Parent = folder
14+
}
15+
folder.Size = size
16+
folder.Files = files
17+
return folder
18+
}
19+
20+
// TstFile provides easy interface to craete files for automated tests
21+
// Never use in production code!
22+
func TstFile(name string, size int64) *File {
23+
return &File{name, nil, size, false, []*File{}}
24+
}
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@ import (
55
"testing"
66
)
77

8-
func TstFolder(name string, files ...*File) *File {
9-
folder := &File{name, nil, 0, true, []*File{}}
10-
if files == nil {
11-
return folder
12-
}
13-
var size int64
14-
for _, file := range files {
15-
size += file.Size
16-
file.Parent = folder
17-
}
18-
folder.Size = size
19-
folder.Files = files
20-
return folder
21-
}
22-
23-
func TstFile(name string, size int64) *File {
24-
return &File{name, nil, size, false, []*File{}}
25-
}
26-
278
func TestBuildFile(t *testing.T) {
289
a := &File{"a", nil, 100, false, []*File{}}
2910
build := TstFile("a", 100)

interactive/printer_test.go

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package interactive
33
import (
44
"bufio"
55
"bytes"
6+
"reflect"
67
"strings"
78
"testing"
89

@@ -23,45 +24,59 @@ func TestPrintMarkedFilesNone(t *testing.T) {
2324
}
2425

2526
func TestPrintMarkedFiles(t *testing.T) {
27+
root := core.TstFolder(".",
28+
core.TstFolder("d1",
29+
core.TstFile("f1", 0),
30+
core.TstFolder("d3",
31+
core.TstFile("f2", 0),
32+
),
33+
),
34+
core.TstFolder("d2"),
35+
core.TstFile("f3", 0),
36+
)
2637
marked := make(map[*core.File]struct{})
27-
root := &core.File{Name: "."}
28-
f1 := &core.File{Name: "f1"}
29-
f2 := &core.File{Name: "f2"}
30-
f3 := &core.File{Name: "f3", Parent: nil}
31-
d1 := &core.File{Name: "d1", IsDir: true, Parent: root, Files: []*core.File{f1}}
32-
d2 := &core.File{Name: "d2", IsDir: true, Parent: root}
33-
d3 := &core.File{Name: "d3", IsDir: true, Parent: d1, Files: []*core.File{f2}}
34-
f1.Parent = d1
35-
f2.Parent = d3
36-
marked[d1] = struct{}{}
37-
marked[d2] = struct{}{}
38-
marked[f1] = struct{}{}
39-
marked[f2] = struct{}{}
40-
marked[f3] = struct{}{}
38+
marked[getFileByName(root, "d1")] = struct{}{}
39+
marked[getFileByName(root, "d2")] = struct{}{}
40+
marked[getFileByName(root, "f1")] = struct{}{}
41+
marked[getFileByName(root, "f2")] = struct{}{}
42+
//marked[getFileByName(root, "f3")] = struct{}{}
4143
state := &core.State{MarkedFiles: marked}
4244
var buffer bytes.Buffer
4345
writer := bufio.NewWriter(&buffer)
4446
PrintMarkedFiles(state, writer)
4547
writer.Flush()
4648
result := buffer.String()
4749
// We don't know the order, as we are using a map to store marked files :/
48-
expected := `'d1'
49-
'd2'
50-
'd1/d3/f2'`
51-
if hasSameLines(result, expected) {
50+
expected := "'d1'\n'd2'\n'd1/d3/f2'\n"
51+
if !hasSameLines(result, expected) {
5252
t.Errorf("Expected '%s' from PrintMarkedFiles, got '%s'", expected, result)
5353
}
5454
}
5555

56-
func hasSameLines(s1, s2 string) bool {
57-
if len(s1) != len(s2) {
56+
func hasSameLines(value, expected string) bool {
57+
valueMap := map[string]struct{}{}
58+
expectedMap := map[string]struct{}{}
59+
values := strings.Split(value, "\n")
60+
expecteds := strings.Split(expected, "\n")
61+
if len(values) != len(expecteds) {
5862
return false
5963
}
60-
ss1 := strings.Split(s1, "\n")
61-
for _, l1 := range ss1 {
62-
if !strings.Contains(s2, l1+"\n") {
63-
return false
64+
for i := 0; i < len(values); i++ {
65+
valueMap[values[i]] = struct{}{}
66+
expectedMap[expecteds[i]] = struct{}{}
67+
}
68+
return reflect.DeepEqual(valueMap, expectedMap)
69+
}
70+
71+
func getFileByName(tree *core.File, name string) *core.File {
72+
if tree.Name == name {
73+
return tree
74+
}
75+
for _, file := range tree.Files {
76+
result := getFileByName(file, name)
77+
if result != nil {
78+
return result
6479
}
6580
}
66-
return true
81+
return nil
6782
}

interactive/reporter_test.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
func TestReportTree(t *testing.T) {
1111
marked := make(map[*core.File]struct{})
12-
testTree := &core.File{"b", nil, 180, true, []*core.File{
13-
&core.File{"c", nil, 100, false, []*core.File{}},
14-
&core.File{"d", nil, 80, true, []*core.File{
15-
&core.File{"e", nil, 50, false, []*core.File{}},
16-
&core.File{"f", nil, 30, false, []*core.File{}},
17-
}},
18-
}}
12+
testTree := core.TstFolder("b",
13+
core.TstFile("c", 100),
14+
core.TstFolder("d",
15+
core.TstFile("e", 50),
16+
core.TstFile("f", 30),
17+
),
18+
)
1919
marked[testTree.Files[0]] = struct{}{}
2020
expected := []Line{
2121
Line{Text: []rune("* 100B c"), IsMarked: true},
@@ -26,23 +26,21 @@ func TestReportTree(t *testing.T) {
2626

2727
func TestPrintsEmptyDir(t *testing.T) {
2828
marked := make(map[*core.File]struct{})
29-
testTree := &core.File{"", nil, 50, true, []*core.File{
30-
&core.File{"a", nil, 50, true, []*core.File{}},
31-
}}
29+
testTree := core.TstFolder("", core.TstFolder("a"))
3230
expected := []Line{
33-
Line{Text: []rune(" 50B a/")},
31+
Line{Text: []rune(" 0B a/")},
3432
}
3533
testTreeAgainstOutput(testTree, marked, expected, t)
3634
}
3735

3836
func TestFiveCharSize(t *testing.T) {
3937
marked := make(map[*core.File]struct{})
40-
testTree := &core.File{"X", nil, 0, true, []*core.File{
41-
&core.File{"o", nil, 1, false, []*core.File{}},
42-
&core.File{"on", nil, 10, false, []*core.File{}},
43-
&core.File{"one", nil, 100, false, []*core.File{}},
44-
&core.File{"one1", nil, 1000, false, []*core.File{}},
45-
}}
38+
testTree := core.TstFolder("X",
39+
core.TstFile("o", 1),
40+
core.TstFile("on", 10),
41+
core.TstFile("one", 100),
42+
core.TstFile("one1", 1000),
43+
)
4644
ex := []Line{
4745
Line{Text: []rune(" 1B o")},
4846
Line{Text: []rune(" 10B on")},
@@ -54,14 +52,14 @@ func TestFiveCharSize(t *testing.T) {
5452

5553
func TestReportingUnits(t *testing.T) {
5654
marked := make(map[*core.File]struct{})
57-
testTree := &core.File{"X", nil, 0, true, []*core.File{
58-
&core.File{"B", nil, 1 << 0, false, []*core.File{}},
59-
&core.File{"K", nil, 1 << 10, false, []*core.File{}},
60-
&core.File{"M", nil, 1048576, false, []*core.File{}},
61-
&core.File{"G", nil, 1073741824, false, []*core.File{}},
62-
&core.File{"T", nil, 1099511627776, false, []*core.File{}},
63-
&core.File{"P", nil, 1125899906842624, false, []*core.File{}},
64-
}}
55+
testTree := core.TstFolder("X",
56+
core.TstFile("B", 1<<0),
57+
core.TstFile("K", 1<<10),
58+
core.TstFile("M", 1048576),
59+
core.TstFile("G", 1073741824),
60+
core.TstFile("T", 1099511627776),
61+
core.TstFile("P", 1125899906842624),
62+
)
6563
ex := []Line{
6664
Line{Text: []rune(" 1B B")},
6765
Line{Text: []rune(" 1K K")},

0 commit comments

Comments
 (0)