11package assert_test
22
33import (
4- "fmt"
4+ "go/parser"
5+ "go/token"
6+ "io/ioutil"
7+ "runtime"
8+ "strings"
59 "testing"
610
711 "gotest.tools/v3/assert"
812 "gotest.tools/v3/internal/source"
913)
1014
1115func TestEqual_WithGoldenUpdate (t * testing.T ) {
12- t .Run ("assert failed with update=false" , func (t * testing.T ) {
16+ t .Run ("assert failed with - update=false" , func (t * testing.T ) {
1317 ft := & fakeTestingT {}
1418 actual := `not this value`
1519 assert .Equal (ft , actual , expectedOne )
1620 assert .Assert (t , ft .failNowed )
1721 })
1822
19- t .Run ("value is updated when -update=true" , func (t * testing.T ) {
23+ t .Run ("var is updated when -update=true" , func (t * testing.T ) {
2024 patchUpdate (t )
21- ft := & fakeTestingT {}
25+ t .Cleanup (func () {
26+ resetVariable (t , "expectedOne" , "" )
27+ })
2228
2329 actual := `this is the
2430actual value
25- that we are testing against`
26- assert .Equal (ft , actual , expectedOne )
31+ that we are testing
32+ `
33+ assert .Equal (t , actual , expectedOne )
2734
28- // reset
29- fmt .Println ("WHHHHHHHHHHY" )
30- assert .Equal (ft , "\n \n \n " , expectedOne )
31- })
32- }
35+ raw , err := ioutil .ReadFile (fileName (t ))
36+ assert .NilError (t , err )
3337
34- var expectedOne = `
38+ expected := "var expectedOne = `this is the\n actual value\n that we are testing\n `"
39+ assert .Assert (t , strings .Contains (string (raw ), expected ), "actual=%v" , string (raw ))
40+ })
3541
42+ t .Run ("const is updated when -update=true" , func (t * testing.T ) {
43+ patchUpdate (t )
44+ t .Cleanup (func () {
45+ resetVariable (t , "expectedTwo" , "" )
46+ })
3647
48+ actual := `this is the new
49+ expected value
3750`
51+ assert .Equal (t , actual , expectedTwo )
52+
53+ raw , err := ioutil .ReadFile (fileName (t ))
54+ assert .NilError (t , err )
55+
56+ expected := "const expectedTwo = `this is the new\n expected value\n `"
57+ assert .Assert (t , strings .Contains (string (raw ), expected ), "actual=%v" , string (raw ))
58+ })
59+ }
60+
61+ // expectedOne is updated by running the tests with -update
62+ var expectedOne = ``
63+
64+ // expectedTwo is updated by running the tests with -update
65+ const expectedTwo = ``
3866
3967func patchUpdate (t * testing.T ) {
4068 source .Update = true
@@ -43,6 +71,26 @@ func patchUpdate(t *testing.T) {
4371 })
4472}
4573
74+ func fileName (t * testing.T ) string {
75+ t .Helper ()
76+ _ , filename , _ , ok := runtime .Caller (1 )
77+ assert .Assert (t , ok , "failed to get call stack" )
78+ return filename
79+ }
80+
81+ func resetVariable (t * testing.T , varName string , value string ) {
82+ t .Helper ()
83+ _ , filename , _ , ok := runtime .Caller (1 )
84+ assert .Assert (t , ok , "failed to get call stack" )
85+
86+ fileset := token .NewFileSet ()
87+ astFile , err := parser .ParseFile (fileset , filename , nil , parser .AllErrors | parser .ParseComments )
88+ assert .NilError (t , err )
89+
90+ err = source .UpdateVariable (filename , fileset , astFile , varName , value )
91+ assert .NilError (t , err , "failed to reset file" )
92+ }
93+
4694type fakeTestingT struct {
4795 failNowed bool
4896 failed bool
0 commit comments