1// Testing support for go-toml 2 3package toml 4 5import ( 6 "testing" 7) 8 9func TestTomlHas(t *testing.T) { 10 tree, _ := Load(` 11 [test] 12 key = "value" 13 `) 14 15 if !tree.Has("test.key") { 16 t.Errorf("Has - expected test.key to exists") 17 } 18 19 if tree.Has("") { 20 t.Errorf("Should return false if the key is not provided") 21 } 22} 23 24func TestTomlGet(t *testing.T) { 25 tree, _ := Load(` 26 [test] 27 key = "value" 28 `) 29 30 if tree.Get("") != tree { 31 t.Errorf("Get should return the tree itself when given an empty path") 32 } 33 34 if tree.Get("test.key") != "value" { 35 t.Errorf("Get should return the value") 36 } 37 if tree.Get(`\`) != nil { 38 t.Errorf("should return nil when the key is malformed") 39 } 40} 41 42func TestTomlGetDefault(t *testing.T) { 43 tree, _ := Load(` 44 [test] 45 key = "value" 46 `) 47 48 if tree.GetDefault("", "hello") != tree { 49 t.Error("GetDefault should return the tree itself when given an empty path") 50 } 51 52 if tree.GetDefault("test.key", "hello") != "value" { 53 t.Error("Get should return the value") 54 } 55 56 if tree.GetDefault("whatever", "hello") != "hello" { 57 t.Error("GetDefault should return the default value if the key does not exist") 58 } 59} 60 61func TestTomlHasPath(t *testing.T) { 62 tree, _ := Load(` 63 [test] 64 key = "value" 65 `) 66 67 if !tree.HasPath([]string{"test", "key"}) { 68 t.Errorf("HasPath - expected test.key to exists") 69 } 70} 71 72func TestTomlGetPath(t *testing.T) { 73 node := newTree() 74 //TODO: set other node data 75 76 for idx, item := range []struct { 77 Path []string 78 Expected *Tree 79 }{ 80 { // empty path test 81 []string{}, 82 node, 83 }, 84 } { 85 result := node.GetPath(item.Path) 86 if result != item.Expected { 87 t.Errorf("GetPath[%d] %v - expected %v, got %v instead.", idx, item.Path, item.Expected, result) 88 } 89 } 90 91 tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6") 92 if tree.GetPath([]string{"whatever"}) != nil { 93 t.Error("GetPath should return nil when the key does not exist") 94 } 95} 96 97func TestTomlFromMap(t *testing.T) { 98 simpleMap := map[string]interface{}{"hello": 42} 99 tree, err := TreeFromMap(simpleMap) 100 if err != nil { 101 t.Fatal("unexpected error:", err) 102 } 103 if tree.Get("hello") != int64(42) { 104 t.Fatal("hello should be 42, not", tree.Get("hello")) 105 } 106} 107