forked from Yelp/Testify
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Yelp#98 from blampe/bryce_273_compat
restore python 2.5/2.7.3 compatibility
- Loading branch information
Showing
7 changed files
with
157 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from __future__ import with_statement | ||
|
||
import os | ||
import tempfile | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from testify import TestCase | ||
from testify import setup | ||
from testify import turtle | ||
from testify.utils import inspection | ||
|
||
class DummyTestCase(TestCase): | ||
|
||
@setup | ||
def fixture(self): | ||
pass | ||
|
||
turtle_method = turtle.Turtle() | ||
|
||
def instance(self): | ||
pass | ||
|
||
@staticmethod | ||
def static(): | ||
pass | ||
|
||
|
||
class IsFixtureMethodTest(TestCase): | ||
|
||
def test_fixture(self): | ||
assert inspection.is_fixture_method(DummyTestCase.fixture) | ||
|
||
def test_turtle(self): | ||
"""Turtles are callable but not fixtures!""" | ||
assert not inspection.is_fixture_method(DummyTestCase.turtle_method) | ||
|
||
def test_lambda(self): | ||
assert not inspection.is_fixture_method(lambda: None) | ||
|
||
def test_static_method(self): | ||
assert not inspection.is_fixture_method(DummyTestCase.static) | ||
|
||
def test_instance(self): | ||
assert not inspection.is_fixture_method(DummyTestCase.instance) | ||
|
||
|
||
class CallableSetattrTest(TestCase): | ||
|
||
def test_set_function_attr(self): | ||
function = lambda: None | ||
inspection.callable_setattr(function, 'foo', True) | ||
assert function.foo | ||
|
||
def test_set_method_attr(self): | ||
inspection.callable_setattr(DummyTestCase.fixture, 'foo', True) | ||
assert DummyTestCase.fixture.foo | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2012 Yelp | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Helpers for dealing with class and method introspection. | ||
In particular, these functions help to remedy differences in behavior between | ||
Python 2.6.x and 2.7.3+ when getting/setting attributes on instancemethods. | ||
Attributes can be freely set on functions, but not instancemethods. Because we | ||
juggle methods and functions often interchangably, these produce the desired | ||
effect of setting (or getting) the attribute on the function regardless of our | ||
callable's type. | ||
""" | ||
|
||
import inspect | ||
import types | ||
|
||
|
||
def callable_hasattr(callable_, attr_name): | ||
function = get_function(callable_) | ||
return hasattr(function, attr_name) | ||
|
||
def callable_setattr(callable_, attr_name, attr_value): | ||
function = get_function(callable_) | ||
setattr(function, attr_name, attr_value) | ||
|
||
def get_function(callable_): | ||
"""If given a method, returns its function object; otherwise a no-op.""" | ||
if isinstance(callable_, types.MethodType): | ||
return callable_.im_func | ||
return callable_ | ||
|
||
def is_fixture_method(callable_): | ||
"""Whether Testify has decorated this callable as a test fixture.""" | ||
# ensure we don't pick up turtles/mocks as fixtures | ||
if not inspect.isroutine(callable_): | ||
return False | ||
|
||
# _fixture_id indicates this method was tagged by us as a fixture | ||
return callable_hasattr(callable_, '_fixture_type') | ||
|