hcl/scanner/scanner_test.go

139 lines
2.8 KiB
Go
Raw Normal View History

2015-10-04 17:16:43 +00:00
package scanner
2015-10-03 18:06:30 +00:00
import (
"bytes"
"fmt"
"testing"
2015-10-04 17:19:39 +00:00
"github.com/fatih/hcl/token"
2015-10-03 18:06:30 +00:00
)
2015-10-03 21:20:26 +00:00
var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
2015-10-04 17:19:39 +00:00
type tokenPair struct {
tok token.Token
2015-10-03 18:06:30 +00:00
text string
}
2015-10-03 22:29:13 +00:00
func TestBool(t *testing.T) {
2015-10-04 17:19:39 +00:00
var tokenList = []tokenPair{
2015-10-04 17:16:43 +00:00
{token.BOOL, "true"},
{token.BOOL, "false"},
2015-10-03 22:29:13 +00:00
}
// 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-04 17:19:39 +00:00
var tokenList = []tokenPair{
{token.IDENT, "a"},
{token.IDENT, "a0"},
{token.IDENT, "foobar"},
{token.IDENT, "abc123"},
{token.IDENT, "LGTM"},
{token.IDENT, "_"},
{token.IDENT, "_abc123"},
{token.IDENT, "abc123_"},
{token.IDENT, "_abc_123_"},
{token.IDENT, "_äöü"},
{token.IDENT, "_本"},
{token.IDENT, "äöü"},
{token.IDENT, "本"},
{token.IDENT, "a۰۱۸"},
{token.IDENT, "foo६४"},
{token.IDENT, "bar"},
2015-10-03 18:06:30 +00:00
}
// 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-04 17:19:39 +00:00
var tokenList = []tokenPair{
{token.STRING, `" "`},
{token.STRING, `"a"`},
{token.STRING, `"本"`},
2015-10-03 21:20:26 +00:00
// {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
}
}
}