From adffe23d6c5d8b0be089bae60db491759cee198d Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Mon, 29 Oct 2018 20:58:55 +0800 Subject: [PATCH] add the mock terminal --- convert/resize_test.go | 68 +++++++++++++++++++++++++++++++++ terminal/mocks/terminal_mock.go | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 terminal/mocks/terminal_mock.go diff --git a/convert/resize_test.go b/convert/resize_test.go index c1f9723..bcdc9ce 100644 --- a/convert/resize_test.go +++ b/convert/resize_test.go @@ -3,6 +3,7 @@ package convert import ( "fmt" terminal2 "github.com/qeesung/image2ascii/terminal" + "github.com/qeesung/image2ascii/terminal/mocks" "github.com/stretchr/testify/assert" "log" "testing" @@ -125,6 +126,73 @@ func TestCalcFitSize(t *testing.T) { } } +func TestFitTheTerminalScreenSize(t *testing.T) { + assertions := assert.New(t) + // mock + terminalMock := mocks.Terminal{} + terminalMock.On("IsWindows").Return(false) + terminalMock.On("CharWidth").Return(0.5) + terminalMock.On("ScreenSize").Return(100, 80, nil) + handler := ImageResizeHandler{ + terminal: &terminalMock, + } + + imageFilePath := "testdata/cat_2000x1500.jpg" + img, err := OpenImageFile(imageFilePath) + if err != nil { + log.Fatal("open image file " + imageFilePath + " failed") + } + + assertions.False(terminalMock.IsWindows()) + assertions.Equal(terminalMock.CharWidth(), 0.5) + screenWidth, screenHeight, err := terminalMock.ScreenSize() + assertions.Equal(screenWidth, 100) + assertions.Equal(screenHeight, 80) + assertions.True(err == nil) + + options := DefaultOptions + options.Colored = false + options.FitScreen = true + scaledImage := handler.ScaleImage(img, &options) + sz := scaledImage.Bounds() + assertions.Equal(100, sz.Max.X) + assertions.Equal(37, sz.Max.Y) +} + +func TestStretchTheTerminalScreenSize(t *testing.T) { + assertions := assert.New(t) + // mock + terminalMock := mocks.Terminal{} + terminalMock.On("IsWindows").Return(false) + terminalMock.On("CharWidth").Return(0.5) + terminalMock.On("ScreenSize").Return(100, 80, nil) + handler := ImageResizeHandler{ + terminal: &terminalMock, + } + + imageFilePath := "testdata/cat_2000x1500.jpg" + img, err := OpenImageFile(imageFilePath) + if err != nil { + log.Fatal("open image file " + imageFilePath + " failed") + } + + assertions.False(terminalMock.IsWindows()) + assertions.Equal(terminalMock.CharWidth(), 0.5) + screenWidth, screenHeight, err := terminalMock.ScreenSize() + assertions.Equal(screenWidth, 100) + assertions.Equal(screenHeight, 80) + assertions.True(err == nil) + + options := DefaultOptions + options.Colored = false + options.StretchedScreen = true + options.FitScreen = false + scaledImage := handler.ScaleImage(img, &options) + sz := scaledImage.Bounds() + assertions.Equal(100, sz.Max.X) + assertions.Equal(80, sz.Max.Y) +} + // ExampleScaleImage is scale image example func ExampleScaleImage() { handler := NewResizeHandler() diff --git a/terminal/mocks/terminal_mock.go b/terminal/mocks/terminal_mock.go new file mode 100644 index 0000000..abdbd78 --- /dev/null +++ b/terminal/mocks/terminal_mock.go @@ -0,0 +1,66 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// Terminal is an autogenerated mock type for the Terminal type +type Terminal struct { + mock.Mock +} + +// CharWidth provides a mock function with given fields: +func (_m *Terminal) CharWidth() float64 { + ret := _m.Called() + + var r0 float64 + if rf, ok := ret.Get(0).(func() float64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(float64) + } + + return r0 +} + +// IsWindows provides a mock function with given fields: +func (_m *Terminal) IsWindows() bool { + ret := _m.Called() + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// ScreenSize provides a mock function with given fields: +func (_m *Terminal) ScreenSize() (int, int, error) { + ret := _m.Called() + + var r0 int + if rf, ok := ret.Get(0).(func() int); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(int) + } + + var r1 int + if rf, ok := ret.Get(1).(func() int); ok { + r1 = rf() + } else { + r1 = ret.Get(1).(int) + } + + var r2 error + if rf, ok := ret.Get(2).(func() error); ok { + r2 = rf() + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} -- 2.45.2