docs: Format examples in README

pull/1687/head
Oleksandr Redko 2024-12-16 22:27:38 +02:00 committed by Olivier Mengué
parent 7c367bb7bc
commit 3cf0926564
1 changed files with 88 additions and 97 deletions

View File

@ -39,11 +39,11 @@ package yours
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSomething(t *testing.T) { func TestSomething(t *testing.T) {
// assert equality // assert equality
assert.Equal(t, 123, 123, "they should be equal") assert.Equal(t, 123, 123, "they should be equal")
@ -55,13 +55,10 @@ func TestSomething(t *testing.T) {
// assert for not nil (good when you expect something) // assert for not nil (good when you expect something)
if assert.NotNil(t, object) { if assert.NotNil(t, object) {
// now we know that object isn't nil, we are safe to make // now we know that object isn't nil, we are safe to make
// further assertions without causing any errors // further assertions without causing any errors
assert.Equal(t, "Something", object.Value) assert.Equal(t, "Something", object.Value)
} }
} }
``` ```
@ -75,6 +72,7 @@ package yours
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -92,7 +90,6 @@ func TestSomething(t *testing.T) {
// assert for not nil (good when you expect something) // assert for not nil (good when you expect something)
if assert.NotNil(object) { if assert.NotNil(object) {
// now we know that object isn't nil, we are safe to make // now we know that object isn't nil, we are safe to make
// further assertions without causing any errors // further assertions without causing any errors
assert.Equal("Something", object.Value) assert.Equal("Something", object.Value)
@ -121,6 +118,7 @@ package yours
import ( import (
"testing" "testing"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
@ -130,7 +128,7 @@ import (
// MyMockedObject is a mocked object that implements an interface // MyMockedObject is a mocked object that implements an interface
// that describes an object that the code I am testing relies on. // that describes an object that the code I am testing relies on.
type MyMockedObject struct{ type MyMockedObject struct {
mock.Mock mock.Mock
} }
@ -142,10 +140,8 @@ type MyMockedObject struct{
// //
// NOTE: This method is not being tested here, code that uses this object is. // NOTE: This method is not being tested here, code that uses this object is.
func (m *MyMockedObject) DoSomething(number int) (bool, error) { func (m *MyMockedObject) DoSomething(number int) (bool, error) {
args := m.Called(number) args := m.Called(number)
return args.Bool(0), args.Error(1) return args.Bool(0), args.Error(1)
} }
/* /*
@ -155,7 +151,6 @@ func (m *MyMockedObject) DoSomething(number int) (bool, error) {
// TestSomething is an example of how to use our test object to // TestSomething is an example of how to use our test object to
// make assertions about some target code we are testing. // make assertions about some target code we are testing.
func TestSomething(t *testing.T) { func TestSomething(t *testing.T) {
// create an instance of our test object // create an instance of our test object
testObj := new(MyMockedObject) testObj := new(MyMockedObject)
@ -167,8 +162,6 @@ func TestSomething(t *testing.T) {
// assert that the expectations were met // assert that the expectations were met
testObj.AssertExpectations(t) testObj.AssertExpectations(t)
} }
// TestSomethingWithPlaceholder is a second example of how to use our test object to // TestSomethingWithPlaceholder is a second example of how to use our test object to
@ -177,7 +170,6 @@ func TestSomething(t *testing.T) {
// data being passed in is normally dynamically generated and cannot be // data being passed in is normally dynamically generated and cannot be
// predicted beforehand (eg. containing hashes that are time sensitive) // predicted beforehand (eg. containing hashes that are time sensitive)
func TestSomethingWithPlaceholder(t *testing.T) { func TestSomethingWithPlaceholder(t *testing.T) {
// create an instance of our test object // create an instance of our test object
testObj := new(MyMockedObject) testObj := new(MyMockedObject)
@ -190,13 +182,11 @@ func TestSomethingWithPlaceholder(t *testing.T) {
// assert that the expectations were met // assert that the expectations were met
testObj.AssertExpectations(t) testObj.AssertExpectations(t)
} }
// TestSomethingElse2 is a third example that shows how you can use // TestSomethingElse2 is a third example that shows how you can use
// the Unset method to cleanup handlers and then add new ones. // the Unset method to cleanup handlers and then add new ones.
func TestSomethingElse2(t *testing.T) { func TestSomethingElse2(t *testing.T) {
// create an instance of our test object // create an instance of our test object
testObj := new(MyMockedObject) testObj := new(MyMockedObject)
@ -236,6 +226,7 @@ An example suite is shown below:
// Basic imports // Basic imports
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@ -277,6 +268,7 @@ For more information on writing suites, check out the [API documentation for the
// Basic imports // Basic imports
import ( import (
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@ -330,13 +322,12 @@ package yours
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSomething(t *testing.T) { func TestSomething(t *testing.T) {
assert.True(t, true, "True is true!") assert.True(t, true, "True is true!")
} }
``` ```