Ejemplo n.º 1
0
from ivoire import describe, context
from expects import expect
from expects.matchers.built_in import equal, be_below_or_equal
from src.sorting_algorithms.insertion_sort import insertion_sort
import random

with describe(insertion_sort) as it:

    @it.before
    def before(test):
        unodered_list = [random.randrange(-1000, 1000) for x in xrange(100)]
        test.manual_sorted_list, test.swaps = insertion_sort(unodered_list)
        test.automatic_sorted_list = sorted(unodered_list)
        test.worst_case = len(test.manual_sorted_list)**2

    with it('sorts the list') as test:
        expect(test.manual_sorted_list).to(equal(test.automatic_sorted_list))

    with it('performs better/equal to O(n**2) in the worst case') as test:
        expect(test.swaps).to(be_below_or_equal(test.worst_case))
Ejemplo n.º 2
0
from ivoire import describe, context
from expects import expect
from expects.matchers.built_in import equal, be_below_or_equal
from src.searching_algorithms.breadth_first_search import breadth_first_search
import random

with describe(breadth_first_search) as it:

    @it.before
    def before(test):
        test.adjacency_list = [
            [1, 2, 3, 4],
            [0, 3],
            [0],
            [0, 1, 5, 7],
            [0, 7],
            [3, 6, 7],
            [5],
            [3, 4, 5],
        ]

    with it('makes a breadth first traversal') as test:
        response = breadth_first_search(test.adjacency_list, 0)

        expected_response = [{
            'predecessor': None,
            'distance': 0
        }, {
            'predecessor': 0,
            'distance': 1
        }, {
Ejemplo n.º 3
0
from ivoire import describe, load
from ivoire.spec.util import ExampleWithPatch, mock

with describe(load.load_by_name, Example=ExampleWithPatch) as it:

    @it.before
    def before(test):
        test.path_exists = test.patchObject(load.os.path, "exists")
        test.load_from_path = test.patchObject(load, "load_from_path")
        test.__import__ = test.patchObject(load, "__import__", create=True)

    with it("loads paths") as test:
        test.path_exists.return_value = True
        load.load_by_name("foo")
        test.load_from_path.assert_called_once_with("foo")

    with it("loads modules") as test:
        test.path_exists.return_value = False
        load.load_by_name("foo")
        test.__import__.assert_called_once_with("foo")

with describe(load.load_from_path, Example=ExampleWithPatch) as it:

    @it.before
    def before(test):
        test.isdir = test.patchObject(load.os.path, "isdir")
        test.load_source = test.patchObject(load.imp, "load_source")
        test.path = "foo/bar"

    with it("discovers specs if given a directory") as test:
        test.isdir.return_value = True
Ejemplo n.º 4
0
A simple calculator specification.

"""

from ivoire import describe


class Calculator(object):
    def add(self, x, y):
        return x + y

    def divide(self, x, y):
        return x / y


with describe(Calculator) as it:
    @it.before
    def before(test):
        test.calc = Calculator()

    with it("adds two numbers") as test:
        test.assertEqual(test.calc.add(2, 4), 6)

    with it("divides two numbers") as test:
        test.assertEqual(test.calc.divide(8, 4), 2)

    with it("doesn't divide by zero") as test:
        with test.assertRaises(ZeroDivisionError):
            test.calc.divide(8, 0)

    with it("multiplies two numbers") as test:
Ejemplo n.º 5
0
from ivoire import describe, context
from expects import expect
from src.queues.queue_list import QueueList
from src.queues.queue_interface import QueueInterface

with describe(QueueList) as it:
  @it.before
  def before(test):
    test.queue = QueueList()

  with it('inherits from the QueueInterface') as test:
    expect(test.queue).to.be.a(QueueInterface)

  with context(QueueList.__init__):
    with it('creates an empty queue') as test:
      expect(test.queue.list).to.be.empty

  with context(QueueList.isEmpty):
    with it('returns true when queue is empty') as test:
      expect(test.queue.isEmpty()).to.be.true

    with it('returns false when queue is not empty') as test:
      test.queue.insert(1)
      expect(test.queue.isEmpty()).to.be.false

  with context(QueueList.insert):
    with it('adds a new node at the end of the queue') as test:
      test.queue.insert(2)
      test.queue.insert(3)
      expect(test.queue.list[-1].cargo).to.equal(3)
Ejemplo n.º 6
0
from ivoire import describe, context
from expects import expect
from src.list.node import Node

with describe(Node) as it:
  @it.before
  def before(test):
    nextNode = Node(1)
    test.node = Node(2, nextNode)

  with context(Node.__init__):
    with it('assign a cargo value') as test:
      expect(test.node.cargo).to.be(2)
    with it('link to another Node object') as test:
      expect(test.node.next).to.be.a(Node)

  with context(Node.__str__):
    with it('set its representation to the cargo value') as test:
      expect(test.node.__str__()).to.equal(str(test.node.cargo))

  with context(Node.__cmp__):
    with it('compares its cargo value to another one') as test:
      expect(test.node).to.be.above(Node(1))

    with it('returns True when greater') as test:
      expect(test.node > Node(0)).to.be.true

    with it('returns False when lesser') as test:
      expect(test.node > Node(3)).to.be.false

    with it('returns False when equal') as test:
from ivoire import describe, context
from expects import expect
from src.list.unordered_linked_list import UnorderedLinkedList
from src.list.node import Node

with describe(UnorderedLinkedList) as it:
  @it.before
  def before(test):
    test.list = UnorderedLinkedList()

  with context(UnorderedLinkedList.__init__):
    with it('create an empty list') as test:
      expect(test.list.length).to.be(0)
      expect(test.list.head).to.be.none      

  with context(UnorderedLinkedList.isEmpty):
    with it('return true if the list is empty') as test:
      expect(test.list.isEmpty()).to.be.true

  with context(UnorderedLinkedList.addNode):
    with it('does not add a new item when it is None') as test:
      test.list.addNode(None)
      expect(test.list.head).to.be.none

    with it('create a Node object and add it to the list') as test:
      test.list.addNode(33)
      expect(test.list.head).to.be.a(Node)

    with it('increments the length of the linked list') as test:
      test.list.addNode(22)
      expect(test.list.length).to.be(1)
Ejemplo n.º 8
0
from ivoire import describe, load
from ivoire.spec.util import ExampleWithPatch, mock


with describe(load.load_by_name, Example=ExampleWithPatch) as it:
    @it.before
    def before(test):
        test.path_exists = test.patchObject(load.os.path, "exists")
        test.load_from_path = test.patchObject(load, "load_from_path")
        test.__import__ = test.patchObject(load, "__import__", create=True)

    with it("loads paths") as test:
        test.path_exists.return_value = True
        load.load_by_name("foo")
        test.load_from_path.assert_called_once_with("foo")

    with it("loads modules") as test:
        test.path_exists.return_value = False
        load.load_by_name("foo")
        test.__import__.assert_called_once_with("foo")


with describe(load.load_from_path, Example=ExampleWithPatch) as it:
    @it.before
    def before(test):
        test.isdir = test.patchObject(load.os.path, "isdir")
        test.load_source = test.patchObject(load.imp, "load_source")
        test.path = "foo/bar"

    with it("discovers specs if given a directory") as test:
        test.isdir.return_value = True
Ejemplo n.º 9
0
"""
A spec for the next() standard library function.

"""

from ivoire import describe

with describe(next) as it:
    with it("returns the next element of an iterable") as test:
        iterable = iter(range(5))
        test.assertEqual(next(iterable), 0)

    with it("raises StopIteration if no elements are found") as test:
        with test.assertRaises(StopIteration):
            next(iter([]))

    with it("returns default instead of StopIteration if given") as test:
        default = "a default"
        test.assertEqual(next(iter([]), default), default)
Ejemplo n.º 10
0
from ivoire import describe, context
from expects import expect
from src.trees.tree import Tree

with describe(Tree) as it:
    @it.before
    def before(test):
        test.defaultCargo = 2.22
        test.tree = Tree(test.defaultCargo)

    with context(Tree.__init__):
        with it('creates a new tree with a cargo value') as test:
            expect(test.tree).to.have.property('cargo', test.defaultCargo)

        with it('creates a new tree with an empty left node') as test:
            expect(test.tree).to.have.property('left', None)

        with it('creates a new tree with an empty right node') as test:
            expect(test.tree).to.have.property('right', None)

    with context(Tree.__str__):
        with it('returns the representation of the tree: its cargo') as test:
            expect(test.tree.__str__()).to.equal(str(test.tree.cargo))

    with context(Tree.getCargo):
        with it('returns the cargo of the tree') as test:
            expect(test.tree.getCargo()).to.equal(test.tree.cargo)

    with context(Tree.getLeft):
        with it('returns the left node of the tree') as test:
            leftTreeNode = Tree(22)
Ejemplo n.º 11
0
from ivoire import describe, context
from expects import expect
from src.new_data_type.gcd import gcd

with describe(gcd) as it:
    with context('when x divides y evenly'):
        with it('y is the gdc') as test:
            expect(gcd(2, 2)).to.equal(2)
    with context('when x does not divides y evenly'):
        with it('find the gdc of y and the reminder of x divided by y'
                ) as test:
            expect(gcd(9, 4)).to.equal(gcd(4, 9 % 4))
Ejemplo n.º 12
0
from ivoire import describe, result, run
from ivoire.spec.util import ExampleWithPatch, mock
import ivoire


with describe(run.parse, Example=ExampleWithPatch) as it:
    @it.before
    def before(test):
        test.specs = ["a_spec"]

    with it("sets reasonable defaults") as test:
        should_color = test.patchObject(run, "should_color")
        arguments = run.parse(test.specs)
        test.assertEqual(vars(arguments), {
            "Formatter" : result.DotsFormatter,
            "color" : should_color.return_value,
            "exitfirst" : False,
            "specs" : test.specs,
            "func" : run.run,
            "verbose" : False,
        })
        should_color.assert_called_once_with("auto")

    with it("can exitfirst") as test:
        arguments = run.parse(["--exitfirst"] + test.specs)
        test.assertTrue(arguments.exitfirst)

        arguments = run.parse(["-x"] + test.specs)
        test.assertTrue(arguments.exitfirst)

    with it("can be verbose") as test:
Ejemplo n.º 13
0
from ivoire import describe, context
from expects import expect
from src.queues.helper import Helper

with describe(Helper) as it:
  @it.before
  def before(test):
    test.list = [22,100,2,98,0]
  
  with context(Helper.remove_item_from_list_at_index):
    with it('remove an element from a list at the given index') as test:
      Helper.remove_item_from_list_at_index(test.list, 2)
      expect(test.list[2]).to.equal(98)

  with context(Helper.get_max_item_index):
    with it('find and return the index of the element with the max value in the list') as test:
      expect(Helper.get_max_item_index(test.list)).to.equal(1)
from ivoire import describe, context
from expects import expect
from expects.matchers.built_in import equal, be_below_or_equal
from src.sorting_algorithms.selection_sort import selection_sort
import random

with describe(selection_sort) as it:

    @it.before
    def before(test):
        unodered_list = [random.randrange(-1000, 1000) for x in xrange(100)]
        test.manual_sorted_list = selection_sort(unodered_list)
        test.automatic_sorted_list = sorted(unodered_list)

    with it('sorts the list') as test:
        expect(test.manual_sorted_list).to(equal(test.automatic_sorted_list))
Ejemplo n.º 15
0
from ivoire import describe, context
from expects import expect
from src.new_data_type.gcd import gcd

with describe(gcd) as it:
  with context('when x divides y evenly'):
    with it('y is the gdc') as test:
      expect(gcd(2,2)).to.equal(2)
  with context('when x does not divides y evenly'):
    with it('find the gdc of y and the reminder of x divided by y') as test:
      expect(gcd(9,4)).to.equal(gcd(4,9%4))
Ejemplo n.º 16
0
from io import StringIO
import sys

from ivoire import describe, result
from ivoire.compat import indent
from ivoire.spec.util import ExampleWithPatch, mock


def fake_exc_info():
    try:
        raise Exception("Used to construct exc_info")
    except Exception:
        return sys.exc_info()


with describe(result.ExampleResult, Example=ExampleWithPatch) as it:
    @it.before
    def before(test):
        test.formatter = mock.Mock()
        test.result = result.ExampleResult(test.formatter)
        test.test = mock.Mock()
        test.exc_info = fake_exc_info()

    def assertShown(test, output):
        test.formatter.show.assert_called_with(output)

    with it("enters groups") as test:
        test.result.enterGroup(test.test.group)
        test.formatter.enter_group.assert_called_once_with(test.test.group)
        assertShown(test, test.formatter.enter_group.return_value)
Ejemplo n.º 17
0
from ivoire import describe, transform
from ivoire.spec.util import ExampleWithPatch, mock


def dump(node):  # pragma: no cover
    return(dedent("""
    --- Dumping Node ---

    {}

    --- End ---
    """.format(ast.dump(node))))


with describe(transform.ExampleTransformer, Example=ExampleWithPatch) as it:
    @it.before
    def before(test):
        test.transformer = transform.ExampleTransformer()

    def execute(test, source):
        test.globals, test.locals = {}, {}
        test.node = ast.parse(dedent(source))
        test.transformed = test.transformer.transform(test.node)
        compiled = compile(test.transformed, "<testing transform>", "exec")

        try:
            exec(compiled, test.globals, test.locals)
        except Exception:  # pragma: no cover
            print(dump(test.transformed))
            raise
Ejemplo n.º 18
0
from ivoire import describe, result, run
from ivoire.spec.util import ExampleWithPatch, mock
import ivoire

with describe(run.parse, Example=ExampleWithPatch) as it:

    @it.before
    def before(test):
        test.specs = ["a_spec"]

    with it("sets reasonable defaults") as test:
        should_color = test.patchObject(run, "should_color")
        arguments = run.parse(test.specs)
        test.assertEqual(
            vars(arguments), {
                "Formatter": result.DotsFormatter,
                "color": should_color.return_value,
                "exitfirst": False,
                "specs": test.specs,
                "func": run.run,
                "verbose": False,
            })
        should_color.assert_called_once_with("auto")

    with it("can exitfirst") as test:
        arguments = run.parse(["--exitfirst"] + test.specs)
        test.assertTrue(arguments.exitfirst)

        arguments = run.parse(["-x"] + test.specs)
        test.assertTrue(arguments.exitfirst)
Ejemplo n.º 19
0
A simple calculator specification.

"""

from ivoire import describe


class Calculator(object):
    def add(self, x, y):
        return x + y

    def divide(self, x, y):
        return x / y


with describe(Calculator) as it:

    @it.before
    def before(test):
        test.calc = Calculator()

    with it("adds two numbers") as test:
        test.assertEqual(test.calc.add(2, 4), 6)

    with it("divides two numbers") as test:
        test.assertEqual(test.calc.divide(8, 4), 2)

    with it("doesn't divide by zero") as test:
        with test.assertRaises(ZeroDivisionError):
            test.calc.divide(8, 0)
from ivoire import describe, context
from expects import expect
from src.queues.priority_queue_list import PriorityQueueList
from src.queues.priority_queue_interface import PriorityQueueInterface

with describe(PriorityQueueList) as it:

    @it.before
    def before(test):
        test.queue = PriorityQueueList()

    with it('inherits from PriorityQueueInterface') as test:
        expect(test.queue).to.be.a(PriorityQueueInterface)

    with context(PriorityQueueList.__init__):
        with it('creates an empty queue') as test:
            expect(test.queue.items).to.be.empty

    with context(PriorityQueueList.insert):
        with it('add a new item at the end of the queue') as test:
            test.queue.insert(22)
            test.queue.insert(33)
            expect(test.queue.items[-1]).to.equal(33)

    with context(PriorityQueueList.remove):
        with it('remove and return the item with the highest priority from the queue'
                ) as test:
            test.queue.insert(55)
            test.queue.insert(8)
            test.queue.insert(100)
            test.queue.insert(22)
from ivoire import describe, context
from expects import expect
from src.queues.linked_queue_improved import LinkedQueueImproved
from src.queues.queue_interface import QueueInterface

with describe(LinkedQueueImproved) as it:
  @it.before
  def before(test):
    test.queue = LinkedQueueImproved()

  with it('inherits from QueueInterface') as test:
    expect(test.queue).to.be.a(QueueInterface)

  with context(LinkedQueueImproved.__init__):
    with it('creates an empty queue') as test:
      expect(test.queue).to.have.property('length', 0)
      expect(test.queue).to.have.property('head', None)
      expect(test.queue).to.have.property('tail', None)

  with context(LinkedQueueImproved.isEmpty):
    with it('returns True when queue is empty') as test:
      expect(test.queue.isEmpty()).to.be.true

    with it('returns False when queue is not empty') as test:
      test.queue.insert(88)
      expect(test.queue.isEmpty()).to.be.false

  with context(LinkedQueueImproved.insert):
    with it('adds a new node to the end/tail of the queue') as test:
      test.queue.insert(11)
      test.queue.insert(22)
Ejemplo n.º 22
0
from ivoire import describe
from ivoire.manager import Context
from ivoire.spec.util import ExampleWithPatch, mock


with describe(Context, Example=ExampleWithPatch) as it:
    @it.before
    def before(test):
        test.manager = mock.Mock()
        test.context = Context("a test context", test.manager)

    with it("calls its manager when used as a context manager") as test:
        with test.context:
            test.manager.enter.assert_called_once_with(test.context)
        test.manager.exit.assert_called_once_with()
Ejemplo n.º 23
0
from ivoire import describe, context
from expects import expect
from src.new_data_type.fraction import Fraction

with describe(Fraction) as it:

    @it.before
    def before(test):
        test.fraction = Fraction(2, 3)
        test.other_fraction = Fraction(3, 2)
        test.other_int = 3
        test.fraction_equal = Fraction(2, 3)
        test.fraction_almost_equal = Fraction(2, 4)

    with context(Fraction.__init__):

        with it('set the fraction numerator') as test:
            expect(test.fraction.numerator).not_to.be.none

        with it('set the fraction denominator') as test:
            expect(test.fraction.denominator).not_to.be.none

    with context(Fraction.__mul__):

        with it('multiply a fraction by another one') as test:
            expect(test.fraction * test.other_fraction).to.equal(Fraction(
                1, 1))

        with it('multiply a fraction by an int number') as test:
            expect(test.fraction * test.other_int).to.equal(Fraction(2, 1))
from ivoire import describe, context
from expects import expect
from src.stack.stack_list import StackList
from src.stack.stack_interface import StackInterface

with describe(StackList) as it:
  @it.before
  def before(test):
    test.stack = StackList()

  with it('inherits from StackInterface') as test:
      expect(test.stack).to.be.a(StackInterface)

  with context(StackList.__init__):
    with it('create an empty stack') as test:
      expect(test.stack.items).to.be.empty

  with context(StackList.push):
    with it('add a new item to the end of the stack') as test:
      item = 4
      test.stack.push(item)
      last_item = test.stack.items[-1]
      
      expect(last_item).to.be(item)

  with context(StackList.pop):
    with it('remove and return the last added item from the stack') as test:
      item = 4
      test.stack.push(item)
      last_item = test.stack.pop()
      
Ejemplo n.º 25
0
from ivoire import describe, context
from expects import expect
from src.new_data_type.fraction import Fraction

with describe(Fraction) as it:
  @it.before
  def before(test):
    test.fraction = Fraction(2,3)
    test.other_fraction = Fraction(3,2)
    test.other_int = 3
    test.fraction_equal = Fraction(2,3)
    test.fraction_almost_equal = Fraction(2,4)

  with context(Fraction.__init__):
    
    with it('set the fraction numerator') as test:
      expect(test.fraction.numerator).not_to.be.none

    with it('set the fraction denominator') as test:
      expect(test.fraction.denominator).not_to.be.none

  with context(Fraction.__mul__):
    
    with it('multiply a fraction by another one') as test:
      expect(test.fraction * test.other_fraction).to.equal(Fraction(1,1))
    
    with it('multiply a fraction by an int number') as test:
      expect(test.fraction * test.other_int).to.equal(Fraction(2,1))

    with it('multiply an int number by a fraction') as test:
      expect(test.other_int * test.fraction).to.equal(Fraction(2,1))
from ivoire import describe, context
from expects import expect
from src.deque.deque_list import DequeList
from src.deque.deque_interface import DequeInterface

with describe(DequeList) as it:

    @it.before
    def before(test):
        test.deque = DequeList()

    with it('inherits from the DequeInterface') as test:
        expect(test.deque).to.be.a(DequeInterface)

    with context(DequeList.__init__):
        with it('creates an empty deque') as test:
            expect(test.deque.items).to.be.empty

    with context(DequeList.isEmpty):
        with it('returns true when deque is empty') as test:
            expect(test.deque.isEmpty()).to.be.true

        with it('returns false when deque is not empty') as test:
            test.deque.insertFirst(10)
            test.deque.insertLast(9)

            expect(test.deque.isEmpty()).to.be.false

    with context(DequeList.insertFirst):
        with it('adds an item at the top of the deque') as test:
            items = [11, 22, 33]
from ivoire import describe, context
from expects import expect
from src.deque.deque_list import DequeList
from src.deque.deque_interface import DequeInterface

with describe(DequeList) as it:
    @it.before
    def before(test):
        test.deque = DequeList()

    with it('inherits from the DequeInterface') as test:
        expect(test.deque).to.be.a(DequeInterface)

    with context(DequeList.__init__):
        with it('creates an empty deque') as test:
            expect(test.deque.items).to.be.empty

    with context(DequeList.isEmpty):
        with it('returns true when deque is empty') as test:
            expect(test.deque.isEmpty()).to.be.true

        with it('returns false when deque is not empty') as test:
            test.deque.insertFirst(10)
            test.deque.insertLast(9)

            expect(test.deque.isEmpty()).to.be.false

    with context(DequeList.insertFirst):
        with it('adds an item at the top of the deque') as test:
            items = [11,22,33]
            topItem = items[-1]
Ejemplo n.º 28
0
from ivoire import describe, transform
from ivoire.spec.util import ExampleWithPatch


def dump(node):  # pragma: no cover
    return (dedent("""
    --- Dumping Node ---

    {}

    --- End ---
    """.format(ast.dump(node))))


with describe(transform.ExampleTransformer, Example=ExampleWithPatch) as it:

    @it.before
    def before(test):
        test.transformer = transform.ExampleTransformer()

    def execute(test, source):
        test.globals, test.locals = {}, {}
        test.node = ast.parse(dedent(source))
        test.transformed = test.transformer.transform(test.node)
        compiled = compile(test.transformed, "<testing transform>", "exec")

        try:
            exec(compiled, test.globals, test.locals)
        except Exception:  # pragma: no cover
            print(dump(test.transformed))
Ejemplo n.º 29
0
from ivoire import describe, context
from expects import expect
from src.list.node import Node

with describe(Node) as it:

    @it.before
    def before(test):
        nextNode = Node(1)
        test.node = Node(2, nextNode)

    with context(Node.__init__):
        with it('assign a cargo value') as test:
            expect(test.node.cargo).to.be(2)
        with it('link to another Node object') as test:
            expect(test.node.next).to.be.a(Node)

    with context(Node.__str__):
        with it('set its representation to the cargo value') as test:
            expect(test.node.__str__()).to.equal(str(test.node.cargo))

    with context(Node.__cmp__):
        with it('compares its cargo value to another one') as test:
            expect(test.node).to.be.above(Node(1))

        with it('returns True when greater') as test:
            expect(test.node > Node(0)).to.be.true

        with it('returns False when lesser') as test:
            expect(test.node > Node(3)).to.be.false
Ejemplo n.º 30
0
from ivoire import context, describe, ContextManager
from ivoire.manager import Context
from ivoire.spec.util import ExampleWithPatch, mock

with describe(ContextManager, Example=ExampleWithPatch) as it:

    @it.before
    def before(test):
        test.result = mock.Mock()
        test.manager = ContextManager(test.result)
        test.context = test.manager.create_context("a test context")

    with context(context):
        with it("creates contexts") as test:
            context = test.manager.create_context("a test context")
            test.assertEqual(context, Context("a test context", test.manager))

        with it("is a bit nasty and tries to get __name__s") as test:

            def foo():
                pass

            context = test.manager.create_context(foo)
            test.assertEqual(context, Context("foo", test.manager))

    with it("starts off at a global context depth of 0") as test:
        test.assertEqual(test.manager.context_depth, 0)

    with it("enters and exits contexts") as test:
        test.manager.enter(test.context)
        test.result.enterContext.assert_called_once_with(test.context, depth=1)
from ivoire import describe, context
from expects import expect
from src.queues.linked_queue_improved import LinkedQueueImproved
from src.queues.queue_interface import QueueInterface

with describe(LinkedQueueImproved) as it:

    @it.before
    def before(test):
        test.queue = LinkedQueueImproved()

    with it('inherits from QueueInterface') as test:
        expect(test.queue).to.be.a(QueueInterface)

    with context(LinkedQueueImproved.__init__):
        with it('creates an empty queue') as test:
            expect(test.queue).to.have.property('length', 0)
            expect(test.queue).to.have.property('head', None)
            expect(test.queue).to.have.property('tail', None)

    with context(LinkedQueueImproved.isEmpty):
        with it('returns True when queue is empty') as test:
            expect(test.queue.isEmpty()).to.be.true

        with it('returns False when queue is not empty') as test:
            test.queue.insert(88)
            expect(test.queue.isEmpty()).to.be.false

    with context(LinkedQueueImproved.insert):
        with it('adds a new node to the end/tail of the queue') as test:
            test.queue.insert(11)
from ivoire import describe, context
from expects import expect
from src.queues.linked_queue import LinkedQueue

with describe(LinkedQueue) as it:
  @it.before
  def before(test):
    test.queue = LinkedQueue()

  @it.after
  def after(test):
    test.queue = None

  with context(LinkedQueue.__init__):
    with it('creates an empty queue') as test:
      expect(test.queue).to.have.property('length', 0)
      expect(test.queue).to.have.property('head', None)

  with context(LinkedQueue.isEmpty):
    with it('return true if the queue is empty') as test:
      expect(test.queue.isEmpty()).to.be.true

    with it('returns false if the queue is not empty') as test:
      test.queue.insert(7)
      expect(test.queue.isEmpty()).to.be.false

  with context(LinkedQueue.insert):
    with it('add node at the end of the queue') as test:
      test.queue.insert(1)
      test.queue.insert(2)
      test.queue.insert(7)
from ivoire import describe, context
from expects import expect
from src.queues.linked_priority_queue import PriorityQueueLinkedList
from src.queues.priority_queue_interface import PriorityQueueInterface
from src.list.node import Node

with describe(PriorityQueueLinkedList) as it:
  @it.before
  def before(test):
    test.queue = PriorityQueueLinkedList()

  with it('inherits from PriorityQueueInterface') as test:
    expect(test.queue).to.be.a(PriorityQueueInterface)

  with context(PriorityQueueLinkedList.__init__):
    with it('creates an empty queue') as test:
      expect(test.queue).to.have.property('length', 0)
      expect(test.queue).to.have.property('head', None)

  with context(PriorityQueueLinkedList.insert):
    with it('when queue is empty, add a node at the top of the queue') as test:
      test.queue.insert(100)
      expect(test.queue).to.have.property('head', Node(100))

    with it('increments the length of the queue') as test:
      test.queue.insert(100)
      expect(test.queue).to.have.property('length', 1)

    with it('add a node at the queue in a position based on its priority') as test:
      test.queue.insert(33)
      test.queue.insert(2)
Ejemplo n.º 34
0
"""
A spec for the next() standard library function.

"""

from ivoire import describe


with describe(next) as it:
    with it("returns the next element of an iterable") as test:
        iterable = iter(range(5))
        test.assertEqual(next(iterable), 0)

    with it("raises StopIteration if no elements are found") as test:
        with test.assertRaises(StopIteration):
            next(iter([]))

    with it("returns default instead of StopIteration if given") as test:
        default = "a default"
        test.assertEqual(next(iter([]), default), default)
Ejemplo n.º 35
0
from ivoire import describe, context
from expects import expect
from src.list.unordered_linked_list import UnorderedLinkedList
from src.list.node import Node

with describe(UnorderedLinkedList) as it:

    @it.before
    def before(test):
        test.list = UnorderedLinkedList()

    with context(UnorderedLinkedList.__init__):
        with it('create an empty list') as test:
            expect(test.list.length).to.be(0)
            expect(test.list.head).to.be.none

    with context(UnorderedLinkedList.isEmpty):
        with it('return true if the list is empty') as test:
            expect(test.list.isEmpty()).to.be.true

    with context(UnorderedLinkedList.addNode):
        with it('does not add a new item when it is None') as test:
            test.list.addNode(None)
            expect(test.list.head).to.be.none

        with it('create a Node object and add it to the list') as test:
            test.list.addNode(33)
            expect(test.list.head).to.be.a(Node)

        with it('increments the length of the linked list') as test:
            test.list.addNode(22)