Skip to content

Commit

Permalink
Ensure scalar text generators
Browse files Browse the repository at this point in the history
Text::Lorem 0.32 started returning lists in list context.  That breaks
expectations of users of this module that expected scalars.  Fixed so
that we always return scalars and documented accordingly.

Fixes #13.
  • Loading branch information
xdg committed Feb 26, 2021
1 parent 6586c07 commit e58e16c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Revision history for Data-Fake

{{$NEXT}}

[FIXED]

- Text generators like fake_words() are now guaranteed to return a scalar.
They did originally, but Text::Lorem 0.32 broke its API and started
returning lists in list context.

0.005 2020-10-01 16:59:05-04:00 America/New_York

[FIXED]
Expand Down
24 changes: 12 additions & 12 deletions lib/Data/Fake/Text.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ my $LOREM;
$generator = fake_words($n); # N "lorem" words, space separated
$generator = fake_words( fake_int(1, 3) ); # random number of them
Returns a generator that provides space-separated L<Text::Lorem> words.
The argument is the number of words to return (or a code reference to
provide the number of words); the default is one.
Returns a generator that provides space-separated L<Text::Lorem> words as a
single scalar value. The argument is the number of words to return (or a
code reference to provide the number of words); the default is one.
=cut

Expand All @@ -36,7 +36,7 @@ sub fake_words {
$count = 1 unless defined $count;
require Text::Lorem;
$LOREM ||= Text::Lorem->new;
return sub { $LOREM->words( _transform($count) ) };
return sub { scalar $LOREM->words( _transform($count) ) };
}

=func fake_sentences
Expand All @@ -45,9 +45,9 @@ sub fake_words {
$generator = fake_sentences($n); # N sentences
$generator = fake_sentences( fake_int(1, 3) ); # random number of them
Returns a generator that provides L<Text::Lorem> sentences. The argument
is the number of sentences to return (or a code reference to provide the
number of sentences); the default is one.
Returns a generator that provides L<Text::Lorem> sentences as a single
scalar value. The argument is the number of sentences to return (or a code
reference to provide the number of sentences); the default is one.
=cut

Expand All @@ -58,7 +58,7 @@ sub fake_sentences {
if $count == 0;
require Text::Lorem;
$LOREM ||= Text::Lorem->new;
return sub { $LOREM->sentences( _transform($count) ) };
return sub { scalar $LOREM->sentences( _transform($count) ) };
}

=func fake_paragraphs
Expand All @@ -67,9 +67,9 @@ sub fake_sentences {
$generator = fake_paragraphs($n); # N paragraph
$generator = fake_paragraphs( fake_int(1, 3) ); # random number of them
Returns a generator that provides L<Text::Lorem> paragraphs. The argument
is the number of paragraphs to return (or a code reference to provide the
number of paragraphs); the default is one.
Returns a generator that provides L<Text::Lorem> paragraphs as a single
scalar value. The argument is the number of paragraphs to return (or a
code reference to provide the number of paragraphs); the default is one.
=cut

Expand All @@ -78,7 +78,7 @@ sub fake_paragraphs {
$count = 1 unless defined $count;
require Text::Lorem;
$LOREM ||= Text::Lorem->new;
return sub { $LOREM->paragraphs( _transform($count) ) };
return sub { scalar $LOREM->paragraphs( _transform($count) ) };
}

1;
Expand Down
9 changes: 9 additions & 0 deletions t/text.t
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ subtest 'fake_paragraphs' => sub {
}
};

subtest 'scalar context' => sub {
my @words = fake_words(2)->();
is(scalar(@words), 1, "words");
my @sentences = fake_sentences(2)->();
is(scalar(@sentences), 1, "sentences");
my @paragraphs = fake_paragraphs(2)->();
is(scalar(@paragraphs), 1, "paragraphs");
};

done_testing;
# COPYRIGHT

Expand Down

0 comments on commit e58e16c

Please sign in to comment.