73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package toml
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestAST_ConstructStructure(t *testing.T) {
|
|
p := newParser()
|
|
p.Root["ding"] = newItem(pInteger, 10)
|
|
p.Root["dong"] = newItem(pString, "not a song")
|
|
subTable1, _ := p.newTable(newItem(pKey, "key1", "key2 a"))
|
|
subTable1["dooh"] = newItem(pBoolean, true)
|
|
subTable1["dah"] = newItem(pBoolean, false)
|
|
subTable2, _ := p.newTable(newItem(pKey, "key1", "key2 b"))
|
|
subTable2["dieh"] = newItem(pFloat, 1.111)
|
|
subTable2["duhh"] = newItem(pFloat, 1.18e-12)
|
|
}
|
|
|
|
func TestAST_StoreValueInRootTable(t *testing.T) {
|
|
testError(t, func() error {
|
|
p := newParser()
|
|
p.setValue(newItem(pKey, "key1"), newItem(pString, "value1"))
|
|
return p.setValue(newItem(pKey, "key2"), newItem(pString, "value2"))
|
|
}, "")
|
|
}
|
|
|
|
func TestAST_StoreValueWithMultipartKey_CreatesTableHierarchy(t *testing.T) {
|
|
testError(t, func() error {
|
|
p := newParser()
|
|
return p.setValue(newItem(pKey, "key1", "key2", "key3"), newItem(pString, "value"))
|
|
}, "")
|
|
// TODO an actual test assertion
|
|
}
|
|
|
|
func TestAST_StoreValueWithMultipartKey_UnderSubtable_CreatesTableHierarchy(t *testing.T) {
|
|
testError(t, func() error {
|
|
p := newParser()
|
|
p.newTable(newItem(pKey, "subkey1", "subkey2"))
|
|
err := p.setValue(newItem(pKey, "key1", "key2", "key3"), newItem(pString, "value"))
|
|
fmt.Printf("%s", p.Root)
|
|
return err
|
|
}, "")
|
|
t.Fail()
|
|
// TODO an actual test assertion
|
|
}
|
|
|
|
func TestAST_StoreDuplicateKeyInRootTable_ReturnsError(t *testing.T) {
|
|
testError(t, func() error {
|
|
p := newParser()
|
|
p.setValue(newItem(pKey, "key"), newItem(pString, "value"))
|
|
return p.setValue(newItem(pKey, "key"), newItem(pInteger, 321))
|
|
}, `Cannot store value: string item already exists at key "key"`)
|
|
}
|
|
|
|
func TestAST_GivenExistingTableAtKey_CreatingTableAtSameKey_ReturnsError(t *testing.T) {
|
|
testError(t, func() error {
|
|
p := newParser()
|
|
p.newTable(newItem(pKey, "key1", "key2"))
|
|
_, err := p.newTable(newItem(pKey, "key1", "key2"))
|
|
return err
|
|
}, `Cannot create table: table for key "key1"."key2" already exists`)
|
|
}
|
|
|
|
func TestAST_GivenExistingItemAtKey_CreatingTableAtSameKey_ReturnsError(t *testing.T) {
|
|
testError(t, func() error {
|
|
p := newParser()
|
|
p.Root["key"] = newItem(pString, "value")
|
|
_, err := p.newTable(newItem(pKey, "key"))
|
|
return err
|
|
}, `Cannot create table: string item already exists at key "key"`)
|
|
}
|