1// Copyright 2012 The Gorilla Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package scanner
6
7import (
8	"testing"
9)
10
11func TestMatchers(t *testing.T) {
12	// Just basic checks, not exhaustive at all.
13	checkMatch := func(s string, ttList ...interface{}) {
14		scanner := New(s)
15
16		i := 0
17		for i < len(ttList) {
18			tt := ttList[i].(tokenType)
19			tVal := ttList[i+1].(string)
20			if tok := scanner.Next(); tok.Type != tt || tok.Value != tVal {
21				t.Errorf("did not match: %s (got %v)", s, tok)
22			}
23
24			i += 2
25		}
26
27		if tok := scanner.Next(); tok.Type != TokenEOF {
28			t.Errorf("missing EOF after token %s, got %+v", s, tok)
29		}
30	}
31
32	checkMatch("abcd", TokenIdent, "abcd")
33	checkMatch(`"abcd"`, TokenString, `"abcd"`)
34	checkMatch(`"ab'cd"`, TokenString, `"ab'cd"`)
35	checkMatch(`"ab\"cd"`, TokenString, `"ab\"cd"`)
36	checkMatch(`"ab\\cd"`, TokenString, `"ab\\cd"`)
37	checkMatch("'abcd'", TokenString, "'abcd'")
38	checkMatch(`'ab"cd'`, TokenString, `'ab"cd'`)
39	checkMatch(`'ab\'cd'`, TokenString, `'ab\'cd'`)
40	checkMatch(`'ab\\cd'`, TokenString, `'ab\\cd'`)
41	checkMatch("#name", TokenHash, "#name")
42	checkMatch("42''", TokenNumber, "42", TokenString, "''")
43	checkMatch("4.2", TokenNumber, "4.2")
44	checkMatch(".42", TokenNumber, ".42")
45	checkMatch("42%", TokenPercentage, "42%")
46	checkMatch("4.2%", TokenPercentage, "4.2%")
47	checkMatch(".42%", TokenPercentage, ".42%")
48	checkMatch("42px", TokenDimension, "42px")
49	checkMatch("url(http://domain.com)", TokenURI, "url(http://domain.com)")
50	checkMatch("url( http://domain.com/uri/between/space )", TokenURI, "url( http://domain.com/uri/between/space )")
51	checkMatch("url('http://domain.com/uri/between/single/quote')", TokenURI, "url('http://domain.com/uri/between/single/quote')")
52	checkMatch(`url("http://domain.com/uri/between/double/quote")`, TokenURI, `url("http://domain.com/uri/between/double/quote")`)
53	checkMatch("url(http://domain.com/?parentheses=%28)", TokenURI, "url(http://domain.com/?parentheses=%28)")
54	checkMatch("url( http://domain.com/?parentheses=%28&between=space )", TokenURI, "url( http://domain.com/?parentheses=%28&between=space )")
55	checkMatch("url('http://domain.com/uri/(parentheses)/between/single/quote')", TokenURI, "url('http://domain.com/uri/(parentheses)/between/single/quote')")
56	checkMatch(`url("http://domain.com/uri/(parentheses)/between/double/quote")`, TokenURI, `url("http://domain.com/uri/(parentheses)/between/double/quote")`)
57	checkMatch("url(http://domain.com/uri/1)url(http://domain.com/uri/2)",
58		TokenURI, "url(http://domain.com/uri/1)",
59		TokenURI, "url(http://domain.com/uri/2)",
60	)
61	checkMatch("U+0042", TokenUnicodeRange, "U+0042")
62	checkMatch("<!--", TokenCDO, "<!--")
63	checkMatch("-->", TokenCDC, "-->")
64	checkMatch("   \n   \t   \n", TokenS, "   \n   \t   \n")
65	checkMatch("/* foo */", TokenComment, "/* foo */")
66	checkMatch("bar(", TokenFunction, "bar(")
67	checkMatch("~=", TokenIncludes, "~=")
68	checkMatch("|=", TokenDashMatch, "|=")
69	checkMatch("^=", TokenPrefixMatch, "^=")
70	checkMatch("$=", TokenSuffixMatch, "$=")
71	checkMatch("*=", TokenSubstringMatch, "*=")
72	checkMatch("{", TokenChar, "{")
73	checkMatch("\uFEFF", TokenBOM, "\uFEFF")
74	checkMatch(`╯︵┻━┻"stuff"`, TokenIdent, "╯︵┻━┻", TokenString, `"stuff"`)
75}
76