Now that we have a trivial first test, we’ll begin growing the implementation one test at a time. We’ll be following Uncle Bob’s Three Rules of TDD, summarized here as:
Write no production code without a failing test
Write just enough of a test to get the test to fail
Write just enough production code to get the test to pass
We will additionally do the following:
Keep existing tests passing while making the new ones pass
Keep the code clean by refactoring it every so often
Frequently committing code using git
In general, there are for things we might do at any point:
Write test code
Write production code
Refactor test code
Refactor production code
We’ll strive to do only one of these at a time and only switch to another one of these actions when all tests are passing.
Moving Towards Binary Expression
Rather than immediately going to a full binary expression, we’ll add a test with a number and an operator.
Create a new test:
Run your tests, you should see an error similar to:
A little bit of regex magic allows us to get the tests passing:
Run your tests, they are passing.
When I wrote that code, I was tempted to add a check to verify something I suspected. Rather than do that, with all of the tests passing, I suggest going back to the first test and making a change. There’s near duplication between the two tests. Make them more similar.
Update the first test:
Run the tests, you’ll notice now that the first test fails:
Suspicion confirmed, update the code:
Run your tests, and it’s back to passing.
Now we might be ready for a complete binary expression. Let’s give that a try, then we’ll do some refactoring of the tests.
Add a new test:
Sure enough, running the tests shows that we’re not quite done with a binary expression:
We need to do this more than once, and check for digits and not digits. This will do it:
Run your tests, they should pass.
A quick check after your tests are passing will verify that the final if is not necessary:
Run your tests, they should be passing.
Now is a great time to commit your changes because we are going to refactor and if things go badly, this is a good place to be able to easily get back to.
Noticing some duplication and after a two successful tries, I ended up with the following:
I’m not a PowerShell expert and I do not know how common/popular/idiomatic the use of [ref] is, but it nicely collapses the code. I even notice something that will come up later (I see a pattern I’ve not noticed before). So I’ll chose some tests to exploit that. But before doing that, thre’s a few more things to do with our tests in terms of refactoring and test cases.
The test file has a bit of duplication. It’s time to collapse that. To do so, we’ll use the -TestCases feature of Pester.
Update your test by adding a new test, which duplicates the first test:
Run your tests, and they all pass:
This new tests duplicates the first test, so it is safe to remove it. While you are at it, convert the other two tests into this last one:
Now back to checking/extending the behavior. Let’s make sure our code handles white space, and more than one operator, multi-character operators, and even variables.
Add a new test case:
Run the tests, this seems to work fine. The while loop covers an expression as long as we need.
Now let’s make our code handle variables. Add another test case:
I was initially surprised this worked, but that’s the issue with regular expressions. They are often more flexible than you at first realize. The letters are not digits, which is how we are checking for digits versus operators. Knowing this, I’m going to change that test case to a multi-letter variable because I want to start with a broken test.
This fails as I expected:
Here’s a quick fix to make this work:
Note that the regular expression was simply \d+ and now it is [\d\w]+. This might seem too clever, too simple or maybe it seems like I’m cheating. In fact, if that’s the case, then to “prove” I’m cheating, you want to find a test that will cause my code to break. However, I’m fine with that solution for now. If this were a real problem, I think I’d have a known list of operators and check for them explicitly. However, this is a simple example and so I’m OK with simple tests and simple solutions.
What about a multi-character operator? Add a new test:
Run your tests, and this fails.
Update the regular expression to fix this:
When I run the tests, I find my confidence was too high:
The second regular expression was originally the opposite of the first, but we added \w to the first, so we need to do the same for the second one:
That’s a good lesson. I though it was simple, and it was, just not in the way I though. My tests allowed me to experiment, learn, adjust and make progress.
Now is probably a good time to commit your changes.
Now let’s handle white space. We can match white space much like we do everything else, or we could simply remove it. Regardless, a test will keep us obvious. I think this is going to be simple, so I’ll start with a “big” test:
Once again, my confidence has bitten me. I figured I could simply replace all of the white space:
Close, but no cigar:
Here’s a second attempt:
There are three changes. First, the regular expression is in the while loop. Second, it only matches at the beginning of the string, third, the bottom regular expression is also excluding \s (white space characters). However, those changes results in passing tests:
This seems like enough progress on binary expressions. Next up, handling parenthesis.
PreviousUpNext
Comments