From 30e6564187efbdea3e8d131696b0637dd843adf5 Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Mon, 22 Oct 2018 23:46:19 +0800 Subject: [PATCH] Add ascii package test case --- ascii/ascii_test.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ ascii/option.go | 4 +- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 ascii/ascii_test.go diff --git a/ascii/ascii_test.go b/ascii/ascii_test.go new file mode 100644 index 0000000..8f984cd --- /dev/null +++ b/ascii/ascii_test.go @@ -0,0 +1,96 @@ +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() + assert := assert.New(t) + assert.True(newOptions.Colored, "Default colored option should be true") + assert.False(newOptions.Reverse, "Default reverse option should be false") + assert.Equal(" .,:;i1tfLCG08@", string(newOptions.Pixels), "Default pixels should be .,:;i1tfLCG08@") +} + +// TestMergeOptions test merge the options +func TestMergeOptions(t *testing.T) { + assert := assert.New(t) + options1 := NewOptions() + options2 := NewOptions() + options2.Colored = false + options1.mergeOptions(&options2) + assert.False(options1.Reverse, "Merged reverse option should be false") + assert.False(options1.Colored, "Merged colored option should be false") +} + +// TestConvertPixelToASCIIWhiteColor convert a white image pixel to ascii string +func TestConvertPixelToASCIIWhiteColor(t *testing.T) { + assert := 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] + assert.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] + assert.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) { + assert := 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] + assert.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] + assert.Equal(convertedChar, string([]byte{lastPixelChar}), + fmt.Sprintf("Reverse Black color chould be converted to %s", string([]byte{lastPixelChar}))) +} + +// 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)) +} diff --git a/ascii/option.go b/ascii/option.go index 3b6be87..452ea3b 100644 --- a/ascii/option.go +++ b/ascii/option.go @@ -16,7 +16,9 @@ var DefaultOptions = Options{ // NewOptions create a new convert option func NewOptions() Options { - return DefaultOptions + newOptions := Options{} + newOptions.mergeOptions(&DefaultOptions) + return newOptions } // mergeOptions merge two options -- 2.45.2