Skip to content
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

Do not collapse empty lines in snippets #595

Merged
merged 2 commits into from
Aug 7, 2021
Merged
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
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('%') {
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
11 changes: 9 additions & 2 deletions tests/core.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

source "${NAVI_HOME}/scripts/install"

NEWLINE_CHAR="\036"

PASSED=0
FAILED=0
SKIPPED=0
Expand Down Expand Up @@ -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}'"
Expand Down
13 changes: 11 additions & 2 deletions tests/no_prompt_cheats/cases.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/run
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down Expand Up @@ -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

Expand Down