mal-go/list/list_test.go
ajet df56597332
All checks were successful
Go / test (push) Successful in 3s
rename test
2025-11-04 17:51:07 -10:00

64 lines
1.5 KiB
Go

package list
import (
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"pgregory.net/rapid"
)
func intListGen() *rapid.Generator[IList] {
return rapid.OneOf(rapid.Custom(func(t *rapid.T) IList {
myList := Empty()
t.Repeat(map[string]func(*rapid.T){
"conj": func(t *rapid.T) {
myList = myList.Conj(rapid.Int().Draw(t, "el"))
},
})
return myList
}), rapid.Just(Empty()))
}
func TestConjIncreasesLength(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
assert.Equal(t, myList.Len()+1, myList.Conj(42).Len())
})
}
func TestRestDecreasesLength(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
if myList.IsEmpty() {
assert.Equal(t, 0, myList.Rest().Len())
} else {
assert.Equal(t, myList.Len()-1, myList.Rest().Len())
}
})
}
func TestFirstIsNilOnlyWhenEmpty(t *testing.T) {
assert.Equal(t, 1, New(3).Conj(2).Conj(1).First())
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
assert.Equal(t, myList.IsEmpty(), myList.First() == nil)
})
}
func TestStringifyIntList(t *testing.T) {
assert.Equal(t, "(1 2 3)", New(3).Conj(2).Conj(1).String())
rapid.Check(t, func(t *rapid.T) {
myList := intListGen().Draw(t, "myList")
s := myList.String()
r := regexp.MustCompile(`\(.*\)`)
assert.Equal(t, true, r.Match([]byte(s)))
if !myList.IsEmpty() {
assert.Equal(t, myList.Len(), strings.Count(s, " ")+1)
} else {
assert.Equal(t, 0, strings.Count(s, " "))
}
})
}