Created
March 17, 2014 13:23
-
-
Save rwilcox/9599091 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import re | |
THE_PATTERN= r'(^\s*(?P<function>(?P<function_name>[a-zA-Z0-9_@\.]+)\W*(=|:)\W*(\([a-zA-Z0-9_@,= \.@"{}\[\]]+\))?\W*(\-\>|\=\>)\W*))' | |
test_re = re.compile(THE_PATTERN) | |
class TestCoffeescriptFunctionParsing(unittest.TestCase): | |
def test_parse_simple_method(self): | |
test_string = """myFunction = (a, b, c) -> | |
console.log "hi" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
def test_parse_optional_parameters(self): | |
test_string = """myFunction = (a, b, c="hi") -> | |
console.log "hi" """ | |
test_string_two = """myFunction = (a, b, c=42) -> | |
console.log "hi" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
res = test_re.match(test_string_two) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
def test_parse_object_destructuring_assignment(self): | |
test_string = """myFunction = ([a, b,c], d) -> | |
console.log "hi" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
def test_parameter_straight_into_instance_variable(self): | |
test_string = """myFunction = (@a, d) -> | |
console.log "hi" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
unittest.skip("not implemted") | |
def test_function_does_not_get_tripped_up_on_embedded_functions(self): | |
test_string = """myFunction = ([a, b,c], d) -> | |
console.log "hi", (bad, params) -> | |
consolw.log "items" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
unittest.skip("not implemted") | |
"""function named group should stop when it encounters a line thst starts with the amount of whitespace *it* started with (and named group should not include this line)""" | |
def test_function_does_not_run_into_other_functions_in_string(self): | |
pass | |
def test_respects_fat_arrow(self): | |
test_string = """myFunction = (a, b, c="hi") => | |
console.log "hi" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
def test_handles_one_line_functions(self): | |
test_string = """myFunction = ([a, b,c], d) -> console.log "hi" """ | |
res = test_re.match(test_string) | |
self.assertIsNotNone(res) | |
self.assertEqual(res.group('function_name'), "myFunction") | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment