Skip to content

Commit

Permalink
assert: tests for golden variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed May 29, 2022
1 parent a0e2cd3 commit ad7894a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
70 changes: 58 additions & 12 deletions assert/assert_ext_test.go
@@ -1,40 +1,66 @@
package assert_test

import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"runtime"
"strings"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/internal/source"
)

func TestEqual_WithGoldenUpdate(t *testing.T) {
t.Run("assert failed with update=false", func(t *testing.T) {
t.Run("assert failed with -update=false", func(t *testing.T) {
ft := &fakeTestingT{}
actual := `not this value`
assert.Equal(ft, actual, expectedOne)
assert.Assert(t, ft.failNowed)
})

t.Run("value is updated when -update=true", func(t *testing.T) {
t.Run("var is updated when -update=true", func(t *testing.T) {
patchUpdate(t)
ft := &fakeTestingT{}
t.Cleanup(func() {
resetVariable(t, "expectedOne", "")
})

actual := `this is the
actual value
that we are testing against`
assert.Equal(ft, actual, expectedOne)
that we are testing
`
assert.Equal(t, actual, expectedOne)

// reset
fmt.Println("WHHHHHHHHHHY")
assert.Equal(ft, "\n\n\n", expectedOne)
})
}
raw, err := ioutil.ReadFile(fileName(t))
assert.NilError(t, err)

var expectedOne = `
expected := "var expectedOne = `this is the\nactual value\nthat we are testing\n`"
assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw))
})

t.Run("const is updated when -update=true", func(t *testing.T) {
patchUpdate(t)
t.Cleanup(func() {
resetVariable(t, "expectedTwo", "")
})

actual := `this is the new
expected value
`
assert.Equal(t, actual, expectedTwo)

raw, err := ioutil.ReadFile(fileName(t))
assert.NilError(t, err)

expected := "const expectedTwo = `this is the new\nexpected value\n`"
assert.Assert(t, strings.Contains(string(raw), expected), "actual=%v", string(raw))
})
}

var expectedOne = ``

const expectedTwo = ``

func patchUpdate(t *testing.T) {
source.Update = true
Expand All @@ -43,6 +69,26 @@ func patchUpdate(t *testing.T) {
})
}

func fileName(t *testing.T) string {
t.Helper()
_, filename, _, ok := runtime.Caller(1)
assert.Assert(t, ok, "failed to get call stack")
return filename
}

func resetVariable(t *testing.T, varName string, value string) {
t.Helper()
_, filename, _, ok := runtime.Caller(1)
assert.Assert(t, ok, "failed to get call stack")

fileset := token.NewFileSet()
astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors|parser.ParseComments)
assert.NilError(t, err)

err = source.UpdateVariable(filename, fileset, astFile, varName, value)
assert.NilError(t, err, "failed to reset file")
}

type fakeTestingT struct {
failNowed bool
failed bool
Expand Down
23 changes: 19 additions & 4 deletions internal/source/update.go
Expand Up @@ -68,6 +68,23 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
value = y
}

strValue, ok := value.(string)
if !ok {
debug("value must be type string, got %T", value)
return ErrNotFound
}
return UpdateVariable(filename, fileset, astFile, varName, strValue)
}

// UpdateVariable writes to filename the contents of astFile with the value of
// the variable updated to value.
func UpdateVariable(
filename string,
fileset *token.FileSet,
astFile *ast.File,
varName string,
value string,
) error {
obj := astFile.Scope.Objects[varName]
if obj == nil {
return ErrNotFound
Expand All @@ -87,11 +104,9 @@ func UpdateExpectedValue(stackIndex int, x, y interface{}) error {
return ErrNotFound
}

// TODO: allow a function to wrap the string literal
spec.Values[0] = &ast.BasicLit{
Kind: token.STRING,
// TODO: safer
Value: "`" + value.(string) + "`",
Kind: token.STRING,
Value: "`" + value + "`",
}

debug("after modification: %v", debugFormatNode{astFile})
Expand Down

0 comments on commit ad7894a

Please sign in to comment.