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

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
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
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
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

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
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
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
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New

Sub Categories: