Skip to content

Commit

Permalink
Merge branch 'test-suite' into develop
Browse files Browse the repository at this point in the history
* test-suite:
  Allow blank lines in test assertions
  Fix TestView should not auto indent by default
  💄
  Improve copy scope to clipboard
  Assertion failure now provides more information
  Fix method name typo
  • Loading branch information
gerardroche committed Feb 11, 2015
2 parents 6ae75f8 + 877d351 commit 42d7e16
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
59 changes: 32 additions & 27 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ def __init__(self):

if DEBUG_MODE:

#
# Only register development utils if in development mode
#

class PhpGrammarDevUtilScopeStatus(sublime_plugin.EventListener):

"""
Expand All @@ -59,24 +55,40 @@ class PhpGrammarDevUtilCopyScopeNameToClipboard(sublime_plugin.TextCommand):
Copy scope name under cursor to clipboard
"""

def run(self, edit):
def run(self, edit, assertion = None):

start_of_test_file = 0
for line_region in self.view.split_by_newlines(sublime.Region(0, self.view.size())):
if '--FILE--' in self.view.substr(line_region):
start_of_test_file = self.view.rowcol(line_region.begin())[0] + 1

scope_name = ''
for sel in self.view.sel():
scope_name = self.view.scope_name(sel.begin()).strip()
sublime.set_clipboard(scope_name)
for point in range(sel.begin(), sel.end() + 1):
if assertion is not None:
row_col = self.view.rowcol(point)
scope_name += assertion + ':%s:%s:' % (row_col[0] - start_of_test_file, row_col[1])

scope_name += self.view.scope_name(point).strip() + "\n"

sublime.set_clipboard(scope_name)

class PHPGrammarTestView():

def __init__(self):
self.view = sublime.active_window().new_file()
self.view.set_scratch(True)
self.view.settings().set('auto_indent', False)
self.view.settings().set('indent_to_bracket', False)
self.view.settings().set('tab_size', 4)
self.view.settings().set('trim_automatic_white_space', False)
self.view.set_syntax_file(config.language_file_path)

def close(self):
self.view.window().run_command('close'),

def insert(self, string):
self.view.run_command('insert', {'characters': string})
# self.view.run_command('move_to', {'to': 'bof'})

def to_str(self):
return self.view.substr(sublime.Region(0, self.view.size()))
Expand All @@ -85,19 +97,13 @@ class IndentationTestView(PHPGrammarTestView):

def __init__(self):
super(IndentationTestView, self).__init__()
self.view.settings().set('auto_indent', False)
self.view.settings().set('smart_indent', True)
self.view.settings().set('indent_to_bracket', False)
self.view.settings().set('draw_white_space', 'all')
self.view.settings().set('tab_size', 4)
self.view.settings().set('trim_automatic_white_space', True)

def reindent(self):
self.view.run_command('reindent', {
'force_indent': True,
'single_line': False
})
# self.view.run_command('move_to', {'to': 'bof'})

class LanguageTestView(PHPGrammarTestView):

Expand All @@ -122,10 +128,6 @@ def to_scope_name_repr(self):

return content

#
# Unit tests
#

class TestIndentation(unittest.TestCase):

def setUp(self):
Expand All @@ -140,7 +142,7 @@ def getFileContents(self, name, ext):
with open(file_name) as f:
return f.read()

def createFileDataProiderTest(test_file_name):
def createFileDataProviderTest(test_file_name):
def indentationTest(self):
test_content = self.getFileContents(test_file_name, config.indentation_test_file_extension)

Expand Down Expand Up @@ -169,11 +171,14 @@ def tearDown(self):
self.view.close()

def assertMatchSelector(self, line, offset, selector):
score = self.view.score_selector(self.view.text_point(line, offset), selector)
self.assertGreater(score, 0, 'Expected selector: %s' % selector)
point = self.view.text_point(line, offset)
selector_score = self.view.score_selector(point, selector)
actual_scope = self.view.scope_name(point)
self.assertGreater(selector_score, 0, 'Expected selector score greater than 0 for (line:%s, offset:%s, point:%s, selector:%s) *** ACTUAL: "%s"' % (line, offset, point, selector, actual_scope))

def assertEqualsScope(self, line, offset, expected_scope):
actual_scope = self.view.scope_name(self.view.text_point(line, offset))
point = self.view.text_point(line, offset)
actual_scope = self.view.scope_name(point)
self.assertEqual(expected_scope, actual_scope)

def getFileContents(self, name, ext):
Expand All @@ -196,6 +201,10 @@ def languageTest(self):
else:
assertions = res[3].splitlines()
for assertion in assertions:
if not len(assertion) > 0:
# allow blank lines
continue

assertion = assertion.split(':')

name = assertion[0]
Expand All @@ -212,10 +221,6 @@ def languageTest(self):

return languageTest

#
# Test Commands
#

class PhpGrammarTestIndentation(sublime_plugin.WindowCommand):

def run(self):
Expand All @@ -227,7 +232,7 @@ def run(self):
for test_file_name in glob.glob(config.indentation_tests_path + '/*' + config.indentation_test_file_extension):
name = os.path.basename(test_file_name).rpartition(config.indentation_test_file_extension)[0]
if re.search('^[a-z][a-z0-9_]*[a-z0-9]$', name):
setattr(TestIndentation, 'test_file_data_provider_%s' % name, TestIndentation.createFileDataProiderTest(name))
setattr(TestIndentation, 'test_file_data_provider_%s' % name, TestIndentation.createFileDataProviderTest(name))
else:
raise RuntimeError('Invalid indentation test file name: %s' % name)

Expand Down
20 changes: 18 additions & 2 deletions test/Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@
]
},
{
"keys": ["ctrl+k", "ctrl+c"],
"command": "php_grammar_copy_scope_name_to_clipboard",
"keys": ["ctrl+k", "ctrl+c", "ctrl+a"],
"command": "php_grammar_dev_util_copy_scope_name_to_clipboard",
"context": [
{ "key": "setting.php-grammar.development_mode" }
]
},
{
"keys": ["ctrl+k", "ctrl+c", "ctrl+e"],
"command": "php_grammar_dev_util_copy_scope_name_to_clipboard",
"args": {"assertion": "equal"},
"context": [
{ "key": "setting.php-grammar.development_mode" }
]
},
{
"keys": ["ctrl+k", "ctrl+c", "ctrl+m"],
"command": "php_grammar_dev_util_copy_scope_name_to_clipboard",
"args": {"assertion": "match"},
"context": [
{ "key": "setting.php-grammar.development_mode" }
]
Expand Down

0 comments on commit 42d7e16

Please sign in to comment.