2015-04-08 05:30:25 +00:00
|
|
|
package kvbuilder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilder_basic(t *testing.T) {
|
|
|
|
var b Builder
|
2015-06-16 17:00:02 +00:00
|
|
|
err := b.Add("foo=bar", "bar=baz", "baz=")
|
2015-04-08 05:30:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
2015-06-16 17:00:02 +00:00
|
|
|
"baz": "",
|
2015-04-08 05:30:25 +00:00
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_escapedAt(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
err := b.Add("foo=bar", "bar=\\@baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "@baz",
|
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_stdin(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
b.Stdin = bytes.NewBufferString("baz")
|
|
|
|
err := b.Add("foo=bar", "bar=-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_stdinMap(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
b.Stdin = bytes.NewBufferString(`{"foo": "bar"}`)
|
|
|
|
err := b.Add("-", "bar=baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz",
|
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_stdinTwice(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
b.Stdin = bytes.NewBufferString(`{"foo": "bar"}`)
|
|
|
|
err := b.Add("-", "-")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 20:02:31 +00:00
|
|
|
|
|
|
|
func TestBuilder_sameKeyTwice(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
err := b.Add("foo=bar", "foo=baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": []interface{}{"bar", "baz"},
|
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_sameKeyMultipleTimes(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
err := b.Add("foo=bar", "foo=baz", "foo=bay", "foo=bax", "bar=baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"foo": []interface{}{"bar", "baz", "bay", "bax"},
|
|
|
|
"bar": "baz",
|
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 20:28:57 +00:00
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
func TestBuilder_specialCharactersInKey(t *testing.T) {
|
2017-07-12 20:28:57 +00:00
|
|
|
var b Builder
|
|
|
|
b.Stdin = bytes.NewBufferString("{\"foo\": \"bay\"}")
|
|
|
|
err := b.Add("@foo=bar", "-foo=baz", "-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"@foo": "bar",
|
|
|
|
"-foo": "baz",
|
|
|
|
"foo": "bay",
|
|
|
|
}
|
|
|
|
actual := b.Map()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|