Exemplo n.º 1
0
def init():
    class XPath(Base):
        def matches(self):
            root = etree.fromstring(self.actual)
            return root.xpath(self.expected) is not None

        def failure_message(self):
            return 'Expected "%s" to match xpath "%s"' % (self.actual,
                                                          self.expected)

    expect.register('xpath', XPath)
Exemplo n.º 2
0
def init():
    class XPath(Base):

        def matches(self):
            root = etree.fromstring(self.actual)
            return root.xpath(self.expected) is not None

        def failure_message(self):
            return 'Expected "%s" to match xpath "%s"' % (self.actual, self.expected)

    expect.register('xpath', XPath)
Exemplo n.º 3
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Truthy(Base):
    """
    expect('str').to.be.truthy()
    expect('str').to.be.falsy()
    """
    def matches(self):
        return bool(self.actual)

    @property
    def explanation(self):
        return Explanation(self.actual,
                           self.is_negative,
                           action='be truthy',
                           negative_action='be falsy')


expect.register('truthy', Truthy)
expect.register('falsy', Truthy, is_negative=True)
Exemplo n.º 4
0
class Length(Base):
    """
    expect('str').to.have.length(3)
    expect([1, 2, 3]).to.have.length(3)
    """
    def matches(self):
        return len(self.actual) == self.expected

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'have length of',
                           self.expected)


class Empty(Base):
    """
    expect('').to.be.empty()
    expect([]).to.be.empty()
    """
    def matches(self):
        return len(self.actual) == 0

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be empty')


expect.register('length', Length)
expect.register('empty', Empty)
Exemplo n.º 5
0
 def test_it_can_return_matcher(self):
     expect.register('test_matcher', TestMatcher)
     matcher = expect.matcher('test_matcher')
     expect(matcher) == TestMatcher
