package cnf import ( "bytes" "testing" ) func TestGet(t *testing.T) { tests := []struct { raw string key string }{ {`{ "key": "value" }`, "key"}, {`{ "ignore": "xxx", "key": "value" }`, "key"}, {`{ "parent": { "child": "value" } }`, "parent.child"}, {`{ "ignore": "xxx", "parent": { "child": "value" } }`, "parent.child"}, } for _, test := range tests { parser := new(Json) reader := bytes.NewBufferString(test.raw) // try to extract value err := parser.Parse(reader) if err != nil { t.Errorf("parse error: %s", err) continue } // extract value value, found := parser.Get(test.key) if !found { t.Errorf("expected a result, got none") continue } // check value if value != "value" { t.Errorf("expected 'value' got '%s'", value) } } } func TestGetNotString(t *testing.T) { tests := []struct { raw string key string }{ {`{ "key": ["value"] }`, "key"}, {`{ "key": {"subkey": "value"} }`, "key"}, {`{ "parent": { "child": [ "value" ] } }`, "parent.child"}, {`{ "parent": { "child": { "subkey": "value" } } }`, "parent.child"}, } for _, test := range tests { parser := new(Json) reader := bytes.NewBufferString(test.raw) // try to extract value err := parser.Parse(reader) if err != nil { t.Errorf("parse error: %s", err) continue } // extract value value, found := parser.Get(test.key) if found || len(value) > 0 { t.Errorf("expected no result, got '%s'", value) continue } } } func TestSetPathExistsAndIsString(t *testing.T) { tests := []struct { raw string key string value string }{ {`{ "key": "value" }`, "key", "newvalue"}, {`{ "ignore": "xxx", "key": "value" }`, "key", "newvalue"}, {`{ "parent": { "child": "value" } }`, "parent.child", "newvalue"}, {`{ "ignore": "xxx", "parent": { "child": "value" } }`, "parent.child", "newvalue"}, } for _, test := range tests { parser := new(Json) reader := bytes.NewBufferString(test.raw) // try to extract value err := parser.Parse(reader) if err != nil { t.Errorf("parse error: %s", err) continue } // update value if !parser.Set(test.key, test.value) { t.Errorf("cannot set '%s' to '%s'", test.key, test.value) continue } // check new value value, found := parser.Get(test.key) if !found { t.Errorf("expected a result, got none") continue } // check value if value != test.value { t.Errorf("expected '%s' got '%s'", test.value, value) } } } func TestSetCreatePath(t *testing.T) { tests := []struct { raw string key string value string }{ {`{ }`, "key", "newvalue"}, {`{ }`, "parent.child", "newvalue"}, {`{ }`, "parent.child.subchild", "newvalue"}, {`{ "ignore": "xxx" }`, "key", "newvalue"}, {`{ "parent": { } }`, "parent.child", "newvalue"}, {`{ "ignore": "xxx", "parent": { } }`, "parent.child", "newvalue"}, } for i, test := range tests { parser := new(Json) reader := bytes.NewBufferString(test.raw) // try to extract value err := parser.Parse(reader) if err != nil { t.Errorf("[%d] parse error: %s", i, err) continue } // update value if !parser.Set(test.key, test.value) { t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value) continue } // check new value value, found := parser.Get(test.key) if !found { t.Errorf("[%d] expected a result, got none", i) continue } // check value if value != test.value { t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value) } } }