github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/keystore/keystore_test.gno (about)

     1  package keystore
     2  
     3  import (
     4  	"fmt"
     5  	"std"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gno.land/p/demo/testutils"
    10  )
    11  
    12  func TestRender(t *testing.T) {
    13  	const (
    14  		author1 std.Address = testutils.TestAddress("author1")
    15  		author2 std.Address = testutils.TestAddress("author2")
    16  	)
    17  
    18  	tt := []struct {
    19  		caller std.Address
    20  		owner  std.Address
    21  		ps     []string
    22  		exp    string
    23  	}{
    24  		// can set database if the owner is the caller
    25  		{author1, author1, []string{"set", "hello", "gno"}, StatusOK},
    26  		{author1, author1, []string{"size"}, "1"},
    27  		{author1, author1, []string{"set", "hello", "world"}, StatusOK},
    28  		{author1, author1, []string{"size"}, "1"},
    29  		{author1, author1, []string{"set", "hi", "gno"}, StatusOK},
    30  		{author1, author1, []string{"size"}, "2"},
    31  		// only owner can remove
    32  		{author1, author1, []string{"remove", "hi"}, StatusOK},
    33  		{author1, author1, []string{"get", "hi"}, StatusNotFound},
    34  		{author1, author1, []string{"size"}, "1"},
    35  		// add back
    36  		{author1, author1, []string{"set", "hi", "gno"}, StatusOK},
    37  		{author1, author1, []string{"size"}, "2"},
    38  
    39  		// different owner has different database
    40  		{author2, author2, []string{"set", "hello", "universe"}, StatusOK},
    41  		// either author can get the other info
    42  		{author1, author2, []string{"get", "hello"}, "universe"},
    43  		// either author can get the other info
    44  		{author2, author1, []string{"get", "hello"}, "world"},
    45  		{author1, author2, []string{"get", "hello"}, "universe"},
    46  		// anyone can view the databases
    47  		{author1, author2, []string{}, `- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/keystore:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) (2 keys)
    48  - [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/keystore:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) (1 keys)`},
    49  		// anyone can view the keys in a database
    50  		{author1, author2, []string{""}, `# g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00 database
    51  
    52  - 0 [hello](/r/demo/keystore:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00:get:hello)`},
    53  	}
    54  	for _, tc := range tt {
    55  		p := ""
    56  		if len(tc.ps) > 0 {
    57  			p = tc.owner.String()
    58  			for i, psv := range tc.ps {
    59  				p += ":" + psv
    60  			}
    61  		}
    62  		p = strings.TrimSuffix(p, ":")
    63  		t.Run(p, func(t *testing.T) {
    64  			std.TestSetOrigCaller(tc.caller)
    65  			var act string
    66  			if len(tc.ps) > 0 && tc.ps[0] == "set" {
    67  				act = strings.TrimSpace(Set(tc.ps[1], tc.ps[2]))
    68  			} else if len(tc.ps) > 0 && tc.ps[0] == "remove" {
    69  				act = strings.TrimSpace(Remove(tc.ps[1]))
    70  			} else {
    71  				act = strings.TrimSpace(Render(p))
    72  			}
    73  			if act != tc.exp {
    74  				t.Errorf("%v -> '%s', got '%s', wanted '%s'", tc.ps, p, act, tc.exp)
    75  			}
    76  		})
    77  	}
    78  }