The saga continues. I haven’t finished it yet, but put all the code into functions, and started testing each.
The previous posts in this series:
I didn’t do Test Driven Development here, because I didn’t really know the Hamurabi “spec”, and the code wasn’t even in a testable form.
Refactoring Spaghetti Code into Functions
So the first step was to put the code into a form that’s testable. Everything is now in functions; mostly, it’s a line-by-line port, but the evaluation function was untangled, and a few other things were untangled. Doing that is always going to risk breaking the logic of the program, but it seemed inevitable.
Then I wrote some “framework” code to run the game. This is really a sketch more than a real program. My vision is that there’s a screen with an output area, and an input area, and a “play” button to play the turn.
The output and play button handler is pretty simple. The sliders or input validation will be different. BASIC is line-oriented, so validation is performed after every input. You can’t go back and fix values. So, that means you can perform validations on a line of input that relies on values from a prior line of input.
In a GUI, when any single input changes, you should test all the values, and then flag errors. So a change in one value can trigger one or more errors not only on that input, but possibly other inputs. For example, increasing the amount of grain to feed people will reduce the grain available to plant crops.
So the input controls, and their events, need to be decoupled from validation code. The validation code is in separate functions.
The next step was to create a minimal testing framework. These are the test_* functions at the end of the code. There are a couple assert_* functions, and a function to call other tests. There is a tester for each function in the code. I just did:
grep function hamurabi.html
And copy-pasted that text, edited it, and created my test functions. Easy. Then, I added a call to the testing function at the bottom of the code.
Up at the top of the page, I added and META tag to refresh the page. That just reloaded, and re-ran the test.
Load the page into Firefox, and open the Web Console, so I could see the error messages.
Then, I went through a generally relaxing process of, “Money see error. Monkey fix error.”
So far, I’ve gotten through all the easier-to-test functions.
I’m not going to post code, but the work in progess will be uploaded as an attachment. You can view source.
First draft with some testing:
Hamurabi port in progress.
Update: Finally Started With Whitebox Testing
Whitebox or clearbox testing is when you write tests by looking at the code being tested. While this is not TDD, it’s necessary because we don’t have a program specification.
The program is the specification.
This latest version fixes a few bugs in the testing functions. I also finished up the easy test functions and started on the game functions.
I also started to implement exception handling in the game functions. More on this in another post.🔥0 views