Sunday, 12 December 2010

More TDD

Let's start some tests to make sure that I'm reading and writing this XML correctly. I've moved most of the code into a DLL, leaving the main window as a stub for the exe. This is the Makefile:

BOOFILES=\
        Narrative.boo Scene.boo TreePanel.boo \
        ScenePanel.boo NarrativePanel.boo

SceneEdit.exe: scene_edit.dll main.boo
        booc main.boo -r:scene_edit.dll -o:SceneEdit.exe

scene_edit.dll: ${BOOFILES}
        booc ${BOOFILES} -t:library -o:scene_edit.dll


And then I've created a test dll using NUnit that links in scene_edit.dll

tests.dll: tests.boo
        ${BOOC} tests.boo -t:library -o:tests.dll -r:nunit.framework.dll -r:scene_edit.dll
        
test: tests.dll
        nunit-console tests.dll


Now I can write my tests in tests.boo in much the same formal as the Lua ones earlier.

import System.IO
import NUnit.Framework

[TestFixture]
class SceneXml:
        [Test]
        def simple_save():
                sc = Scene()
                sc.name         = "intro"
                sc.default      = "waking_up"
                sc.desc         = "here we go"
#
#               this is what we expect the output to look like
#
                target = """<scene name="intro" default="waking_up" desc="here we go" />"""
#
#               I'll over load the load and save funcs to take a stream
#               It's easier to test using stringwriters and readers.
#               I can overload the filename version to create a reader and
#               call this version, so the tests will largely still apply
#
                using sw = StringWriter():
                        sc.save(sw)
#
#                       stringifying the writer gets the underlying string
#
                        result = "${sw}"
                        Assert.AreEqual(target, result)


Here are the new  save funcs from Scene. As you can see, I've not tried particularly hard to generate conformant XML

        def save(filename as string):
                using sw = StreamWriter(filename):
                        save(sw)

        def save(w as TextWriter):
                w.Write("hiya")


So it's no great surprise when the test fails:

Test Case Failures:
1) SceneXml.simple_save :   Expected string length 60 but was 4. Strings differ at index 0.
  Expected: "<scene name="intro" default="waking_up" desc="here we go" />"
  But was:  "hiya"
  -----------^


But hey! At least we know the test works, even if the code tested doesn't yet pass :)

To pass the test we need this:

        def save(tw as TextWriter):
                xs = XmlWriterSettings()
                xs.Indent = true
                xs.OmitXmlDeclaration = true

                using w = XmlWriter.Create(tw, xs):
                        w.WriteStartElement("scene")
                        w.WriteAttributeString("name", _name)
                        w.WriteAttributeString("default", _default)
                        w.WriteAttributeString("desc", _desc)


The XmlWriterSettings object turns off the <?xml header which I doubt we'll be needing, and enables indenting, which I'm hoping will save me faffing around with whitespace

Now let's see if it can cope with a narrative or two...

2 comments: