Skip to content

Commit

Permalink
Fix #76 Testing nearest doesn't always work
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardroche committed Dec 8, 2017
1 parent e1b3910 commit d981e7d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,12 @@

All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [2.4.6] - 2017-12-08

### Fixed

* Fixed: [#76](https://github.com/gerardroche/sublime-phpunit/issues/76): Testing nearest doesn't always work

## [2.4.5] - 2017-12-05

### Fixed
Expand Down Expand Up @@ -459,6 +465,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt

* Initial import; PHPUnit support

[2.4.6]: https://github.com/gerardroche/sublime-phpunit/compare/2.4.5...2.4.6
[2.4.5]: https://github.com/gerardroche/sublime-phpunit/compare/2.4.4...2.4.5
[2.4.4]: https://github.com/gerardroche/sublime-phpunit/compare/2.4.3...2.4.4
[2.4.3]: https://github.com/gerardroche/sublime-phpunit/compare/2.4.2...2.4.3
Expand Down
19 changes: 15 additions & 4 deletions plugin.py
Expand Up @@ -154,8 +154,14 @@ def find_selected_test_methods(view):
Selection can be anywhere inside one or more test methods.
"""
method_names = []
function_areas = view.find_by_selector('meta.function')

function_regions = view.find_by_selector('entity.name.function')
function_areas = []
# Only include areas that contain function declarations.
for function_area in view.find_by_selector('meta.function'):
for function_region in function_regions:
if function_region.intersects(function_area):
function_areas.append(function_area)

for region in view.sel():
for i, area in enumerate(function_areas):
Expand All @@ -173,11 +179,16 @@ def find_selected_test_methods(view):
# BC: < 3114
if not method_names: # pragma: no cover
for region in view.sel():
word = view.substr(view.word(region))
if not is_valid_php_identifier(word) or word[:4] != 'test':
word_region = view.word(region)
word = view.substr(word_region)
if not is_valid_php_identifier(word):
return []

method_names.append(word)
scope_score = view.score_selector(word_region.begin(), 'entity.name.function.php')
if scope_score > 0:
method_names.append(word)
else:
return []

ignore_methods = ['setup', 'teardown']

Expand Down
25 changes: 22 additions & 3 deletions tests/test_find_selected_test_methods.py
Expand Up @@ -167,6 +167,10 @@ class ClassNameTest extends \\PHPUnit_Framework_TestCase
find_selected_test_methods(self.view))

def test_many_when_cursor_is_anywhere_on_method_declarations(self):
if _is_php_syntax_using_php_grammar():
# Skip because php-grammar does not support this feature
return

self.fixture("""<?php
class ClassNameTest extends \\PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -291,7 +295,7 @@ class CreateThreadsTest extends TestCase
{
use DatabaseMigrations, MockeryPHPUnitIntegration;
pu|blic function setUp()
public function se|tUp()
{
parent::setUp();
Expand All @@ -303,7 +307,7 @@ class CreateThreadsTest extends TestCase
}
/** @test */
function guests_may_not_create_threads()
function |guests_may_not_create_threads()
{
$this->withExceptionHandling();
Expand All @@ -316,4 +320,19 @@ class CreateThreadsTest extends TestCase
}
""")

self.assertEqual([], find_selected_test_methods(self.view))
self.assertEqual(['guests_may_not_create_threads'],
find_selected_test_methods(self.view))

def test_issue_76_list_index_out_of_range(self):
self.fixture("""<?php
class ClassNameTest extends \\PHPUnit_Framework_TestCase {
public function setUp() {
$func = function () {};
}
/** @test */
function x_|y_z() {}
}
""")

self.assertEqual(['x_y_z'], find_selected_test_methods(self.view))

0 comments on commit d981e7d

Please sign in to comment.