Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Focused tests #194

Merged
merged 1 commit into from
Nov 28, 2016
Merged

[WIP] Focused tests #194

merged 1 commit into from
Nov 28, 2016

Conversation

brianium
Copy link
Member

This is a work in progress, to implement #185

@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 89.329% when pulling 4463441 on focused-specs into 01fa66e on feature/focused-specs.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 89.329% when pulling 4463441 on focused-specs into 01fa66e on feature/focused-specs.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 89.329% when pulling 4463441 on focused-specs into 01fa66e on feature/focused-specs.

Copy link
Member Author

@brianium brianium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good! Left a couple comments/questions.

Definitely feeling the growing parameter list in constructors and Context methods, but I think it is appropriate for where we are now. Maybe an item on the punchlist for 2.0 to refactor towards a config object or some other mechanism to not get locked in like that in the future.

I'll have to try it, but I think this approach would work just fine with concurrency too.

it('should return true even if nested tests are not focused', function() {
$test = new Test("test", function() {});
$this->suite->addTest($test);
assert($this->suite->getFocused(), "suite should not be focused");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we asserting here that the suite should be focused?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good catch.

*/
public function getFocused()
{
foreach ($this->tests as $test) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for crawling tests first? Wouldn't it be more efficient to check $this->focused first? It does require the explicit check for true, but would that be preferable?

if ($this->focused === true) {
    return true;
}

// check tests

return false;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's true, probably a worthwhile change.

*
* @return bool
*/
public function getFocused();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this method read better as isFocused?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, but that would also be inconsistent with getPending(). In light of that, what do you think I should call it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember there being some reasoning to getPending() . I think I remember it having something to do with pending being one of three states instead of just true/false.

I think the original thought was that null means a pending state does not exist, where true/false are explicit pending states. Hence Suite.php has the following:

    protected function runTest(TestInterface $test, TestResult $result)
    {
        if ($this->getPending() !== null) {
            $test->setPending($this->getPending());
        }

        $test->setEventEmitter($this->eventEmitter);
        $test->run($result);
    }

Looking at it now, I can't remember why the explicit setting was favored....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes some degree of sense. Since the focused state doesn't use null, it can probably be isFocused() then. I'll update that in a sec.

@@ -20,7 +20,7 @@ function describe($description, callable $fn)
*/
function context($description, callable $fn)
{
describe($description, $fn);
Context::getInstance()->addSuite($description, $fn);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we just favoring explicitness here? I always kind of viewed context and all of its forms as just an alias for describe. Why not just have it delegate to the describe function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I probably shouldn't have changed that here. My logic was "less method calls = more faster", especially for PHP versions < 7. In any case, it belongs in its own PR.

@@ -111,4 +127,23 @@ protected function runTest(TestInterface $test, TestResult $result)
$test->setEventEmitter($this->eventEmitter);
$test->run($result);
}

private function focusedTests()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple thoughts on this:

I don't think I ever published any real coding standards, so I am open to being shot down on this :) - but for this project (and generally in the PHP realm for me) - I tended more towards the get language for things returning something. It is a little more verbose, but I think it fits the language and the rest of the project a little better. Something like getFocusedTests. In JavaScript and other functional languages I use, I like to ditch some of that verbosity. Thoughts?

Second, I'm not sure highlighting focused here is the best solution. It is currently our only need so again I'm probably wrong :). It kind of seems like we are applying a transformation of state to the test tree before we run things, and this seems like something that might fit well into an array_filter type scenario:

// or maybe getTestsToRun?
protected function getFilteredTests()
{
    $tests = array_filter($this->tests, function (TestInterface $test) {
        return $test->getFocused();
    });

    return empty($tests) ? $this->tests : $tests;
}

Basically, if the state of the things to run has changed we know we can return that changed set. Otherwise return the original. The language might be a bit more flexible if we add similar functionality later on, and it also eliminates the need for the additional $hasFocusedTests variable. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have no problem with any of that. I tend not to use the "get" prefix in my own stuff, but I know Peridot does tend to use it, so I think I just did it out of habit.

Having a more generic name for the method makes total sense, as does the filter approach. It also becomes more relevant if we change that method from private to protected, as it could then be interpreted as being part of Peridot's API, so having a future-proof name is a good idea.

@ezzatron
Copy link
Contributor

Thanks for the review man, I'll probably be able to get your suggestions in today sometime.

@coveralls
Copy link

coveralls commented Nov 27, 2016

Coverage Status

Coverage increased (+0.03%) to 89.292% when pulling 5108a03 on focused-specs into 01fa66e on feature/focused-specs.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 89.292% when pulling 0ad7e19 on focused-specs into 01fa66e on feature/focused-specs.

1 similar comment
@coveralls
Copy link

coveralls commented Nov 28, 2016

Coverage Status

Coverage increased (+0.03%) to 89.292% when pulling 0ad7e19 on focused-specs into 01fa66e on feature/focused-specs.

@brianium
Copy link
Member Author

@ezzatron what is your stance on squashing commits? I tend to lean more towards cleaning up commit messages addressing review feedback, but I am open to changing that mindset if you have a preference

Thanks again man!

@ezzatron
Copy link
Contributor

I tend to squash once all feedback has been addressed. I think I covered all your review points, so I can do that now if that works for you?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 89.292% when pulling 1e320a1 on focused-specs into 01fa66e on feature/focused-specs.

1 similar comment
@coveralls
Copy link

coveralls commented Nov 28, 2016

Coverage Status

Coverage increased (+0.03%) to 89.292% when pulling 1e320a1 on focused-specs into 01fa66e on feature/focused-specs.

@brianium brianium merged commit 6969f45 into feature/focused-specs Nov 28, 2016
@brianium brianium deleted the focused-specs branch November 28, 2016 03:10
@ezzatron ezzatron modified the milestone: Next release Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants