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")
}

First Post!

LeamHall

LeamHall

Hello @Siarhei! Oddly enough, I had that same issue this morning. My text editor (vim) was configured to not expand tabs, I was using tabs for indenting, and the tabstop as set at 2. I solved the issue by editing the main.go file and using spaces instead of tabs.

Where Next?

Popular Pragmatic Bookshelf topics Top

telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
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
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
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

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
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
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New

Sub Categories: