70 lines
2.7 KiB
Go
70 lines
2.7 KiB
Go
package toml
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestAST_ConstructStructure(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
p.setValue(newItem(pKey, "ding"), newItem(pInteger, 10))
|
|
p.setValue(newItem(pKey, "dong"), newItem(pString, "not a song"))
|
|
p.openTable(newItem(pKey, "key1", "key2 a"))
|
|
p.setValue(newItem(pKey, "dooh"), newItem(pBoolean, true))
|
|
p.setValue(newItem(pKey, "dah"), newItem(pBoolean, false))
|
|
p.openTable(newItem(pKey, "key1", "key2 b"))
|
|
p.setValue(newItem(pKey, "dieh"), newItem(pFloat, 1.111))
|
|
p.setValue(newItem(pKey, "duh"), newItem(pFloat, 1.18e-12))
|
|
return nil, p
|
|
}, "", `{"ding": 10, "dong": "not a song", "key1": {"key2 a": {"dah": false, "dooh": true}, "key2 b": {"dieh": 1.111, "duh": 1.18e-12}}}`)
|
|
}
|
|
|
|
func TestAST_StoreValueInRootTable(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
p.setValue(newItem(pKey, "key1"), newItem(pString, "value1"))
|
|
return p.setValue(newItem(pKey, "key2"), newItem(pString, "value2")), p
|
|
}, "", `{"key1": "value1", "key2": "value2"}`)
|
|
}
|
|
|
|
func TestAST_StoreValueWithMultipartKey_CreatesTableHierarchy(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
return p.setValue(newItem(pKey, "key1", "key2", "key3"), newItem(pString, "value")), p
|
|
}, "", `{"key1": {"key2": {"key3": "value"}}}`)
|
|
}
|
|
|
|
func TestAST_StoreValueWithMultipartKey_UnderSubtable_CreatesTableHierarchy(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
p.openTable(newItem(pKey, "tablekey1", "tablekey2"))
|
|
return p.setValue(newItem(pKey, "valuekey1", "valuekey2", "valuekey3"), newItem(pString, "value")), p
|
|
}, "", `{"tablekey1": {"tablekey2": {"valuekey1": {"valuekey2": {"valuekey3": "value"}}}}}`)
|
|
}
|
|
|
|
func TestAST_StoreDuplicateKeyInRootTable_ReturnsError(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
p.setValue(newItem(pKey, "key"), newItem(pString, "value"))
|
|
return p.setValue(newItem(pKey, "key"), newItem(pInteger, 321)), p
|
|
}, `Cannot store value: string item already exists at key "key"`, "")
|
|
}
|
|
|
|
func TestAST_GivenExistingTableAtKey_CreatingTableAtSameKey_ReturnsError(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
p.openTable(newItem(pKey, "key1", "key2"))
|
|
_, err := p.openTable(newItem(pKey, "key1", "key2"))
|
|
return err, p
|
|
}, `Cannot create table: table for key "key1"."key2" already exists`, "")
|
|
}
|
|
|
|
func TestAST_GivenExistingItemAtKey_CreatingTableAtSameKey_ReturnsError(t *testing.T) {
|
|
testAST(t, func() (error, *parser) {
|
|
p := newParser()
|
|
p.Root["key"] = newItem(pString, "value")
|
|
_, err := p.openTable(newItem(pKey, "key"))
|
|
return err, p
|
|
}, `Cannot create table: string item already exists at key "key"`, "")
|
|
}
|