Thursday, 9 December 2010

Testing Option Funcs

OK, let's test the epanded option class:

function test_option_func1()
        local xml = [[
         <Option Text = "foo" Next = "bar">
          <Func>
           userdata.flag = true
           return true
          </Func>
         </Option>
        ]]
end
    

OK, that passes. Mainly because it doesn't do anything, It does get us to the first stumbling block, though. The Option constructor expects a parsed XML sub-tree. I could change the class to recognise a string input and parse it internally, but, in the interests of testing one thing at a time, I'm going to parse the XML externally

        --
        -- collect returns an array of root level XML parse trees
        -- there's only one here, but we still need to subscript
        -- the result
        --
        local xmldoc = xml.collect(xml_opt)[1]
        local option = Option:new(xmldoc)


And that seems to work. Better test the text and next properties to make sure I didn't mess it up

        --
        -- just to make sure
        --
        assert_equal("foo", option.text)
        assert_equal("bar", option.next)


... and that works too. Not doing too well on the writing tests to fail side of things, but at least we know the object was created correctly from XML. In any case, we're only now getting to the real test...

        --
        -- test the condition
        --
        rc = option.test()
        assert_true(rc)


... and of course, this is where it all goes pear-shaped.

No comments: