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
class IPaintable(Interface): | |
def get_color(): | |
pass | |
def paint(color): | |
pass | |
class BicyclePainter(object): | |
"""Adapts a Bicycle to the IPaintable interface.""" | |
def __init__(self, context): |
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
def not_empty_string(value): | |
assert(len(value) > 0) | |
def valid_email(value): | |
assert("@" in value) | |
def greater_than_zero(value): | |
assert(int(value) > 0) | |
FIELD_VALIDATORS = { |
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
CarList = function(){ | |
this.cars = []; | |
}; | |
CarList.prototype.add_car = function(car){ | |
this.cars.push(car); | |
this.render(); | |
}; | |
/* | |
Some code here for managing rendering of the list of cars. | |
*/ |
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
DEFAULT_EMAIL_TYPE = "text" | |
DEFAULT_RECIPIENT = "alecmunro@gmale.com" | |
DEFAULT_SUBJECT = "Good news, everyone!" | |
class EmailReporter(object): | |
email_type = DEFAULT_EMAIL_TYPE | |
def send_report(self, settings): | |
data = self.collect_data() |
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
"""Mocker: | |
Chaining attribute or method access in Mocker is trivial. During | |
the record phase, every mock operation returns a mock.""" | |
from mocker import Mocker | |
def mocker_test(): | |
mocker = Mocker() | |
a_mock = mocker.mock() |
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
#Need to create this because mocker.patch() and flexmock(object) | |
#don't work with builtins. | |
class Something(object): | |
def do_something(self): | |
pass | |
"""Mocker: |
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
"""Mocker: | |
Setting expectations with Mocker is as simple as calling the | |
methods on the mocks, and then switching the mocker instance | |
into replay mode.""" | |
from mocker import Mocker | |
def mocker_test(): | |
mocker = Mocker() |
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
"""Mocker: | |
Mocks are obtained from a mocker instance, which is created | |
automatically if you create a test case that inherits from | |
MockerTestCase. | |
""" | |
from mocker import Mocker, MockerTestCase | |
class TestCaseForMocker(MockerTestCase): |
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
def delete_file(self, name): | |
path = os.path.join(BASE_DIR, name) | |
if self.os.path.exists(path): | |
os.remove(path) |
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
import time | |
def __init__(self, time_mod=time): | |
self.time = time_mod | |
def some_method(self): | |
self.time.sleep(10) |