Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This gist is to show that this question here:
https://stackoverflow.com/a/38978201/3787051
Is not answered by this answer here:
https://stackoverflow.com/q/55220417/3787051
This is Bash unit test code to test various solutions that were suggested as answers to my question.
==============
setUp() {
data="aaa
PAT1
bbb
ccc
ddd
PAT2
eee
PAT1
2
46
PAT2
xyz"
expected="bbb
ccc
ddd"
}
# These four relate to:
# https://stackoverflow.com/q/55220417/3787051
testLinkedNotApplicableSolution1() {
actual=$(gsed -n '/PAT1/,/PAT2/{/PAT1/!{/PAT2/!p}}' <<< $data)
assertNotEquals "$expected" "$actual"
}
testLinkedNotApplicableSolution2() {
actual=$(gsed -n '/PAT1/,/PAT2/{//!p}' <<< $data)
assertNotEquals "$expected" "$actual"
}
testLinkedNotApplicableSolution3() {
actual=$(gsed -n '/PAT1/,/PAT2/p' <<< $data)
assertNotEquals "$expected" "$actual"
}
testLinkedNotApplicableSolution4() {
actual=$(gsed -n '/PAT1/,/PAT2/{/PAT2/!p}' <<< $data)
assertNotEquals "$expected" "$actual"
}
# These all relate to solutions proposed in the chat
testIniansIncorrectSolution1InChat() {
actual=$(awk '/PAT1/{flag=1; next} /PAT2/{flag=0} flag' <<< $data)
assertNotEquals "$expected" "$actual"
}
testIniansIncorrectSolution2InChat() {
actual=$(gsed -n '/PAT1/,/PAT2/{//!p}' <<< $data)
assertNotEquals "$expected" "$actual"
}
# This is a correct solution, but also an original solution first proposed in chat.
testJamesBrownsCorrectSolutionInChat() {
actual=$(awk '/PAT1/{flag=1; next} /PAT2/{exit} flag' <<< $data)
assertEquals "$expected" "$actual"
}
# This is my solution, and still I think the cleanest, and the one I wanted to document.
testMyOwnCorrectSolution() {
actual=$(gsed '0,/PAT1/d;/PAT2/Q' <<< $data)
assertEquals "$expected" "$actual"
}
. shunit2
=============
Results:
▶ bash test.sh
testLinkedNotApplicableSolution1
testLinkedNotApplicableSolution2
testLinkedNotApplicableSolution3
testLinkedNotApplicableSolution4
testIniansIncorrectSolution1InChat
testIniansIncorrectSolution2InChat
testJamesBrownsCorrectSolutionInChat
testMyOwnCorrectSolution
Ran 8 tests.
OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment