選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

findup_test.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package gofindup
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/spf13/afero"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type given struct {
  9. file string
  10. base string
  11. }
  12. type expect struct {
  13. found bool
  14. err string
  15. }
  16. type assertion struct {
  17. given given
  18. expect expect
  19. }
  20. type assertions []assertion
  21. type finderFunc func(string, string, afero.Fs) (bool, error)
  22. var fakefs = afero.NewMemMapFs()
  23. func init() {
  24. // Build fake file system
  25. fakefs.MkdirAll("/test", 0755)
  26. fakefs.MkdirAll("/test/a", 0755)
  27. fakefs.MkdirAll("/test/a/ab", 0755)
  28. afero.WriteFile(fakefs, "/test/a/ab/f.txt", []byte("some mock file"), 0644)
  29. fakefs.MkdirAll("/test/a/ac", 0755)
  30. afero.WriteFile(fakefs, "/test/a/ad.txt", []byte("some mock file"), 0644)
  31. fakefs.MkdirAll("/test/b", 0755)
  32. fakefs.MkdirAll("/test/b/bc", 0755)
  33. afero.WriteFile(fakefs, "/test/b/bc/f.txt", []byte("some mock file"), 0644)
  34. afero.WriteFile(fakefs, "/test/b/bd.txt", []byte("some mock file"), 0644)
  35. fakefs.MkdirAll("/test/c", 0755)
  36. fakefs.MkdirAll("/test/c/cd", 0755)
  37. afero.WriteFile(fakefs, "/test/c/cd/f.txt", []byte("some mock file"), 0644)
  38. afero.WriteFile(fakefs, "/test/t.txt", []byte("some mock file"), 0644)
  39. afero.WriteFile(fakefs, "r.txt", []byte("some mock file"), 0644)
  40. }
  41. func testAssertions(t *testing.T, as assertions, fn finderFunc) {
  42. for _, a := range as {
  43. found, err := fn(a.given.file, a.given.base, fakefs)
  44. if a.expect.found {
  45. assert.True(t, found)
  46. } else {
  47. assert.False(t, found)
  48. }
  49. if a.expect.err == "" {
  50. assert.Nil(t, err)
  51. } else {
  52. assert.EqualError(t, err, a.expect.err)
  53. }
  54. }
  55. }
  56. func TestFindIn(t *testing.T) {
  57. assertions := assertions{
  58. {given{"ab", "/test/a"}, expect{true, ""}},
  59. {given{"ac", "/test/a"}, expect{true, ""}},
  60. {given{"ae", "/test/a"}, expect{false, ""}},
  61. {given{"de", "/test/d"}, expect{false, "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
  62. }
  63. testAssertions(t, assertions, findIn)
  64. }
  65. func TestFindUpFrom(t *testing.T) {
  66. assertions := assertions{
  67. {given{"ab", "/test/a"}, expect{true, ""}},
  68. {given{"ab", "/test/a/ac"}, expect{true, ""}},
  69. {given{"t.txt", "/test/a/ac"}, expect{true, ""}},
  70. {given{"r.txt", "/test/a/ac"}, expect{true, ""}},
  71. {given{"r.txt", "/test/d"}, expect{false, "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
  72. }
  73. testAssertions(t, assertions, findupFrom)
  74. }