github.com/rajeev159/opa@v0.45.0/ast/map_test.go (about)

     1  // Copyright 2016 The OPA Authors.  All rights reserved.
     2  // Use of this source code is governed by an Apache2
     3  // license that can be found in the LICENSE file.
     4  
     5  package ast
     6  
     7  import (
     8  	"reflect"
     9  	"sort"
    10  	"testing"
    11  )
    12  
    13  func TestValueMapOverwrite(t *testing.T) {
    14  
    15  	a := NewValueMap()
    16  	a.Put(String("x"), String("foo"))
    17  	a.Put(String("x"), String("bar"))
    18  	if a.Get(String("x")) != String("bar") {
    19  		t.Fatalf("Expected a['x'] = 'bar' but got: %v", a.Get(String("x")))
    20  	}
    21  
    22  }
    23  
    24  func TestValueMapIter(t *testing.T) {
    25  	a := NewValueMap()
    26  	a.Put(String("x"), String("foo"))
    27  	a.Put(String("y"), String("bar"))
    28  	a.Put(String("z"), String("baz"))
    29  	values := []string{}
    30  	a.Iter(func(k, v Value) bool {
    31  		values = append(values, string(v.(String)))
    32  		return false
    33  	})
    34  	sort.Strings(values)
    35  	expected := []string{"bar", "baz", "foo"}
    36  	if !reflect.DeepEqual(values, expected) {
    37  		t.Fatalf("Unexpected value from iteration: %v", values)
    38  	}
    39  }
    40  
    41  func TestValueMapCopy(t *testing.T) {
    42  	a := NewValueMap()
    43  	a.Put(String("x"), String("foo"))
    44  	a.Put(String("y"), String("bar"))
    45  	b := a.Copy()
    46  	b.Delete(String("y"))
    47  	if a.Get(String("y")) != String("bar") {
    48  		t.Fatalf("Unexpected a['y'] value: %v", a.Get(String("y")))
    49  	}
    50  }
    51  
    52  func TestValueMapEqual(t *testing.T) {
    53  	a := NewValueMap()
    54  	a.Put(String("x"), String("foo"))
    55  	a.Put(String("y"), String("bar"))
    56  	b := a.Copy()
    57  	if !a.Equal(b) {
    58  		t.Fatalf("Expected a == b but not for: %v / %v", a, b)
    59  	}
    60  	if a.Hash() != b.Hash() {
    61  		t.Fatalf("Expected a.Hash() == b.Hash() but not for: %v / %v", a, b)
    62  	}
    63  	a.Delete(String("x"))
    64  	if a.Equal(b) {
    65  		t.Fatalf("Expected a != b but not for: %v / %v", a, b)
    66  	}
    67  }
    68  
    69  func TestValueMapGetMissing(t *testing.T) {
    70  	a := NewValueMap()
    71  	a.Put(String("x"), String("foo"))
    72  	a.Put(String("y"), String("bar"))
    73  	if a.Get(String("z")) != nil {
    74  		t.Fatalf("Expected a['z'] = nil but got: %v", a.Get(String("z")))
    75  	}
    76  }
    77  
    78  func TestValueMapString(t *testing.T) {
    79  	a := NewValueMap()
    80  	a.Put(MustParseRef("a.b.c[x]"), String("foo"))
    81  	a.Put(Var("x"), Number("1"))
    82  	result := a.String()
    83  	o1 := `{a.b.c[x]: "foo", x: 1}`
    84  	o2 := `{x: 1, a.b.c[x]: "foo"}`
    85  	if result != o1 && result != o2 {
    86  		t.Fatalf("Expected string to equal either %v or %v but got: %v", o1, o2, result)
    87  	}
    88  }
    89  
    90  func TestValueMapNil(t *testing.T) {
    91  	var a *ValueMap
    92  	if a.Copy() != nil {
    93  		t.Fatalf("Expected nil map copy to be nil")
    94  	}
    95  	a.Delete(String("foo"))
    96  	var b *ValueMap
    97  	if !a.Equal(b) {
    98  		t.Fatalf("Expected nil maps to be equal")
    99  	}
   100  	b = NewValueMap()
   101  	if !a.Equal(b) {
   102  		t.Fatalf("Expected nil map to equal non-nil, empty map")
   103  	}
   104  	b.Put(String("foo"), String("bar"))
   105  	if a.Equal(b) {
   106  		t.Fatalf("Expected nil map to not equal non-empty map")
   107  	}
   108  	if b.Equal(a) {
   109  		t.Fatalf("Expected non-nil map to not equal nil map")
   110  	}
   111  	if a.Hash() != 0 {
   112  		t.Fatalf("Expected nil map to hash to zero")
   113  	}
   114  	if a.Iter(func(Value, Value) bool { return true }) {
   115  		t.Fatalf("Expected nil map iteration to return false")
   116  	}
   117  	if a.Len() != 0 {
   118  		t.Fatalf("Expected nil map length to be zero")
   119  	}
   120  	if a.String() != "{}" {
   121  		t.Fatalf("Expected nil map string to be {}")
   122  	}
   123  	defer func() {
   124  		if r := recover(); r == nil {
   125  			t.Fatalf("Expected put to panic")
   126  		}
   127  	}()
   128  	a.Put(String("foo"), String("bar"))
   129  }