Exemplo n.º 6
0
    expect(1).to == 1
    expect(1) == 1
    """

    def matches(self):
        return self.actual == self.expected

    def failure_message(self):
        return 'Expected "%s" to equal "%s"' % (self.actual, self.expected)

class NotEqual(Base):
    """
    expect(1).to.ne(2)
    expect(1).to.not_eq(2)
    expect(1).to != 2
    expect(2) 1= 2
    """

    def matches(self):
        return self.actual != self.expected

    def failure_message(self):
        return 'Expected "%s" to not equal "%s"' % (self.actual, self.expected)

expect.register('eq', Equal)
expect.register('__eq__', Equal)

expect.register('ne', NotEqual)
expect.register('__ne__', NotEqual)
expect.register('not_eq', NotEqual)
Exemplo n.º 7
0
    def verb(self):
        return 'be raised'

    @property
    def explanation(self):
        if self.raised:
            got = self.raised.__class__.__name__
        else:
            got = 'nothing'

        return Explanation(self.expected.__name__,
                           self.is_negative,
                           self.verb,
                           other=got)


class ExactExceptionMatcher(ExceptionMatcher):
    """
    expect(lambda: call_something(with_some_params)).to.throw_exactly(any_exception)
    """
    def matches(self):
        return type(self.raised) == self.expected

    @property
    def verb(self):
        return 'be exactly raised'


expect.register('throw', ExceptionMatcher)
expect.register('throw_exactly', ExactExceptionMatcher)
Exemplo n.º 8
0
    def match(self):
        return self

    def by(self, amount=0):
        self.changed = self.callable(self.obj) - self.obj
        self.amount = amount
        message = self.message or self.explanation.message

        if (self.changed == amount) == (not self.is_negative):
            return True

        raise BadExpectation(message)

    @property
    def explanation(self):
        return Explanation(
            self.callable.__name__, self.is_negative, 'change', self.obj,
            another_action='by', another_expected=self.amount,
            other=self.changed, force_disable_repr=True
        )


expect.register('above', Above)
expect.register('below', Below)
expect.register('more_than', Above)
expect.register('less_than', Below)
expect.register('greater_than', Above)
expect.register('smaller_than', Below)
expect.register('within', Within)
expect.register('change', Change)
Exemplo n.º 9
0
from robber import expect
from base import Base

class Identical(Base):
    """
    expect(1).to.equal(1)
    """

    def matches(self):
        return self.actual is self.expected

    def failure_message(self):
        return 'Expected "%s" to be "%s"' % (self.actual, self.expected)

class NotIdentical(Base):
    """
    expect(1).to.not_equal(2)
    """

    def matches(self):
        return self.actual is not self.expected

    def failure_message(self):
        return 'Expected "%s" not to be "%s"' % (self.actual, self.expected)

expect.register('equal', Identical)
expect.register('not_equal', NotIdentical)
Exemplo n.º 10
0
from robber import expect
from robber.matchers.base import Base


class Contain(Base):
    """
    expect({'key': value}).to.contain('key')
    expect([1, 2, 3]).to.contain(2)
    """
    def matches(self):
        return self.expected in self.actual

    def failure_message(self):
        return 'Expected {0} to contain {1}'.format(self.actual, self.expected)

class NotContain(Base):
    """
    expect({'key': value}).to.not_contain('other')
    expect([1, 2, 3]).to.not_contain(4)
    """
    def matches(self):
        return self.expected not in self.actual

    def failure_message(self):
        return 'Expected {0} to not contain {1}'.format(self.actual, self.expected)

expect.register('contain', Contain)

expect.register('not_contain', NotContain)
expect.register('exclude', NotContain)
Exemplo n.º 11
0
    """
    expect(mock).to.be.called_once_with(*args, **kwargs)
    """

    def matches(self):
        try:
            called_once = self.actual.call_count == 1
            called_with = self.actual.call_args == self.call_args
        except AttributeError:
            raise TypeError('{actual} is not a mock'.format(actual=self.actual))
        else:
            return called_once and called_with

    @property
    def explanation(self):
        if not self.actual.called:
            return Explanation(
                self.actual, self.is_negative, 'be called once with', self.expected_args_str,
                more_detail='Actually not called', force_disable_repr=True
            )

        additional_info = 'Actually called {call_count} times with Z'.format(call_count=self.actual.call_count)

        return Explanation(
            self.actual, self.is_negative, 'be called once with', self.expected_args_str,
            other=self.call_args_str, more_detail=additional_info, force_disable_repr=True
        )


expect.register('called_once_with', CalledOnceWith)
Exemplo n.º 12
0
            # run when contain chain triggered
            elements = set(expected_list).difference(self.actual)

        else:
            # run when not contain/excluded chain triggered
            elements = set(expected_list).intersection(self.actual)

        existed, self.expected_arg = self._get_first(elements)
        return existed is self.is_negative

    @staticmethod
    def _get_first(elements):
        try:
            first_element = elements.pop()
        except KeyError:
            return False, None
        else:
            return True, first_element

    @property
    def explanation(self):
        return Explanation(self.actual,
                           self.is_negative,
                           'contain',
                           self.expected_arg,
                           negative_action='exclude')


expect.register('contain', Contain)
expect.register('exclude', Contain, is_negative=True)
Exemplo n.º 13
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Boolean(Base):
    """
    expect(true).to.be.true()
    expect(true).to.be.false()
    """

    def matches(self):
        return self.actual is True

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be True', negative_action='be False')


expect.register('true', Boolean)
expect.register('false', Boolean, is_negative=True)
Exemplo n.º 14
0
from robber import expect
from robber.matchers.base import Base


class Identical(Base):
    """
    expect(1).to.equal(1)
    """

    def matches(self):
        return self.actual is self.expected

    def failure_message(self):
        return 'Expected "%s" to be "%s"' % (self.actual, self.expected)

class NotIdentical(Base):
    """
    expect(1).to.not_equal(2)
    """

    def matches(self):
        return self.actual is not self.expected

    def failure_message(self):
        return 'Expected "%s" not to be "%s"' % (self.actual, self.expected)

expect.register('equal', Identical)
expect.register('not_equal', NotIdentical)
Exemplo n.º 15
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base
from robber.matchers.mock_mixin import MockMixin


class EverCalledWith(Base, MockMixin):
    """
    expect(mock).to.have.been.ever_called_with(*args, **kwargs)
    expect(mock).to.have.any_call(*args, **kwargs)
    """

    def matches(self):
        try:
            return self.call_args in self.actual.call_args_list
        except AttributeError:
            raise TypeError('{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        return Explanation(
            self.actual, self.is_negative, 'have been ever called with', self.expected_args_str,
            force_disable_repr=True
        )


expect.register('ever_called_with', EverCalledWith)
expect.register('any_call', EverCalledWith)
Exemplo n.º 16
0
    @property
    def verb(self):
        return 'be raised'

    @property
    def explanation(self):
        if self.raised:
            got = self.raised.__class__.__name__
        else:
            got = 'nothing'

        return Explanation(self.expected.__name__, self.is_negative, self.verb, other=got)


class ExactExceptionMatcher(ExceptionMatcher):
    """
    expect(lambda: call_something(with_some_params)).to.throw_exactly(any_exception)
    """

    def matches(self):
        return type(self.raised) == self.expected

    @property
    def verb(self):
        return 'be exactly raised'


expect.register('throw', ExceptionMatcher)
expect.register('throw_exactly', ExactExceptionMatcher)
Exemplo n.º 17
0
import re

from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Match(Base):
    """
    expect('foo').to.match(r'foo')
    """
    def matches(self):
        return bool(re.match(self.expected, self.actual))

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'match',
                           self.expected)


expect.register('match', Match)
Exemplo n.º 18
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Called(Base):
    """
    expect(mock).to.be.called()
    """

    def matches(self):
        try:
            return self.actual.called
        except AttributeError:
            raise TypeError('{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be called')


expect.register('called', Called)
Exemplo n.º 19
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Callable(Base):
    """
    expect(object).to.be.callable()
    """
    def matches(self):
        return callable(self.actual)

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be callable')


expect.register('callable', Callable)
Exemplo n.º 20
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Truthy(Base):
    """
    expect('str').to.be.truthy()
    expect('str').to.be.falsy()
    """

    def matches(self):
        return bool(self.actual)

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, action='be truthy', negative_action='be falsy')


expect.register('truthy', Truthy)
expect.register('falsy', Truthy, is_negative=True)
Exemplo n.º 21
0
import re
from robber import expect
from robber.matchers.base import Base


class Match(Base):
    """
    expect('foo').to.match(r'foo')
    """
    def matches(self):
        return bool(re.match(self.expected, self.actual))

    def failure_message(self):
        return 'Expected "%s" to match "%s"' % (self.actual, self.expected)


class NotMatch(Base):
    """
    expect('bar').to.not_match(r'foo')
    """
    def matches(self):
        return not re.match(self.expected, self.actual)

    def failure_message(self):
        return 'Expected "%s" to not match "%s"' % (self.actual, self.expected)


expect.register('match', Match)
expect.register('not_match', NotMatch)
Exemplo n.º 22
0
 def test_registered_a_matcher(self):
     expect.register('test_matcher', TestMatcher)
     expect(expect.registered('test_matcher')).to.eq(True)
     expect(expect.registered('not_registered')).to.eq(False)
Exemplo n.º 23
0
    """
    def matches(self):
        return self.actual > self.expected

    def failure_message(self):
        return 'Expected %d to be above %d' % (self.actual, self.expected)

