|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package view_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors" |
| 13 | + "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/view" |
| 14 | + view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/view" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +type Hello struct { |
| 20 | + Message string |
| 21 | +} |
| 22 | + |
| 23 | +type HelloView struct { |
| 24 | + Hello |
| 25 | +} |
| 26 | + |
| 27 | +func (h *HelloView) Call(context view2.Context) (any, error) { |
| 28 | + return h.Message, nil |
| 29 | +} |
| 30 | + |
| 31 | +type HelloLocalFactory struct{} |
| 32 | + |
| 33 | +func (h *HelloLocalFactory) NewViewWithArg(arg any) (view2.View, error) { |
| 34 | + hello, ok := arg.(*Hello) |
| 35 | + if !ok || arg == nil { |
| 36 | + return nil, errors.New("arg is not a Hello") |
| 37 | + } |
| 38 | + return &HelloView{Hello: *hello}, nil |
| 39 | +} |
| 40 | + |
| 41 | +func TestNewLocalView(t *testing.T) { |
| 42 | + registry := view.NewRegistry() |
| 43 | + require.NoError(t, registry.RegisterLocalFactory("hello", &HelloLocalFactory{})) |
| 44 | + |
| 45 | + view, err := registry.NewLocalView("hello", &Hello{Message: "hello world"}) |
| 46 | + require.NoError(t, err) |
| 47 | + msgBoxed, err := view.Call(nil) |
| 48 | + require.NoError(t, err) |
| 49 | + msg, ok := msgBoxed.(string) |
| 50 | + assert.True(t, ok) |
| 51 | + assert.Equal(t, msg, "hello world") |
| 52 | +} |
0 commit comments