index_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package help
  2. import (
  3. "testing"
  4. )
  5. func TestIndexOf(t *testing.T) {
  6. tests := []struct {
  7. input interface{}
  8. predicate func(interface{}) bool
  9. expected int
  10. }{
  11. {
  12. input: [9]int{1, 2, 3, 4, 5, 6, 7, 8, 9},
  13. predicate: func(i interface{}) bool {
  14. return i.(int) == 3
  15. },
  16. expected: 2,
  17. },
  18. {
  19. input: "sstr",
  20. predicate: func(i interface{}) bool {
  21. return i.(rune) == 'r'
  22. },
  23. expected: 3,
  24. },
  25. {
  26. input: "gadsgsadgsda",
  27. predicate: func(i interface{}) bool {
  28. return i.(rune) == 'z'
  29. },
  30. expected: -1,
  31. },
  32. }
  33. for _, test := range tests {
  34. index := From(test.input).IndexOf(test.predicate)
  35. if index != test.expected {
  36. t.Errorf("From(%v).IndexOf() expected %v received %v", test.input, test.expected, index)
  37. }
  38. index = From(test.input).IndexOfT(test.predicate)
  39. if index != test.expected {
  40. t.Errorf("From(%v).IndexOfT() expected %v received %v", test.input, test.expected, index)
  41. }
  42. }
  43. }
  44. func TestIndexOfT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
  45. mustPanicWithError(t, "IndexOfT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
  46. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).IndexOfT(func(item int) int { return item + 2 })
  47. })
  48. }