slice.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/sh
  2. source $(dirname "$0")/test-commons.inc
  3. # can we use [0] as a bareword still?
  4. if not test [0] = "[0]" {
  5. fail cannot use '[0]' as a bareword anymore
  6. }
  7. # can we use [0,2] as a bareword still?
  8. if not test [0,2] = "[0,2]" {
  9. fail cannot use '[0,2]' as a bareword anymore
  10. }
  11. # can we use [0..2] as a bareword still?
  12. if not test [0..2] = "[0..2]" {
  13. fail cannot use '[0..2]' as a bareword anymore
  14. }
  15. # Lists
  16. x=(1 2 3)
  17. if not test $x[0] -eq 1 {
  18. fail invalid first element
  19. }
  20. if not test $x[1] -eq 2 {
  21. fail invalid second element
  22. }
  23. if not test $x[-1] -eq 3 {
  24. fail invalid first-from-end element
  25. }
  26. if not test $x[-2] -eq 2 {
  27. fail invalid second-from-end element
  28. }
  29. ## Multiple indices
  30. if not test "$x[1,2]" = "2 3" {
  31. fail invalid multi-select '(1, 2)'
  32. }
  33. if not test "$x[0..2]" = "1 2 3" {
  34. fail invalid multi-select with range '[0..2]'
  35. }
  36. # Strings
  37. x="Well Hello Friends!"
  38. if not test $x[0] = W {
  39. fail invalid string first element
  40. }
  41. if not test $x[1] = e {
  42. fail invalid string second element
  43. }
  44. if not test $x[-1] = '!' {
  45. fail invalid string first-from-end element
  46. }
  47. if not test $x[-2] = 's' {
  48. fail invalid string second-from-end element
  49. }
  50. if not test $x[0,5,11,-1] = 'WHF!' {
  51. fail invalid string multi-select
  52. }
  53. if not test $x[5..9] = "Hello" {
  54. fail invalid string multi-select with range '[5..9]'
  55. }
  56. pass