From bf9aa098bb315e27e97ad8fc5b759ec96c29a7e7 Mon Sep 17 00:00:00 2001 From: greg-dennis Date: Tue, 23 Aug 2022 13:53:51 -0400 Subject: [PATCH] Fix SetProperty and add unit test. --- gtr/gtr.go | 2 +- gtr/gtr_test.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/gtr/gtr.go b/gtr/gtr.go index c205123..bb05d87 100644 --- a/gtr/gtr.go +++ b/gtr/gtr.go @@ -78,7 +78,7 @@ func (p *Package) SetProperty(key, value string) { // then add the specieid key-value property. i := 0 for _, prop := range p.Properties { - if key == prop.Name { + if key != prop.Name { p.Properties[i] = prop i++ } diff --git a/gtr/gtr_test.go b/gtr/gtr_test.go index 06cf3ae..06f1ce1 100644 --- a/gtr/gtr_test.go +++ b/gtr/gtr_test.go @@ -1,6 +1,10 @@ package gtr -import "testing" +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) func TestTrimPrefixSpaces(t *testing.T) { tests := []struct { @@ -24,3 +28,15 @@ func TestTrimPrefixSpaces(t *testing.T) { } } } + +func TestSetProperty(t *testing.T) { + pkg := Package{} + pkg.SetProperty("a", "b") + pkg.SetProperty("c", "d") + pkg.SetProperty("a", "e") + + want := []Property{{Name: "c", Value: "d"}, {Name: "a", Value: "e"}} + if diff := cmp.Diff(want, pkg.Properties); diff != "" { + t.Errorf("SetProperty got unexpected diff: %s", diff) + } +}