hcl/parser/lexer_test.go

137 lines
2.6 KiB
Go
Raw Normal View History

2015-10-03 18:06:30 +00:00
package parser
import (
"bytes"
"fmt"
"testing"
)
2015-10-03 21:20:26 +00:00
var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
2015-10-03 18:06:30 +00:00
type token struct {
tok Token
text string
}
2015-10-03 22:29:13 +00:00
func TestBool(t *testing.T) {
var tokenList = []token{
{BOOL, "true"},
{BOOL, "false"},
}
// create artifical source code
buf := new(bytes.Buffer)
for _, ident := range tokenList {
fmt.Fprintf(buf, " \t%s\n", ident.text)
}
l, err := NewLexer(buf)
if err != nil {
t.Fatal(err)
}
for _, ident := range tokenList {
2015-10-03 22:35:29 +00:00
tok := l.Scan()
2015-10-03 22:29:13 +00:00
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
}
2015-10-03 22:35:29 +00:00
if l.TokenText() != ident.text {
2015-10-04 17:13:38 +00:00
t.Errorf("text = %s want %s", l.TokenText(), ident.text)
2015-10-03 22:29:13 +00:00
}
}
}
2015-10-03 18:06:30 +00:00
func TestIdent(t *testing.T) {
2015-10-03 22:29:13 +00:00
var tokenList = []token{
2015-10-03 18:06:30 +00:00
{IDENT, "a"},
{IDENT, "a0"},
{IDENT, "foobar"},
{IDENT, "abc123"},
{IDENT, "LGTM"},
{IDENT, "_"},
{IDENT, "_abc123"},
{IDENT, "abc123_"},
{IDENT, "_abc_123_"},
{IDENT, "_äöü"},
{IDENT, "_本"},
{IDENT, "äöü"},
{IDENT, "本"},
{IDENT, "a۰۱۸"},
{IDENT, "foo६४"},
{IDENT, "bar"},
}
// create artifical source code
buf := new(bytes.Buffer)
2015-10-03 22:29:13 +00:00
for _, ident := range tokenList {
2015-10-03 18:06:30 +00:00
fmt.Fprintf(buf, " \t%s\n", ident.text)
}
2015-10-03 20:50:50 +00:00
l, err := NewLexer(buf)
if err != nil {
t.Fatal(err)
}
2015-10-03 18:06:30 +00:00
2015-10-03 22:29:13 +00:00
for _, ident := range tokenList {
2015-10-03 22:35:29 +00:00
tok := l.Scan()
2015-10-03 18:06:30 +00:00
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
}
2015-10-03 22:35:29 +00:00
if l.TokenText() != ident.text {
2015-10-04 17:13:38 +00:00
t.Errorf("text = %s want %s", l.TokenText(), ident.text)
2015-10-03 18:06:30 +00:00
}
}
}
2015-10-03 21:20:26 +00:00
func TestString(t *testing.T) {
2015-10-03 22:29:13 +00:00
var tokenList = []token{
2015-10-03 21:20:26 +00:00
{STRING, `" "`},
{STRING, `"a"`},
{STRING, `"本"`},
// {STRING, `"\a"`},
// {STRING, `"\b"`},
// {STRING, `"\f"`},
// {STRING, `"\n"`},
// {STRING, `"\r"`},
// {STRING, `"\t"`},
// {STRING, `"\v"`},
// {STRING, `"\""`},
// {STRING, `"\000"`},
// {STRING, `"\777"`},
// {STRING, `"\x00"`},
// {STRING, `"\xff"`},
// {STRING, `"\u0000"`},
// {STRING, `"\ufA16"`},
// {STRING, `"\U00000000"`},
// {STRING, `"\U0000ffAB"`},
// {STRING, `"` + f100 + `"`},
}
// create artifical source code
buf := new(bytes.Buffer)
2015-10-03 22:29:13 +00:00
for _, ident := range tokenList {
2015-10-03 21:20:26 +00:00
fmt.Fprintf(buf, " \t%s\n", ident.text)
}
l, err := NewLexer(buf)
if err != nil {
t.Fatal(err)
}
2015-10-03 22:29:13 +00:00
for _, ident := range tokenList {
2015-10-03 22:35:29 +00:00
tok := l.Scan()
2015-10-03 21:20:26 +00:00
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
}
2015-10-03 22:35:29 +00:00
if l.TokenText() != ident.text {
2015-10-04 17:13:38 +00:00
t.Errorf("text = %s want %s", l.TokenText(), ident.text)
2015-10-03 21:20:26 +00:00
}
}
}