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
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
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
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
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
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Sub Categories: