|
| 1 | +package loci |
| 2 | + |
| 3 | +import ( |
| 4 | + "std" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gno.land/p/demo/testutils" |
| 8 | +) |
| 9 | + |
| 10 | +func TestLociStore(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + |
| 13 | + u1 := testutils.TestAddress("u1") |
| 14 | + u2 := testutils.TestAddress("u1") |
| 15 | + |
| 16 | + t.Run("TestSet", func(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + store := New() |
| 19 | + u1 := testutils.TestAddress("u1") |
| 20 | + |
| 21 | + m1 := []byte("hello") |
| 22 | + m2 := []byte("world") |
| 23 | + std.TestSetOrigCaller(u1) |
| 24 | + |
| 25 | + // Ensure that the value is nil before setting it. |
| 26 | + r1 := store.Get(u1) |
| 27 | + if r1 != nil { |
| 28 | + t.Errorf("expected value to be nil, got '%s'", r1) |
| 29 | + } |
| 30 | + store.Set(m1) |
| 31 | + // Ensure that the value is correct after setting it. |
| 32 | + r2 := store.Get(u1) |
| 33 | + if string(r2) != "hello" { |
| 34 | + t.Errorf("expected value to be 'hello', got '%s'", r2) |
| 35 | + } |
| 36 | + store.Set(m2) |
| 37 | + // Ensure that the value is correct after overwriting it. |
| 38 | + r3 := store.Get(u1) |
| 39 | + if string(r3) != "world" { |
| 40 | + t.Errorf("expected value to be 'world', got '%s'", r3) |
| 41 | + } |
| 42 | + }) |
| 43 | + t.Run("TestGet", func(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + store := New() |
| 46 | + u1 := testutils.TestAddress("u1") |
| 47 | + u2 := testutils.TestAddress("u2") |
| 48 | + u3 := testutils.TestAddress("u3") |
| 49 | + u4 := testutils.TestAddress("u4") |
| 50 | + |
| 51 | + m1 := []byte("hello") |
| 52 | + m2 := []byte("world") |
| 53 | + m3 := []byte("goodbye") |
| 54 | + |
| 55 | + std.TestSetOrigCaller(u1) |
| 56 | + store.Set(m1) |
| 57 | + std.TestSetOrigCaller(u2) |
| 58 | + store.Set(m2) |
| 59 | + std.TestSetOrigCaller(u3) |
| 60 | + store.Set(m3) |
| 61 | + |
| 62 | + // Ensure that the value is correct after setting it. |
| 63 | + r0 := store.Get(u4) |
| 64 | + if r0 != nil { |
| 65 | + t.Errorf("expected value to be nil, got '%s'", r0) |
| 66 | + } |
| 67 | + // Ensure that the value is correct after setting it. |
| 68 | + r1 := store.Get(u1) |
| 69 | + if string(r1) != "hello" { |
| 70 | + t.Errorf("expected value to be 'hello', got '%s'", r1) |
| 71 | + } |
| 72 | + // Ensure that the value is correct after setting it. |
| 73 | + r2 := store.Get(u2) |
| 74 | + if string(r2) != "world" { |
| 75 | + t.Errorf("expected value to be 'world', got '%s'", r2) |
| 76 | + } |
| 77 | + // Ensure that the value is correct after setting it. |
| 78 | + r3 := store.Get(u3) |
| 79 | + if string(r3) != "goodbye" { |
| 80 | + t.Errorf("expected value to be 'goodbye', got '%s'", r3) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | +} |
0 commit comments