summaryrefslogtreecommitdiff
path: root/Userland/Shell/Tests/slice.sh
blob: 875ea48acc79976973e9c575ca573a9f560fd849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/sh

source $(dirname "$0")/test-commons.inc

# can we use [0] as a bareword still?
if not test [0] = "[0]" {
    fail cannot use '[0]' as a bareword anymore
}

# can we use [0,2] as a bareword still?
if not test [0,2] = "[0,2]" {
    fail cannot use '[0,2]' as a bareword anymore
}

# can we use [0..2] as a bareword still?
if not test [0..2] = "[0..2]" {
    fail cannot use '[0..2]' as a bareword anymore
}

# Lists
x=(1 2 3)
if not test $x[0] -eq 1 {
    fail invalid first element
}

if not test $x[1] -eq 2 {
    fail invalid second element
}

if not test $x[-1] -eq 3 {
    fail invalid first-from-end element
}

if not test $x[-2] -eq 2 {
    fail invalid second-from-end element
}

## Multiple indices
if not test "$x[1,2]" = "2 3" {
    fail invalid multi-select '(1, 2)'
}

if not test "$x[0..2]" = "1 2 3" {
    fail invalid multi-select with range '[0..2]'
}

# Strings
x="Well Hello Friends!"
if not test $x[0] = W {
    fail invalid string first element
}

if not test $x[1] = e {
    fail invalid string second element
}

if not test $x[-1] = '!' {
    fail invalid string first-from-end element
}

if not test $x[-2] = 's' {
    fail invalid string second-from-end element
}

if not test $x[0,5,11,-1] = 'WHF!' {
    fail invalid string multi-select
}

if not test $x[5..9] = "Hello" {
    fail invalid string multi-select with range '[5..9]'
}

pass