Skip to content

Add automated tests #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ jobs:
chmod +x /usr/local/bin/tidy
- name: Validate HTML
run: tidy -errors -quiet --drop-empty-elements no regex.html

test_sed:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Run sed tests
run: tests/test_sed.sh

set_vim:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run Vim tests
run: tests/test_vim.sh
7 changes: 4 additions & 3 deletions regex.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h1>Syntax</h1>

<table class="table table-bordered table-striped">
<thead>
<tr><th>What</th><th><a href="http://perldoc.perl.org/perlre.html">Perl</a>/PCRE</th><th><a href="https://docs.python.org/library/re.html">Python's <code>re</code></a></th><th>POSIX (BRE)</th><th>POSIX extended (ERE)</th><th>Vim</th></tr>
<tr><th>What</th><th><a href="http://perldoc.perl.org/perlre.html">Perl</a>/PCRE</th><th><a href="https://docs.python.org/library/re.html">Python's <code>re</code></a></th><th>POSIX (BRE)</th><th>POSIX extended (ERE)</th><th>Vim (with <code>:set magic</code>)</th></tr>
</thead>
<tbody id="syntax-basics" class="subgroup">
<tr><th colspan="6">Basics</th></tr>
Expand All @@ -99,7 +99,7 @@ <h1>Syntax</h1>
<tr><td>Any character (including newline)</td><td class="na"></td><td class="na"></td><td class="na"></td><td class="na"></td><td><code>\_.</code></td></tr>
<tr><td>Match a "word" character (alphanumeric plus <code>_</code>)</td><td><code>\w</code> <code>[[:word:]]</code></td><td><code>\w</code></td><td><code>\w</code></td><td><code>\w</code></td><td><code>\w</code></td></tr>
<tr><td>Case</td><td><code>[[:upper:]]</code> / <code>[[:lower:]]</code></td><td class="na"></td><td><code>[[:upper:]]</code> / <code>[[:lower:]]</code></td><td><code>[[:upper:]]</code> / <code>[[:lower:]]</code></td><td><code>\u</code> <code>[[:upper:]]</code> / <code>\l</code> <code>[[:lower:]]</code></td></tr>
<tr><td>Match a non-"word" character</td><td><code>\W</code></td><td><code>\W</code></td><td class="na"></td><td class="na"></td><td><code>\W</code></td></tr>
<tr><td>Match a non-"word" character</td><td><code>\W</code></td><td><code>\W</code></td><td><code>\W</code></td><td><code>\W</code></td><td><code>\W</code></td></tr>
<tr><td>Match a whitespace character (except newline)</td><td class="na"></td><td class="na"></td><td><code>\s</code> <code>[[:space:]]</code></td><td><code>\s</code> <code>[[:space:]]</code></td><td><code>\s</code> <code>[[:space:]]</code></td></tr>
<tr><td>Whitespace including newline</td><td><code>\s</code> <code>[[:space:]]</code></td><td><code>\s</code></td><td class="na"></td><td class="na"></td><td><code>\_s</code></td></tr>
<tr><td>Match a non-whitespace character</td><td><code>\S</code></td><td><code>\S</code></td><td><code>[^[:space:]]</code></td><td><code>[^[:space:]]</code></td><td><code>\S</code> <code>[^[:space:]]</code></td></tr>
Expand Down Expand Up @@ -153,7 +153,8 @@ <h1>Syntax</h1>
<tbody id="syntax-other" class="subgroup">
<tr><th colspan="6">Other</th></tr>
<tr><td>Independent non-backtracking pattern</td><td><code>(?&gt;...)</code></td><td class="na"></td><td class="na"></td><td class="na"></td><td><code>\(...\)\@&gt;</code></td></tr>
<tr><td>Make case-sensitive/insensitive</td><td><code>(?i)</code> / <code>(?-i)</code></td><td><code>(?i)</code> / <code>(?-i)</code></td><td class="na"></td><td class="na"></td><td><code>\c</code> / <code>\C</code></td></tr>
<tr><td>Make case-insensitive</td><td><code>(?i)</code></td><td><code>(?i)</code></td><td class="na"></td><td class="na"></td><td><code>\c</code></td></tr>
<tr><td>Make case-sensitive</td><td><code>(?-i)</code></td><td class="na"></td><td class="na"></td><td class="na"></td><td><code>\C</code></td></tr>
</tbody>
</table>
</section>
Expand Down
296 changes: 296 additions & 0 deletions tests/test_sed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
#!/bin/bash

