M main.go => main.go +17 -8
@@ 184,15 184,11 @@ func load() ([]job, error) {
var jobs []job
scanner := bufio.NewScanner(file)
for scanner.Scan() {
- line := scanner.Text()
- parsed := (strings.Split(line, ":"))
- if len(parsed) != 2 {
- return nil, fmt.Errorf("Parsing error: '%s'", line)
+ job, err := parse(scanner.Text())
+ if err != nil {
+ return nil, err
}
- jobs = append(jobs, job{
- name: strings.TrimSpace(parsed[0]),
- cmd: strings.Split(strings.TrimSpace(parsed[1]), " "),
- })
+ jobs = append(jobs, job)
}
if err := scanner.Err(); err != nil {
@@ 201,3 197,16 @@ func load() ([]job, error) {
return jobs, nil
}
+
+func parse(s string) (job, error) {
+ parsed := strings.SplitN(s, ":", 2)
+
+ if len(parsed) < 2 {
+ return job{}, fmt.Errorf("Parsing error: '%s'", s)
+ }
+
+ return job{
+ name: strings.TrimSpace(parsed[0]),
+ cmd: strings.Split(strings.TrimSpace(parsed[1]), " "),
+ }, nil
+}
A main_test.go => main_test.go +40 -0
@@ 0,0 1,40 @@
+package main
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestParse1(t *testing.T) {
+ j, err := parse("google: ping google.com")
+
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+
+ expected := job{
+ name: "google",
+ cmd: []string{"ping", "google.com"},
+ }
+
+ if !reflect.DeepEqual(j, expected) {
+ t.Errorf("expected %v, but got: %v", expected, j)
+ }
+}
+
+func TestParse2(t *testing.T) {
+ j, err := parse("extracolon: something -abc http://google.com")
+
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+
+ expected := job{
+ name: "extracolon",
+ cmd: []string{"something", "-abc", "http://google.com"},
+ }
+
+ if !reflect.DeepEqual(j, expected) {
+ t.Errorf("expected %v, but got: %v", expected, j)
+ }
+}