blob: ad3ad9624f18e55d4e126fb42bca7bfedc8a62f1 [file] [log] [blame]
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package codifier
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestStoreKeyValue(t *testing.T) {
var p *Proc
if p.StoreKeyValue("k", "v") != nil {
t.Error("expected nil Proc")
}
p = NewProcFromString("foo").StoreKeyValue("k", "v")
if p == nil {
t.Error("expected non-nil Proc")
}
if p.Store["k"].(string) != "v" {
t.Error("value not stored")
}
p = p.StoreKeyValue("k", "v2")
if p != nil {
t.Error("expected nil Proc due to existing key")
}
}
func TestValue(t *testing.T) {
var p *Proc
if p.Value("k") != "" {
t.Error("expected blank due to nil Proc")
}
p = NewProcFromString("foo")
v := p.Value("k")
if v != "" {
t.Error("expected blank due to absent key")
}
p.Store["k"] = "v"
v = p.Value("k")
if v != "v" {
t.Error("expected key/value to be found")
}
}
func TestExtractValue(t *testing.T) {
var p *Proc
if p.ExtractValue("k") != nil {
t.Error("expected nil Proc")
}
p = NewProcFromString(`foo x="y" bar`)
if p.ExtractValue("k") == nil {
t.Error("expected non-nil Proc for absent key")
}
v := p.Value("x")
if v != "" {
t.Error("expected empty key/value to be found")
}
p = p.ExtractValue("x")
if p == nil {
t.Error("expected non-nil Proc")
}
v = p.Value("x")
if v != "y" {
t.Error("expected key/value to be found")
}
}
func TestExtractListOp(t *testing.T) {
var p *Proc
if p.ExtractList("l") != nil {
t.Error("expected nil Proc")
}
p = NewProcFromString(`foo l= [ w, x, y ,z]" bar`)
if p.ExtractList("k") != nil {
t.Error("expected nil Proc for absent key")
}
l := p.Value("l")
if l != "" {
t.Error("expected empty key/value to be found")
}
p = p.ExtractList("l")
if p == nil {
t.Error("expected non-nil Proc")
}
sl := p.ListValue("l")
if !cmp.Equal(sl, []string{"w", "x", "y", "z"}) {
t.Error("expected list to be found")
}
}
func TestListValue(t *testing.T) {
var p *Proc
if p.ListValue("l") != nil {
t.Error("expected nil Proc")
}
p = NewProcFromString("foo")
if p.ListValue("k") != nil {
t.Error("expected nil Proc for absent key")
}
p.Store["l"] = []string{"w", "x", "y", "z"}
sl := p.ListValue("l")
if !cmp.Equal(sl, []string{"w", "x", "y", "z"}) {
t.Error("expected list to be found")
}
}
func TestLastItemInList(t *testing.T) {
var p *Proc
if p.lastItemInList("l") != "" {
t.Error("expected blank due to nil Proc")
}
p = NewProcFromString("foo")
if p.lastItemInList("k") != "" {
t.Error("expected blank for absent key")
}
p.Store["l"] = []string{}
li := p.lastItemInList("l")
if li != "" {
t.Error("expected blank list to be found")
}
p.Store["l"] = []string{"w", "x", "y", "z"}
li = p.lastItemInList("l")
if li != "z" {
t.Error("expected list item to be found")
}
}
func TestCopyStore(t *testing.T) {
var p *Proc
if p.copyStore(newProc()) != nil {
t.Error("expected nil Proc")
}
p = newProc()
q := newProc()
if len(p.Store) != 0 { // Initially empty.
t.Error("copyStore failed on initial store length")
}
p = p.copyStore(q)
if len(p.Store) != 0 { // Copied empty.
t.Error("copyStore failed on initial store length")
}
q.Store["k1"] = "v1"
q.Store["k2"] = "v2"
p = p.copyStore(q)
if len(p.Store) != 2 {
t.Error("copyStore failed on store length")
}
for k, v := range p.Store {
if q.Store[k] != v {
t.Error("copyStore failed on store")
}
}
}
func TestClearStore(t *testing.T) {
var p *Proc
if p.ClearStore() != nil {
t.Error("expected nil Proc")
}
p = newProc()
p = p.ClearStore()
if len(p.Store) != 0 {
t.Error("ClearStore failed on initial store length")
}
p.Store["k1"] = "v1"
p.Store["k2"] = "v2"
if len(p.Store) != 2 {
t.Error("copy failed on store d")
}
p = p.ClearStore()
if len(p.Store) != 0 {
t.Error("ClearStore failed on store length")
}
}