class Below(Base):
    """
    expect(1).to.be.below(2)
    """
    def matches(self):
        return self.actual < self.expected

    def failure_message(self):
        return 'Expected %d to be below %d' % (self.actual, self.expected)

class Within(Base):
    """
    expect(1).to.be.within(0, 2)
    """
    def matches(self):
        return self.expected <= self.actual <= self.args[0]

    def failure_message(self):
        return 'Expected %d to be within %d and %d' % (self.actual, self.expected, self.args[0])

expect.register('above', Above)
expect.register('below', Below)
expect.register('within', Within)
Exemplo n.º 24
0
import re
from robber import expect
from base import Base

class Match(Base):
    """
    expect('foo').to.match(r'foo')
    """
    def matches(self):
        return bool(re.match(self.expected, self.actual))

    def failure_message(self):
        return 'Expected "%s" to match "%s"' % (self.actual, self.expected)

class NotMatch(Base):
    """
    expect('bar').to.not_match(r'foo')
    """
    def matches(self):
        return not re.match(self.expected, self.actual)

    def failure_message(self):
        return 'Expected "%s" to not match "%s"' % (self.actual, self.expected)

expect.register('match', Match)
expect.register('not_match', NotMatch)
Exemplo n.º 25
0
from robber import expect
from robber.matchers.base import Base


