Skip to content

Commit 71a4d90

Browse files
test: add pointer_test.go
* Added tests and benchmarks for funcs NilPtr() & NotNilPtr() * Changed lefthook.yml to run golangci-lint over all files. * Added .github/workflows/testing.yml w/ jobs test & benchmark. Closes #1
1 parent a25ce3d commit 71a4d90

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

.github/workflows/testing.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Test & benchmark
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v5
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Setup Go environment
17+
uses: actions/setup-go@v6
18+
with:
19+
go-version: 1.25.1
20+
21+
- name: Run tests
22+
run: |
23+
go mod tidy
24+
go test -v ./...
25+
26+
benchmark:
27+
runs-on: ubuntu-latest
28+
29+
steps:
30+
- uses: actions/checkout@v5
31+
with:
32+
fetch-depth: 0
33+
34+
- name: Setup Go environment
35+
uses: actions/setup-go@v6
36+
with:
37+
go-version: 1.25.1
38+
39+
- name: Install benchstat
40+
run: go install golang.org/x/perf/cmd/benchstat@latest
41+
42+
- name: Run benchmark on base branch
43+
if: github.event_name == 'pull_request'
44+
run: |
45+
git checkout ${{ github.event.pull_request.base.sha }}
46+
go mod tidy
47+
go test -v -run ^$ -bench . -count ${{ vars.GO_BENCHMARK_COUNT }} -benchmem > base-bench.txt
48+
git checkout ${{ github.event.pull_request.head.sha }}
49+
50+
- name: Run benchmark on HEAD commit
51+
run: |
52+
go mod tidy
53+
go test -v -run ^$ -bench . -count ${{ vars.GO_BENCHMARK_COUNT }} -benchmem > head-bench.txt
54+
55+
- name: Compare benchmarks
56+
if: github.event_name == 'pull_request'
57+
run: |
58+
cat > benchmark-comment.md << EOF
59+
## Benchmark comparison w/ base branch
60+
61+
\`\`\`
62+
$(benchstat base-bench.txt head-bench.txt)
63+
\`\`\`
64+
EOF
65+
66+
- name: Comment PR w/ benchmark comparison
67+
if: github.event_name == 'pull_request'
68+
uses: actions/github-script@v8
69+
with:
70+
script: |
71+
github.rest.issues.createComment({
72+
issue_number: context.issue.number,
73+
owner: context.repo.owner,
74+
repo: context.repo.repo,
75+
body: require('fs').readFileSync('benchmark-comment.md', 'utf8')
76+
});

lefthook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ pre-commit:
1515

1616
lint:
1717
glob: "*.go"
18-
run: golangci-lint run --fix {staged_files}
18+
run: golangci-lint run --fix

pointer_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package require
2+
3+
import (
4+
"testing"
5+
6+
assert "github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestNilPtr(t *testing.T) {
10+
t.Run("should accept nil pointer & return nil", func(t *testing.T) {
11+
var nilPtr *int
12+
assert.Nil(t, NilPtr("Test pointer", nilPtr))
13+
})
14+
t.Run("should panic when pointer is not nil", func(t *testing.T) {
15+
assert.PanicsWithValue(t, "assertion failed: Test pointer should be a nil pointer", func() {
16+
value := "test"
17+
NilPtr("Test pointer", &value)
18+
})
19+
})
20+
t.Run("should use given name in panic message", func(t *testing.T) {
21+
assert.PanicsWithValue(t, "assertion failed: Other pointer should be a nil pointer", func() {
22+
value := 42
23+
NilPtr("Other pointer", &value)
24+
})
25+
})
26+
}
27+
28+
func TestNotNilPtr(t *testing.T) {
29+
t.Run("should accept not-nil pointer & return same pointer", func(t *testing.T) {
30+
value := 42
31+
assert.Same(t, &value, NotNilPtr("Test pointer", &value))
32+
})
33+
t.Run("should panic when pointer is nil", func(t *testing.T) {
34+
assert.PanicsWithValue(t, "assertion failed: Test pointer should not be a nil pointer", func() {
35+
var nilPtr *string
36+
NotNilPtr("Test pointer", nilPtr)
37+
})
38+
})
39+
t.Run("should use given name in panic message", func(t *testing.T) {
40+
assert.PanicsWithValue(t, "assertion failed: Other pointer should not be a nil pointer", func() {
41+
var nilPtr *float64
42+
NotNilPtr("Other pointer", nilPtr)
43+
})
44+
})
45+
}
46+
47+
func BenchmarkNilPtr(b *testing.B) {
48+
var nilPtr *int
49+
b.ResetTimer()
50+
for i := 0; i < b.N; i++ {
51+
NilPtr("Benchmark pointer", nilPtr)
52+
}
53+
}
54+
55+
func BenchmarkNotNilPtr(b *testing.B) {
56+
value := "benchmark"
57+
b.ResetTimer()
58+
for i := 0; i < b.N; i++ {
59+
NotNilPtr("Benchmark pointer", &value)
60+
}
61+
}

0 commit comments

Comments
 (0)