Package: shouldbe
Requirementsforbiddenfruita version of python with which forbidden fruit will work
(must implement the CTypes/CPython Python API)Python 2.7 or 3.3
(it may work with other versions, such as other 3.x versions,
but it has not been tested with these versions)AssertionsSeeASSERTIONS.rstExample>>> import should_be.all
>>> class Cheese(object):
...     crackers = 3
...
>>> swiss = Cheese()
>>> swiss.crackers.should_be(4)
AssertionError: swiss.crackers should have been 4, but was 3NoteBecause of the way the Python REPL shows stack traces, if the ‘should_be’
assertion is typed in a line on the REPL, ‘(unknown)’ will show instead
of ‘swiss.crackers’.  This is not an issue when the ‘should_be’ statement
is in a file instead.InstallationThe easy way$sudopipinstallhttps://github.com/DirectXMan12/should_be.git#egg=ShouldBeThe slightly-less-easy way$gitclonehttps://github.com/DirectXMan12/should_be.git$cdshould_be$./setup.pybuild$sudo./setup.pyinstallExtendingWriting your own assertions is fairly easy.  There are two core parts of
ShouldBe:BaseMixinandshould_follow.All assertions should be placed in classes that inherit fromBaseMixin.BaseMixinprovides the basic utilities for extending built-in objects
with your assertions.The class which holds your assertions should have a class variable calledtarget_class.  This is the class on which your assertions will be run.
By default, this is set toobject.  If you wish to have your assertions
run onobject, there are a few additional considerations to make (see
warning below).Then, assertions should be defined as instance methods.  Each method should callself.should_followone or more times.  Think ofshould_followasassertTrueon steroids.  It has the following signature:should_follow(self,assertion,msg=None,**kwargs).  Obviously, assertion is
an expression which, whenFalse, causesshould_followto raise anAssertionError.
So far, pretty normal.msgis where things get interesting.msgshould be
a new-style Python format string which contains only named substitutions.  By
default,should_followwill pass thetxtandselfkeys to theformatmethod,
in addition to any keyword arguments passed toshould_follow.selfis, obviously,
the current object.txtis the code that represents the current object.  For instance,
if we wrote(3).should_be(4),txtwould be ‘(3)’.  If we wrotecheese.variety.should_be('cheddar'),txtwould be ‘cheese.variety’.Once all of your assertions are written, you can simply writeMyAssertionMixin.mix()to load your assertions.  Asetuptoolshook is on the way for autoloading custom assertion mixins
withimportshould_be.all.WarningWhen you extend object, you need to also create the proper mixins forNoneType, sinceNonedoesn’t have instance methods per-se (sinceselfgets set toNone, the Python interpreter complains).  Thankfully,
this is fairly easy.  Simply create a class which inherits fromNoneTypeMixin,
and set the class variablesource_classto the name of yourobjectassertions
class.  You can then simply runMyNoneTypeMixin.mix(), and your methods will
be automatically retrieved and converted from yourobjectmixin class.NoteAssertions for ABCs (such as Sequence) will be automatically mixed in to ‘registered’
classes that do not inherit methods from the ABCs normally (such as list, etc) when
themix()method is called (this will also check for classes that are registered
to subclasses of the ABCs).