from_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package help
  2. import "testing"
  3. func TestFrom(t *testing.T) {
  4. c := make(chan interface{}, 3)
  5. c <- -1
  6. c <- 0
  7. c <- 1
  8. close(c)
  9. ct := make(chan int, 3)
  10. ct <- -10
  11. ct <- 0
  12. ct <- 10
  13. close(ct)
  14. tests := []struct {
  15. input interface{}
  16. output []interface{}
  17. want bool
  18. }{
  19. {[]int{1, 2, 3}, []interface{}{1, 2, 3}, true},
  20. {[]int{1, 2, 4}, []interface{}{1, 2, 3}, false},
  21. {[3]int{1, 2, 3}, []interface{}{1, 2, 3}, true},
  22. {[3]int{1, 2, 4}, []interface{}{1, 2, 3}, false},
  23. {"str", []interface{}{'s', 't', 'r'}, true},
  24. {"str", []interface{}{'s', 't', 'g'}, false},
  25. {map[string]bool{"foo": true}, []interface{}{KeyValue{"foo", true}}, true},
  26. {map[string]bool{"foo": true}, []interface{}{KeyValue{"foo", false}}, false},
  27. {c, []interface{}{-1, 0, 1}, true},
  28. {ct, []interface{}{-10, 0, 10}, true},
  29. {foo{f1: 1, f2: true, f3: "string"}, []interface{}{1, true, "string"}, true},
  30. }
  31. for _, test := range tests {
  32. if q := From(test.input); validateQuery(q, test.output) != test.want {
  33. if test.want {
  34. t.Errorf("From(%v)=%v expected %v", test.input, toSlice(q), test.output)
  35. } else {
  36. t.Errorf("From(%v)=%v expected not equal", test.input, test.output)
  37. }
  38. }
  39. }
  40. }
  41. func TestFromChannel(t *testing.T) {
  42. c := make(chan interface{}, 3)
  43. c <- 10
  44. c <- 15
  45. c <- -3
  46. close(c)
  47. w := []interface{}{10, 15, -3}
  48. if q := FromChannel(c); !validateQuery(q, w) {
  49. t.Errorf("FromChannel() failed expected %v", w)
  50. }
  51. }
  52. func TestFromChannelT(t *testing.T) {
  53. c := make(chan int, 3)
  54. c <- 10
  55. c <- 15
  56. c <- -3
  57. close(c)
  58. w := []interface{}{10, 15, -3}
  59. if q := FromChannelT(c); !validateQuery(q, w) {
  60. t.Errorf("FromChannelT() failed expected %v", w)
  61. }
  62. }
  63. func TestFromString(t *testing.T) {
  64. s := "string"
  65. w := []interface{}{'s', 't', 'r', 'i', 'n', 'g'}
  66. if q := FromString(s); !validateQuery(q, w) {
  67. t.Errorf("FromString(%v)!=%v", s, w)
  68. }
  69. }
  70. func TestFromIterable(t *testing.T) {
  71. s := foo{f1: 1, f2: true, f3: "string"}
  72. w := []interface{}{1, true, "string"}
  73. if q := FromIterable(s); !validateQuery(q, w) {
  74. t.Errorf("FromIterable(%v)!=%v", s, w)
  75. }
  76. }
  77. func TestRange(t *testing.T) {
  78. w := []interface{}{-2, -1, 0, 1, 2}
  79. if q := Range(-2, 5); !validateQuery(q, w) {
  80. t.Errorf("Range(-2, 5)=%v expected %v", toSlice(q), w)
  81. }
  82. }
  83. func TestRepeat(t *testing.T) {
  84. w := []interface{}{1, 1, 1, 1, 1}
  85. if q := Repeat(1, 5); !validateQuery(q, w) {
  86. t.Errorf("Repeat(1, 5)=%v expected %v", toSlice(q), w)
  87. }
  88. }