From e6d60b6855fbcc079ef7fb6935b99e75fc4d3189 Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Fri, 26 Oct 2018 00:54:40 +0800 Subject: [PATCH] add main test case --- convert/convert.go | 2 +- image2ascii.go | 13 ++++++++++--- image2ascii_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 image2ascii_test.go diff --git a/convert/convert.go b/convert/convert.go index a4090f2..58068fa 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -21,7 +21,7 @@ type Options struct { Colored bool } -var defaultOptions = Options{ +var DefaultOptions = Options{ Ratio: 1, ExpectedWidth: -1, ExpectedHeight: -1, diff --git a/image2ascii.go b/image2ascii.go index b69ea38..5a457fc 100644 --- a/image2ascii.go +++ b/image2ascii.go @@ -1,6 +1,7 @@ package main import ( + "errors" "flag" "fmt" "github.com/qeesung/image2ascii/convert" @@ -28,10 +29,16 @@ func init() { func main() { flag.Parse() + if convertOptions, err := parseOptions(); err == nil { + fmt.Print(convert.ImageFile2ASCIIString(imageFilename, convertOptions)) + } else { + usage() + } +} +func parseOptions() (*convert.Options, error) { if imageFilename == "" { - usage() - return + return nil, errors.New("image file should not be empty") } // config the options convertOptions := &convert.Options{ @@ -41,7 +48,7 @@ func main() { FitScreen: fitScreen, Colored: colored, } - fmt.Print(convert.ImageFile2ASCIIString(imageFilename, convertOptions)) + return convertOptions, nil } func usage() { diff --git a/image2ascii_test.go b/image2ascii_test.go new file mode 100644 index 0000000..af8060e --- /dev/null +++ b/image2ascii_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestParseEmptyFilenameOptions(t *testing.T) { + assertions := assert.New(t) + imageFilename = "" + _, err :=parseOptions() + assertions.True(err != nil) +} + +func TestParseOptions(t *testing.T) { + assertions := assert.New(t) + imageFilename = "filename" + ratio = 0.5 + fitScreen = false + colored = false + expectedHeight = 100 + expectedWidth = 100 + opt, err :=parseOptions() + assertions.True(err == nil) + assertions.Equal(ratio, opt.Ratio) + assertions.False(fitScreen) + assertions.False(colored) + assertions.Equal(expectedWidth, opt.ExpectedWidth) + assertions.Equal(expectedHeight, opt.ExpectedHeight) +} + +func TestParseUsage(t *testing.T) { + usage() +} -- 2.45.2