Permalink
Browse files

First ParseConfig implementation, shaky tests

  • Loading branch information...
1 parent 1d3bd77 commit 9a8836d42f92d3342ed847b9bda6acb9f703e0ed @mrnugget committed Mar 26, 2013
Showing with 59 additions and 3 deletions.
  1. +27 −1 config.go
  2. +31 −1 config_test.go
  3. +1 −1 fixtures/example_config.json
View
@@ -1,11 +1,37 @@
package main
+import (
+ "encoding/json"
+ "io/ioutil"
+)
+
+var f interface{}
+
type Rule struct {
Path string
Run string
}
func ParseConfig(path string) (rules []*Rule, err error) {
rules = make([]*Rule, 0)
- return
+
+ c, err := ioutil.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+
+ err = json.Unmarshal(c, &f)
+ if err != nil {
+ return nil, err
+ }
+
+ config := f.(map[string]interface{})
+ for path, v := range config {
+ attributes := v.(map[string]interface{})
+ run := attributes["run"].(string)
+
+ rules = append(rules, &Rule{path, run})
+ }
+
+ return rules, nil
}
View
@@ -4,9 +4,39 @@ import (
"testing"
)
+var parseTests = []struct {
+ index int
+ expectedPath string
+ expectedRun string
+}{
+ {
+ 0,
+ "/tmp/foo",
+ "/usr/local/bar/foobar",
+ },
+ {
+ 1,
+ "/tmp/bar",
+ "/usr/local/bar/barfoo",
+ },
+}
+
func TestParseConfig(t *testing.T) {
- _, err := ParseConfig(fixtures + "/example_config.json")
+ rules, err := ParseConfig(fixtures + "/example_config.json")
if err != nil {
t.Fatal(err)
}
+ if len(rules) != 2 {
+ t.Fatal("Did not include all rules")
+ }
+
+ for _, pt := range parseTests {
+ actualPath := rules[pt.index].Path
+ actualRun := rules[pt.index].Run
+
+ if actualPath != pt.expectedPath && actualRun != pt.expectedRun {
+ t.Errorf("ACTUALPATH %v - EXPECTEDPATH %v, ACTUALRUN %v - EXPECTEDRUN %v", actualPath, pt.expectedPath, actualRun, pt.expectedRun)
+ continue
+ }
+ }
}
@@ -3,6 +3,6 @@
"run": "/usr/local/bar/foobar"
},
"/tmp/bar": {
- "run": "/usr/local/bar/foobar"
+ "run": "/usr/local/bar/barfoo"
}
}

0 comments on commit 9a8836d

Please sign in to comment.