Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21c Test: Checking Errors #55

Merged
merged 3 commits into from
May 7, 2022
Merged

21c Test: Checking Errors #55

merged 3 commits into from
May 7, 2022

Conversation

ngjunsiang
Copy link
Contributor

Besides checking that our code successfully executes what should be executed, we also want to check that our code raises errors where it should. These are captured in the result object (which we'll have to turn into a class soon, before the result interface gets locked into a huge body of test code).

@ngjunsiang
Copy link
Contributor Author

Checking error-free code

We start by checking our existing test code did not raise any errors:

# Procedure should complete successfully
self.assertIsNone(self.result['error'])

Test design

To keep our tests simple to maintain, let's bundle the test code together with its script instead of putting it in a separate file:

TESTCODE = """
PROCEDURE TestBool(Succeeded : BOOLEAN)
IF Succeeded = TRUE AND NOT (-1.2 = -1.5)
THEN
OUTPUT "Yay!"
ELSE
OUTPUT "Awww!"
ENDIF
ENDPROCEDURE
CALL TestBool(TRUE)
"""

self.result = pseudo.run(TESTCODE)

Now we have one less file, and skip file-reading issues. This way we can also design our tests so that each test script tests one code block.

@ngjunsiang
Copy link
Contributor Author

Checking error-raising code

Let's add a test_error.py test:

TESTCODE = """
IF TRUE
"""
class ProcedureTestCase(unittest.TestCase):
def setUp(self):
pseudo = pseudocode.Pseudo()
self.result = pseudo.run(TESTCODE)

This should definitely raise a ParseError, if our code is working fine!

So let's get the error from result and check that it's an error of the correct type:

def test_error(self):
# Procedure should complete successfully
error = self.result['error']
self.assertTrue(
issubclass(
type(error),
pseudocode.builtin.PseudoError,
)
)
self.assertIs(
type(error),
pseudocode.builtin.ParseError,
)

And that's it! We'll be adding more test cases like these when we encounter code that should return a particular kind of error, but a bug in our code causes a different kind of error to be raised.

@ngjunsiang ngjunsiang merged commit 9bb16a5 into main May 7, 2022
@ngjunsiang ngjunsiang deleted the error branch May 8, 2022 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant