Add injection backends
This commit is contained in:
parent
a7f50fed75
commit
9ee301fbeb
7 changed files with 290 additions and 4 deletions
102
internal/inject/inject_test.go
Normal file
102
internal/inject/inject_test.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package inject
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeClipboard struct {
|
||||
called bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeClipboard) WriteClipboard(ctx context.Context, text string) error {
|
||||
f.called = true
|
||||
return f.err
|
||||
}
|
||||
|
||||
type fakePaster struct {
|
||||
called bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakePaster) Paste(ctx context.Context) error {
|
||||
f.called = true
|
||||
return f.err
|
||||
}
|
||||
|
||||
type fakeTyper struct {
|
||||
called bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeTyper) TypeText(ctx context.Context, text string) error {
|
||||
f.called = true
|
||||
return f.err
|
||||
}
|
||||
|
||||
func TestClipboardBackend(t *testing.T) {
|
||||
cb := &fakeClipboard{}
|
||||
p := &fakePaster{}
|
||||
b := ClipboardBackend{Writer: cb, Paster: p}
|
||||
|
||||
err := b.Inject(context.Background(), "hello")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !cb.called || !p.called {
|
||||
t.Fatalf("expected clipboard and paster to be called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClipboardBackendClipboardError(t *testing.T) {
|
||||
cb := &fakeClipboard{err: errors.New("boom")}
|
||||
p := &fakePaster{}
|
||||
b := ClipboardBackend{Writer: cb, Paster: p}
|
||||
|
||||
err := b.Inject(context.Background(), "hello")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if !cb.called {
|
||||
t.Fatalf("expected clipboard to be called")
|
||||
}
|
||||
if p.called {
|
||||
t.Fatalf("did not expect paster to be called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectionBackend(t *testing.T) {
|
||||
typ := &fakeTyper{}
|
||||
b := InjectionBackend{Typer: typ}
|
||||
|
||||
err := b.Inject(context.Background(), "hello")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !typ.called {
|
||||
t.Fatalf("expected typer to be called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBackend(t *testing.T) {
|
||||
cb := &fakeClipboard{}
|
||||
p := &fakePaster{}
|
||||
typ := &fakeTyper{}
|
||||
|
||||
b, err := NewBackend("clipboard", Deps{Clipboard: cb, Paster: p, Typer: typ})
|
||||
if err != nil || b == nil {
|
||||
t.Fatalf("expected clipboard backend")
|
||||
}
|
||||
|
||||
b, err = NewBackend("injection", Deps{Clipboard: cb, Paster: p, Typer: typ})
|
||||
if err != nil || b == nil {
|
||||
t.Fatalf("expected injection backend")
|
||||
}
|
||||
|
||||
b, err = NewBackend("unknown", Deps{Clipboard: cb, Paster: p, Typer: typ})
|
||||
if err == nil || b != nil {
|
||||
t.Fatalf("expected error for unknown backend")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue