From 808180dba46930da8575bb480354222878e883ad Mon Sep 17 00:00:00 2001 From: felipeog <17603069+felipeog@users.noreply.github.com> Date: Thu, 14 Mar 2024 22:30:22 -0300 Subject: [PATCH] Make 001 accept multiple lines and empty lines --- .../001-two-fer/{empty.txt => empty-file.txt} | 0 exercises/001-two-fer/empty-lines.txt | 2 + .../001-two-fer/one-name-and-empty-lines.txt | 3 ++ .../001-two-fer/{one.txt => one-name.txt} | 0 exercises/001-two-fer/tests.bats | 39 ++++++++++++++++++- exercises/001-two-fer/two-fer.awk | 9 +++-- .../001-two-fer/two-names-and-empty-lines.txt | 4 ++ exercises/001-two-fer/two-names.txt | 2 + 8 files changed, 53 insertions(+), 6 deletions(-) rename exercises/001-two-fer/{empty.txt => empty-file.txt} (100%) create mode 100644 exercises/001-two-fer/empty-lines.txt create mode 100644 exercises/001-two-fer/one-name-and-empty-lines.txt rename exercises/001-two-fer/{one.txt => one-name.txt} (100%) create mode 100644 exercises/001-two-fer/two-names-and-empty-lines.txt create mode 100644 exercises/001-two-fer/two-names.txt diff --git a/exercises/001-two-fer/empty.txt b/exercises/001-two-fer/empty-file.txt similarity index 100% rename from exercises/001-two-fer/empty.txt rename to exercises/001-two-fer/empty-file.txt diff --git a/exercises/001-two-fer/empty-lines.txt b/exercises/001-two-fer/empty-lines.txt new file mode 100644 index 0000000..139597f --- /dev/null +++ b/exercises/001-two-fer/empty-lines.txt @@ -0,0 +1,2 @@ + + diff --git a/exercises/001-two-fer/one-name-and-empty-lines.txt b/exercises/001-two-fer/one-name-and-empty-lines.txt new file mode 100644 index 0000000..9a546c7 --- /dev/null +++ b/exercises/001-two-fer/one-name-and-empty-lines.txt @@ -0,0 +1,3 @@ + +Pierre + diff --git a/exercises/001-two-fer/one.txt b/exercises/001-two-fer/one-name.txt similarity index 100% rename from exercises/001-two-fer/one.txt rename to exercises/001-two-fer/one-name.txt diff --git a/exercises/001-two-fer/tests.bats b/exercises/001-two-fer/tests.bats index 2a32978..3bf93eb 100644 --- a/exercises/001-two-fer/tests.bats +++ b/exercises/001-two-fer/tests.bats @@ -18,14 +18,14 @@ setup() { } @test "empty file given" { - run awk -f $AWK_FILE $TEST_FOLDER/empty.txt + run awk -f $AWK_FILE $TEST_FOLDER/empty-file.txt assert_success assert_output "One for you, one for me." } @test "a name given" { - run awk -f $AWK_FILE $TEST_FOLDER/one.txt + run awk -f $AWK_FILE $TEST_FOLDER/one-name.txt assert_success assert_output "One for Alice, one for me." @@ -37,3 +37,38 @@ setup() { assert_success assert_output "One for Mary Ann, one for me." } + +@test "empty lines" { + run awk -f $AWK_FILE $TEST_FOLDER/empty-lines.txt + + assert_success + assert_line --index 0 "One for you, one for me." + assert_line --index 1 "One for you, one for me." +} + +@test "name with empty lines" { + run awk -f $AWK_FILE $TEST_FOLDER/one-name-and-empty-lines.txt + + assert_success + assert_line --index 0 "One for you, one for me." + assert_line --index 1 "One for Pierre, one for me." + assert_line --index 2 "One for you, one for me." +} + +@test "two names" { + run awk -f $AWK_FILE $TEST_FOLDER/two-names.txt + + assert_success + assert_line --index 0 "One for Alice, one for me." + assert_line --index 1 "One for Mary Ann, one for me." +} + +@test "two names and empty lines" { + run awk -f $AWK_FILE $TEST_FOLDER/two-names-and-empty-lines.txt + + assert_success + assert_line --index 0 "One for you, one for me." + assert_line --index 1 "One for Alice, one for me." + assert_line --index 2 "One for you, one for me." + assert_line --index 3 "One for Mary Ann, one for me." +} diff --git a/exercises/001-two-fer/two-fer.awk b/exercises/001-two-fer/two-fer.awk index bde42e7..6238372 100644 --- a/exercises/001-two-fer/two-fer.awk +++ b/exercises/001-two-fer/two-fer.awk @@ -1,4 +1,5 @@ -END { - name = length($0) ? $0 : "you"; - print "One for " name ", one for me." -} +# NF = Number of Fields in current record +# NR = Number of the current Record + +{ print "One for " (NF ? $0 : "you") ", one for me." } +END { if (!NR) print "One for you, one for me." } diff --git a/exercises/001-two-fer/two-names-and-empty-lines.txt b/exercises/001-two-fer/two-names-and-empty-lines.txt new file mode 100644 index 0000000..4bddb6f --- /dev/null +++ b/exercises/001-two-fer/two-names-and-empty-lines.txt @@ -0,0 +1,4 @@ + +Alice + +Mary Ann \ No newline at end of file diff --git a/exercises/001-two-fer/two-names.txt b/exercises/001-two-fer/two-names.txt new file mode 100644 index 0000000..73d7d86 --- /dev/null +++ b/exercises/001-two-fer/two-names.txt @@ -0,0 +1,2 @@ +Alice +Mary Ann \ No newline at end of file