Skip to content

Commit

Permalink
Test lexer functions (match/span*)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanwalters committed Jan 24, 2017
1 parent 4f25b01 commit 9ea3467
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Copyright (C) 2015 by Alexandru Cojocaru */

/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

package useragent

import (
"testing"
)

// verify output and state
func (l lex) checkLex(t *testing.T, success, expectSuccess bool, output, expectOutput, expectState string) {
if success != expectSuccess {
t.Errorf("success value '%t' does not match expected '%t'", success, expectSuccess)
}
if output != expectOutput {
t.Errorf("output\n'%s' does not match expected\n'%s'", output, expectOutput)
}
if l.s[l.p:] != expectState {
t.Errorf("lexer state\n'%s' does not match expected\n'%s'", l.s[l.p:], expectState)
}
}

func (l lex) assertLex(t *testing.T, success, expectSuccess bool, output, expectOutput, expectState string) {
l.checkLex(t, success, expectSuccess, output, expectOutput, expectState)
if t.Failed() {
t.Fatalf("assertLex failed for (%t,%s,%s)", expectSuccess, expectOutput, expectState)
}
}

func TestSpans(t *testing.T) {
testLex := newLex("☕Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 這是什麼 Firefox/38.0")

// quick test of match:
if testLex.match("Mozilla") {
t.Fatalf("Matched non-starting string!")
}
if !testLex.match("☕M") {
t.Fatalf("failed to match unicode start")
}

m, ok := testLex.span("M")
testLex.assertLex(t, ok, false, m, "", "ozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 這是什麼 Firefox/38.0")
m, ok = testLex.spanAny("MZYA")
testLex.assertLex(t, ok, false, m, "", "ozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 這是什麼 Firefox/38.0")
m, ok = testLex.spanBefore(";", "(")
testLex.assertLex(t, ok, false, m, "", "ozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 這是什麼 Firefox/38.0")

m, ok = testLex.span(";")
testLex.assertLex(t, ok, true, m, "ozilla/5.0 (X11", " Linux i686; rv:38.0) Gecko/20100101 這是什麼 Firefox/38.0")

m, ok = testLex.spanBefore("8", ")")
testLex.assertLex(t, ok, true, m, " Linux i6", "6; rv:38.0) Gecko/20100101 這是什麼 Firefox/38.0")
m, ok = testLex.spanBefore("8", ")")
testLex.assertLex(t, ok, true, m, "6; rv:3", ".0) Gecko/20100101 這是什麼 Firefox/38.0")
m, ok = testLex.spanBefore("8", ")")
testLex.assertLex(t, ok, false, m, "", ".0) Gecko/20100101 這是什麼 Firefox/38.0")

m, ok = testLex.spanAny("☕/這什")
testLex.assertLex(t, ok, true, m, ".0) Gecko", "20100101 這是什麼 Firefox/38.0")
m, ok = testLex.spanAny("☕/這什")
testLex.assertLex(t, ok, true, m, "20100101 ", "是什麼 Firefox/38.0")
m, ok = testLex.spanAny("☕這Q")
testLex.assertLex(t, ok, false, m, "", "是什麼 Firefox/38.0")

m, ok = testLex.spanBefore("是", "Q")
testLex.assertLex(t, ok, true, m, "", "什麼 Firefox/38.0")
m, ok = testLex.span("麼")
testLex.assertLex(t, ok, true, m, "什", " Firefox/38.0")
}

0 comments on commit 9ea3467

Please sign in to comment.