File: refute_regex.bats

package info (click to toggle)
bats-assert 2.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 344 kB
  • sloc: sh: 606; makefile: 4
file content (108 lines) | stat: -rw-r--r-- 2,301 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bats

load test_helper

#
# Literal matching
#

# Correctness
@test "refute_regex() <value> <pattern>: fails if a <value> substring matches extended regular expression <pattern>" {
  run refute_regex 'abc' '^[a-z]b'
  assert_test_fail <<'ERR_MSG'

-- value matches regular expression --
value    : abc
pattern  : ^[a-z]b
match    : ab
case     : sensitive
--
ERR_MSG
}

@test "refute_regex() <value> <pattern>: succeeds if no <value> substring matches extended regular expression <pattern>" {
  run refute_regex 'bcd' '^[a-z]b[c-z]+'
  assert_test_pass
}

@test "refute_regex() <value> <pattern>: provides results in BASH_REMATCH on failure" {
  unset -v BASH_REMATCH

  refute_regex 'abcd' 'b.d' \
  || {
      declare -p BASH_REMATCH && \
      [ "${BASH_REMATCH[0]}" = 'bcd' ]
  }
}

@test "refute_regex() <value> <pattern>: matches case-insensitively when 'nocasematch' is set" {
  shopt -s nocasematch

  run refute_regex 'aBc' 'ABC'
  assert_test_fail <<'ERR_MSG'

-- value matches regular expression --
value    : aBc
pattern  : ABC
match    : aBc
case     : insensitive
--
ERR_MSG
}

@test "refute_regex() <value> <pattern>: outputs multi-line <value> nicely when it fails" {
  run refute_regex $'abc\n123' '^[a-z]b[c-z]+'
  assert_test_fail <<'ERR_MSG'

-- value matches regular expression --
value (2 lines):
  abc
  123
pattern (1 lines):
  ^[a-z]b[c-z]+
match (1 lines):
  abc
case (1 lines):
  sensitive
--
ERR_MSG

  shopt -s nocasematch
  run refute_regex $'aBc\n123' '^[a-z]b[c-z]+'
  assert_test_fail <<'ERR_MSG'

-- value matches regular expression --
value (2 lines):
  aBc
  123
pattern (1 lines):
  ^[a-z]b[c-z]+
match (1 lines):
  aBc
case (1 lines):
  insensitive
--
ERR_MSG
}

# Error handling
@test "refute_regex() <value> <pattern>: returns 1 and displays an error message if <pattern> is not a valid extended regular expression" {
  run refute_regex value '[.*'

  if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >=3) )); then
    [[ "$output" =~ "invalid regular expression "([^$'\n']+) ]]
    assert_test_fail <<ERR_MSG

-- ERROR: refute_regex --
invalid regular expression ${BASH_REMATCH[1]}
--
ERR_MSG
  else
    assert_test_fail <<'ERR_MSG'

-- ERROR: refute_regex --
Invalid extended regular expression: `[.*'
--
ERR_MSG
  fi
}