github.com/elves/elvish@v0.15.0/pkg/eval/vars/callback_test.go (about)

     1  package vars
     2  
     3  import "testing"
     4  
     5  func TestFromSetGet(t *testing.T) {
     6  	getCalled := false
     7  	get := func() interface{} {
     8  		getCalled = true
     9  		return "cb"
    10  	}
    11  	var setCalledWith interface{}
    12  	set := func(v interface{}) error {
    13  		setCalledWith = v
    14  		return nil
    15  	}
    16  
    17  	v := FromSetGet(set, get)
    18  	if v.Get() != "cb" {
    19  		t.Errorf("cbVariable doesn't return value from callback")
    20  	}
    21  	if !getCalled {
    22  		t.Errorf("cbVariable doesn't call callback")
    23  	}
    24  	v.Set("setting")
    25  	if setCalledWith != "setting" {
    26  		t.Errorf("cbVariable.Set doesn't call setter with value")
    27  	}
    28  }
    29  
    30  func TestFromGet(t *testing.T) {
    31  	getCalled := false
    32  	get := func() interface{} {
    33  		getCalled = true
    34  		return "cb"
    35  	}
    36  	v := FromGet(get)
    37  	if v.Get() != "cb" {
    38  		t.Errorf("roCbVariable doesn't return value from callback")
    39  	}
    40  	if !getCalled {
    41  		t.Errorf("roCbVariable doesn't call callback")
    42  	}
    43  	if v.Set("lala") == nil {
    44  		t.Errorf("roCbVariable.Set doesn't error")
    45  	}
    46  }