github.com/ladydascalie/elvish@v0.0.0-20170703214355-2964dd3ece7f/edit/history/fuser_test.go (about) 1 package history 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 ) 8 9 func TestNewFuser(t *testing.T) { 10 mockError := errors.New("mock error") 11 _, err := NewFuser(&mockStore{oneOffError: mockError}) 12 if err != mockError { 13 t.Errorf("NewFuser -> error %v, want %v", err, mockError) 14 } 15 } 16 17 var fuserStore = &mockStore{cmds: []string{"store 1"}} 18 19 func TestFuser(t *testing.T) { 20 f, err := NewFuser(fuserStore) 21 if err != nil { 22 t.Errorf("NewFuser -> error %v, want nil", err) 23 } 24 25 // AddCmd should not add command to session history if backend has an error 26 // adding the command. 27 mockError := errors.New("mock error") 28 fuserStore.oneOffError = mockError 29 err = f.AddCmd("haha") 30 if err != mockError { 31 t.Errorf("AddCmd doesn't forward backend error") 32 } 33 if len(f.cmds) != 0 { 34 t.Errorf("AddCmd adds command to session history when backend errors") 35 } 36 37 // AddCmd should add command to both storage and session 38 f.AddCmd("session 1") 39 if !reflect.DeepEqual(fuserStore.cmds, []string{"store 1", "session 1"}) { 40 t.Errorf("AddCmd doesn't add command to backend storage") 41 } 42 if !reflect.DeepEqual(f.SessionCmds(), []string{"session 1"}) { 43 t.Errorf("AddCmd doesn't add command to session history") 44 } 45 46 // AllCmds should return all commands from the storage when the Fuser was 47 // created followed by session commands 48 fuserStore.AddCmd("other session 1") 49 fuserStore.AddCmd("other session 2") 50 f.AddCmd("session 2") 51 cmds, err := f.AllCmds() 52 if err != nil { 53 t.Errorf("AllCmds returns error") 54 } 55 if !reflect.DeepEqual(cmds, []string{"store 1", "session 1", "session 2"}) { 56 t.Errorf("AllCmds doesn't return all commands") 57 } 58 59 // AllCmds should forward backend storage error 60 mockError = errors.New("another mock error") 61 fuserStore.oneOffError = mockError 62 _, err = f.AllCmds() 63 if err != mockError { 64 t.Errorf("AllCmds doesn't forward backend error") 65 } 66 67 // Walker should return a walker that walks through all commands 68 w := f.Walker("") 69 wantCmd(t, w.Prev, 4, "session 2") 70 wantCmd(t, w.Prev, 1, "session 1") 71 wantCmd(t, w.Prev, 0, "store 1") 72 wantErr(t, w.Prev, ErrEndOfHistory) 73 }