Add LoadFile
This commit is contained in:
+19
-2
@@ -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)}},
|
||||
})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user