~dricottone/image2ascii

ref: 9a8a79a4a2cd6a35671d1ca574dc75734d5dfe96 image2ascii/ascii/ascii_test.go -rw-r--r-- 3.7 KiB
9a8a79a4 — 秦世成 Update travis ci config file to support converalls 6 years ago
                                                                                
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
a560de55 qeesung
30e65641 qeesung
76c74bcd qeesung
30e65641 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
package ascii

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"image/color"
	"reflect"
	"testing"
)

// TestNewOptions create options with default values
func TestNewOptions(t *testing.T) {
	newOptions := NewOptions()
	assertions := assert.New(t)
	assertions.True(newOptions.Colored, "Default colored option should be true")
	assertions.False(newOptions.Reverse, "Default reverse option should be false")
	assertions.Equal(" .,:;i1tfLCG08@", string(newOptions.Pixels), "Default pixels should be  .,:;i1tfLCG08@")
}

// TestMergeOptions test merge the options
func TestMergeOptions(t *testing.T) {
	assertions := assert.New(t)
	options1 := NewOptions()
	options2 := NewOptions()
	options2.Colored = false
	options1.mergeOptions(&options2)
	assertions.False(options1.Reverse, "Merged reverse option should be false")
	assertions.False(options1.Colored, "Merged colored option should be false")
}

// TestConvertPixelToASCIIWhiteColor convert a white image pixel to ascii string
func TestConvertPixelToASCIIWhiteColor(t *testing.T) {
	assertions := assert.New(t)
	r, g, b, a := uint8(255), uint8(255), uint8(255), uint8(255)
	pixel := color.RGBA{
		R: r,
		G: g,
		B: b,
		A: a,
	}

	defaultOptions := NewOptions()
	defaultOptions.Colored = false
	convertedChar := ConvertPixelToASCII(pixel, &defaultOptions)
	lastPixelChar := defaultOptions.Pixels[len(defaultOptions.Pixels)-1]
	assertions.Equal(convertedChar, string([]byte{lastPixelChar}),
		fmt.Sprintf("White color chould be converted to %s", string([]byte{lastPixelChar})))

	defaultOptions.Colored = false
	defaultOptions.Reverse = true
	convertedChar = ConvertPixelToASCII(pixel, &defaultOptions)
	firstPixelChar := reverse(defaultOptions.Pixels)[0]
	assertions.Equal(convertedChar, string([]byte{firstPixelChar}),
		fmt.Sprintf("Reverse white color chould be converted to %s", string([]byte{firstPixelChar})))
}

// TestConvertPixelToASCIIBlackColor convert a white image pixel to ascii string
func TestConvertPixelToASCIIBlackColor(t *testing.T) {
	assertions := assert.New(t)
	r, g, b, a := uint8(0), uint8(0), uint8(0), uint8(0)
	pixel := color.RGBA{
		R: r,
		G: g,
		B: b,
		A: a,
	}

	defaultOptions := NewOptions()
	defaultOptions.Colored = false
	convertedChar := ConvertPixelToASCII(pixel, &defaultOptions)
	firstPixelChar := defaultOptions.Pixels[0]
	assertions.Equal(convertedChar, string([]byte{firstPixelChar}),
		fmt.Sprintf("Black color chould be converted to %s", string([]byte{firstPixelChar})))

	defaultOptions.Colored = false
	defaultOptions.Reverse = true
	convertedChar = ConvertPixelToASCII(pixel, &defaultOptions)
	lastPixelChar := reverse(defaultOptions.Pixels)[len(defaultOptions.Pixels)-1]
	assertions.Equal(convertedChar, string([]byte{lastPixelChar}),
		fmt.Sprintf("Reverse Black color chould be converted to %s", string([]byte{lastPixelChar})))
}

func TestColoredASCIIChar(t *testing.T) {
	assertions := assert.New(t)
	r, g, b, a := uint8(123), uint8(123), uint8(123), uint8(255)
	pixel := color.RGBA{
		R: r,
		G: g,
		B: b,
		A: a,
	}
	defaultOptions := NewOptions()
	defaultOptions.Colored = true
	coloredChar := ConvertPixelToASCII(pixel, &defaultOptions)
	assertions.True(len(coloredChar) > 1)
}

// TestReverseSlice test reverse a slice
func TestReverseSlice(t *testing.T) {
	s := []byte{1, 2, 3, 4, 5}
	reversedSlice := reverse(s)
	expectedReversedSlice := []byte{5, 4, 3, 2, 1}
	assert.True(t, reflect.DeepEqual(reversedSlice, expectedReversedSlice),
		fmt.Sprintf("%+v reversed should equal to %+v", s, expectedReversedSlice))

	s = []byte{1, 2, 3, 4}
	reversedSlice = reverse(s)
	expectedReversedSlice = []byte{4, 3, 2, 1}
	assert.True(t, reflect.DeepEqual(reversedSlice, expectedReversedSlice),
		fmt.Sprintf("%+v reversed should equal to %+v", s, expectedReversedSlice))
}