-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade andrew, add new article and update the styles.
Also use a real valid list instead of that crazy list element.
- Loading branch information
1 parent
f204da2
commit 973dee8
Showing
4 changed files
with
141 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ FROM golang:1.22 AS base | |
WORKDIR /usr/src/app | ||
|
||
ENV CGO_ENABLED=0 | ||
RUN go install github.com/playtechnique/andrew/cmd/[email protected].6 | ||
RUN go install github.com/playtechnique/andrew/cmd/[email protected].7 | ||
|
||
FROM scratch | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<!DOCTYPE html> | ||
|
||
<head> | ||
<script type="text/javascript" src="/main.js"></script> | ||
<title>testing panics in go</title> | ||
<meta name="andrew-published-at" content="2024-04-20" </head> | ||
|
||
<body> | ||
<nav class="navigation"> | ||
<a href="/" class="link">front page</a> | ||
<a href="/articles/index.html" class="link">articles</a> | ||
<a href="/projects/index.html" class="link">projects</a> | ||
</nav> | ||
|
||
<link rel="stylesheet" href="/styles.css"> | ||
<div id="header"></div> | ||
<main> | ||
<article> | ||
<section id="introduction"> | ||
<h1>You want to call a panic in a piece of code and have the test not fail?</h1> | ||
<p>In a deferred function, call <code>recover()</code>. If it has a return value that is not nil, it <i>succeeded and caught the panic</i> | ||
i.e. <code> if err == nil </code> then you can call <code>t.Fatal()</code> in your test.</p> | ||
</section> | ||
|
||
<section id="setup"> | ||
<h1>Here's the setup code</h1> | ||
<p>Let's start a new package to demonstrate this:</p> | ||
<pre><code>mkdir panicker && cd panicker && go mod init panicker && touch panicker_test.go | ||
</code> | ||
</pre> | ||
|
||
<p>Here's the content of panicker_test.go:</p> | ||
<pre><code>package panicker | ||
import "testing" | ||
|
||
func Panics() { | ||
panic("I panic!") | ||
} | ||
|
||
func TestPanics(t *testing.T) { | ||
Panics() | ||
} | ||
</code> | ||
</pre> | ||
|
||
<p>Here's how she runs:</p> | ||
<pre><code>; go test -v panicker | ||
=== RUN TestPanics | ||
--- FAIL: TestPanics (0.00s) | ||
panic: I panic! [recovered] | ||
panic: I panic! | ||
|
||
goroutine 34 [running]: | ||
testing.tRunner.func1.2({0x10441d540, 0x1044455e0}) | ||
/opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1631 +0x1c4 | ||
testing.tRunner.func1() | ||
/opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1634 +0x33c | ||
panic({0x10441d540?, 0x1044455e0?}) | ||
/opt/homebrew/Cellar/go/1.22.2/libexec/src/runtime/panic.go:770 +0x124 | ||
panicker.Panics(...) | ||
/Users/gwyn/Source/panicker/panicker_test.go:6 | ||
panicker.TestPanics(0x14000124680?) | ||
/Users/gwyn/Source/panicker/panicker_test.go:17 +0x30 | ||
testing.tRunner(0x14000124680, 0x104444dc0) | ||
/opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1689 +0xec | ||
created by testing.(*T).Run in goroutine 1 | ||
/opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1742 +0x318 | ||
FAIL panicker 0.082s | ||
FAIL | ||
</code> | ||
</pre> | ||
</section> | ||
|
||
<section id="code"> | ||
<h1>Here's the implementation</h1> | ||
<p>Update TestPanics to contain a deferred call to recover().</p> | ||
<pre><code>package panicker | ||
|
||
import "testing" | ||
|
||
func Panics() { | ||
panic("I panic!") | ||
} | ||
|
||
func TestPanics(t *testing.T) { | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Fatalf("Expected panic with invalid address, received %v", err) | ||
} | ||
}() | ||
|
||
Panics() | ||
} | ||
</code> | ||
</pre> | ||
<p>How does she run now?</p> | ||
<pre><code>; go test -v panicker | ||
=== RUN TestPanics | ||
--- PASS: TestPanics (0.00s) | ||
PASS | ||
ok panicker 0.123s | ||
</code> | ||
</pre> | ||
<p>Notice the error check condition: recover() is non-nil if it's actually called; if recover()'s return value is nil, it was never triggered, | ||
so we didn't actually trigger our expected error. | ||
</p> | ||
</section> | ||
</article> | ||
</main> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters