mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-08 16:20:02 +00:00
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
|
package plugins
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"gopkg.in/yaml.v2"
|
||
|
)
|
||
|
|
||
|
func TestFieldCollectionJSONMarshal(t *testing.T) {
|
||
|
var (
|
||
|
buf = new(bytes.Buffer)
|
||
|
raw = `{"key1":"test1","key2":"test2"}`
|
||
|
f = NewFieldCollection()
|
||
|
)
|
||
|
|
||
|
if err := json.NewDecoder(strings.NewReader(raw)).Decode(f); err != nil {
|
||
|
t.Fatalf("Unable to unmarshal: %s", err)
|
||
|
}
|
||
|
|
||
|
if err := json.NewEncoder(buf).Encode(f); err != nil {
|
||
|
t.Fatalf("Unable to marshal: %s", err)
|
||
|
}
|
||
|
|
||
|
if raw != strings.TrimSpace(buf.String()) {
|
||
|
t.Errorf("Marshalled JSON does not match expectation: res=%s exp=%s", buf.String(), raw)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestFieldCollectionYAMLMarshal(t *testing.T) {
|
||
|
var (
|
||
|
buf = new(bytes.Buffer)
|
||
|
raw = "key1: test1\nkey2: test2"
|
||
|
f = NewFieldCollection()
|
||
|
)
|
||
|
|
||
|
if err := yaml.NewDecoder(strings.NewReader(raw)).Decode(f); err != nil {
|
||
|
t.Fatalf("Unable to unmarshal: %s", err)
|
||
|
}
|
||
|
|
||
|
if err := yaml.NewEncoder(buf).Encode(f); err != nil {
|
||
|
t.Fatalf("Unable to marshal: %s", err)
|
||
|
}
|
||
|
|
||
|
if raw != strings.TrimSpace(buf.String()) {
|
||
|
t.Errorf("Marshalled YAML does not match expectation: res=%s exp=%s", buf.String(), raw)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestFieldCollectionNilModify(t *testing.T) {
|
||
|
var f *FieldCollection
|
||
|
|
||
|
f.Set("foo", "bar")
|
||
|
|
||
|
f = nil
|
||
|
f.SetFromData(map[string]interface{}{"foo": "bar"})
|
||
|
}
|
||
|
|
||
|
func TestFieldCollectionNilClone(t *testing.T) {
|
||
|
var f *FieldCollection
|
||
|
|
||
|
f.Clone()
|
||
|
}
|
||
|
|
||
|
func TestFieldCollectionNilDataGet(t *testing.T) {
|
||
|
var f *FieldCollection
|
||
|
|
||
|
for name, fn := range map[string]func(name string) bool{
|
||
|
"bool": f.CanBool,
|
||
|
"duration": f.CanDuration,
|
||
|
"int64": f.CanInt64,
|
||
|
"string": f.CanString,
|
||
|
} {
|
||
|
if fn("foo") {
|
||
|
t.Errorf("%s key is available", name)
|
||
|
}
|
||
|
}
|
||
|
}
|