Skip to content

Commit

Permalink
feat: add the ability to manually specify the want value
Browse files Browse the repository at this point in the history
This is necessary to separate data in table tests when there are few
cases with big data and there are cases with short data that are better
represented in the table.

Perhaps this method is not a good idea, since the test data is mixed,
but perhaps this opportunity will do itself well in practice.
  • Loading branch information
xorcare committed Nov 12, 2019
1 parent 7e04551 commit b87f686
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
35 changes: 34 additions & 1 deletion golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type Tool struct {
flag *bool
prefix string
extension string
// want it stores manually set expected data, if it is nil, then the
// data will be read from the files, otherwise the value from this
// field will be taken.
want []byte

mkdirAll func(path string, perm os.FileMode) error
readFile func(filename string) ([]byte, error)
Expand Down Expand Up @@ -116,6 +120,14 @@ func SetTest(t TestingTB) Tool {
return _golden.SetTest(t)
}

// SetWant a place to set the expected want manually.
// want value it stores manually set expected data, if it is nil,
// then the data will be read from the files, otherwise the value
// from this field will be taken.
func SetWant(t TestingTB, bs []byte) Tool {
return _golden.SetTest(t).SetWant(bs)
}

// Assert is a tool to compare the actual value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
Expand Down Expand Up @@ -191,10 +203,17 @@ func JSONEq(tb TestingTB, got string) Conclusion {
// Read is a functional for reading both input and golden files using
// the appropriate target.
func (t Tool) Read() (bs []byte) {
const f = "golden: read the value of nil since it is not found file: %s"
if t.want != nil {
t.test.Logf("golden: read the value from the want field")
bs = make([]byte, len(t.want))
copy(bs, t.want)

return bs
}

bs, err := t.readFile(t.path())
if os.IsNotExist(err) {
const f = "golden: read the value of nil since it is not found file: %s"
t.test.Logf(f, t.path())
return nil
} else if err != nil {
Expand Down Expand Up @@ -232,6 +251,20 @@ func (t Tool) SetTest(tb TestingTB) Tool {
return t
}

// SetWant a place to set the expected want manually.
// want value it stores manually set expected data, if it is nil,
// then the data will be read from the files, otherwise the value
// from this field will be taken.
func (t Tool) SetWant(bs []byte) Tool {
t.want = nil
if bs != nil {
t.want = make([]byte, len(bs))
copy(t.want, bs)
}

return t
}

// Update functional reviewer is the need to update the golden files
// and doing it.
func (t Tool) Update(bs []byte) {
Expand Down
6 changes: 6 additions & 0 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ func TestTool_Read(t *testing.T) {
}
})
}
t.Run("with-set-want-field", func(t *testing.T) {
tb := &bufferTB{name: t.Name()}
tool := SetWant(tb, []byte(t.Name()))
assert.Equal(t, t.Name(), string(tool.Read()))
_goldie.SetTest(t).Assert(tb.Bytes())
})
}

func TestTool_Run(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions testdata/TestTool_Read/with-set-want-field.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golden: read the value from the want field

0 comments on commit b87f686

Please sign in to comment.