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