def behavior(its): from specfor import the success_spec = spec.of("for raise error") @success_spec.that("with the.raising[exception]: do_raise_error()") def behavior(it): with the.raising[KeyError]: {}["abc"] pass pass fail_spec = spec.of("for raise error") @fail_spec.that("with the.raising[exception]: do_raise_error()") def behavior(it): with the.raising[KeyError]: pass pass @fail_spec.that("with the.raising[exception]: raise_invalid_error()") def behavior(it): with the.raising[KeyError]: []["abc"] pass pass assert spec.run(success_spec).wasSuccessful() assert len(spec.run(fail_spec).failures) == 2 pass
def behavior(its): from specfor import the success_spec = spec.of("inequality") @success_spec.that("the[val].should.not_be[exp]") def behavior(it): the["a" + "b"].should.not_be["abc"] pass @success_spec.that("the[val].should != exp") def behavior(it): the["a" + "b"].should != "abc" pass fail_spec = spec.of("inequality") @fail_spec.that("the[val].should.not_be[exp]") def behavior(it): the["a" + "b"].should.not_be["ab"] pass @fail_spec.that("the[val].should == exp") def behavior(it): the["a" + "b"].should != "ab" pass assert spec.run(success_spec).wasSuccessful() assert len(spec.run(fail_spec).failures) == 2 pass
def behavior(its): from specfor import the success_spec = spec.of("be true") @success_spec.that("the[val].should.be_true") def behavior(it): the[True].should.be_true pass @success_spec.that("the[val].should.be_ok") def behavior(it): the[10].should.be_ok pass fail_spec = spec.of("be true") @fail_spec.that("the[val].should.be_true") def behavior(it): the[0].should.be_true pass @fail_spec.that("the[val].should.be_ok") def behavior(it): the[{}].should.be_ok pass assert spec.run(success_spec).wasSuccessful() assert len(spec.run(fail_spec).failures) == 2 pass
def behavior(its): from specfor import the success_spec = spec.of("be false") @success_spec.that("the[val].should.be_false") def behavior(it): the[False].should.be_false pass @success_spec.that("the[val].should.be_no") def behavior(it): the[{}].should.be_no pass fail_spec = spec.of("be false") @fail_spec.that("the[val].should.be_false") def behavior(it): the["abc"].should.be_false pass @fail_spec.that("the[val].should.be_ok") def behavior(it): the[ValueError].should.be_no pass assert spec.run(success_spec).wasSuccessful() assert len(spec.run(fail_spec).failures) == 2 pass
def behavior(its): log = [] a_spec = spec.of("a spec") @a_spec.that("check done") def behavior(it): log.append("behavior") pass @a_spec.before() def prepare1(it): log.append("before1") pass @a_spec.before() def prepare2(it): log.append("before2") pass @a_spec.after() def cleanup1(it): log.append("after1") pass @a_spec.after() def cleanup2(it): log.append("after2") pass # check as unittest.TestCase test, r = run_as(a_spec) assert len(log) == 5 assert log[0] == "before1" assert log[1] == "before2" assert log[2] == "behavior" assert log[3] == "after1" assert log[4] == "after2" pass
def behavior(its): from specfor import the success_spec = spec.of("not_exist") @success_spec.that("the[val].should.not_exist") def behavior(it): the[None].should.not_exist pass fail_spec = spec.of("not_exist") @fail_spec.that("the[val].should.not_exist") def behavior(it): the[{}].should.not_exist pass assert spec.run(success_spec).wasSuccessful() assert not spec.run(fail_spec).wasSuccessful() pass
def behavior(its): from specfor import the success_spec = spec.of("not_be_in") @success_spec.that("the[val].should.not_be_in[collection]") def behavior(it): the["abd"].should.not_be_in[["abc", "def"]] pass fail_spec = spec.of("not_be_in") @fail_spec.that("the[val].should.not_be_in[collection]") def behavior(it): the["abc"].should.not_be_in[set(["abc", "def"])] pass assert spec.run(success_spec).wasSuccessful() assert not spec.run(fail_spec).wasSuccessful() pass
def behavior(its): from specfor import the success_spec = spec.of("be_same_order_as") @success_spec.that("the[val].should.be_same_order_as[collection]") def behavior(it): the[["abc", "def"]].should.be_same_order_as[["abc", "def"]] pass fail_spec = spec.of("be_same_order_as") @fail_spec.that("the[val].should.be_same_order_as[collection]") def behavior(it): the[["def", "abc"]].should.be_same_order_as[["abc", "def"]] pass assert spec.run(success_spec).wasSuccessful() assert not spec.run(fail_spec).wasSuccessful() pass
def behavior(its): from specfor import the success_spec = spec.of("instance") @success_spec.that("the[val].should.be_instance_of[exp_type]") def behavior(it): the[{}].should.be_instance_of[dict] pass fail_spec = spec.of("instance") @fail_spec.that("the[val].should.be_instance_of[exp_type]") def behavior(it): the[{}].should.be_instance_of[list] pass assert spec.run(success_spec).wasSuccessful() assert not spec.run(fail_spec).wasSuccessful() pass
def behavior(its): a_spec = spec.of("a spec") @a_spec.that("raise AssertionError") def behavior(it): assert False, "error" pass # check as unittest.TestCase test, r = run_as(a_spec) assert not r.wasSuccessful() pass
def behavior(its): a_spec = spec.of("spec A") b_spec = spec.of("spec B") @a_spec.before() def prepare(it): it.name = "A" @b_spec.before() def prepare(it): it.name = "B" @a_spec.that("check done for A") @b_spec.that("check done for B") def behavior(it): it.value = "done " + it.name pass # check as unittest.TestCase a_test, r = run_as(a_spec) b_test, r = run_as(b_spec) assert a_test.value == "done A" assert b_test.value == "done B" pass
def behavior(its): log = [] a_spec = spec.of("a spec") @a_spec.that("check done") def behavior(it): log.append("done") pass test, r = run_as(a_spec) assert log[0] == "done" assert r.wasSuccessful() pass
def behavior(its): # refreshed module import specfor new_specfor = specfor.new_specfor() spec = new_specfor.spec the = new_specfor.the match = new_specfor.match # define class MatchNotInstance(match.MatchAction): def __call__(self, expected): message = "it[%s] should not be instance of %s" % ( repr(self.expectation.value), repr(expected)) assert not isinstance(self.expectation.value, expected), message pass pass match.register( "not_be_instance_of", property(lambda self: MatchNotInstance(self.expectation))) # use success_spec = spec.of("not instance") @success_spec.that("the[val].should.not_be_instance_of[exp_type]") def behavior(it): the[{}].should.not_be_instance_of[list] pass fail_spec = spec.of("not instance") @fail_spec.that("the[val].should.not_be_instance_of[exp_type]") def behavior(it): the[{}].should.not_be_instance_of[dict] pass the[spec.run(success_spec).wasSuccessful()].should.be_ok the[spec.run(fail_spec).wasSuccessful()].should.be_no pass
def behavior(its): bundle = spec.behaviors_of("behaviors bundle") @bundle.that("a behavior") def behavior(it): it.value = it.name + " done" pass a_spec = spec.of("a spec") @a_spec.of(bundle) def glue(it): it.name = "a spec" pass test, r = run_as(a_spec) assert test.value == "a spec done" pass
def behavior(its): log = [] a_spec = spec.of("a spec") @a_spec.that("check done") def behavior(it): log.append("behavior") pass @a_spec.before() def prepare(it): log.append("prepare") pass # check as unittest.TestCase test, r = run_as(a_spec) assert len(log) == 2 assert log[0] == "prepare" assert log[1] == "behavior" pass
def behavior(its): log = [] a_spec = spec.of("a spec") @a_spec.that("check done") def behavior(it): assert False log.append("behavior") pass @a_spec.after() def cleanup(it): log.append("after") pass # check as unittest.TestCase test, r = run_as(a_spec) assert len(log) == 1 assert log[0] == "after" pass
def behavior(its): log = [] bundle = spec.behaviors_of("behaviors bundle") @bundle.that("a behavior") def behavior(it): log.append("behavior") pass @bundle.before() def prepare(it): log.append("bundle before") pass @bundle.after() def cleanup(it): log.append("bundle after") pass a_spec = spec.of("a spec") @a_spec.before() def prepare_spec(it): log.append("spec before") pass @a_spec.after() def cleanup_spec(it): log.append("spec after") pass @a_spec.of(bundle) def glue(it): log.append("glue") pass test, r = run_as(a_spec) assert len(log) == 6 assert log[0] == "spec before" assert log[1] == "glue" assert log[2] == "bundle before" assert log[3] == "behavior" assert log[4] == "bundle after" assert log[5] == "spec after" pass
from specfor import spec spec_for_spec = spec.of("spec for specfor.spec") @spec_for_spec.that("successful behavior") def behavior(its): log = [] a_spec = spec.of("a spec") @a_spec.that("check done") def behavior(it): log.append("done") pass test, r = run_as(a_spec) assert log[0] == "done" assert r.wasSuccessful() pass @spec_for_spec.that("failure behavior") def behavior(its): a_spec = spec.of("a spec") @a_spec.that("raise AssertionError") def behavior(it): assert False, "error" pass # check as unittest.TestCase test, r = run_as(a_spec) assert not r.wasSuccessful() pass
result = any(its.target) the[result].should == its.expected_any pass list_spec = spec.behaviors_of("list") @list_spec.that("append/pop") def behavior(its): its.target.append(its.item) result = its.target.pop() the[result].should == its.item pass # type example empty_list = spec.of("empty list") @empty_list.before() def define(its): its.target = [] pass @empty_list.of(seq_spec) def prepare(its): its.expected_sum = 0 its.expected_any = False pass @empty_list.of(list_spec) def prepare(its): its.item = "something" pass
from __future__ import with_statement from specfor import the, spec mock_spec = spec.of("mock") @mock_spec.that("define mock and create instance and chek") def behavior(its): from specfor import mock empty_mock = mock.define("empty") empty = empty_mock("id-0") mock.check(empty) pass @mock_spec.that("define method") def behavior(its): from specfor import mock converter_mock = mock.define("converter") method = converter_mock.method("convert") @method.define(just=dict(item=0)) def convert(self, item): return "zero" @method.define(just=dict(item=1)) def convert(self, item): return "one" @method.define(like=dict(item=int)) def convert(self, item): return "many" converter = converter_mock("id-0")
# spec_sum.py from specfor import the, spec empty_list = spec.of("empty list") int_list = spec.of("int list") @empty_list.before() def prepare(its): its.list = [] its.sum = 0 pass @int_list.before() def prepare(its): its.list = [2, 3, 5, 7, 11] its.sum = 28 pass @empty_list.that("sum") @int_list.that("sum") def sum_spec(its): result = sum(its.list) the[result].should == its.sum pass spec.publish(globals())
from specfor import the, spec nil_spec = spec.of("nil") @nil_spec.that("ignore any sideeffects") def behavior(its): from specfor import mock nil = mock.nil nil["abc"] = 1000 nil.abd = "def" del nil.foobar del nil["anc"] pass @nil_spec.that("as empty sequence") def behavior(its): from specfor import mock nil = mock.nil the[len(nil)].should.be[0] the[list(nil)].should.be[[]] the["abc" in nil].should.be_false pass @nil_spec.that("compare is same as None") def behavior(its): from specfor import mock nil = mock.nil the[nil < 10].should.be_true the[nil > 10].should.be_false
from __future__ import with_statement from specfor import the, spec ordered_mock_spec = spec.of("mock with ordered call") @ordered_mock_spec.that("define methods with ordered count") def behavior(its): from specfor import mock callbacks_mock = mock.define("callbacks") method_def = callbacks_mock.method("init") @method_def.define(ordered=1) def init(self): pass # next of ordered count should increment just 1 method_def = callbacks_mock.method("prepare") @method_def.define(ordered=2) def prepare(self): pass method_def = callbacks_mock.method("call") @method_def.define(ordered=3) def call(self): pass method_def = callbacks_mock.method("cleanup") @method_def.define(ordered=4) def cleanup(self): pass method_def = callbacks_mock.method("destroy")
from specfor import spec, the spec_for_match_plugin = spec.of("match plugin") @spec_for_match_plugin.that("add new verb") def behavior(its): # refreshed module import specfor new_specfor = specfor.new_specfor() spec = new_specfor.spec the = new_specfor.the match = new_specfor.match # define class MatchNotInstance(match.MatchAction): def __call__(self, expected): message = "it[%s] should not be instance of %s" % ( repr(self.expectation.value), repr(expected)) assert not isinstance(self.expectation.value, expected), message pass pass match.register( "not_be_instance_of", property(lambda self: MatchNotInstance(self.expectation))) # use success_spec = spec.of("not instance") @success_spec.that("the[val].should.not_be_instance_of[exp_type]") def behavior(it): the[{}].should.not_be_instance_of[list] pass
from __future__ import with_statement from specfor import the, spec count_mock_spec = spec.of("mock with count call") @count_mock_spec.that("count just") def behavior(its): from specfor import mock counter_mock = mock.define("counter") method_def = counter_mock.method("count") @method_def.define(at=1) def count(self): return 1 counter = counter_mock("id-0") with the.raising[AssertionError]: mock.check(counter) counter.count() mock.check(counter) counter.count() with the.raising[AssertionError]: mock.check(counter) pass @count_mock_spec.that("count over") def behavior(its): from specfor import mock counter_mock = mock.define("counter") method_def = counter_mock.method("count") @method_def.define(over=1)
from __future__ import with_statement from specfor import spec spec_for_expect = spec.of("expectation") @spec_for_expect.that("for raising error") def behavior(its): from specfor import the success_spec = spec.of("for raise error") @success_spec.that("with the.raising[exception]: do_raise_error()") def behavior(it): with the.raising[KeyError]: {}["abc"] pass pass fail_spec = spec.of("for raise error") @fail_spec.that("with the.raising[exception]: do_raise_error()") def behavior(it): with the.raising[KeyError]: pass pass @fail_spec.that("with the.raising[exception]: raise_invalid_error()") def behavior(it): with the.raising[KeyError]: []["abc"] pass pass
from __future__ import with_statement from specfor import spec, the spec_mock_plugin = spec.of("mock plugin") @spec_mock_plugin.that("add new restriction") def behavior(its): import specfor new_specfor = specfor.new_specfor() mock = new_specfor.mock plugins = new_specfor.mockings.plugins # new restriction class NeverRestriction(plugins.Restriction): name = "never" def prepare(self, responsibilities, *args, **kwargs): return True def called(self, responsibilities, returns, *args, **kwargs): return False # fail if called def completed(self, responsibilities): return def __repr__(self): return "[never]" pass plugins.register("never", lambda resp, val: NeverRestriction()) # mock person_mock = mock.define("person") prop_def = person_mock.property("age") @prop_def.get.always def age(self): return 18
# run test: python -m examples.first # run nose: nosetest examples/first.py # generate md doc: python -m specfor.doc examples.first # import from __future__ import with_statement from specfor import the, spec # [Ex1] describe a spec empty = spec.of("Empty List") # prepare sample data @empty.before() def prepare(its): its.value = [] pass # define behavior @empty.that("length should be 0") def behavior(its): result = len(its.value) # describe expectations for the[result].should.exist the[result].should.be[0] the[result].applying(str).should == "0" pass @empty.that("length should not be 1") def behavior(its): result = its.value # other style of expectations