Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: shorten data type names and add doc example #170

Merged
merged 1 commit into from
Sep 4, 2024
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
4 changes: 2 additions & 2 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1637,14 +1637,14 @@ func TestFileContains(t *testing.T) {
tc := newCase(t, `expected file contents`)
t.Cleanup(tc.assert)

path := util.TempFile(t, util.Pattern("test"), util.StringData("real data"))
path := util.TempFile(t, util.Pattern("test"), util.String("real data"))
FileContains(tc, path, "fake")
})
t.Run("file contains data", func(t *testing.T) {
tc := newCase(t, "")
t.Cleanup(tc.assertNot)

path := util.TempFile(t, util.Pattern("test"), util.StringData("real data"))
path := util.TempFile(t, util.Pattern("test"), util.String("real data"))
FileContains(tc, path, "real")
})
}
Expand Down
25 changes: 25 additions & 0 deletions util/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) The Test Authors
// SPDX-License-Identifier: MPL-2.0

package util_test

import (
"fmt"
"os"
"testing"

"github.com/shoenig/test/util"
)

var t = new(testing.T)

func ExampleTempFile() {
path := util.TempFile(t,
util.String("hello!"),
util.Mode(0o640),
)

b, _ := os.ReadFile(path)
fmt.Println(string(b))
// Output: hello!
}
9 changes: 5 additions & 4 deletions util/tempfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) The Test Authors
// SPDX-License-Identifier: MPL-2.0

// Package util provides utility functions for writing concise test cases.
package util

import (
Expand Down Expand Up @@ -42,15 +43,15 @@ func Mode(mode fs.FileMode) TempFileSetting {
}
}

// StringData writes data to the temporary file.
func StringData(data string) TempFileSetting {
// String writes data to the temporary file.
func String(data string) TempFileSetting {
return func(s *TempFileSettings) {
s.data = []byte(data)
}
}

// ByteData writes data to the temporary file.
func ByteData(data []byte) TempFileSetting {
// Bytes writes data to the temporary file.
func Bytes(data []byte) TempFileSetting {
return func(s *TempFileSettings) {
s.data = data
}
Expand Down
6 changes: 3 additions & 3 deletions util/tempfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestTempFile(t *testing.T) {
path := util.TempFile(t,
util.Mode(expectedMode),
util.Pattern(pattern),
util.StringData(expectedData))
util.String(expectedData))

t.Run("Mode sets a custom file mode", func(t *testing.T) {
info, err := os.Stat(path)
Expand Down Expand Up @@ -150,9 +150,9 @@ func TestTempFile(t *testing.T) {
})
})

t.Run("ByteData sets binary data", func(t *testing.T) {
t.Run("Bytes sets binary data", func(t *testing.T) {
expectedData := []byte("important data")
path := util.TempFile(t, util.ByteData(expectedData))
path := util.TempFile(t, util.Bytes(expectedData))
actualData, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read temp file: %v", err)
Expand Down
Loading