Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwilcox/9599091 to your computer and use it in GitHub Desktop.
Save rwilcox/9599091 to your computer and use it in GitHub Desktop.
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