class RespondTo(Base):
    """
    expect(obj).to.respond_to('method')
    """
    def matches(self):
        return hasattr(self.actual, self.expected) \
                and callable(getattr(self.actual, self.expected))

    def failure_message(self):
        return 'Expected "%s" to respond to "%s"' % (self.actual,
                                                     self.expected)


expect.register('respond_to', RespondTo)
Exemplo n.º 26
0
from robber import expect
from robber.matchers.base import Base


class TrueMatcher(Base):
    """
    expect(true).to.be.true()
    """
    def matches(self):
        return self.actual == True

    def failure_message(self):
        return 'Expected %s to be True' % self.actual


class FalseMatcher(Base):
    """
    expect(false).to.be.false()
    """
    def matches(self):
        return self.actual != True

    def failure_message(self):
        return 'Expected %s to be False' % self.actual


expect.register('true', TrueMatcher)
expect.register('false', FalseMatcher)
Exemplo n.º 27
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Identical(Base):
    """
    expect(1).to.equal(1)
    """

    def matches(self):
        return self.actual is self.expected

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be', self.expected)


expect.register('equal', Identical)
Exemplo n.º 28
0
import re

from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Match(Base):
    """
    expect('foo').to.match(r'foo')
    """

    def matches(self):
        return bool(re.match(self.expected, self.actual))

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'match', self.expected)


expect.register('match', Match)
Exemplo n.º 29
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base
from robber.matchers.mock_mixin import MockMixin


class EverCalledWith(Base, MockMixin):
    """
    expect(mock).to.have.been.ever_called_with(*args, **kwargs)
    expect(mock).to.have.any_call(*args, **kwargs)
    """
    def matches(self):
        try:
            return self.call_args in self.actual.call_args_list
        except AttributeError:
            raise TypeError(
                '{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        return Explanation(self.actual,
                           self.is_negative,
                           'have been ever called with',
                           self.expected_args_str,
                           force_disable_repr=True)


expect.register('ever_called_with', EverCalledWith)
expect.register('any_call', EverCalledWith)
Exemplo n.º 30
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Boolean(Base):
    """
    expect(true).to.be.true()
    expect(true).to.be.false()
    """
    def matches(self):
        return self.actual is True

    @property
    def explanation(self):
        return Explanation(self.actual,
                           self.is_negative,
                           'be True',
                           negative_action='be False')


expect.register('true', Boolean)
expect.register('false', Boolean, is_negative=True)
Exemplo n.º 31
0
        return obj

    @classmethod
    def _unicode_string_to_str(cls, u_string):
        return u_string.encode('utf-8')

    @classmethod
    def _unicode_list_to_str_list(cls, u_list):
        str_list = list(u_list)

        for i in range(0, len(str_list)):
            str_list[i] = cls.unicode_to_str(str_list[i])

        return str_list

    @classmethod
    def _unicode_dict_to_str_dict(cls, u_dict):
        str_dict = dict(u_dict)

        for key in str_dict:
            str_dict[key] = cls.unicode_to_str(str_dict[key])

        return str_dict


expect.register('eq', Equal)
expect.register('__eq__', Equal)

expect.register('ne', Equal, is_negative=True)
expect.register('__ne__', Equal, is_negative=True)
Exemplo n.º 32
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class Called(Base):
    """
    expect(mock).to.be.called()
    """
    def matches(self):
        try:
            return self.actual.called
        except AttributeError:
            raise TypeError(
                '{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be called')


expect.register('called', Called)
Exemplo n.º 33
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class InstanceOf(Base):
    """
    expect(obj).to.be.an.instanceof(Klass)
    """
    def matches(self):
        return isinstance(self.actual, self.expected)

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be an instance of',
                           self.expected)


