Skip to content

Commit

Permalink
added Check func test
Browse files Browse the repository at this point in the history
  • Loading branch information
iglov committed Aug 19, 2024
1 parent a4ac659 commit 5b6e2f9
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/oschwald/maxminddb-golang"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"bytes"
"errors"
"fmt"
"os"
"testing"
Expand All @@ -17,6 +19,49 @@ const (
TestDataset = "./testdata/dataset.json"
)

func captureOutput(f func()) string {
// Create a pipe to capture the output
r, w, _ := os.Pipe()
// Save the original stdout
stdout := os.Stdout
// Set stdout to the write end of the pipe
os.Stdout = w

// Run the function (which will print to stdout)
f()

// Close the writer end and restore stdout
w.Close()
os.Stdout = stdout

// Read the captured output from the read end of the pipe
var buf bytes.Buffer
buf.ReadFrom(r)

Check failure on line 39 in main_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `buf.ReadFrom` is not checked (errcheck)

return buf.String()
}

func TestCheck(t *testing.T) {
output := captureOutput(func() {
Check(func() error {
return nil
})
})

// Check that there was no output (since no error should be printed)
assert.Empty(t, output)

output = captureOutput(func() {
Check(func() error {
return errors.New("test error")
})
})

// Check that the error message was printed
expectedOutput := "Received error: test error\n"
assert.Equal(t, expectedOutput, output)
}

func TestToMMDBType(t *testing.T) {
tests := []struct {
key string
Expand Down

0 comments on commit 5b6e2f9

Please sign in to comment.