From 5faf638733a4ce85ccebac8e4bdeebdd216839fc Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Fri, 26 Oct 2018 00:26:38 +0800 Subject: [PATCH] add convert test case --- convert/convert.go | 11 ++- convert/convert_test.go | 94 ++++++++++++++++++++++++++ convert/resize_test.go | 19 ------ convert/testdata/3x3_black.png | Bin 0 -> 119 bytes convert/testdata/3x3_white.png | Bin 0 -> 122 bytes convert/testdata/8x3_multi_colors.png | Bin 0 -> 156 bytes 6 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 convert/convert_test.go create mode 100644 convert/testdata/3x3_black.png create mode 100644 convert/testdata/3x3_white.png create mode 100644 convert/testdata/8x3_multi_colors.png diff --git a/convert/convert.go b/convert/convert.go index c14a512..a4090f2 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -62,6 +62,15 @@ func Image2ASCIIString(image image.Image, options *Options) string { return buffer.String() } +// ImageFile2ASCIIMatrix converts a image file to ascii matrix +func ImageFile2ASCIIMatrix(imageFilename string, option *Options) []string { + img, err := OpenImageFile(imageFilename) + if err != nil { + log.Fatal("open image failed : " + err.Error()) + } + return Image2ASCIIMatrix(img, option) +} + // ImageFile2ASCIIString converts a image file to ascii string func ImageFile2ASCIIString(imageFilename string, option *Options) string { img, err := OpenImageFile(imageFilename) @@ -75,7 +84,7 @@ func ImageFile2ASCIIString(imageFilename string, option *Options) string { func OpenImageFile(imageFilename string) (image.Image, error) { f, err := os.Open(imageFilename) if err != nil { - log.Fatal(err) + return nil, err } img, _, err := image.Decode(f) diff --git a/convert/convert_test.go b/convert/convert_test.go new file mode 100644 index 0000000..5257d33 --- /dev/null +++ b/convert/convert_test.go @@ -0,0 +1,94 @@ +package convert + +import ( + "github.com/stretchr/testify/assert" + "reflect" + "testing" +) + +// TestOpenImageFile test open different type image file +func TestOpenImageFile(t *testing.T) { + assertions := assert.New(t) + jpgFilename := "testdata/jpg_sample_image.jpg" + openedImage, err := OpenImageFile(jpgFilename) + assertions.True(err == nil, "jpg image format should be supported") + assertions.True(openedImage != nil, "opened jpg file should not be nil") + + pngFilename := "testdata/png_sample_image.png" + openedImage, err = OpenImageFile(pngFilename) + assertions.True(err == nil, "png image format should be supported") + assertions.True(openedImage != nil, "opened jpg file should not be nil") + + notSupported := "testdata/not_supported_sample_image" + openedImage, err = OpenImageFile(notSupported) + assertions.True(err != nil, "should not open unsupported image") + assertions.True(openedImage == nil, "not supported image should be nil") +} + +// TestOpenNotExistsFile test open a not exists file +func TestOpenNotExistsFile(t *testing.T) { + assertions := assert.New(t) + _, err := OpenImageFile("not exists") + assertions.True(err != nil) +} + +// TestImage2ASCIIMatrix test convert a image to ascii matrix +func TestImage2ASCIIMatrix(t *testing.T) { + imageTests := []struct { + imageFilename string + asciiMatrix []string + }{ + {"testdata/3x3_black.png", []string{ + " ", " ", " ", "\n", + " ", " ", " ", "\n", + " ", " ", " ", "\n"}}, + {"testdata/3x3_white.png", []string{ + "@", "@", "@", "\n", + "@", "@", "@", "\n", + "@", "@", "@", "\n"}}, + {"testdata/8x3_multi_colors.png", []string{ + "L", "0", "L", "t", "0", "t", "G", "0", "\n", + "i", "L", "t", "1", "L", "1", "f", "L", "\n", + "i", "f", "i", "i", ";", "L", ";", "t", "\n", + }}, + } + + for _, tt := range imageTests { + t.Run(tt.imageFilename, func(t *testing.T) { + convertOptions := defaultOptions + convertOptions.FitScreen = false + convertOptions.Colored = false + + matrix := ImageFile2ASCIIMatrix(tt.imageFilename, &convertOptions) + if !reflect.DeepEqual(matrix, tt.asciiMatrix) { + t.Errorf("image %s convert expected to %+v, but get %+v", + tt.imageFilename, tt.asciiMatrix, matrix) + } + }) + } +} + +func TestImageFile2ASCIIString(t *testing.T) { + imageTests := []struct { + imageFilename string + asciiString string + }{ + {"testdata/3x3_black.png", " \n \n \n"}, + {"testdata/3x3_white.png", "@@@\n@@@\n@@@\n"}, + {"testdata/8x3_multi_colors.png", "L0Lt0tG0\niLt1L1fL\nifii;L;t\n"}, + } + + for _, tt := range imageTests { + t.Run(tt.imageFilename, func(t *testing.T) { + convertOptions := defaultOptions + convertOptions.FitScreen = false + convertOptions.Colored = false + + charString := ImageFile2ASCIIString(tt.imageFilename, &convertOptions) + if charString != tt.asciiString { + t.Errorf("image %s convert expected to %+v, but get %+v", + tt.imageFilename, tt.asciiString, charString) + } + }) + } +} diff --git a/convert/resize_test.go b/convert/resize_test.go index 59e178b..9227a15 100644 --- a/convert/resize_test.go +++ b/convert/resize_test.go @@ -23,25 +23,6 @@ func TestGetTerminalScreenSize(t *testing.T) { } } -// TestOpenImageFile test open different type image file -func TestOpenImageFile(t *testing.T) { - assertions := assert.New(t) - jpgFilename := "testdata/jpg_sample_image.jpg" - openedImage, err := OpenImageFile(jpgFilename) - assertions.True(err == nil, "jpg image format should be supported") - assertions.True(openedImage != nil, "opened jpg file should not be nil") - - pngFilename := "testdata/png_sample_image.png" - openedImage, err = OpenImageFile(pngFilename) - assertions.True(err == nil, "png image format should be supported") - assertions.True(openedImage != nil, "opened jpg file should not be nil") - - notSupported := "testdata/not_supported_sample_image" - openedImage, err = OpenImageFile(notSupported) - assertions.True(err != nil, "should not open unsupported image") - assertions.True(openedImage == nil, "not supported image should be nil") -} - // TestScaleImageWithFixedHeight test scale the image by fixed height func TestScaleImageWithFixedHeight(t *testing.T) { assertions := assert.New(t) diff --git a/convert/testdata/3x3_black.png b/convert/testdata/3x3_black.png new file mode 100644 index 0000000000000000000000000000000000000000..f3462ca50d60ded85a92a6547468f4efe86bb5f2 GIT binary patch literal 119 zcmeAS@N?(olHy`uVBq!ia0vp^%plCc1SD^IDZKzvjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucK^{*R#}J9|X~a literal 0 HcmV?d00001 diff --git a/convert/testdata/3x3_white.png b/convert/testdata/3x3_white.png new file mode 100644 index 0000000000000000000000000000000000000000..08621e3d8b27e57d4b37ec33e5d2424ce2c256fa GIT binary patch literal 122 zcmeAS@N?(olHy`uVBq!ia0vp^%plCc1SD^IDZKzvjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucL4Hpc#}J9|!2~1~SKX8cQjEnx?oJHr&dIz4a#+$GeH|GX zHuiJ>Nn{1`ISV`@iy0XB4ude`@%$AjKtV%K7sn8ZaMkXEf(!~ghm52ar<#}_j*^#Q x;agSvNnLJk6jyih&5}8ZS(B|qH467@#>Ku@*KGg6djqJM!PC{xWt~$(69BvZDeeFO literal 0 HcmV?d00001 -- 2.45.2