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("'abcd'", TokenString, "'abcd'") 35 checkMatch("#name", TokenHash, "#name") 36 checkMatch("42''", TokenNumber, "42", TokenString, "''") 37 checkMatch("4.2", TokenNumber, "4.2") 38 checkMatch(".42", TokenNumber, ".42") 39 checkMatch("42%", TokenPercentage, "42%") 40 checkMatch("4.2%", TokenPercentage, "4.2%") 41 checkMatch(".42%", TokenPercentage, ".42%") 42 checkMatch("42px", TokenDimension, "42px") 43 checkMatch("url('http://www.google.com/')", TokenURI, "url('http://www.google.com/')") 44 checkMatch("U+0042", TokenUnicodeRange, "U+0042") 45 checkMatch("<!--", TokenCDO, "<!--") 46 checkMatch("-->", TokenCDC, "-->") 47 checkMatch(" \n \t \n", TokenS, " \n \t \n") 48 checkMatch("/* foo */", TokenComment, "/* foo */") 49 checkMatch("bar(", TokenFunction, "bar(") 50 checkMatch("~=", TokenIncludes, "~=") 51 checkMatch("|=", TokenDashMatch, "|=") 52 checkMatch("^=", TokenPrefixMatch, "^=") 53 checkMatch("$=", TokenSuffixMatch, "$=") 54 checkMatch("*=", TokenSubstringMatch, "*=") 55 checkMatch("{", TokenChar, "{") 56 checkMatch("\uFEFF", TokenBOM, "\uFEFF") 57 checkMatch(`╯︵┻━┻"stuff"`, TokenIdent, "╯︵┻━┻", TokenString, `"stuff"`) 58} 59