Skip to content

Instantly share code, notes, and snippets.

View blaix's full-sized avatar

Justin Blake blaix

View GitHub Profile
@blaix
blaix / test.py
Last active August 29, 2015 13:56
def test_returns_2_when_x_is_1_and_y_is_1(self):
self.assertEqual(2, sum(1, 2))
def test_returns_1_when_x_is_2_and_y_is_neg1(self):
self.assertEqual(1, sum(2, -1))
# etc....
# versus:
jblake@sauron:~$ man help
No manual entry for help
jblake@sauron:~$ help man
-bash: help: no help topics match `man'. Try `help help' or `man -k man' or `info man'.
def test_subdirectory_gets_linked_to_parent(self):
# When a Site with ResourceDirectories is serialized and passed in for
# duplication, we have to re-link the parent child hierarchy since
# primary keys have changed.
...
class Observable(object):
"""Mixin for use cases that can fire events to be handled by observers"""
def __init__(self):
self._observers = []
def attach_observer(self, observer):
self._observers.append(observer)
def notify_observers(self, event, *args, **kwargs):
class PropertyObserverExample
def number
@number ||= 0
end
def number=(new_number)
old_number = @number
puts "About to change to #{new_number}"
@number = new_number
puts "Just changed from #{old_number} to #{new_number}!"
index 69fa316..932df6a 100644
--- a/tddjango/pages/tests/test_views.py
+++ b/tddjango/pages/tests/test_views.py
@@ -7,7 +7,9 @@ from pages.views import PageView
class TestPageView(TestCase):
def setUp(self):
self.page_repository = Mock()
- self.view = PageView.as_view(repository=self.page_repository)
+ self.page_presenter = Mock()
+ self.view = PageView.as_view(
@blaix
blaix / gist:5a6a199beab5de8b8ab9
Created January 22, 2015 21:31
DI container in ruby
class Factory
def foo(**kwargs)
kwargs[:bar] ||= bar
Foo.new(**kwargs)
end
def bar(**kwargs)
kwargs[:baz] ||= baz
Bar.new(**kwargs)
end
@blaix
blaix / adrian.py
Created February 24, 2015 12:02
Unit vs Integration testing in python using mocks
# consider this simple password hasher (over-simplified of course):
def password_hash(self, salt, password):
return (salt + password).reverse
# the unit tests are simple input vs output style:
def test_password_hash_returns_reverse_concatenated_salt_and_password(self):
hashed = password_hash('foo', 'bar')
self.assertEqual(hashed, 'raboof')
@blaix
blaix / east-oriented-code.md
Created March 1, 2015 22:48
The 4 Rules of East-Oriented Code
@blaix
blaix / wren.rb
Last active August 29, 2015 14:17
A ruby web-dev framework that favors lots of small objects and dependency injection over DSLs and magic methods.
require "wren/application"
# The simplest Wren application has a route and a handler:
class HelloApp
extend Wren::Routing # provides get/put/post/etc methods
extend Wren::Application # provides handle and mount methods
# and a rack-compatible call method
handle get("/hello/:name"), with: -> (name:, request:, response:) {