expect.register('instanceof', InstanceOf)
Exemplo n.º 34
0
    expect(1).to == 1
    expect(1) == 1
    """
    def matches(self):
        return self.actual == self.expected

    def failure_message(self):
        return 'Expected "%s" to equal "%s"' % (self.actual, self.expected)


class NotEqual(Base):
    """
    expect(1).to.ne(2)
    expect(1).to.not_eq(2)
    expect(1).to != 2
    expect(2) 1= 2
    """
    def matches(self):
        return self.actual != self.expected

    def failure_message(self):
        return 'Expected "%s" to not equal "%s"' % (self.actual, self.expected)


expect.register('eq', Equal)
expect.register('__eq__', Equal)

expect.register('ne', NotEqual)
expect.register('__ne__', NotEqual)
expect.register('not_eq', NotEqual)
Exemplo n.º 35
0
from robber import expect
from base import Base

class RespondTo(Base):
    """
    expect(obj).to.respond_to('method')
    """
    def matches(self):
        return hasattr(self.actual, self.expected) \
                and callable(getattr(self.actual, self.expected))

    def failure_message(self):
        return 'Expected "%s" to respond to "%s"' % (self.actual, self.expected)

expect.register('respond_to', RespondTo)
Exemplo n.º 36
0
    """

    def matches(self):
        return isinstance(self.actual, tuple)

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be a tuple')


class Non(Base):
    """
    expect(None).to.be.none()
    """

    def matches(self):
        return self.actual is None

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'be None')


expect.register('string', String)
expect.register('integer', Integer)
expect.register('float', Float)
expect.register('list', List)
expect.register('dict', Dict)
expect.register('tuple', Tuple)
expect.register('none', Non)
Exemplo n.º 37
0
from robber import expect
from robber.matchers.base import Base


class Instanceof(Base):
    """
    expect(obj).to.be.an.instanceof(Klass)
    """

    def matches(self):
        return isinstance(self.actual, self.expected)

    def failure_message(self):
        return 'Expected "%s" to be an instance of "%s"' % (self.actual, self.expected)

expect.register('instanceof', Instanceof)
Exemplo n.º 38
0
        return len(self.actual) == self.expected

    def failure_message(self):
        return 'Expected "%s" to have a length of %d' % (self.actual, self.expected)

class Empty(Base):
    """
    expect('').to.be.empty()
    expect([]).to.be.empty()
    """
    def matches(self):
        return len(self.actual) == 0

    def failure_message(self):
        return 'Expected "%s" to be empty' % self.actual

class NotEmpty(Base):
    """
    expect('foo').to.be.not_empty()
    expect([1, 2, 3]).to.be.not_empty()
    """
    def matches(self):
        return len(self.actual) > 0

    def failure_message(self):
        return 'Expected "%s" to be nonempty' % self.actual

expect.register('length', Length)
expect.register('empty', Empty)
expect.register('not_empty', NotEmpty)
Exemplo n.º 39
0
        expected_list = list(self.args)
        expected_list.insert(0, self.expected)

        if not self.is_negative:
            # run when contain chain triggered
            elements = set(expected_list).difference(self.actual)

        else:
            # run when not contain/excluded chain triggered
            elements = set(expected_list).intersection(self.actual)

        existed, self.expected_arg = self._get_first(elements)
        return existed is self.is_negative

    @staticmethod
    def _get_first(elements):
        try:
            first_element = elements.pop()
        except KeyError:
            return False, None
        else:
            return True, first_element

    @property
    def explanation(self):
        return Explanation(self.actual, self.is_negative, 'contain', self.expected_arg, negative_action='exclude')


expect.register('contain', Contain)
expect.register('exclude', Contain, is_negative=True)
Exemplo n.º 40
0
 def test_registered_a_matcher(self):
     expect.register('test_matcher', TestMatcher)
     expect(expect.registered('test_matcher')) == True
     expect(expect.registered('not_registered')) == False
Exemplo n.º 41
0
from robber.matchers.base import Base
from robber.matchers.mock_mixin import MockMixin


