Skip to content

Commit 55849c7

Browse files
committed
Add words package and its test
1 parent 6c7a061 commit 55849c7

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed

kadai3-1/lfcd85/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
build: cmd/*.go typinggame/*.go
1+
build: cmd/*.go typinggame/*.go words/*.go
22
GO111MODULE=on go build -o bin/typinggame cmd/main.go
33

44
fmt:

kadai3-1/lfcd85/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/gopherdojo/dojo5/kadai3-1/lfcd85
22

33
go 1.12
4+
5+
require github.com/hashicorp/go-multierror v1.0.0

kadai3-1/lfcd85/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
2+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
3+
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
4+
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=

kadai3-1/lfcd85/testdata/abc.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a
2+
b
3+
c

kadai3-1/lfcd85/words/words.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package words
2+
3+
import (
4+
"bufio"
5+
"os"
6+
7+
"github.com/gopherdojo/dojo5/kadai3-1/lfcd85/typinggame"
8+
"github.com/hashicorp/go-multierror"
9+
)
10+
11+
func Import(path string) (typinggame.Words, error) {
12+
var words typinggame.Words
13+
var result error
14+
15+
f, err := os.Open(path)
16+
if err != nil {
17+
result = multierror.Append(result, err)
18+
}
19+
defer func() {
20+
if err := f.Close(); err != nil {
21+
result = multierror.Append(result, err)
22+
}
23+
}()
24+
25+
s := bufio.NewScanner(f)
26+
for s.Scan() {
27+
words = append(words, s.Text())
28+
}
29+
if err := s.Err(); err != nil {
30+
result = multierror.Append(result, err)
31+
}
32+
33+
return words, result
34+
}

kadai3-1/lfcd85/words/words_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package words_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gopherdojo/dojo5/kadai3-1/lfcd85/words"
7+
)
8+
9+
func TestImport(t *testing.T) {
10+
cases := []struct {
11+
path string
12+
firstWord string
13+
lastWord string
14+
length int
15+
}{
16+
{"../testdata/abc.txt", "a", "c", 3},
17+
{"../testdata/go_standard_library.txt", "archive", "unsafe", 154},
18+
}
19+
20+
for _, c := range cases {
21+
c := c
22+
t.Run(c.path, func(t *testing.T) {
23+
t.Parallel()
24+
25+
words, err := words.Import(c.path)
26+
if err != nil {
27+
t.Errorf("failed to Import %v: %v", c.path, err)
28+
}
29+
if words[0] != c.firstWord {
30+
t.Errorf("the first item of imported words is %v; it should be %v", words[0], c.firstWord)
31+
}
32+
if words[len(words)-1] != c.lastWord {
33+
t.Errorf("the first item of imported words is %v; it should be %v", words[0], c.lastWord)
34+
}
35+
if len(words) != c.length {
36+
t.Errorf("the length of imported words is %v; it should be %v", len(words), c.length)
37+
}
38+
})
39+
}
40+
}

0 commit comments

Comments
 (0)