Making CodeaUnit Better
Just because I think I should do SOMETHING, I think I’ll improve how CodeaUnit displays results on the screen.
Frequent visitors may recall that when CodeaUnit detects even one error, the entire output display turns red:
That certainly gets my attention, but I’d like to be able to discern quickly what it’s whining about. Now, of course, one really looks in the console to get the specifics but it would be better, I think, if specific results were shown in their appropriate color. I freely grant that I don’t know what should really be done here. I just hate that big red blob of text.
Let’s begin with a moderately large story, and then perhaps whittle it down to something reasonable.
As a …
No, we do not use that famous format for our stories. Not now, not ever, never. Our starting story is:
Change coloring of the CodeaUnit screen display to make it easy to detect that there are errors and ignored tests, and also easy to see which specific test suites have errors or ignores and which do not.
Now that’s pretty vague, and left to my own devices, I’m sure that whatever I decide to do will not be to the liking of my user, that is, to my own liking. So I want to think in terms of putting a fair amount of flexibility in, of course without causing myself a lot of extra work for this first cut.
What things could we change to help with this story?
- Background color could be set differently, depending on the global situation, all passed, some failed, some ignored.
- Text color ditto.
- Larger and smaller text.
- Additional text such as <—- indicating interesting lines
That last one is rather interesting. Let’s take a quick look at CodeaUnit. I just happen to have it open at the right place:
...
local passed = self.tests - self.failures - self.ignored
local summary = string.format("%d Passed, %d Ignored, %d Failed", passed, self.ignored, self.failures)
CodeaUnit._summary = CodeaUnit._summary .. summary .. "\n"
if (CodeaUnit_Detailed) then print(summary) end
...
What if, if failures or ignored are non-zero, we just append an arrow? I gotta try this, it’s too easy to ignore.
I try this:
local passed = self.tests - self.failures - self.ignored
local summary = string.format("%d Passed, %d Ignored, %d Failed", passed, self.ignored, self.failures)
if self.failures + self.ignored > 0 then
summary = summary .. " <----"
end
CodeaUnit._summary = CodeaUnit._summary .. summary .."\n"
if (CodeaUnit_Detailed) then print(summary) end
end
With this result:
If you look carefully, you’ll see the arrow up on the third line. I’m afraid that’s just not good enough. Well, it was worth a try.
How about if we display all the OK lines in white, the error lines in red, the ignored lines in yellow? I’d prefer to just highlight the part that says “3 Failed” etc., but that’s hard: there’s no good way to interleave a color change in the middle of a text string. At least, I don’t know one.
To do that, we’ll have to process the lines one at a time. That’s OK and probably not too difficult.
To do this, I’ll have to elaborate CodeaUnit’s color setting and summary display:
function CodeaUnit.setColor()
local s = CodeaUnit._summary
if s:find("[1-9] Failed") or s:find("[0-9][0-9] Failed") then
stroke(255,0,0)
fill(255,0,0)
elseif s:find("[1-9] Ignored") or s:find("[0-9][0-9] Ignored") then
stroke(255,255,0)
fill(255,255,0)
else
stroke(0, 128, 0)
fill(0,128,0)
end
end
function CodeaUnit.showTests()
pushMatrix()
pushStyle()
fontSize(20)
textAlign(CENTER)
textMode(CENTER)
CodeaUnit.setColor()
text(CodeaUnit._summary, WIDTH/2, HEIGHT/2)
popStyle()
popMatrix()
end
To that end, I’ll paste those into another program, that has tests, and when I get what I want, put it back.
I rather quickly come up with this:
function CodeaUnit.setColor(s)
if s:find("[1-9] Failed") or s:find("[0-9][0-9] Failed") then
stroke(255,0,0)
fill(255,0,0)
elseif s:find("[1-9] Ignored") or s:find("[0-9][0-9] Ignored") then
stroke(255,255,0)
fill(255,255,0)
else
stroke(200)
fill(200)
end
end
function CodeaUnit.showTests()
pushMatrix()
pushStyle()
fontSize(20)
textAlign(CENTER)
textMode(CENTER)
local s = CodeaUnit._summary
local h = HEIGHT-50
local m = s:gmatch("(.-)\n")
for l in m do
CodeaUnit.setColor(l)
text(l, WIDTH/2, h)
h = h - 20
end
popStyle()
popMatrix()
end
I just changed the setColor to accept a parameter, then parsed the summary into lines and process them one at a time. So we do lots of setting of color but the result is as intended:
I think I’ll make the OK lines even less prominent. I’ll try 150, a darker shade of gray.
OK, I like that. Move that code back to CodeaUnit.
OK, that story’s done. I often wonder whether I like the text centered or would prefer it left justified, but I always leave it as it is. I could imagine being truly clever and centering the messages vertically, but that seems silly. This is fine.
Let’s sum up.
Summary
Just a tiny patch of work to improve my working environment just a bit.
Using it briefly, I decide to go to this:
function CodeaUnit.setColor(s)
local red = color(255,0,0)
local yellow = color(255,255,0)
local green = color(0,175,0)
if s:find("[1-9] Failed") or s:find("[0-9][0-9] Failed") then
fill(red)
elseif s:find("[1-9] Ignored") or s:find("[0-9][0-9] Ignored") then
fill(yellow)
else
fill(green)
end
end
I prefer the greenish color when things are green. I checked and determined that text uses fill color but not stroke, so I removed the calls to stroke
. And I broke out the colors to make it easier to change them.
If I were shipping this, I might make those colors visible so that people could set them to their liking. But I’m not, though I will include a copy of the program in an attachment to this article.
What’s to learn? Just that small changes to our toolset are possible and often useful.
I also learned that it’s a pain to update CodeaUnit and then test it, which can’t really be done in place very readily. I think I can improve that … another time …
See you another time? I hope so.