Skip to content

Instantly share code, notes, and snippets.

@thomasst
Created April 16, 2013 21:42
Show Gist options
  • Select an option

  • Save thomasst/5399894 to your computer and use it in GitHub Desktop.

Select an option

Save thomasst/5399894 to your computer and use it in GitHub Desktop.

Revisions

  1. thomasst created this gist Apr 16, 2013.
    43 changes: 43 additions & 0 deletions search2.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    from pyparsing import *
    import unittest


    unicode_printables = u''.join(unichr(c) for c in xrange(65536)
    if not unichr(c).isspace())

    word = Word(unicode_printables)
    exact = QuotedString('"', unquoteResults=True, escChar='\\')
    term = exact | word


    class ParserTestCase(unittest.TestCase):
    """ Tests the internals of the parser. """

    def assertMatch(self, parser, input):
    parser.parseString(input, parseAll=True)

    def assertNoMatch(self, parser, input):
    try:
    parser.parseString(input, parseAll=True)
    except ParseException:
    pass
    else:
    raise ValueError('match should fail', input)

    def test_word(self):
    self.assertMatch(word, 'john')
    self.assertNoMatch(word, 'john taylor')

    def test_exact(self):
    self.assertMatch(exact, '"john taylor"')
    self.assertMatch(exact, r'"John said \"Hello world\""')
    self.assertNoMatch(exact, 'john')

    def test_term(self):
    self.assertMatch(term, 'john')
    self.assertMatch(term, '"john taylor"')
    self.assertNoMatch(term, 'john taylor')


    if __name__ == '__main__':
    unittest.main()