1
0
Fork 0
Little S-Expression Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sxpf/pair_test.go

138 lines
3.6 KiB

//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
//
// This file is part of sxpf.
//
// sxpf is licensed under the latest version of the EUPL // (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//-----------------------------------------------------------------------------
package sxpf_test
import (
"testing"
"codeberg.org/t73fde/sxpf"
)
func TestPairString(t *testing.T) {
t.Parallel()
n := sxpf.Nil()
smk := sxpf.NewTrivialSymbolMaker()
a, b, c := smk.MakeSymbol("A"), smk.MakeSymbol("B"), smk.MakeSymbol("C")
np := sxpf.NewPair
testcases := []struct {
pair *sxpf.Pair
exp string
}{
{nil, "()"}, {n, "()"},
{np(nil, nil), "(())"}, {np(n, nil), "(())"}, {np(nil, n), "(())"}, {np(n, n), "(())"},
{np(a, nil), "(A)"}, {np(a, n), "(A)"},
{np(nil, a), "(() . A)"}, {np(n, a), "(() . A)"},
{np(a, np(b, nil)), "(A B)"}, {np(a, np(b, n)), "(A B)"},
{np(a, np(b, c)), "(A B . C)"},
{np(np(a, b), c), "((A . B) . C)"},
{np(np(a, b), np(c, nil)), "((A . B) C)"}, {np(np(a, b), np(c, n)), "((A . B) C)"},
}
for i, tc := range testcases {
got := tc.pair.String()
if tc.exp != got {
t.Errorf("%d: expected %q, but got %q", i, tc.exp, got)
}
}
}
func TestPairNilEmpty(t *testing.T) {
t.Parallel()
p := sxpf.NewPair(sxpf.NewString("car"), sxpf.NewString("cdr"))
if p.IsEmpty() {
t.Errorf("%v is seen as empty, but is not", p)
}
if sxpf.IsNil(p) {
t.Errorf("%v is seen as nil, but is not", p)
}
p = nil
if p != sxpf.Nil() {
t.Error("nil != Nil()")
}
var val sxpf.Value = nil
if !sxpf.IsNil(val) {
t.Error("nil value is not seen as IsNil")
}
val = sxpf.Nil()
if !sxpf.IsNil(val) {
t.Error("Nil() value is not seen as IsNil")
}
if _, ok := val.(*sxpf.Pair); !ok {
t.Error("Nil() does not typecast to *Pair")
}
var str *sxpf.String = nil
if !sxpf.IsNil(str) {
t.Error("nil string is not seen as IsNil")
}
val = str
if !sxpf.IsNil(val) {
t.Error("nil value is not seen as IsNil")
}
}
func TestPairLength(t *testing.T) {
t.Parallel()
if got := sxpf.Nil().Length(); got != 0 {
t.Errorf("Nil().Length() != 0, got %v", got)
}
p := sxpf.NewPair(sxpf.NewString("last"), nil)
if got := p.Length(); got != 1 {
t.Errorf("('').Length() != 1, got %v", got)
}
p = sxpf.NewPair(sxpf.NewString("s"), p)
if got := p.Length(); got != 2 {
t.Errorf("('','').Length() != 2, got %v", got)
}
p = sxpf.NewPair(sxpf.NewString("car"), sxpf.NewString("cdr"))
if got := p.Length(); got != 1 {
t.Errorf("('' . '').Length() != 1, got %v", got)
}
}
func TestPairEqual(t *testing.T) {
t.Parallel()
if !sxpf.Nil().Equal(sxpf.Nil()) {
t.Error("Nil() is not equal to itself")
}
s1 := sxpf.NewString("a")
p1 := sxpf.NewPair(s1, sxpf.Nil())
if !p1.Equal(p1) {
t.Errorf("%v is not equal to itself", p1)
}
p2 := sxpf.NewPair(s1, sxpf.Nil())
if !p1.Equal(p2) {
t.Errorf("%v is not equal to %v", p1, p2)
}
if !p2.Equal(p1) {
t.Errorf("%v is not equal to %v", p2, p1)
}
p3 := sxpf.NewPair(p1, p2)
if !p3.Equal(p3) {
t.Errorf("%v is not equal to itself", p3)
}
if p3.Equal(s1) {
t.Errorf("%v is equal to %v", p3, s1)
}
}
func TestPairGetValue(t *testing.T) {
t.Parallel()
if i, err := sxpf.Nil().GetInteger(); err == nil {
t.Errorf("Nil() has an integer value: %v", i)
}
if i, err := sxpf.NewPair(sxpf.NewString("a"), nil).GetInteger(); err == nil {
t.Errorf("('') has an integer value: %v", i)
}
if i, err := sxpf.NewPair(sxpf.NewInteger(7), nil).GetInteger(); err != nil {
t.Errorf("(7) has no integer value: %v", err)
} else if i != 7 {
t.Errorf("(7) != 7: %v", i)
}
}