Sunday, 12 December 2010

Formatting XML Target Strings

A small detour. Boo lets me define multi-line strings with a triple-double-quote, or """ sequence. I'd like to use that to set out my XML targets like this

                source = """
                        <foo>
                         <bar>
                          <baz />
                         </bar>
                        </foo>
                """


That way, it's easy to verify that the target string is as intended. The problem is that the string above includes all the whitespace. So there's an initial newline, before the XML starts, and then a pile of tabs at the start of each line. So unless I want to generate XML based on the formatting requirements of the test file, that's not going to work.

On the other hand, I could write a function to remove the excess tab and newlines...

        def detab(s as string):
                out = ""
                using sw = StringReader(s):
                        while line = sw.ReadLine():
                                if line =~ /^\t*$/:
                                        continue
                                line = /^\t+/.Replace(line, "")
                                out += line
                                out += "\n"
                return out


How do I know it works correctly? I wrote a test :)

        [Test]
        def de_tabber():
                target = ""
                target += "<foo>\n"
                target += " <bar>\n"
                target += "  <baz />\n"
                target += " </bar>\n"
                target += "</foo>\n"

                source = """
                        <foo>
                         <bar>
                          <baz />
                         </bar>
                        </foo>
                """
                Assert.AreEqual(target, detab(source))


And that passes :)

No comments: