-
Notifications
You must be signed in to change notification settings - Fork 0
/
convline.bats
executable file
·82 lines (73 loc) · 1.97 KB
/
convline.bats
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
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bats
#The convline function accepts as input an argument that
#is a string. If the string matches a pattern like
#aaa ... bb (1 or more a's followed by 0 or more of
#anything followed by 1 or more b's) it outputs the b's followed
#by a space followed by an X followed by a space followed by
#the a's. For example, for the input aajbxabbb, the function
#would output bbb X aa. In addition, it returns 0.
#If the number of arguments is not 1 or the string
#doesn't match the pattern, the function returns 1.
convline() {
if [ "$#" -ne 1 ]; then return 1
fi
VAR=$(echo "$1" | sed -r 's/^(a+)([^a].*[^b])(b+)$/\3 X \1/;s/^(a+)(b+)$/\2 X \1/')
if [ "$VAR" = "$1" ]; then return 1
fi
echo "$VAR"
return 0
}
#test with no arguments; should return 1
@test "convline" {
#convline with no argument
run convline
#assertions
[ $status -eq 1 ]
}
@test "convline aaabbb" {
#convline with matching argument
run convline 'aaabbb'
echo $output
[ "$output" == "bbb X aaa" ]
[ $status -eq 0 ]
}
@test "convline aabbb" {
#convline with matching argument
run convline 'aabbb'
echo $output
[ "$output" == "bbb X aa" ]
[ $status -eq 0 ]
}
@test "convline aaabjbbbb" {
#convline with matching argument
run convline 'aaabjbbbb'
echo $output
[ "$output" == "bbbb X aaa" ]
[ $status -eq 0 ]
}
@test "convline aaabjbkabbbb" {
#convline with matching argument
run convline 'aaabjbkabbbb'
echo $output
[ "$output" == "bbbb X aaa" ]
[ $status -eq 0 ]
}
@test "convline abababbabbbb" {
#convline with matching argument
run convline 'abababbabbbb'
echo $output
[ "$output" == "bbbb X a" ]
[ $status -eq 0 ]
}
#test with an argument that doesn't match
@test "convline jabababbabbbb" {
#convline with argument that doesn't match
run convline 'jabababbabbbb'
[ $status -eq 1 ]
}
#test with an argument that doesn't match
@test "convline abababbabbbba" {
#convline with argument that doesn't match
run convline 'abababbabbbba'
[ $status -eq 1 ]
}