export LC_ALL=en_US.UTF-8

check_(){
RESULT="$(echo "$4" | sed "$1" "$5")"
if [ "$RESULT" != "$6" ]; then
echo "Test failed: $3 ($2)" >&2
echo "----- expected -----
$6
----- returned -----"
echo "$RESULT"
exit 1
fi
}

# Check BRE
checkb(){
check_ "-e" "BRE" "$@"
}
# Check ERE
checke(){
check_ "-E" "ERE" "$@"
}
# Check both
check(){
checkb "$@"
checke "$@"
}

fail_(){
if echo | sed "$1" "$4" &>/dev/null; then
echo "Test didn't fail as expected: $3 ($2)" >&2
exit 1
fi
}

# Fail check BRE
failb(){
fail_ "-e" "BRE" "$@"
}
# Fail check ERE
faile(){
fail_ "-E" "ERE" "$@"
}
# Fail check both
fail(){
failb "$@"
faile "$@"
}

# Basics

check 'Custom character class' \
'adn
aqv' 's/[b-eq]/x/g' \
'axn
axv'

check 'Negated custom character class' \
'abcdefgh' \
's/[^b-dg-l]/x/g' \
'xbcdxxgh'

check 'Backslash not special in class' \
"a]\\b
a]\\b" \
"1s/[\\m-p]/x/g
2s/[]]/x/g" \
"a]xb
ax\\b"

check 'Ranges' \
'a-e-i
a-e-i' \
'1s/[d-f]/x/g
2s/[d-f-]/x/g' \
'a-x-i
axxxi'

checkb 'Alternation' \
'acd' \
's/b\|c/x/g' \
'axd'
checke 'Alternation' \
'acd' \
's/b|c/x/g' \
'axd'

check 'Escaped character' \
'abc' \
"s/\\061\\x62\\x(99)/x/g" \
'abc'

# Charater classes

check 'Any character' \
'a ;d
efg' \
's/./x/g' \
'xxxx
xxx'

check 'Word character' \
'hello w0r|_d!' \
's/\w/x/g' \
'xxxxx xxx|xx!'

fail 'Word class' \
's/[[:word:]]/x/g'

check 'Upper case' \
'Hell0 W0r|_D' \
's/[[:upper:]]/X/g' \
'Xell0 X0r|_X'

check 'Lower case' \
'Hell0 W0r|_D' \
's/[[:lower:]]/x/g' \
'Hxxx0 W0x|_D'

check 'Whitespace' \
'Hello world !' \
's/\s/_/g' \
'Hello_world_!'

check 'Whitespace' \
'Hello world !' \
's/[[:space:]]/_/g' \
'Hello_world_!'

check 'Non-whitespace' \
'Hello world !' \
's/[^[:space:]]/x/g' \
'xxxxx xxxxx x'

check 'Digit' \
'H3ll0 W0r|_D' \
's/[[:digit:]]/+/g' \
'H+ll+ W+r|_D'

check 'Hexadecimal digit' \
'H3ll0 W0r|_D' \
's/[[:xdigit:]]/+/g' \
'H+ll+ W+r|_+'

fail 'Octal digit' \
's/[[:odigit:]]/x/g'

check 'Punctuation' \
'+- 01: hello, world! ;) -+' \
's/[[:punct:]]/_/g' \
'__ 01_ hello_ world_ __ __'

check 'Alphabetical characters' \
'+- 01: hello, world! ;) -+' \
's/[[:alpha:]]/x/g' \
'+- 01: xxxxx, xxxxx! ;) -+'

