github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/core/elvish/store/shared_var_test.go (about) 1 package store 2 3 import "testing" 4 5 func TestSharedVar(t *testing.T) { 6 tStore := NewSharedVar() 7 varname := "foo" 8 value1 := "lorem ipsum" 9 value2 := "o mores, o tempora" 10 11 // Getting an nonexistent variable should return ErrNoVar. 12 _, err := tStore.SharedVar(varname) 13 if err != ErrNoVar { 14 t.Error("want ErrNoVar, got", err) 15 } 16 17 // Setting a variable for the first time creates it. 18 err = tStore.SetSharedVar(varname, value1) 19 if err != nil { 20 t.Error("want no error, got", err) 21 } 22 v, err := tStore.SharedVar(varname) 23 if v != value1 || err != nil { 24 t.Errorf("want %q and no error, got %q and %v", value1, v, err) 25 } 26 27 // Setting an existing variable updates its value. 28 err = tStore.SetSharedVar(varname, value2) 29 if err != nil { 30 t.Error("want no error, got", err) 31 } 32 v, err = tStore.SharedVar(varname) 33 if v != value2 || err != nil { 34 t.Errorf("want %q and no error, got %q and %v", value2, v, err) 35 } 36 37 // After deleting a variable, access to it cause ErrNoVar. 38 err = tStore.DelSharedVar(varname) 39 if err != nil { 40 t.Error("want no error, got", err) 41 } 42 _, err = tStore.SharedVar(varname) 43 if err != ErrNoVar { 44 t.Error("want ErrNoVar, got", err) 45 } 46 }