|2012-04-19 15:24:00||puzzled||Fiji – K-series|
On the last of the JUnit tests, namely testing writeSwampToDisk(). This is called within the game if the user decides he or she is bored with watching creatures move around and try to kill each other.
The way I’d want to test it is to invoke a game and record the current state, e.g. the current value of Swamp() or drawSwampmap(). Then I’d save the game (which automatically closes it. Then I’d recall the game and compare the recalled state with the previously-noted saved state. If the two are the same, the read and write have succeeded.
The write method is within Swamp(), so that it can be called from Swamp()‘s basic routine. However, the read method is within a separate interface.
(The interface presents the user with a choice of new game or saved game.
- If the user wants a new game, an new instance of Swamp() is kicked off.
- If the user wants to recall a game from disk and a saved game exists, the interface’s read method is invoked.
- [If there isn’t a saved game, the user gets a blocking for not reading the warning previously presented and a new game is started anyway.])
The read method can’t be in Swamp() because this would involve Swamp() trying to read itself. I’d like move the write method fromSwamp() to the interface (partly so I don’t have to write this test but mostly because that would seem neater) but the interface abdicates control to Swamp()‘s basic routine and then exits.
I guess the saved thing could be Swamp()‘s list of creatures, rather than Swamp() itself…… yes, that seems to work.
Some experimentation later
The test didn’t work when I compared lists of creatures. So to make it obvious, I’ve changed to comparing maps – purely textual output. While the two bits of text look identical, the test fails – implying they’re not!