class CalledWith(Base, MockMixin):
    """
    expect(mock).to.be.called_with(*args, **kwargs)
    """

    def matches(self):
        try:
            return self.actual.called and self.actual.call_args == self.call_args
        except AttributeError:
            raise TypeError('{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        if not self.actual.called:
            return Explanation(
                self.actual, self.is_negative, 'be called with', self.expected_args_str,
                more_detail='Actually not called', force_disable_repr=True
            )

        return Explanation(
            self.actual, self.is_negative, 'be called with', self.expected_args_str,
            other=self.call_args_str, force_disable_repr=True
        )


expect.register('called_with', CalledWith)
Exemplo n.º 42
0
from robber import expect
from robber.matchers.base import Base


class TrueMatcher(Base):
    """
    expect(true).to.be.true()
    """

    def matches(self):
        return self.actual == True

    def failure_message(self):
        return 'Expected %s to be True' % self.actual

class FalseMatcher(Base):
    """
    expect(false).to.be.false()
    """

    def matches(self):
        return self.actual != True

    def failure_message(self):
        return 'Expected %s to be False' % self.actual

expect.register('true', TrueMatcher)
expect.register('false', FalseMatcher)
Exemplo n.º 43
0
from robber import expect
from base import Base


class TrueMatcher(Base):
    """
    expect(true).to.be.true()
    """

    def matches(self):
        return self.actual == True

    def failure_message(self):
        return "Expected %s to be True" % self.actual


class FalseMatcher(Base):
    """
    expect(false).to.be.false()
    """

    def matches(self):
        return self.actual != True

    def failure_message(self):
        return "Expected %s to be False" % self.actual


expect.register("true", TrueMatcher)
expect.register("false", FalseMatcher)
Exemplo n.º 44
0
from robber import expect
from robber.matchers.base import Base


class Truthy(Base):
    """
    expect('str').to.be.truthy()
    """
    def matches(self):
        return bool(self.actual)

    def failure_message(self):
        return 'Expected "%s" to be truthy' % self.actual


class Falsy(Base):
    """
    expect('').to.be.falsy()
    """
    def matches(self):
        return not bool(self.actual)

    def failure_message(self):
        return 'Expected "%s" to be falsy' % self.actual


expect.register('truthy', Truthy)
expect.register('falsy', Falsy)
Exemplo n.º 45
0
class Empty(Base):
    """
    expect('').to.be.empty()
    expect([]).to.be.empty()
    """

    def matches(self):
        return len(self.actual) == 0

    def failure_message(self):
        return 'Expected "%s" to be empty' % self.actual


class NotEmpty(Base):
    """
    expect('foo').to.be.not_empty()
    expect([1, 2, 3]).to.be.not_empty()
    """

    def matches(self):
        return len(self.actual) > 0

    def failure_message(self):
        return 'Expected "%s" to be nonempty' % self.actual


expect.register("length", Length)
expect.register("empty", Empty)
expect.register("not_empty", NotEmpty)
Exemplo n.º 46
0
 def test_unregisted_a_matcher(self):
     expect.register('test_matcher', TestMatcher)
     expect.unregister('test_matcher')
     expect(expect.registered('test_matcher')).to.eq(False)
Exemplo n.º 47
0
class Tuple(Base):
    """
    expect((1, 2)).to.be.a.tuple()
    """
    def matches(self):
        return isinstance(self.actual, tuple)

    def failure_message(self):
        return 'Expected "%s" to be a tuple' % self.actual


class Non(Base):
    """
    expect(None).to.be.none()
    """
    def matches(self):
        return self.actual == None

    def failure_message(self):
        return 'Expected "%s" to be None' % self.actual


expect.register('string', String)
expect.register('integer', Integer)
expect.register('float', Float)
expect.register('list', List)
expect.register('dict', Dict)
expect.register('tuple', Tuple)
expect.register('none', Non)
Exemplo n.º 48
0
 def test_register_a_matcher(self):
     expect.register('test_matcher', TestMatcher)
     expect('test').test_matcher('bar')
Exemplo n.º 49
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class CalledOnce(Base):
    """
    expect(mock).to.be.called_once()
    """

    def matches(self):
        try:
            return self.actual.call_count == 1
        except AttributeError:
            raise TypeError('{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        return Explanation(
            self.actual, self.is_negative, 'be called once',
            more_detail='Called {0} times'.format(self.actual.call_count)
        )


expect.register('called_once', CalledOnce)
Exemplo n.º 50
0
from robber import expect
from robber.matchers.base import Base


class Truthy(Base):
    """
    expect('str').to.be.truthy()
    """
    def matches(self):
        return bool(self.actual)

    def failure_message(self):
        return 'Expected "%s" to be truthy' % self.actual

class Falsy(Base):
    """
    expect('').to.be.falsy()
    """
    def matches(self):
        return not bool(self.actual)

    def failure_message(self):
        return 'Expected "%s" to be falsy' % self.actual

expect.register('truthy', Truthy)
expect.register('falsy', Falsy)
Exemplo n.º 51
0
            return True

        raise BadExpectation(message)

    @property
    def explanation(self):
        return Explanation(self.callable.__name__,
                           self.is_negative,
                           'change',
                           self.obj,
                           another_action='by',
                           another_expected=self.amount,
                           other=self.changed,
                           force_disable_repr=True)


expect.register('above', Above)
expect.register('below', Below)
expect.register('more_than', Above)
expect.register('less_than', Below)
expect.register('greater_than', Above)
expect.register('smaller_than', Below)
expect.register('above_or_equal', AboveEqual)
expect.register('below_or_equal', BelowEqual)
expect.register('more_than_or_equal_to', AboveEqual)
expect.register('less_than_or_equal_to', BelowEqual)
expect.register('greater_than_or_equal_to', AboveEqual)
expect.register('smaller_than_or_equal_to', BelowEqual)
expect.register('within', Within)
expect.register('change', Change)
Exemplo n.º 52
0
class Contain(Base):
    """
    expect({'key': value}).to.contain('key')
    expect([1, 2, 3]).to.contain(2)
    """
    def matches(self):
        return self.expected in self.actual

    def failure_message(self):
        return 'Expected {0} to contain {1}'.format(self.actual, self.expected)


class NotContain(Base):
    """
    expect({'key': value}).to.not_contain('other')
    expect([1, 2, 3]).to.not_contain(4)
    """
    def matches(self):
        return self.expected not in self.actual

    def failure_message(self):
        return 'Expected {0} to not contain {1}'.format(
            self.actual, self.expected)


expect.register('contain', Contain)

expect.register('not_contain', NotContain)
expect.register('exclude', NotContain)
Exemplo n.º 53
0
                                                         self.expected)


