From 063b44a57e648b51b81bb5abcb0a2b3407ce6c38 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Thu, 28 Oct 2021 00:08:45 +0700 Subject: [PATCH] test: testing panics and empty input --- .github/workflows/ci.yml | 18 ------------------ asciitxt.go | 4 ++++ asciitxt_test.go | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cba5f89..2494f87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,24 +3,6 @@ name: CI on: [push, pull_request] jobs: - build-test: - name: Build test - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - go-version: [1.16.x, 1.17.x] - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go-version }} - - - name: Build - run: go build ./ coverage: name: Coverage runs-on: ubuntu-latest diff --git a/asciitxt.go b/asciitxt.go index f95bfad..15a9ad4 100644 --- a/asciitxt.go +++ b/asciitxt.go @@ -32,6 +32,10 @@ func WithConfig(txt string, config Config) string { config.Style = StyleStandard } + if txt == "" { + return "" + } + letters := strings.Split(txt, "") var arr [][]string llen := getStyleLength(config.Style) diff --git a/asciitxt_test.go b/asciitxt_test.go index 874ed1f..58b5b63 100644 --- a/asciitxt_test.go +++ b/asciitxt_test.go @@ -20,3 +20,24 @@ func TestWithConfig(t *testing.T) { t.Error("should not be empty") } } + +func TestEmpty(t *testing.T) { + s := asciitxt.New("") + if s != "" { + t.Error("should be empty, got:", s) + } +} + +func TestInvalidStyle(t *testing.T) { + // this should panic + assertPanic(t, func() { asciitxt.WithConfig("hello", asciitxt.Config{Style: 2}) }) +} + +func assertPanic(t *testing.T, f func()) { + defer func() { + if r := recover(); r == nil { + t.Errorf("The code did not panic") + } + }() + f() +}