github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/logging/structure/structure_test.go (about)

     1  // Copyright Monax Industries Limited
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package structure
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hyperledger/burrow/util/slice"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestValuesAndContext(t *testing.T) {
    15  	keyvals := []interface{}{"hello", 1, "dog", 2, "fish", 3, "fork", 5}
    16  	vals, ctx := ValuesAndContext(keyvals, "hello", "fish")
    17  	assert.Equal(t, map[string]interface{}{"hello": 1, "fish": 3}, vals)
    18  	assert.Equal(t, []interface{}{"dog", 2, "fork", 5}, ctx)
    19  }
    20  
    21  func TestKeyValuesMap(t *testing.T) {
    22  	keyvals := []interface{}{
    23  		[][]interface{}{{2}}, 3,
    24  		"hello", 1,
    25  		"fish", 3,
    26  		"dog", 2,
    27  		"fork", 5,
    28  	}
    29  	vals := KeyValuesMap(keyvals)
    30  	assert.Equal(t, map[string]interface{}{
    31  		"[[2]]": 3,
    32  		"hello": 1,
    33  		"fish":  3,
    34  		"dog":   2,
    35  		"fork":  5,
    36  	}, vals)
    37  }
    38  
    39  func TestVectorise(t *testing.T) {
    40  	kvs := []interface{}{
    41  		"scope", "lawnmower",
    42  		"hub", "budub",
    43  		"occupation", "fish brewer",
    44  		"scope", "hose pipe",
    45  		"flub", "dub",
    46  		"scope", "rake",
    47  		"flub", "brub",
    48  	}
    49  
    50  	kvsVector := Vectorise(kvs, "occupation", "scope")
    51  	// Vectorise scope
    52  	assert.Equal(t, []interface{}{
    53  		"scope", Vector{"lawnmower", "hose pipe", "rake"},
    54  		"hub", "budub",
    55  		"occupation", "fish brewer",
    56  		"flub", Vector{"dub", "brub"},
    57  	},
    58  		kvsVector)
    59  }
    60  
    61  func TestVector_String(t *testing.T) {
    62  	vec := Vector{"one", "two", "grue"}
    63  	assert.Equal(t, "[one two grue]", vec.String())
    64  }
    65  
    66  func TestRemoveKeys(t *testing.T) {
    67  	// Remove multiple of same key
    68  	assert.Equal(t, []interface{}{"Fish", 9},
    69  		RemoveKeys([]interface{}{"Foo", "Bar", "Fish", 9, "Foo", "Baz", "odd-key"},
    70  			"Foo"))
    71  
    72  	// Remove multiple different keys
    73  	assert.Equal(t, []interface{}{"Fish", 9},
    74  		RemoveKeys([]interface{}{"Foo", "Bar", "Fish", 9, "Foo", "Baz", "Bar", 89},
    75  			"Foo", "Bar"))
    76  
    77  	// Remove nothing but supply keys
    78  	assert.Equal(t, []interface{}{"Foo", "Bar", "Fish", 9},
    79  		RemoveKeys([]interface{}{"Foo", "Bar", "Fish", 9},
    80  			"A", "B", "C"))
    81  
    82  	// Remove nothing since no keys supplied
    83  	assert.Equal(t, []interface{}{"Foo", "Bar", "Fish", 9},
    84  		RemoveKeys([]interface{}{"Foo", "Bar", "Fish", 9}))
    85  }
    86  
    87  func TestDelete(t *testing.T) {
    88  	assert.Equal(t, []interface{}{1, 2, 4, 5}, Delete([]interface{}{1, 2, 3, 4, 5}, 2, 1))
    89  }
    90  
    91  func TestCopyPrepend(t *testing.T) {
    92  	assert.Equal(t, []interface{}{"three", 4, 1, "two"},
    93  		slice.CopyPrepend([]interface{}{1, "two"}, "three", 4))
    94  	assert.Equal(t, []interface{}{}, slice.CopyPrepend(nil))
    95  	assert.Equal(t, []interface{}{1}, slice.CopyPrepend(nil, 1))
    96  	assert.Equal(t, []interface{}{1}, slice.CopyPrepend([]interface{}{1}))
    97  }