Last active
April 24, 2019 12:04
-
-
Save alex-harvey-z3q/3f157cf006006fd95bc59e0a905265d9 to your computer and use it in GitHub Desktop.
Test SO Sed question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Unit test code | |
run() { | |
case "$SCRIPT" in | |
AlexHarvey) | |
gsed $1 ' | |
1{ | |
/999/d | |
} | |
$!N | |
/999/d | |
P | |
D | |
' FILE | |
;; | |
potong) | |
gsed -n ' | |
:a | |
$!N | |
/\n.*999/ { | |
:b | |
n | |
/999/bb | |
ba | |
} | |
/999/!P | |
D | |
' FILE | |
;; | |
EdMorton) | |
awk ' | |
$NF == 999 { | |
prev = "" | |
next | |
} | |
{ | |
printf "%s", prev | |
prev = $0 ORS | |
} | |
END { | |
printf "%s", prev | |
} | |
' FILE | |
;; | |
esac | |
} | |
test999InFirstLine() { | |
cat > FILE <<EOF | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
EOF | |
output=$(run) | |
assertEquals "SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT" "$output" | |
} | |
test999InSecondLine() { | |
cat > FILE <<EOF | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
EOF | |
output=$(run) | |
assertEquals "SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT" "$output" | |
} | |
test999InSecondLastLine() { | |
cat > FILE <<EOF | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
EOF | |
output=$(run) | |
assertEquals "SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT" "$output" | |
} | |
test999InLastLine() { | |
cat > FILE <<EOF | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
EOF | |
output=$(run) | |
assertEquals "SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT" "$output" | |
} | |
test999InNoneOfAbove() { | |
cat > FILE <<EOF | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD PRINT | |
EOF | |
output=$(run) | |
assertEquals "SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT" "$output" | |
} | |
test999InThreeLinesInARow() { | |
cat > FILE <<EOF | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD NOT PRINT | |
This is test line 999 | |
This is test line 999 | |
This is test line 999 | |
SHOULD PRINT | |
SHOULD PRINT | |
EOF | |
output=$(run) | |
assertEquals "SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT | |
SHOULD PRINT" "$output" | |
} | |
testSample() { | |
cat > FILE <<EOF | |
This is test line 11 | |
This is test line 999 | |
This is test line 12 | |
This is test line 13 | |
This is test line 16 | |
This is test line 999 | |
This is test line 17 | |
This is test line 18 | |
EOF | |
output=$(run) | |
assertEquals "This is test line 12 | |
This is test line 13 | |
This is test line 17 | |
This is test line 18" "$output" | |
} | |
. shunit2 | |
## Results | |
▶ for i in AlexHarvey potong EdMorton ; do | |
echo "Testing $i's solution:" | |
SHUNIT_COLOR=none SCRIPT=$i bash test.sh | |
echo "====" | |
done | |
Testing AlexHarvey's solution: | |
test999InFirstLine | |
test999InSecondLine | |
test999InSecondLastLine | |
test999InLastLine | |
test999InNoneOfAbove | |
test999InThreeLinesInARow | |
testSample | |
Ran 7 tests. | |
OK | |
==== | |
Testing potong's solution: | |
test999InFirstLine | |
test999InSecondLine | |
test999InSecondLastLine | |
test999InLastLine | |
test999InNoneOfAbove | |
test999InThreeLinesInARow | |
testSample | |
Ran 7 tests. | |
OK | |
==== | |
Testing EdMorton's solution: | |
test999InFirstLine | |
test999InSecondLine | |
test999InSecondLastLine | |
test999InLastLine | |
test999InNoneOfAbove | |
test999InThreeLinesInARow | |
testSample | |
Ran 7 tests. | |
OK | |
==== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment