Skip to content

Commit

Permalink
Use microtest
Browse files Browse the repository at this point in the history
  • Loading branch information
rogisolorzano committed Mar 14, 2023
1 parent f11cce7 commit 94e63ca
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
An async circular queue using uasyncio, built for using with micropython.

## Usage
Just copy `async_queue.py` over to your wherever you're using it.
Just copy `async_queue.py` over to wherever you're using it.

There's a minifed version in `/release` if you'd prefer. The main `async_queue.py` is intentionally verbose.

Expand Down
2 changes: 1 addition & 1 deletion async_queue.test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from test_suite import test, expect
from microtest import test, expect
from async_queue import AsyncQueue
import uasyncio

Expand Down
87 changes: 87 additions & 0 deletions microtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class Expect:
def __init__(self, value):
self.value = value
self._not = False

def it_not(self):
self._not = True
return self

def evaluate(self, value):
return not value if self._not else value

def not_text(self):
return 'not ' if self._not else ''

def to_be(self, expected):
if (self.evaluate(self.value != expected)):
raise Exception('Expected: {}{}\nReceived: {}'.format(self.not_text(), expected, self.value))

def to_have_been_called(self):
if (self.evaluate(len(self.value.calls) == 0)):
raise Exception('Expected spy to {}be called.'.format(self.not_text()))

def to_have_been_called_with(self, *expected):
has_expected = False

for arg in self.value.calls:
has_expected = arg == expected
if (has_expected):
break

if (self.evaluate(not has_expected)):
raise Exception('Expected spy to {}have been called with {}.'.format(self.not_text(), expected))

def to_have_been_called_times(self, expected):
count = len(self.value.calls)
if (self.evaluate(count != expected)):
raise Exception('Expected spy to {}be called {} times. It was called {} times.'.format(self.not_text(), expected, count))

class Spy:
def __init__(self):
self.return_value = None
self.calls = []

def returns(self, value):
self.return_value = value
return self

def __call__(self, *args):
self.calls.append(args)
return self.return_value

class AsyncSpy(Spy):
async def __call__(self, *args):
self.calls.append(args)
return self.return_value

def spy():
return Spy()

def async_spy():
return AsyncSpy()

def expect(value):
return Expect(value)

async def test(*functions):
print('\n------------------------------------------------')
passed = 0
failed = 0

for test_function in functions:
name = test_function.__name__.replace("_", " ")

try:
await test_function()
print('PASS', name)
passed += 1
except Exception as e:
print('FAIL', name)
print(e)
failed += 1

print('\nSummary')
print('-----------')
print('Passed: {}'.format(passed))
print('Failed: {}\n'.format(failed))
52 changes: 0 additions & 52 deletions test_suite.py

This file was deleted.

0 comments on commit 94e63ca

Please sign in to comment.