diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..69257ed --- /dev/null +++ b/.github/workflows/testing.yml @@ -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') + }); diff --git a/lefthook.yml b/lefthook.yml index b258cc2..e419217 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -15,4 +15,4 @@ pre-commit: lint: glob: "*.go" - run: golangci-lint run --fix {staged_files} + run: golangci-lint run --fix diff --git a/pointer_test.go b/pointer_test.go new file mode 100644 index 0000000..14c749e --- /dev/null +++ b/pointer_test.go @@ -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) + } +}