golden_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package testutil
  5. import (
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestUpdate(t *testing.T) {
  11. before := updateRegex
  12. defer func() {
  13. updateRegex = before
  14. }()
  15. t.Run("no flag", func(t *testing.T) {
  16. updateRegex = nil
  17. assert.False(t, Update("TestUpdate"))
  18. })
  19. tests := []struct {
  20. regex string
  21. name string
  22. want bool
  23. }{
  24. {regex: "", name: "TestUpdate", want: false},
  25. {regex: "TestNotFound", name: "TestUpdate", want: false},
  26. {regex: ".*", name: "TestUpdate", want: true},
  27. }
  28. for _, test := range tests {
  29. t.Run("", func(t *testing.T) {
  30. updateRegex = &test.regex
  31. assert.Equal(t, test.want, Update(test.name))
  32. })
  33. }
  34. }
  35. func TestAssertGolden(t *testing.T) {
  36. // Make sure it does not blow up
  37. AssertGolden(t, filepath.Join("testdata", "golden"), false, "{\n \"Message\": \"This is a golden file.\"\n}")
  38. AssertGolden(t, filepath.Join("testdata", "golden"), false, []byte("{\n \"Message\": \"This is a golden file.\"\n}"))
  39. type T struct {
  40. Message string
  41. }
  42. AssertGolden(t, filepath.Join("testdata", "golden"), false, T{"This is a golden file."})
  43. }