class Empty(Base):
    """
    expect('').to.be.empty()
    expect([]).to.be.empty()
    """
    def matches(self):
        return len(self.actual) == 0

    def failure_message(self):
        return 'Expected "%s" to be empty' % self.actual


class NotEmpty(Base):
    """
    expect('foo').to.be.not_empty()
    expect([1, 2, 3]).to.be.not_empty()
    """
    def matches(self):
        return len(self.actual) > 0

    def failure_message(self):
        return 'Expected "%s" to be nonempty' % self.actual


expect.register('length', Length)
expect.register('empty', Empty)
expect.register('not_empty', NotEmpty)
Exemplo n.º 54
0
from robber import expect
from robber.explanation import Explanation
from robber.matchers.base import Base


class CalledOnce(Base):
    """
    expect(mock).to.be.called_once()
    """
    def matches(self):
        try:
            return self.actual.call_count == 1
        except AttributeError:
            raise TypeError(
                '{actual} is not a mock'.format(actual=self.actual))

    @property
    def explanation(self):
        return Explanation(self.actual,
                           self.is_negative,
                           'be called once',
                           more_detail='Called {0} times'.format(
                               self.actual.call_count))


expect.register('called_once', CalledOnce)
Exemplo n.º 55
0
        return obj

    @classmethod
    def _unicode_string_to_str(cls, u_string):
        return u_string.encode('utf-8')

    @classmethod
    def _unicode_list_to_str_list(cls, u_list):
        str_list = list(u_list)

        for i in range(0, len(str_list)):
            str_list[i] = cls.unicode_to_str(str_list[i])

        return str_list

    @classmethod
    def _unicode_dict_to_str_dict(cls, u_dict):
        str_dict = dict(u_dict)

        for key in str_dict:
            str_dict[key] = cls.unicode_to_str(str_dict[key])

        return str_dict


expect.register('eq', Equal)
expect.register('__eq__', Equal)

expect.register('ne', Equal, is_negative=True)
expect.register('__ne__', Equal, is_negative=True)