check 'Alphanumerical characters' \
'+- 01: hello, world! ;) -+' \
's/[[:alnum:]]/x/g' \
'+- xx: xxxxx, xxxxx! ;) -+'

fail 'ASCII class' \
's/[[:ascii:]]/x/g'

check 'Character equivalents' \
'Rémi est prêt' \
's/[[=e=]]/_/g' \
'R_mi _st pr_t'

check 'Word boundary' \
'Hello, world' \
's/o\b/x/g' \
'Hellx, world'

check 'Not word boundary' \
'Hello, world' \
's/o\B/x/g' \
'Hello, wxrld'

check 'Begining of line' \
'testing tests' \
's/^t/r/g' \
'resting tests'

check 'End of line' \
'testing tests' \
's/s$/x/g' \
'testing testx'

# Captures and groups

checkb 'Capturing group' \
'Name is Remi!' \
's/^.*is \(.*\)!$/\1/' \
'Remi'
checke 'Capturing group' \
'Name is Remi!' \
's/^.*is (.*)!$/\1/' \
'Remi'

checkb 'Non-capturing parentheses' \
'Some (dumb)text' \
's/(.*)//g' \
'Some text'
checke 'Non-capturing parentheses' \
'Some (dumb)text' \
's/\(.*\)//g' \
'Some text'

checkb 'Backreference' \
'ab be cd cc df' \
's/\([a-z]\)\1/xx/g' \
'ab be cd xx df'
checke 'Backreference' \
'ab be cd cc df' \
's/([a-z])\1/xx/g' \
'ab be cd xx df'

# Look-around not supported in POSIX

# Multiplicity

checkb '0 or 1' \
'bb bab baab baa?b baaab' \
's/baa\?b/x/g' \
'bb x x baa?b baaab'
checke '0 or 1' \
'bb bab baab baa?b baaab' \
's/baa?b/x/g' \
'bb x x baa?b baaab'

checkb '0 or 1 (negative)' \
'bb bab baab baa?b baaab' \
's/baa?b/x/g' \
'bb bab baab x baaab'
checke '0 or 1 (negative)' \
'bb bab baab baa?b baaab' \
's/baa\?b/x/g' \
'bb bab baab x baaab'

checkb '1 or more' \
'bb bab baab ba+b baaab' \
's/ba\+b/x/g' \
'bb x x ba+b x'
checke '1 or more' \
'bb bab baab ba+b baaab' \
's/ba+b/x/g' \
'bb x x ba+b x'

checkb '1 or more (negative)' \
'bb bab baab ba+b baaab' \
's/ba+b/x/g' \
'bb bab baab x baaab'
checke '1 or more (negative)' \
'bb bab baab ba+b baaab' \
's/ba\+b/x/g' \
'bb bab baab x baaab'

checkb 'Specific number (1)' \
'bb bab baab baaab baaaab' \
's/ba\{2\}b/x/g' \
'bb bab x baaab baaaab'
checke 'Specific number (1)' \
'bb bab baab baaab baaaab' \
's/ba{2}b/x/g' \
'bb bab x baaab baaaab'

checkb 'Specific number (closed)' \
'bb bab baab baaab baaaab' \
's/ba\{1,3\}b/x/g' \
'bb x x x baaaab'
checke 'Specific number (closed)' \
'bb bab baab baaab baaaab' \
's/ba{1,3}b/x/g' \
'bb x x x baaaab'

checkb 'Specific number (open left)' \
'bb bab baaab baaaab' \
's/ba\{,3\}b/x/g' \
'x x x baaaab'
checke 'Specific number (open left)' \
'bb bab baaab baaaab' \
's/ba{,3}b/x/g' \
'x x x baaaab'

checkb 'Specific number (open right)' \
'bb bab baaab baaaab' \
's/ba\{2,\}b/x/g' \
'bb bab x x'
checke 'Specific number (open right)' \
'bb bab baaab baaaab' \
's/ba{2,}b/x/g' \
'bb bab x x'
Loading