pass strings = [( # assignment + expressions 'a = b', '\na = b + c', 'a = b+c/3.0 - 12', 'a = 12px', 'a= 12px + 1/4%', 'a = a b', # @declares '@import("abc.css")', '@dothis()', '@other(1, 3, 4+5, 45/manhatten)', ),()] def make_pass(text): def meta(self, *a, **b): result = grammar.process(text) self.assertEquals(str(result), text) # self.assertEquals(''.join(str(tk) for tk in result), text) return meta for st in strings[0]: setattr(Parse, st, make_pass(st)) all_tests = test_suite = magictest.suite(__name__) ## import unittest ## unittest.TextTestRunner(verbosity=2).run(test_suite()) # vim: et sw=4 sts=4
class SmallTest(TestCase): pass def make_pass(grammar, text): def meta(self): try: return grammar.process(text) except ParseError: return grammar.process(text, debug=True) return meta def make_fail(grammar, text): def meta(self): try: tree = grammar.process(text) self.fail('Expected "%s" to fail' % text.encode('string_escape')) except ParseError: pass return meta for func, strings in tests.iteritems(): grammar = make_grammar(func) name = func.__name__ for i, good in enumerate(strings[0]): setattr(SmallTest, name + '_good_%d' % i, make_pass(grammar, good)) for i, bad in enumerate(strings[1]): setattr(SmallTest, name + '_bad_%d' % i, make_fail(grammar, bad)) all_tests = suite(__name__)
print css self.assertEqual(a, css) return meta for i, (ccss, css, name) in enumerate(cases): setattr(Convert, "convert_%d_%s" % (i, name), make_convert(ccss, css)) def check_parse(text): try: return grammar.process(text) except: return grammar.process(text, debug=True) def check_fail(text): try: grammar.process(text) except: pass else: grammar.process(text, debug=True) raise Exception("was supposed to fail on '%s'" % text.encode("string_escape")) all_tests = magictest.suite(__name__) # vim: et sw=4 sts=4
class Basic(TestCase): pass def make_func(func, *args, **kwargs): def meta(self): func(*args, **kwargs) return meta def make_token(string): def meta(self): list(grammar.get_tokens(string)) return meta def make_trans(a, b): def meta(self): nw = JSON.from_string(a) self.assertEqual(nw, b) return meta to_parse = ['[]','{}','[1,2,3]','{"hello":5, "man":[23,43,"yo"]}', "'he\\'llo'", '"double\\" \'escape"'] for i, string in enumerate(to_parse): setattr(Basic, 'parse_%d' % i, make_func(grammar.process, string)) parsed = [[], {}, [1,2,3], {'hello':5, 'man':[23, 43, 'yo']}, "he'llo", 'double" \'escape'] for i, (string, py) in enumerate(zip(to_parse, parsed)): setattr(Basic, 'translate_%d' % i, make_trans(string, py)) all_tests = suite(__name__) # vim: et sw=4 sts=4
] def make_pass(text): def meta(self, *a, **b): result = grammar.process(text) self.assertEquals(str(result), text) # self.assertEquals(''.join(str(tk) for tk in result), text) return meta for st in strings[0]: setattr(Parse, st, make_pass(st)) all_tests = test_suite = magictest.suite(__name__) ## import unittest ## unittest.TextTestRunner(verbosity=2).run(test_suite()) # vim: et sw=4 sts=4 ########NEW FILE######## __FILENAME__ = parsing #!/usr/bin/env python import magictest from magictest import MagicTest as TestCase import clevercss from clevercss.grammar import grammar from codetalker.pgm.grammar import Text, ParseError