Siarhei

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 Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
NewsBot
A new Kubuntu blog post/announcement has been posted! Get the full details here: https://kubuntu.org/news/kubuntu-25-04-plucky-puffin-r...
New

Latest in PragProg

View all threads ❯