github.com/gopherd/gonum@v0.0.4/graph/encoding/encoding_test.go (about)

     1  // Copyright ©2021 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package encoding
     6  
     7  import (
     8  	"sort"
     9  	"testing"
    10  )
    11  
    12  var setAttributesTests = []struct {
    13  	attr   *Attributes
    14  	opName string
    15  	op     func(AttributeSetter) error
    16  	want   *Attributes
    17  }{
    18  	{
    19  		attr:   &Attributes{},
    20  		opName: "noop",
    21  		op: func(a AttributeSetter) error {
    22  			return a.SetAttribute(Attribute{Key: "", Value: "bar"})
    23  		},
    24  		want: &Attributes{},
    25  	},
    26  	{
    27  		attr:   &Attributes{},
    28  		opName: "add attr to empty",
    29  		op: func(a AttributeSetter) error {
    30  			return a.SetAttribute(Attribute{Key: "foo", Value: "bar"})
    31  		},
    32  		want: &Attributes{{Key: "foo", Value: "bar"}},
    33  	},
    34  	{
    35  		attr:   &Attributes{},
    36  		opName: "remove attr from empty",
    37  		op: func(a AttributeSetter) error {
    38  			return a.SetAttribute(Attribute{Key: "foo", Value: ""})
    39  		},
    40  		want: &Attributes{},
    41  	},
    42  	{
    43  		attr:   &Attributes{{Key: "foo", Value: "bar"}},
    44  		opName: "add attr to non-empty",
    45  		op: func(a AttributeSetter) error {
    46  			return a.SetAttribute(Attribute{Key: "bif", Value: "fud"})
    47  		},
    48  		want: &Attributes{{Key: "foo", Value: "bar"}, {Key: "bif", Value: "fud"}},
    49  	},
    50  	{
    51  		attr:   &Attributes{{Key: "foo", Value: "bar"}},
    52  		opName: "remove attr from singleton",
    53  		op: func(a AttributeSetter) error {
    54  			return a.SetAttribute(Attribute{Key: "foo", Value: ""})
    55  		},
    56  		want: &Attributes{},
    57  	},
    58  	{
    59  		attr:   &Attributes{{Key: "foo", Value: "bar"}, {Key: "bif", Value: "fud"}},
    60  		opName: "remove first attr from pair",
    61  		op: func(a AttributeSetter) error {
    62  			return a.SetAttribute(Attribute{Key: "foo", Value: ""})
    63  		},
    64  		want: &Attributes{{Key: "bif", Value: "fud"}},
    65  	},
    66  	{
    67  		attr:   &Attributes{{Key: "foo", Value: "bar"}, {Key: "bif", Value: "fud"}},
    68  		opName: "remove second attr from pair",
    69  		op: func(a AttributeSetter) error {
    70  			return a.SetAttribute(Attribute{Key: "bif", Value: ""})
    71  		},
    72  		want: &Attributes{{Key: "foo", Value: "bar"}},
    73  	},
    74  	{
    75  		attr:   &Attributes{{Key: "foo", Value: "bar"}, {Key: "bif", Value: "fud"}},
    76  		opName: "replace first attr in pair",
    77  		op: func(a AttributeSetter) error {
    78  			return a.SetAttribute(Attribute{Key: "foo", Value: "not bar"})
    79  		},
    80  		want: &Attributes{{Key: "foo", Value: "not bar"}, {Key: "bif", Value: "fud"}},
    81  	},
    82  	{
    83  		attr:   &Attributes{{Key: "foo", Value: "bar"}, {Key: "bif", Value: "fud"}},
    84  		opName: "replace second attr in pair",
    85  		op: func(a AttributeSetter) error {
    86  			return a.SetAttribute(Attribute{Key: "bif", Value: "not fud"})
    87  		},
    88  		want: &Attributes{{Key: "foo", Value: "bar"}, {Key: "bif", Value: "not fud"}},
    89  	},
    90  }
    91  
    92  func TestSetAttributes(t *testing.T) {
    93  	for _, test := range setAttributesTests {
    94  		err := test.op(test.attr)
    95  		if err != nil {
    96  			t.Errorf("unexpected error for %q: %v", test.opName, err)
    97  			continue
    98  		}
    99  		if !sameAttributes(test.attr, test.want) {
   100  			t.Errorf("unexpected result from %q:\ngot: %+v\nwant:%+v", test.opName, test.attr, test.want)
   101  		}
   102  	}
   103  }
   104  
   105  func sameAttributes(a, b Attributer) bool {
   106  	aAttr := a.Attributes()
   107  	bAttr := b.Attributes()
   108  	if len(aAttr) != len(bAttr) {
   109  		return false
   110  	}
   111  	aAttr = append(aAttr[:0:0], aAttr...)
   112  	sort.Slice(aAttr, func(i, j int) bool { return aAttr[i].Key < aAttr[j].Key })
   113  	bAttr = append(bAttr[:0:0], bAttr...)
   114  	sort.Slice(bAttr, func(i, j int) bool { return bAttr[i].Key < bAttr[j].Key })
   115  	for i, a := range aAttr {
   116  		if bAttr[i] != a {
   117  			return false
   118  		}
   119  	}
   120  	return true
   121  }