~dricottone/image2ascii

ref: 5faf638733a4ce85ccebac8e4bdeebdd216839fc image2ascii/convert/resize_test.go -rw-r--r-- 4.8 KiB
5faf6387 — qeesung add convert test case 6 years ago
                                                                                
1d1c0ee7 qeesung
06e9c87e qeesung
1d1c0ee7 qeesung
06e9c87e qeesung
1d1c0ee7 qeesung
06e9c87e qeesung
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package convert

import (
	"fmt"
	"github.com/mattn/go-isatty"
	"github.com/stretchr/testify/assert"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strings"
	"testing"
)

// TestGetTerminalScreenSize test fetch the terminal screen size
func TestGetTerminalScreenSize(t *testing.T) {
	assertions := assert.New(t)
	_, _, err := getTerminalScreenSize()
	if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
		assertions.True(err != nil)
	} else {
		assertions.True(err == nil)
	}
}

// TestScaleImageWithFixedHeight test scale the image by fixed height
func TestScaleImageWithFixedHeight(t *testing.T) {
	assertions := assert.New(t)
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)
	assertions.True(img != nil)
	assertions.True(err == nil)

	options := defaultOptions
	options.Colored = false
	options.ExpectedHeight = 100

	scaledImage := ScaleImage(img, &options)
	sz := scaledImage.Bounds()
	oldSz := img.Bounds()
	assertions.Equal(100, sz.Max.Y, "scaled image height should be 100")
	assertions.Equal(oldSz.Max.X, sz.Max.X, "scaled image width should be changed")
}

// TestScaleImageWithFixedWidth test scale the image by fixed width
func TestScaleImageWithFixedWidth(t *testing.T) {
	assertions := assert.New(t)
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)
	assertions.True(img != nil)
	assertions.True(err == nil)

	options := defaultOptions
	options.Colored = false
	options.ExpectedWidth = 200

	scaledImage := ScaleImage(img, &options)
	sz := scaledImage.Bounds()
	oldSz := img.Bounds()
	assertions.Equal(oldSz.Max.Y, sz.Max.Y, "scaled image height should be changed")
	assertions.Equal(200, sz.Max.X, "scaled image width should be 200")
}

// TestScaleImageWithFixedWidthHeight test scale the image by fixed width
func TestScaleImageWithFixedWidthHeight(t *testing.T) {
	assertions := assert.New(t)
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)
	assertions.True(img != nil)
	assertions.True(err == nil)

	options := defaultOptions
	options.Colored = false
	options.ExpectedWidth = 200
	options.ExpectedHeight = 100

	scaledImage := ScaleImage(img, &options)
	sz := scaledImage.Bounds()
	assertions.Equal(100, sz.Max.Y, "scaled image height should be 100")
	assertions.Equal(200, sz.Max.X, "scaled image width should be 200")
}

// TestScaleImageByRatio test scale image by ratio
func TestScaleImageByRatio(t *testing.T) {
	assertions := assert.New(t)
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)
	assertions.True(img != nil)
	assertions.True(err == nil)

	options := defaultOptions
	options.Colored = false
	options.Ratio = 0.5

	scaledImage := ScaleImage(img, &options)
	sz := scaledImage.Bounds()
	oldSz := img.Bounds()
	expectedHeight := int(float64(oldSz.Max.Y) * 0.5 * charWidth())
	expectedWidth := int(float64(oldSz.Max.X) * 0.5)
	assertions.Equal(expectedHeight, sz.Max.Y, fmt.Sprintf("scaled image height should be %d", expectedHeight))
	assertions.Equal(expectedWidth, sz.Max.X, fmt.Sprintf("scaled image width should be %d", expectedHeight))
}

// TestScaleToFitTerminalSize test scale image to fit the terminal
func TestScaleToFitTerminalSize(t *testing.T) {
	assertions := assert.New(t)
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)
	assertions.True(img != nil)
	assertions.True(err == nil)

	options := defaultOptions
	options.Colored = false
	options.FitScreen = true

	// not terminal
	if !isatty.IsTerminal(os.Stdout.Fd()) &&
		!isatty.IsCygwinTerminal(os.Stdout.Fd()) &&
		os.Getenv("BE_CRASHER") == "1" {
		ScaleImage(img, &options)
	}

	cmd := exec.Command(os.Args[0], "-test.run=TestScaleToFitTerminalSize")
	cmd.Env = append(os.Environ(), "BE_CRASHER=1")
	stdout, _ := cmd.StderrPipe()
	if err := cmd.Start(); err != nil {
		t.Fatal(err)
	}

	// Check that the log fatal message is what we expected
	gotBytes, _ := ioutil.ReadAll(stdout)
	got := string(gotBytes)
	expected := "can not detect the terminal, please disable the '-s fitScreen' option"
	if !strings.HasSuffix(got[:len(got)-1], expected) {
		t.Fatalf("Unexpected log message. Got %s but should contain %s", got[:len(got)-1], expected)
	}

	// Check that the program exited
	err = cmd.Wait()
	if e, ok := err.(*exec.ExitError); !ok || e.Success() {
		t.Fatalf("Process ran with err %v, want exit status 1", err)
	}
}


// ExampleScaleImage is scale image example
func ExampleScaleImage() {
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)
	if err != nil {
		log.Fatal("open image file "+imageFilePath + " failed")
	}

	options := defaultOptions
	options.Colored = false
	options.ExpectedWidth = 200
	options.ExpectedHeight = 100

	scaledImage := ScaleImage(img, &options)
	sz := scaledImage.Bounds()
	fmt.Print(sz.Max.X, sz.Max.Y)
	// output: 200 100
}