Skip to content

Commit

Permalink
Fix: #27 return None if findWhere returns nothing.
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Dec 4, 2013
1 parent 3fa69dc commit 73e43c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 16 additions & 1 deletion tmuxp/testsuite/test_tmuxobject.py
Expand Up @@ -13,7 +13,7 @@
import random
from .. import Pane, Window, Session
from . import t
from .helpers import TmuxTestCase
from .helpers import TmuxTestCase, TEST_SESSION_PREFIX

import logging

Expand Down Expand Up @@ -58,6 +58,21 @@ def test_findWhere(self):
self.assertIsInstance(window.findWhere(
{'pane_id': pane_id}), Pane)

def test_findWhere_None(self):
""".findWhere returns None if no results found."""

while True:
nonexistant_session = TEST_SESSION_PREFIX + str(
random.randint(0, 9999)
)

if not t.has_session(nonexistant_session):
break

self.assertIsNone(t.findWhere({
'session_name': nonexistant_session
}))

def test_findWhere_multiple_attrs(self):
""".findWhere returns objects with multiple attributes."""

Expand Down
5 changes: 4 additions & 1 deletion tmuxp/util.py
Expand Up @@ -156,7 +156,10 @@ def findWhere(self, attrs):
.. _underscore.js: http://underscorejs.org/
"""
return self.where(attrs)[0] or None
try:
return self.where(attrs)[0]
except IndexError:
return None

def where(self, attrs, first=False):
"""Return objects matching child objects properties.
Expand Down

0 comments on commit 73e43c8

Please sign in to comment.