~dricottone/image2ascii

adffe23d6c5d8b0be089bae60db491759cee198d — qeesung 6 years ago 3e0d95a
add the mock terminal
2 files changed, 134 insertions(+), 0 deletions(-)

M convert/resize_test.go
A terminal/mocks/terminal_mock.go
M convert/resize_test.go => convert/resize_test.go +68 -0
@@ 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()

A terminal/mocks/terminal_mock.go => terminal/mocks/terminal_mock.go +66 -0
@@ 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
}