Add LoadFile
This commit is contained in:
+19
-2
@@ -119,13 +119,30 @@ func TestArrayNested(t *testing.T) {
|
|||||||
func TestMissingValue(t *testing.T) {
|
func TestMissingValue(t *testing.T) {
|
||||||
_, err := Load("a = ")
|
_, err := Load("a = ")
|
||||||
if (err.Error() != "expecting a value") {
|
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) {
|
func TestUnterminatedArray(t *testing.T) {
|
||||||
_, err := Load("a = [1,")
|
_, err := Load("a = [1,")
|
||||||
if (err.Error() != "unterminated array") {
|
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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -89,3 +90,16 @@ func Load(content string) (tree *TomlTree, err error) {
|
|||||||
tree = parse(flow)
|
tree = parse(flow)
|
||||||
return
|
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