Add LoadFile
diff --git a/parser_test.go b/parser_test.go index 0db16d1..ce97375 100644 --- a/parser_test.go +++ b/parser_test.go
@@ -119,13 +119,30 @@ func TestMissingValue(t *testing.T) { _, err := Load("a = ") if (err.Error() != "expecting a value") { - t.Error("Bad error message:", err.Error()); + t.Error("Bad error message:", err.Error()) } } func TestUnterminatedArray(t *testing.T) { _, err := Load("a = [1,") if (err.Error() != "unterminated array") { - t.Error("Bad error message:", err.Error()); + t.Error("Bad error message:", err.Error()) } +} + +func TestMissingFile(t *testing.T) { + _, err := LoadFile("foo.toml") + if (err.Error() != "open foo.toml: no such file or directory") { + t.Error("Bad error message:", err.Error()) + } +} + +func TestParseFile(t *testing.T) { + tree, err := LoadFile("example.toml") + if (err != nil) { + t.Fatal("Non-nil error:", err.Error()) + } + assertTree(t, tree, map[string]interface{}{ + "a": [][]int64{[]int64{int64(42), int64(21)}, []int64{int64(10)}}, + }) } \ No newline at end of file
diff --git a/toml.go b/toml.go index 8e5d1a2..def8334 100644 --- a/toml.go +++ b/toml.go
@@ -6,6 +6,7 @@ import ( "errors" + "io/ioutil" "runtime" "strings" ) @@ -89,3 +90,16 @@ tree = parse(flow) return } + +// Create a TomlTree from a file. +func LoadFile(path string) (tree *TomlTree, err error) { + buff, ferr := ioutil.ReadFile(path) + if (ferr != nil) { + err = ferr + } else { + s := string(buff) + tree, err = Load(s) + } + + return +} \ No newline at end of file