forked from snychka/golang-cli-playbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule5_test.go
77 lines (65 loc) · 1.57 KB
/
module5_test.go
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
package module5
import (
"bufio"
"log"
"os"
"path"
"strings"
"testing"
)
func TestModule5goimportsContent(t *testing.T) {
expected := " \"fmt\""
found := OpenFileAndFindNthString("./module5_code.go", 0, expected)
if found != true {
t.Errorf("the fmt package is not found")
}
expected = " \"net/http\""
found = OpenFileAndFindNthString("./module5_code.go", 0, expected)
if found != true {
t.Errorf("the net/http package is not found")
}
}
// OpenFileAndFindNthString opens a file, look for Nth string splitted by a space, and return if given expected string is found or not
func OpenFileAndFindNthString(filename string, nth int, expected string) bool {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
t := scanner.Text()
trimmed := strings.Trim(t, " ")
if trimmed == "" {
continue
}
// matching logic
ss := strings.Split(trimmed, " ")
if ss[nth] == expected {
return true
}
}
return false
}
// FindFileAtPath returns if theFilename is found at thePath
func FindFileAtPath(thePath string, theFilename string) bool {
if _, err := os.Stat(path.Join(thePath, theFilename)); os.IsNotExist(err) {
return false
}
return true
}
// OpenFileAndCountLines opens a file and returns number of lines in the file
func OpenFileAndCountLines(filename string) int {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
number := 0
for scanner.Scan() {
number++
}
return number
}