Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Test & benchmark

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Setup Go environment
uses: actions/setup-go@v6
with:
go-version: 1.25.1

- name: Run tests
run: |
go mod tidy
go test -v ./...

benchmark:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Setup Go environment
uses: actions/setup-go@v6
with:
go-version: 1.25.1

- name: Install benchstat
run: go install golang.org/x/perf/cmd/benchstat@latest

- name: Run benchmark on base branch
if: github.event_name == 'pull_request'
run: |
git checkout ${{ github.event.pull_request.base.sha }}
go mod tidy
go test -v -run ^$ -bench . -count ${{ vars.GO_BENCHMARK_COUNT }} -benchmem > base-bench.txt
git checkout ${{ github.event.pull_request.head.sha }}

- name: Run benchmark on HEAD commit
run: |
go mod tidy
go test -v -run ^$ -bench . -count ${{ vars.GO_BENCHMARK_COUNT }} -benchmem > head-bench.txt

- name: Compare benchmarks
if: github.event_name == 'pull_request'
run: |
cat > benchmark-comment.md << EOF
## Benchmark comparison w/ base branch

\`\`\`
$(benchstat base-bench.txt head-bench.txt)
\`\`\`
EOF

- name: Comment PR w/ benchmark comparison
if: github.event_name == 'pull_request'
uses: actions/github-script@v8
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: require('fs').readFileSync('benchmark-comment.md', 'utf8')
});
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pre-commit:

lint:
glob: "*.go"
run: golangci-lint run --fix {staged_files}
run: golangci-lint run --fix
61 changes: 61 additions & 0 deletions pointer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package require

import (
"testing"

assert "github.com/stretchr/testify/assert"
)

func TestNilPtr(t *testing.T) {
t.Run("should accept nil pointer & return nil", func(t *testing.T) {
var nilPtr *int
assert.Nil(t, NilPtr("Test pointer", nilPtr))
})
t.Run("should panic when pointer is not nil", func(t *testing.T) {
assert.PanicsWithValue(t, "assertion failed: Test pointer should be a nil pointer", func() {
value := "test"
NilPtr("Test pointer", &value)
})
})
t.Run("should use given name in panic message", func(t *testing.T) {
assert.PanicsWithValue(t, "assertion failed: Other pointer should be a nil pointer", func() {
value := 42
NilPtr("Other pointer", &value)
})
})
}

func TestNotNilPtr(t *testing.T) {
t.Run("should accept not-nil pointer & return same pointer", func(t *testing.T) {
value := 42
assert.Same(t, &value, NotNilPtr("Test pointer", &value))
})
t.Run("should panic when pointer is nil", func(t *testing.T) {
assert.PanicsWithValue(t, "assertion failed: Test pointer should not be a nil pointer", func() {
var nilPtr *string
NotNilPtr("Test pointer", nilPtr)
})
})
t.Run("should use given name in panic message", func(t *testing.T) {
assert.PanicsWithValue(t, "assertion failed: Other pointer should not be a nil pointer", func() {
var nilPtr *float64
NotNilPtr("Other pointer", nilPtr)
})
})
}

func BenchmarkNilPtr(b *testing.B) {
var nilPtr *int
b.ResetTimer()
for i := 0; i < b.N; i++ {
NilPtr("Benchmark pointer", nilPtr)
}
}

func BenchmarkNotNilPtr(b *testing.B) {
value := "benchmark"
b.ResetTimer()
for i := 0; i < b.N; i++ {
NotNilPtr("Benchmark pointer", &value)
}
}
Loading