Do not collapse empty lines in snippets (#595)

Fixes #539
This commit is contained in:
Denis Isidoro 2021-08-07 12:24:39 -03:00 committed by GitHub
parent 59362c0ec8
commit 02fc83991b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 7 deletions

View file

@ -187,6 +187,9 @@ pub fn read_lines(
if !item.tags.is_empty() && !item.comment.is_empty() {}
// blank
if line.is_empty() {
if !(&item.snippet).is_empty() {
item.snippet.push_str(writer::LINE_SEPARATOR);
}
}
// tag
else if line.starts_with('%') {

View file

@ -46,7 +46,7 @@ pub fn write(item: &Item) -> String {
tags = item.tags,
comment = item.comment,
delimiter = DELIMITER,
snippet = &item.snippet,
snippet = &item.snippet.trim_end_matches(LINE_SEPARATOR),
file_index = item.file_index,
)
}

View file

@ -3,6 +3,8 @@
source "${NAVI_HOME}/scripts/install"
NEWLINE_CHAR="\036"
PASSED=0
FAILED=0
SKIPPED=0
@ -38,12 +40,17 @@ test::run() {
"$@" && test::success || test::fail
}
test::_escape() {
tr '\n' "$NEWLINE_CHAR" | sed -E "s/[\s$(printf "$NEWLINE_CHAR") ]+$//g"
}
test::equals() {
local -r actual="$(cat)"
local -r expected="$(echo "${1:-}")"
local -r actual2="$(echo "$actual" | xargs | sed -E 's/\s/ /g')"
local -r expected2="$(echo "$expected" | xargs | sed -E 's/\s/ /g')"
local -r actual2="$(echo "$actual" | test::_escape)"
local -r expected2="$(echo "$expected" | test::_escape)"
if [[ "$actual2" != "$expected2" ]]; then
log::error "Expected '${expected}' but got '${actual}'"

View file

@ -8,14 +8,23 @@ echo "foo"
# map -> "_foo_"
echo "<map1>"
# expand -> "foo"
echo "<expand1>"
# duplicated lines -> "foo\nlorem ipsum\nlorem ipsum\nbaz"
echo foo
echo lorem ipsum
echo lorem ipsum
echo baz
# expand -> "foo"
echo "<expand1>"
# empty line -> "foo\n\n\nbar"
echo "$(cat <<EOF
foo
bar
EOF
)"
# sed with replacement -> "172.17.0.2"
echo "8.8.8.8 via 172.17.0.1 dev eth0 src 172.17.0.2" | sed -E 's/.*src ([0-9.]+).*/\1/p' | head -n1

View file

@ -36,7 +36,7 @@ _get_all_tests() {
cat "${TEST_CHEAT_PATH}/cases.cheat" \
| grep '^#' \
| grep ' ->' \
| sed 's/\\n/ /g' \
| sed 's/\\n/'"$(printf "$NEWLINE_CHAR")"'/g' \
| sed -E 's/# (.*) -> "(.*)"/\1|\2/g'
}
@ -144,7 +144,7 @@ IFS=$'\n'
for i in $(_get_tests "$filter"); do
IFS="$ifs"
query="$(echo "$i" | cut -d'|' -f1)"
expected="$(echo "$i" | cut -d'|' -f2)"
expected="$(echo "$i" | tr "$NEWLINE_CHAR" '\n' | cut -d'|' -f2)"
test::run "$query" _navi_cases_test "$query" "$expected"
done