Raytracer challenge in Golang. Again. AGAIN.
This seems to be a seasonal affliction - restarting the Raytracer Challenge book in Golang
I do like the Raytracer Challenge book. Building the Raytraced Teapot in PHP (about 4 years ago) was a really uplifting experience - I felt I'd built something out of my comfort zone through a fair amount of pain. It also helped with my testing experience. So a couple of years ago I tried to redo it in Golang, as that's the language I'd chosen to get more experience with.
I got two chapters in and bombed, hard. I didn't understand what I was doing with the code, and I couldn't get the tests to work the way I wanted it to work. It was abandoned.
As the summary says, it's now time for me to be bitten by the bug again. I've learned more about Golang thanks to my blog tool, my gui blog tool, my work task management tool, and other itch-scratching experiments. Time to try again.
Got Godogs working
Godogs is a gherkin-for-go testing framework. cumber Gherkin is the User Story description syntax that makes explaining tests easy, and surfaces problems with frequent execution easy. I couldn't get it to work the last time I tried, but this time it did! I'll chalk that up to godogs improvements and the fact that it's combined with the innate go testing capabilities.
My godogs learnings
- Build into the test struct maps to hold the variables you create through the test
1type tuples map[string]Tuple
2type colors map[string]Color
3type canvases map[string]Canvas
4type ppms map[string]string
5type matrices map[string]Matrix
6
7type tupletest struct {
8 Tuples tuples
9 Colors colors
10 Canvases canvases
11 PPMs ppms
12 Matrices matrices
13}
- Use regexp to snag the variable names and numbers via the following
- Variable name:
([a-zA-Z0-9]+)
- Float:
(-?\d+(?:\.\d+)?)
- Variable name:
- In the tests, prefix the variable name with the map you created in step1. That way you're explicit about which function to use in each step (e.g. the equivalence of floats is different from thje equivalence of matrices, but the user story just says
x = y
). - I tried using separate
_test.go
files for each feature file, but it seemed to combine them and get confused. I currently have a singlefeature_test.go
file where all of the features are tested. At least the features are in different feature files. I'd like this to be better. - If you use a
"""
string in a feature file, it will be translated to a*godog.DocString
variable in the function call. You need to use.Content
to get the content. - If you use a
"""\n| ... |
table in a feature file, it will be translated to a*godog.Table
variable in the function call. You will need to go through each.Row
and then.Cell
to get the individual values.
Chapter 2 Result

References
- How to use Godog for behavior driven development - this got me finally able to use godog.
- Two Dimensional Array Slice in Golang
- My github of the Raytrace