logik2021/codego/core/utils/utils_test.go

122 lines
5.1 KiB
Go

package utils_test
/* ---------------------------------------------------------------- *
* UNIT TESTING
* ---------------------------------------------------------------- */
import (
"logik/core/utils"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
/* ---------------------------------------------------------------- *
* TESTCASE JsonToArrayOfStrings
* ---------------------------------------------------------------- */
func TestJsonToArrayOfStrings(test *testing.T) {
var assert = assert.New(test)
var result []string
utils.JsonToArrayOfStrings("[]", &result)
assert.Equal([]string{}, result)
utils.JsonToArrayOfStrings("[ \"ganymed\" ]", &result)
assert.Equal([]string{"ganymed"}, result)
utils.JsonToArrayOfStrings("[ \"ganymed\", \"io\" ]", &result)
assert.Equal([]string{"ganymed", "io"}, result)
assert.Panics(func() { utils.JsonToArrayOfStrings("[ 178 ]", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("true", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("nil", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("\"somestring\"", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
assert.Panics(func() { utils.JsonToArrayOfStrings("'somestring'", &result) }, "Should panic if converting not a JSON-encoded array of strings!")
}
/* ---------------------------------------------------------------- *
* TESTCASE Min2, Max2, MinList, MaxList, SumList
* ---------------------------------------------------------------- */
func TestMin(test *testing.T) {
var assert = assert.New(test)
assert.Equal(3, utils.Min2(3, 8))
assert.Equal(8, utils.Min2(100, 8))
assert.Equal(50, utils.MinList([]int{50}))
assert.Equal(1, utils.MinList([]int{50, 1}))
assert.Equal(2, utils.MinList([]int{50, 34, 10, 2, 8, 89}))
assert.Panics(func() { utils.MinList([]int{}) }, "Min of list should panic on empty list!")
}
func TestMax(test *testing.T) {
var assert = assert.New(test)
assert.Equal(8, utils.Max2(3, 8))
assert.Equal(100, utils.Max2(100, 8))
assert.Equal(50, utils.MaxList([]int{50}))
assert.Equal(50, utils.MaxList([]int{50, 1}))
assert.Equal(89, utils.MaxList([]int{50, 34, 10, 2, 8, 89}))
assert.Panics(func() { utils.MaxList([]int{}) }, "Max of list should panic on empty list!")
}
func TestSumList(test *testing.T) {
var assert = assert.New(test)
assert.Equal(0, utils.SumList([]int{}), "Sum of empty list should be 0.")
assert.Equal(2198, utils.SumList([]int{2198}))
assert.Equal(15, utils.SumList([]int{0, 1, 2, 3, 4, 5}))
assert.Equal(1038, utils.SumList([]int{1000, 1, 37}))
assert.Equal(237, utils.SumList([]int{1000, -800, 37}))
}
/* ---------------------------------------------------------------- *
* TESTCASE SortStrings
* ---------------------------------------------------------------- */
func TestSortStrings(test *testing.T) {
var assert = assert.New(test)
var list = []string{"katze", "Hund", "baby", "Pluto", "Saturn", "Mond"}
utils.SortStrings(&list)
assert.Equal([]string{"baby", "Hund", "katze", "Mond", "Pluto", "Saturn"}, list)
}
/* ---------------------------------------------------------------- *
* TESTCASE SortStrings
* ---------------------------------------------------------------- */
func TestFilterStrings(test *testing.T) {
var assert = assert.New(test)
var list = []string{"abram", "aaron", "aardvark", "aarhus", "alaska", "eel", "aal"}
var list2 = utils.FilterStrings(&list, func(x string) bool { return strings.HasPrefix(x, "aa") })
assert.Equal([]string{"aaron", "aardvark", "aarhus", "aal"}, list2)
}
/* ---------------------------------------------------------------- *
* TESTCASE UnionStrings2, UnionStringsInPlace, UnionStringsList
* ---------------------------------------------------------------- */
func TestUnionStrings2(test *testing.T) {
var assert = assert.New(test)
var list1 = []string{"red", "blue", "blue", "green", "blue", "grey", "black", "green"}
var list2 = []string{"yellow", "orange", "lila", "red"}
var list = utils.UnionStrings2(list1, list2)
utils.SortStrings(&list)
assert.Equal([]string{"black", "blue", "green", "grey", "lila", "orange", "red", "yellow"}, list)
}
func TestUnionStringsInPlace(test *testing.T) {
var assert = assert.New(test)
var list1 = []string{"red", "blue", "green"}
var list2 = []string{"yellow", "red", "blue", "red", "black"}
utils.UnionStringsInPlace(&list1, list2)
utils.SortStrings(&list1)
assert.Equal([]string{"black", "blue", "green", "red", "yellow"}, list1)
}
func TestUnionStringsList(test *testing.T) {
var assert = assert.New(test)
var list []string
var lists [][]string
lists = [][]string{{"red", "blue", "blue", "green"}, {"yellow", "red", "black"}}
list = utils.UnionStringsList(lists)
utils.SortStrings(&list)
assert.Equal([]string{"black", "blue", "green", "red", "yellow"}, list)
}