
Siarhei
Powerful Command-Line Applications in Go: TestParseContent & TestRun failed (pages 53 - 54)
Tests on compare 2 slice of byte fail because there is a difference in the formatting we get after the parseContent()
and goldenFile
functions. The difference lies in different tabulation.
To bring them to a common state I suggest using the package go get -u github.com/yosssi/gohtml
func formatHTML(data []byte) []byte {
// Convert to string for manipulation
content := string(data)
formattedHTML := gohtml.Format(content)
// Return the formatted html as []byte
return []byte(formattedHTML)
}
This formatHTML function will take raw HTML in a []byte
format, format it using gohtml, and return the formatted HTML as []byte
. Here’s a quick breakdown of how it works:
Conversion to String: string(data)
converts the byte slice to a string, as gohtml.Format requires a string input.
Formatting*: gohtml.Format(content)
processes the HTML string, adding indentation and spacing for readability.
Returning as []byte
: []byte(formattedHTML)
converts the formatted string back to []byte
.
if !bytes.Equal(formatHTML(expected), formatHTML(result)) {
t.Logf("golden:\n%s\n", formatHTML(expected))
t.Logf("result:\n%s\n", formatHTML(result))
t.Error("Result content doesn't match golden file")
}
Popular Prag Prog topics








Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)


Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /textmate
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /lua
- /nodejs
- /c
- /julia
- /slackware
- /neovim