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/scanner_test.go

79 lines
1.8 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 (
"bytes"
"strconv"
"strings"
"testing"
"codeberg.org/t73fde/sxpf"
)
func TestValidScans(t *testing.T) {
t.Parallel()
s := sxpf.NewScanner(strings.NewReader(" "))
tok := s.Next()
if tok.Typ != sxpf.TokEOF {
t.Error(tok)
}
}
func TestScanner(t *testing.T) {
t.Parallel()
testcases := []struct {
src string
exp string
}{
{"", ""},
{"a", "a"},
{"a ; comment", "a"},
{"(", "("},
{"(.)[{}]", "(.)[{}]"},
{"a.", "a."},
{`""`, ``},
{`"a"`, `a`},
{`"\""`, `"`},
{`"\\"`, "\\"},
{`"\t"`, "\t"},
{`"\r"`, "\r"},
{`"\n"`, "\n"},
{`"\x"`, `x`}, {`"\x4"`, `x4`}, {`"\x41"`, `A`}, {`"\x4g"`, `x4g`},
{`"\u"`, `u`}, {`"\u0"`, `u0`}, {`"\u00"`, `u00`}, {`"\u004"`, `u004`}, {`"\u0042"`, `B`},
{`"\U"`, `U`}, {`"\U0"`, `U0`}, {`"\U00"`, `U00`}, {`"\U000"`, `U000`}, {`"\U0000"`, `U0000`},
{`"\U00004"`, `U00004`}, {`"\U000043"`, `C`},
{"0", "0"}, {"01", "1"},
}
for i, tc := range testcases {
var buf bytes.Buffer
s := sxpf.NewScanner(strings.NewReader(tc.src))
for {
tok := s.Next()
if tok.Typ == sxpf.TokEOF {
break
}
if tok.Typ == sxpf.TokErr {
buf.WriteByte('E')
break
}
if iv, ok := tok.Val.(int64); ok {
buf.WriteString(strconv.FormatInt(iv, 10))
} else {
buf.WriteString(tok.Val.(string))
}
}
got := buf.String()
if got != tc.exp {
t.Errorf("%d: %q -> %q, but got %q", i, tc.src, tc.exp, got)
}
}
}