Files
asciinema/commands/rec_test.go

102 lines
1.9 KiB
Go
Raw Normal View History

2014-08-03 19:50:39 +02:00
package commands_test
import (
"errors"
"io"
"testing"
2014-11-02 19:04:19 +01:00
"time"
2014-08-03 19:50:39 +02:00
"github.com/asciinema/asciinema-cli/api"
"github.com/asciinema/asciinema-cli/commands"
)
type testTerminal struct {
err error
}
func (t *testTerminal) Size() (int, int, error) {
return 15, 40, nil
}
func (t *testTerminal) Record(command string, stdoutCopy io.Writer) error {
if t.err != nil {
return t.err
}
stdoutCopy.Write([]byte("hello"))
2014-11-02 19:04:19 +01:00
stdoutCopy.Write([]byte("world"))
2014-08-03 19:50:39 +02:00
return nil
}
type testApi struct {
err error
t *testing.T
}
2014-11-02 19:04:19 +01:00
func (a *testApi) CreateAsciicast(frames []api.Frame, duration time.Duration, cols, rows int, command, title string) (string, error) {
if command != "ls" {
2014-08-03 19:50:39 +02:00
a.t.Errorf("expected command to be set on asciicast")
}
2014-11-02 19:04:19 +01:00
if title != "listing" {
2014-08-03 19:50:39 +02:00
a.t.Errorf("expected title to be set on asciicast")
}
2014-11-02 19:04:19 +01:00
if rows != 15 {
2014-08-03 19:50:39 +02:00
a.t.Errorf("expected rows to be set on asciicast")
}
2014-11-02 19:04:19 +01:00
if cols != 40 {
2014-08-03 19:50:39 +02:00
a.t.Errorf("expected cols to be set on asciicast")
}
2014-11-02 19:04:19 +01:00
stdout := string(frames[0].Data)
2014-08-03 19:50:39 +02:00
if stdout != "hello" {
2014-11-02 19:04:19 +01:00
a.t.Errorf(`expected frame data "%v", got "%v"`, "hello", stdout)
}
stdout = string(frames[1].Data)
if stdout != "world" {
a.t.Errorf(`expected frame data "%v", got "%v"`, "world", stdout)
2014-08-03 19:50:39 +02:00
}
if a.err != nil {
return "", a.err
}
return "http://the/url", nil
}
func TestRecordCommand_Execute(t *testing.T) {
recErr := errors.New("can't record")
apiErr := errors.New("can't upload")
var tests = []struct {
recordError error
apiError error
expectedError error
}{
{nil, nil, nil},
{recErr, nil, recErr},
{nil, apiErr, apiErr},
}
for _, test := range tests {
terminal := &testTerminal{err: test.recordError}
api := &testApi{err: test.apiError, t: t}
command := &commands.RecordCommand{
Command: "ls",
Title: "listing",
Terminal: terminal,
Api: api,
}
err := command.Execute(nil)
if err != test.expectedError {
t.Errorf("expected error %v, got %v", test.expectedError, err)
}
}
}