setup_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package help
  2. import "testing"
  3. import "fmt"
  4. type foo struct {
  5. f1 int
  6. f2 bool
  7. f3 string
  8. }
  9. func (f foo) Iterate() Iterator {
  10. i := 0
  11. return func() (item interface{}, ok bool) {
  12. switch i {
  13. case 0:
  14. item = f.f1
  15. ok = true
  16. case 1:
  17. item = f.f2
  18. ok = true
  19. case 2:
  20. item = f.f3
  21. ok = true
  22. default:
  23. ok = false
  24. }
  25. i++
  26. return
  27. }
  28. }
  29. func (f foo) CompareTo(c Comparable) int {
  30. a, b := f.f1, c.(foo).f1
  31. if a < b {
  32. return -1
  33. } else if a > b {
  34. return 1
  35. }
  36. return 0
  37. }
  38. func toSlice(q Query) (result []interface{}) {
  39. next := q.Iterate()
  40. for item, ok := next(); ok; item, ok = next() {
  41. result = append(result, item)
  42. }
  43. return
  44. }
  45. func validateQuery(q Query, output []interface{}) bool {
  46. next := q.Iterate()
  47. for _, oitem := range output {
  48. qitem, _ := next()
  49. if oitem != qitem {
  50. return false
  51. }
  52. }
  53. _, ok := next()
  54. _, ok2 := next()
  55. return !(ok || ok2)
  56. }
  57. func mustPanicWithError(t *testing.T, expectedErr string, f func()) {
  58. defer func() {
  59. r := recover()
  60. err := fmt.Sprintf("%s", r)
  61. if err != expectedErr {
  62. t.Fatalf("got=[%v] expected=[%v]", err, expectedErr)
  63. }
  64. }()
  65. f()
  66. }