Unit tests are extremely useful for catching bugs during development and to help prevent regressions. It's said that they combined with functional tests can help to reduce the number of bugs in a piece of software by as much as 80%.
To show one example of tests I did, we have ones for my bbcode parser:
Not the most elegant scaffolding, but it'll suffice for a test. This basically creates a slice (basically a dynamically resized array, not to be confused with PHP arrays which are an odd mix of lists and hashmaps), adds a bunch of test cases to it, and then, iterates over all of them.
As you can see, it passes the string on the left to bbcodeFullParse (bbcode is currently implemented as a core plugin, so the function is in a weird place) and if it doesn't return precisely the string it expects, then it spits out an error and moves onto the next one.
As one might expect, I've made sure to test all the usual culprits like missing tags, tags which don't exist, nested tags, emojis, and Chinese (to see if any Unicode related problems arise, although Go is surprisingly good at handling Unicode, it even allows you to use Unicode in function names o.o).
Tests are absolutely fantastic for things like parsers, as there are countless test cases which arise for them and you only have so many hours in the day to do manual tests, especially when something that used to work suddenly stops working, plus you can recreate conditions no matter how contrived with ease (as-long as you architecture your software correctly and have functions with as few responsibilities each as possible).
Edit: Added some spaces as your bbcode parser went haywire.
To show one example of tests I did, we have ones for my bbcode parser:
Code:
var msgList []MEPair
msgList = addMEPair(msgList, "", "")
msgList = addMEPair(msgList, " ", " ")
msgList = addMEPair(msgList, " ", " ")
msgList = addMEPair(msgList, " ", " ")
msgList = addMEPair(msgList, "[b]", "<b></b>")
msgList = addMEPair(msgList, "[b][/b]", "<b></b>")
msgList = addMEPair(msgList, "hi", "hi")
msgList = addMEPair(msgList, "", "")
msgList = addMEPair(msgList, "[b][/b]", "<b></b>")
msgList = addMEPair(msgList, "[b][/b]", "<b></b>")
msgList = addMEPair(msgList, "[b]hi[/b]", "<b>hi</b>")
msgList = addMEPair(msgList, "[u]hi[/u]", "<u>hi</u>")
msgList = addMEPair(msgList, "[i]hi[/i]", "<i>hi</i>")
msgList = addMEPair(msgList, "[s]hi[/s]", "<s>hi</s>")
msgList = addMEPair(msgList, "[c]hi[/c]", "[c]hi[/c]")
msgList = addMEPair(msgList, "[ code]hi[ /code]", "<span class='codequotes'>hi</span>")
msgList = addMEPair(msgList, "[ code][b]hi[/b][ /code]", "<span class='codequotes'>[B]hi[/B]</span>")
msgList = addMEPair(msgList, "[ code][b]hi[/ code][/b]", "<span class='codequotes'>[B]hi</span>[/B]")
msgList = addMEPair(msgList, "[quote]hi[/quote]", "<span class='postQuote'>hi</span>")
msgList = addMEPair(msgList, "[quote][B]hi[/B][/quote]", "<span class='postQuote'><b>hi</b></span>")
msgList = addMEPair(msgList, "[quote][B]h[/B][/quote]", "<span class='postQuote'><b>h</b></span>")
msgList = addMEPair(msgList, "[quote][/quote]", "<span class='postQuote'><b></b></span>")
msgList = addMEPair(msgList, "-你好-", "-你好-")
msgList = addMEPair(msgList, "[I]-你好-[/I]", "<i>-你好-</i>")
t.Log("Testing bbcodeFullParse")
for _, item := range msgList {
res = bbcodeFullParse(item.Msg)
if res != item.Expects {
t.Error("Testing string '" + item.Msg + "'")
t.Error("Bad output:", "'"+res+"'")
t.Error("Expected:", "'"+item.Expects+"'")
}
}
As you can see, it passes the string on the left to bbcodeFullParse (bbcode is currently implemented as a core plugin, so the function is in a weird place) and if it doesn't return precisely the string it expects, then it spits out an error and moves onto the next one.
As one might expect, I've made sure to test all the usual culprits like missing tags, tags which don't exist, nested tags, emojis, and Chinese (to see if any Unicode related problems arise, although Go is surprisingly good at handling Unicode, it even allows you to use Unicode in function names o.o).
Tests are absolutely fantastic for things like parsers, as there are countless test cases which arise for them and you only have so many hours in the day to do manual tests, especially when something that used to work suddenly stops working, plus you can recreate conditions no matter how contrived with ease (as-long as you architecture your software correctly and have functions with as few responsibilities each as possible).
Edit: Added some spaces as your bbcode parser went haywire.







