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)
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)
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
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 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.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 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)
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()
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)
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))
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)
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))
from ivoire import describe, context 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("multiplies two numbers") as test: test.assertEqual(test.calc.multiply(2, 3), 6) with context(Calculator.divide): 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.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.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.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]
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.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)