test: testing panics and empty input

This commit is contained in:
Reinaldy Rafli 2021-10-28 00:08:45 +07:00
parent 559192bae5
commit 063b44a57e
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
3 changed files with 25 additions and 18 deletions

View File

@ -3,24 +3,6 @@ name: CI
on: [push, pull_request] on: [push, pull_request]
jobs: 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: coverage:
name: Coverage name: Coverage
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -32,6 +32,10 @@ func WithConfig(txt string, config Config) string {
config.Style = StyleStandard config.Style = StyleStandard
} }
if txt == "" {
return ""
}
letters := strings.Split(txt, "") letters := strings.Split(txt, "")
var arr [][]string var arr [][]string
llen := getStyleLength(config.Style) llen := getStyleLength(config.Style)

View File

@ -20,3 +20,24 @@ func TestWithConfig(t *testing.T) {
t.Error("should not be empty") 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()
}