Skip to content

Commit c6f4f09

Browse files
author
Takumasa Sakao
committed
Enable to parse multiple format
1 parent d3c4078 commit c6f4f09

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ ARTIFACTS_DIR=artifacts/${VERSION}
44
GITHUB_USERNAME=sachaos
55

66
test:
7-
go test -v ./format/life106
8-
go test -v ./format/rle
7+
go test -v ./format/...
98

109
prepare:
1110
go get github.com/gobuffalo/packr/packr

format/parse.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package format
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"io"
7+
"io/ioutil"
8+
9+
"github.com/sachaos/go-life/format/life106"
10+
"github.com/sachaos/go-life/format/rle"
11+
)
12+
13+
const (
14+
Life105 = iota
15+
Life106
16+
RLE
17+
)
18+
19+
func Parse(r io.Reader) ([][]bool, error) {
20+
b, err := ioutil.ReadAll(r)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
formatReader := bytes.NewReader(b)
26+
contentReader := bytes.NewReader(b)
27+
switch DetectFormat(formatReader) {
28+
case Life106:
29+
return life106.Parse(contentReader), nil
30+
default:
31+
return rle.Parse(contentReader), nil
32+
}
33+
}
34+
35+
func DetectFormat(r io.Reader) int {
36+
br := bufio.NewReader(r)
37+
38+
firstLine, _, _ := br.ReadLine()
39+
firstLineStr := string(firstLine)
40+
41+
switch firstLineStr {
42+
case "#Life 1.05":
43+
return Life105
44+
case "#Life 1.06":
45+
return Life106
46+
default:
47+
return RLE
48+
}
49+
}

format/parse_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package format
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestDetectFormat(t *testing.T) {
9+
testCases := []struct {
10+
Name string
11+
Filename string
12+
ExpectedFormat int
13+
}{
14+
{
15+
Name: "life106",
16+
Filename: "./life106/test/glider.life",
17+
ExpectedFormat: Life106,
18+
},
19+
{
20+
Name: "rle",
21+
Filename: "./rle/test/glider.rle",
22+
ExpectedFormat: RLE,
23+
},
24+
}
25+
26+
for _, testCase := range testCases {
27+
t.Run(testCase.Name, func(t *testing.T) {
28+
f, err := os.Open(testCase.Filename)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
detectedFormat := DetectFormat(f)
34+
if detectedFormat != testCase.ExpectedFormat {
35+
t.Errorf("ExpectedFormat: %v, but actually: %v", testCase.ExpectedFormat, detectedFormat)
36+
}
37+
})
38+
}
39+
}

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/gdamore/tcell"
1010
"github.com/gdamore/tcell/encoding"
11-
"github.com/sachaos/go-life/format/rle"
11+
"github.com/sachaos/go-life/format"
1212
"github.com/sachaos/go-life/preset"
1313
"github.com/urfave/cli"
1414
"io"
@@ -200,7 +200,10 @@ func main() {
200200
}
201201
}
202202

203-
defaultCells = rle.Parse(file)
203+
defaultCells, err = format.Parse(file)
204+
if err != nil {
205+
return err
206+
}
204207
}
205208

206209
return startGame(themes, presets, themeIndex, defaultCells)

0 commit comments

Comments
 (0)