Skip to content

Commit

Permalink
Use %q rather than '%s'
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Jul 11, 2019
1 parent 5bbe246 commit 3e326d1
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 13 deletions.
14 changes: 8 additions & 6 deletions hello-world.md
Expand Up @@ -64,7 +64,7 @@ func TestHello(t *testing.T) {
want := "Hello, world"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}
```
Expand Down Expand Up @@ -94,7 +94,9 @@ We're declaring some variables with the syntax `varName := value`, which lets us

#### `t.Errorf`

We are calling the `Errorf` _method_ on our `t` which will print out a message and fail the test. The `f` stands for format which allows us to build a string with values inserted into the placeholder values `%s`. When you made the test fail it should be clear how it works.
We are calling the `Errorf` _method_ on our `t` which will print out a message and fail the test. The `f` stands for format which allows us to build a string with values inserted into the placeholder values `%q`. When you made the test fail it should be clear how it works.

You can read more about the placeholder strings in the [fmt go doc](https://golang.org/pkg/fmt/#hdr-Printing). For tests `%q` is very useful as it wraps your values in double quotes.

We will later explore the difference between methods and functions.

Expand Down Expand Up @@ -124,7 +126,7 @@ func TestHello(t *testing.T) {
want := "Hello, Chris"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}
```
Expand Down Expand Up @@ -223,7 +225,7 @@ func TestHello(t *testing.T) {
want := "Hello, Chris"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
})

Expand All @@ -232,7 +234,7 @@ func TestHello(t *testing.T) {
want := "Hello, World"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
})

Expand All @@ -257,7 +259,7 @@ func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}

Expand Down
2 changes: 1 addition & 1 deletion hello-world/v2/hello_test.go
Expand Up @@ -7,6 +7,6 @@ func TestHello(t *testing.T) {
want := "Hello, world"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}
2 changes: 1 addition & 1 deletion hello-world/v3/hello_test.go
Expand Up @@ -7,6 +7,6 @@ func TestHello(t *testing.T) {
want := "Hello, Chris"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}
2 changes: 1 addition & 1 deletion hello-world/v4/hello_test.go
Expand Up @@ -7,6 +7,6 @@ func TestHello(t *testing.T) {
want := "Hello, Chris"

if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}
2 changes: 1 addition & 1 deletion hello-world/v5/hello_test.go
Expand Up @@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}

Expand Down
2 changes: 1 addition & 1 deletion hello-world/v6/hello_test.go
Expand Up @@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}

Expand Down
2 changes: 1 addition & 1 deletion hello-world/v7/hello_test.go
Expand Up @@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
assertCorrectMessage := func(got, want string) {
t.Helper()
if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}

Expand Down
2 changes: 1 addition & 1 deletion hello-world/v8/hello_test.go
Expand Up @@ -7,7 +7,7 @@ func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got '%s' want '%s'", got, want)
t.Errorf("got %q want %q", got, want)
}
}

Expand Down

0 comments on commit 3e326d1

Please sign in to comment.