1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
package collection
import (
"fmt"
"math"
"path"
"path/filepath"
"testing"
)
func TestEnumerateDirectoryOptions_UniqueBits(t *testing.T) {
isPowerOfTwo := func(subject float64) bool {
a := math.Abs(math.Log2(subject))
b := math.Floor(a)
return a-b < .0000001
}
if !isPowerOfTwo(64) {
t.Log("isPowerOfTwo decided 64 is not a power of two.")
t.FailNow()
}
if isPowerOfTwo(91) {
t.Log("isPowerOfTwo decided 91 is a power of two.")
t.FailNow()
}
seen := make(map[DirectoryOptions]struct{})
declared := []DirectoryOptions{
DirectoryOptionsExcludeFiles,
DirectoryOptionsExcludeDirectories,
DirectoryOptionsRecursive,
}
for _, option := range declared {
if _, ok := seen[option]; ok {
t.Logf("Option: %d has already been declared.", option)
t.Fail()
}
seen[option] = struct{}{}
if !isPowerOfTwo(float64(option)) {
t.Logf("Option should have been a power of two, got %g instead.", float64(option))
t.Fail()
}
}
}
func ExampleDirectory_Enumerate() {
traverser := Directory{
Location: ".",
Options: DirectoryOptionsExcludeDirectories,
}
done := make(chan struct{})
filesOfInterest := traverser.Enumerate(done).Select(func(subject interface{}) (result interface{}) {
cast, ok := subject.(string)
if ok {
result = path.Base(cast)
} else {
result = subject
}
return
}).Where(func(subject interface{}) bool {
cast, ok := subject.(string)
if !ok {
return false
}
return cast == "filesystem_test.go"
})
for entry := range filesOfInterest {
fmt.Println(entry.(string))
}
close(done)
// Output: filesystem_test.go
}
func TestDirectory_Enumerate(t *testing.T) {
subject := Directory{
Location: filepath.Join(".", "testdata", "foo"),
}
testCases := []struct {
options DirectoryOptions
expected map[string]struct{}
}{
{
options: 0,
expected: map[string]struct{}{
filepath.Join("testdata", "foo", "a.txt"): struct{}{},
filepath.Join("testdata", "foo", "c.txt"): struct{}{},
filepath.Join("testdata", "foo", "bar"): struct{}{},
},
},
{
options: DirectoryOptionsExcludeFiles,
expected: map[string]struct{}{
filepath.Join("testdata", "foo", "bar"): struct{}{},
},
},
{
options: DirectoryOptionsExcludeDirectories,
expected: map[string]struct{}{
filepath.Join("testdata", "foo", "a.txt"): struct{}{},
filepath.Join("testdata", "foo", "c.txt"): struct{}{},
},
},
{
options: DirectoryOptionsRecursive,
expected: map[string]struct{}{
filepath.Join("testdata", "foo", "bar"): struct{}{},
filepath.Join("testdata", "foo", "bar", "b.txt"): struct{}{},
filepath.Join("testdata", "foo", "a.txt"): struct{}{},
filepath.Join("testdata", "foo", "c.txt"): struct{}{},
},
},
{
options: DirectoryOptionsExcludeFiles | DirectoryOptionsRecursive,
expected: map[string]struct{}{
filepath.Join("testdata", "foo", "bar"): struct{}{},
},
},
{
options: DirectoryOptionsRecursive | DirectoryOptionsExcludeDirectories,
expected: map[string]struct{}{
filepath.Join("testdata", "foo", "a.txt"): struct{}{},
filepath.Join("testdata", "foo", "bar", "b.txt"): struct{}{},
filepath.Join("testdata", "foo", "c.txt"): struct{}{},
},
},
{
options: DirectoryOptionsExcludeDirectories | DirectoryOptionsExcludeFiles,
expected: map[string]struct{}{},
},
{
options: DirectoryOptionsExcludeFiles | DirectoryOptionsRecursive | DirectoryOptionsExcludeDirectories,
expected: map[string]struct{}{},
},
}
for _, tc := range testCases {
subject.Options = tc.options
t.Run(fmt.Sprintf("%d", uint(tc.options)), func(t *testing.T) {
for entry := range subject.Enumerate(nil) {
cast := entry.(string)
if _, ok := tc.expected[cast]; !ok {
t.Logf("unexpected result: %q", cast)
t.Fail()
}
delete(tc.expected, cast)
}
if len(tc.expected) != 0 {
for unseenFile := range tc.expected {
t.Logf("missing file: %q", unseenFile)
}
t.Fail()
}
})
}
}
|