From 5e4e8ae73c8ad6296bb1359c9121f95b5d6033d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Thu, 19 Sep 2024 21:37:55 +0200 Subject: [PATCH] feat: add delegate binding support (#107) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/binding/binding.go | 12 ++++++- pkg/binding/binding_test.go | 68 +++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/pkg/binding/binding.go b/pkg/binding/binding.go index e181032..e449acc 100644 --- a/pkg/binding/binding.go +++ b/pkg/binding/binding.go @@ -15,8 +15,18 @@ func (b *binding) Value() (any, error) { return b.value, nil } -func NewBinding(value any) Binding { +func NewBinding(value any) *binding { return &binding{ value: value, } } + +type delegate func() (any, error) + +func (b delegate) Value() (any, error) { + return b() +} + +func NewDelegate(delegate func() (any, error)) delegate { + return delegate +} diff --git a/pkg/binding/binding_test.go b/pkg/binding/binding_test.go index e3c7512..27a8ec7 100644 --- a/pkg/binding/binding_test.go +++ b/pkg/binding/binding_test.go @@ -1,15 +1,16 @@ package binding import ( - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func TestNewBinding(t *testing.T) { tests := []struct { name string value any - want Binding + want *binding }{{ name: "nil", value: nil, @@ -29,9 +30,8 @@ func TestNewBinding(t *testing.T) { }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewBinding(tt.value); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewBinding() = %v, want %v", got, tt.want) - } + got := NewBinding(tt.value) + assert.Equal(t, tt.want, got) }) } } @@ -65,16 +65,56 @@ func Test_binding_Value(t *testing.T) { }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - b := &binding{ - value: tt.value, - } - got, err := b.Value() - if (err != nil) != tt.wantErr { - t.Errorf("binding.Value() error = %v, wantErr %v", err, tt.wantErr) - return + subj := NewBinding(tt.value) + got, err := subj.Value() + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, got) } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("binding.Value() = %v, want %v", got, tt.want) + }) + } +} + +func Test_delegate_Value(t *testing.T) { + tests := []struct { + name string + value any + want any + wantErr bool + }{{ + name: "nil", + value: nil, + want: nil, + wantErr: false, + }, { + name: "int", + value: int(42), + want: int(42), + wantErr: false, + }, { + name: "string", + value: "42", + want: "42", + wantErr: false, + }, { + name: "array", + value: []any{"42", 42}, + want: []any{"42", 42}, + wantErr: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + subj := NewDelegate(func() (any, error) { + return tt.value, nil + }) + got, err := subj.Value() + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, got) } }) }