Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created August 30, 2016 01:31
Show Gist options
  • Save JoshCheek/b3f7b8ffc636d6cc0391d590e86448da to your computer and use it in GitHub Desktop.
Save JoshCheek/b3f7b8ffc636d6cc0391d590e86448da to your computer and use it in GitHub Desktop.
Updated test for fish shell
# Part of https://github.com/fish-shell/fish-shell/issues/3341
# Updated from: https://gist.github.com/JoshCheek/87ae893807ad62caa8a1268677bc565d
function isomorphic_test -a expected_length str_pre
string split '' $str_pre \
| string escape \
| sed -E '
/^\'\'$/ {
i\
\\\\n
N
d
}' \
| string join '' \
| read str_intermediate
eval 'set str_post '$str_intermediate
set -l pre_length (string length $str_pre)
set -l post_length (string length $str_post)
set -l reasons
if [ $expected_length -ne $pre_length ]
set reasons $reasons "Expected the *input* length to be $expected_length but it was $pre_length"
end
if [ $expected_length -ne $post_length ]
set reasons $reasons "Expected the *output* length to be $expected_length but it was $post_length"
end
if [ $str_pre != $str_post ]
set reasons $reasons 'str_pre: '(string escape $str_pre)
set reasons $reasons 'str_post: '(string escape $str_post)
end
if [ 0 = (count $reasons) ]
set_color green
echo pass
else
set_color red
echo fail
end
set_color normal
for reason in $reasons
echo ' '$reason
end
end
# Passes
isomorphic_test 5 a\rb\ec
isomorphic_test 5 a\rb\nc
# Fails
# \x1e seems to break `test` (you can compensate by quoting the values)
# We omit \x00 b/c unix itself can't handle that
isomorphic_test 126 \ca\cb\cc\cd\ce\cf\cg\b\t\n\ck\cl\r\cn\co\cp\cq\cr\cs\ct\cu\cv\cw\cx\cy\cz\e\x1c\x1d\x1e\x1f' !"#$%&\'()*+,-./'0123456789':;<=>?@'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[\\]^_`'abcdefghijklmnopqrstuvwxyz'{|}~'
# Passes
# same as above, but we removed \x1e
isomorphic_test 125 \ca\cb\cc\cd\ce\cf\cg\b\t\n\ck\cl\r\cn\co\cp\cq\cr\cs\ct\cu\cv\cw\cx\cy\cz\e\x1c\x1d\x1f' !"#$%&\'()*+,-./'0123456789':;<=>?@'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[\\]^_`'abcdefghijklmnopqrstuvwxyz'{|}~'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment