Skip to content

Commit

Permalink
Add Expectf(format string, v ...any)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Apr 7, 2024
1 parent 13c304c commit a8d5ab0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions actually.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package actually

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -59,3 +60,24 @@ func (a *testingA) Expect(e any) *testingA {

return a
}

// Expectf sets the formatted string value you expect to be the same as the one you got.
// Expectf creates *testingA and returns it.
func Expectf(format string, e ...any) *testingA {
return &testingA{
expect: fmt.Sprintf(format, e...),
setExpect: true,
}
}

// Expectf sets the formatted string value you expect to be the same as the one you got.
func (a *testingA) Expectf(format string, e ...any) *testingA {
if a.setExpect {
panic(panicReason_CalledExpectTwice)
}

a.expect = fmt.Sprintf(format, e...)
a.setExpect = true

return a
}
9 changes: 9 additions & 0 deletions actually_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ func TestDuplicateCall(t *testing.T) {
t.Error("panic wouldn't happen")
}
}

func TestExpectf(t *testing.T) {
Expectf("%s def", "abc").Got("abc def").Same(t)
}

func TestActuallyExpectf(t *testing.T) {
a := &testingA{}
a.Expectf("%s def", "abc").Got("abc def").Same(t)
}

0 comments on commit a8d5ab0

Please sign in to comment.