Add LoadFile

This commit is contained in:
Thomas Pelletier
2013-03-22 17:14:16 +01:00
parent 0d489ff32c
commit 21a8fb6ee0
2 changed files with 33 additions and 2 deletions
+19 -2
View File
@@ -119,13 +119,30 @@ func TestArrayNested(t *testing.T) {
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)}},
})
}
+14
View File
@@ -6,6 +6,7 @@ package toml
import (
"errors"
"io/ioutil"
"runtime"
"strings"
)
@@ -89,3 +90,16 @@ func Load(content string) (tree *TomlTree, err error) {
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
}