From fbaa1a7ec431f88984c6cd2d4b79c4f917bb2e1b Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 29 Nov 2018 22:39:26 +0200 Subject: [PATCH 1/2] Add a test for multiple -k in sort. --- MANIFEST | 1 + t/data/sort/three-words.txt | 8 ++++++++ t/sort.t | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 t/data/sort/three-words.txt diff --git a/MANIFEST b/MANIFEST index 7fb5d310..c5122953 100644 --- a/MANIFEST +++ b/MANIFEST @@ -130,6 +130,7 @@ t/compile.t t/data/cat/cat-n-1.txt t/data/sort/ints1.txt t/data/sort/letters1.txt +t/data/sort/three-words.txt t/cat.t t/echo.t t/factor.t diff --git a/t/data/sort/three-words.txt b/t/data/sort/three-words.txt new file mode 100644 index 00000000..07a2a739 --- /dev/null +++ b/t/data/sort/three-words.txt @@ -0,0 +1,8 @@ +the wonderful unicorn +based little mint +the meta protocol +a little love +mooing persistent cat +mooing yodelling dog +row by row +column by pencil diff --git a/t/sort.t b/t/sort.t index ba723b15..044cbcfa 100644 --- a/t/sort.t +++ b/t/sort.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 2; +use Test::More tests => 3; sub _lines2re { @@ -26,6 +26,23 @@ sub _lines2re qr#\A$ints_re\z#ms, "integers sort" ); } +{ + my $lines_re = _lines2re( split/\n/,<<'EOF' ); +column by pencil +row by row +a little love +based little mint +the meta protocol +mooing persistent cat +the wonderful unicorn +mooing yodelling dog +EOF + + # TEST + like( scalar(`$^X -Ilib bin/sort -k 2 -k 1 t/data/sort/three-words.txt`), + qr#\A$lines_re\z#ms, "-k 2,1 sort" ); +} + __END__ =head1 COPYRIGHT & LICENSE From a584cba1cabd4eb94a4e541225ab07a9d8a54cc4 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 29 Nov 2018 22:54:47 +0200 Subject: [PATCH 2/2] Extract a function to avoid duplicate code. This is Refactoring / code cleanup. "Three strikes and you refactor". See: * https://refactoring.com/catalog/extractMethod.html * https://en.wikipedia.org/wiki/Code_refactoring * https://www.refactoring.com/ * https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/ --- t/sort.t | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/t/sort.t b/t/sort.t index 044cbcfa..42f6efb7 100644 --- a/t/sort.t +++ b/t/sort.t @@ -10,24 +10,44 @@ sub _lines2re return join( qq#\r?\n#, @_ ) . qq#\r?\n?#; } +sub test_sort { - my $letters_re = _lines2re(qw/ a b c d e f /); + local $Test::Builder::Level = $Test::Builder::Level + 1; + my ($args) = @_; - # TEST - like( scalar(`$^X -Ilib bin/sort t/data/sort/letters1.txt`), - qr#\A$letters_re\z#ms, "letters sort" ); + my $re = _lines2re( @{ $args->{lines} } ); + return like( + scalar(`$^X -Ilib bin/sort @{$args->{flags}} @{$args->{files}}`), + qr#\A$re\z#ms, $args->{blurb} ); } -{ - my $ints_re = _lines2re( 1 .. 100 ); +# TEST +test_sort( + { + blurb => "letters sort", + files => [qw( t/data/sort/letters1.txt )], + flags => [], + lines => [qw/ a b c d e f /], + } +); - # TEST - like( scalar(`$^X -Ilib bin/sort -n t/data/sort/ints1.txt`), - qr#\A$ints_re\z#ms, "integers sort" ); -} +# TEST +test_sort( + { + blurb => "integers sort", + files => [qw( t/data/sort/ints1.txt )], + flags => [qw/ -n /], + lines => [ 1 .. 100 ], + } +); -{ - my $lines_re = _lines2re( split/\n/,<<'EOF' ); +# TEST +test_sort( + { + blurb => "multiple -k sort", + files => [qw( t/data/sort/three-words.txt )], + flags => [qw/ -k 2 -k 1 /], + lines => [ split /\n/, <<'EOF'], column by pencil row by row a little love @@ -37,11 +57,8 @@ mooing persistent cat the wonderful unicorn mooing yodelling dog EOF - - # TEST - like( scalar(`$^X -Ilib bin/sort -k 2 -k 1 t/data/sort/three-words.txt`), - qr#\A$lines_re\z#ms, "-k 2,1 sort" ); -} + } +); __END__