Skip to content

Commit

Permalink
Fixed some but not all test failures.
Browse files Browse the repository at this point in the history
Still needing RT#74426 to get classes inside nested modules. Might have
to restructure out of that to make things work if it's not fixed before
R*.
  • Loading branch information
mathw committed Jul 19, 2010
1 parent 26947dc commit aac6d97
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
1 change: 1 addition & 0 deletions README
Expand Up @@ -11,6 +11,7 @@ TODO
* Numeric fields with decimal separator and justification * Numeric fields with decimal separator and justification
* Numeric fields with thousands separators and justification * Numeric fields with thousands separators and justification
* Currencies * Currencies
* Rendering of Complex numbers (currently restricted to Real)
* Everything else * Everything else




Expand Down
2 changes: 1 addition & 1 deletion lib/Form.pm
Expand Up @@ -5,7 +5,7 @@ use Form::Grammar;
use Form::Actions; use Form::Actions;
use Form::Field; use Form::Field;


sub form(*@args is Scalar) returns Str is export { sub form(*@args is Scalar --> Str) is export {
my @lines; my @lines;
my $result = ''; my $result = '';


Expand Down
8 changes: 4 additions & 4 deletions lib/Form/Field.pm
Expand Up @@ -5,7 +5,7 @@ use Form::NumberFormatting;


# RAKUDO: Field is now a class, because overriding multis doesn't # RAKUDO: Field is now a class, because overriding multis doesn't
# work correctly from roles # work correctly from roles
class Field { our class Field {
has Bool $.block is rw; has Bool $.block is rw;
has Int $.width is rw; has Int $.width is rw;
has $.alignment is rw; has $.alignment is rw;
Expand Down Expand Up @@ -49,7 +49,7 @@ class Field {
} }
} }


class TextField is Field { our class TextField is Field {
has $.justify is rw; has $.justify is rw;




Expand Down Expand Up @@ -78,7 +78,7 @@ class TextField is Field {
} }
} }


class NumericField is Field { our class NumericField is Field {
has Num $.ints-width; has Num $.ints-width;
has Num $.fracs-width; has Num $.fracs-width;


Expand All @@ -91,7 +91,7 @@ class NumericField is Field {
} }
} }


class VerbatimField is Field { our class VerbatimField is Field {
multi method format(Str $data) { multi method format(Str $data) {
my @lines = $data.split("\n"); my @lines = $data.split("\n");
$.block or @lines = @lines[^1]; $.block or @lines = @lines[^1];
Expand Down
15 changes: 9 additions & 6 deletions lib/Form/NumberFormatting.pm
Expand Up @@ -13,18 +13,21 @@ Utility functions for formatting numbers in Form.pm.


=begin pod =begin pod
=head2 obtain-number-parts(Num $number) =head2 obtain-number-parts(Real $number)
Splits out the integer and non-integer components of a number. Returns a list of int part, float part. Splits out the integer and non-integer components of a number. Returns a list of int part, float part.
This should probably be done with mathematical operations rather than Str::split, but once floating-point gets involved it breaks, so that's a TODO until RAKUDO has the Rat type and we can do it sanely.
=end pod =end pod


sub obtain-number-parts(Num $number) { our sub obtain-number-parts(Real $number) {
my ($ints, $fractions) = (~$number).split(/\./); my $ints = $number.Int;
my $fractions = $number - $ints;

# it's much easier if we have this as an integer as it's rendered separately to the ints
$fractions.=abs;
$fractions *= 10 while $fractions.Int != $fractions;


return (+$ints, +$fractions); return ($ints, $fractions);
} }


=begin pod =begin pod
Expand Down
19 changes: 8 additions & 11 deletions lib/Form/TextFormatting.pm
Expand Up @@ -23,7 +23,7 @@ sub chop-first-word(Str $source is rw) returns Str {
} }
} }


sub fit-in-width(Str $text, Int $width) { our sub fit-in-width(Str $text, Int $width) {


my Str $fitted = ''; my Str $fitted = '';
my Str $remainder = $text; my Str $remainder = $text;
Expand Down Expand Up @@ -59,7 +59,7 @@ sub fit-in-width(Str $text, Int $width) {
} }




sub unjustified-wrap(Str $text, Int $width) { our sub unjustified-wrap(Str $text, Int $width) {
my $rem = $text; my $rem = $text;
my $line; my $line;


Expand All @@ -80,23 +80,23 @@ sub trim-ending-whitespace(Str $line) returns Str {
return $line.subst(/ <ws> $$ /, ''); return $line.subst(/ <ws> $$ /, '');
} }


sub left-justify(Str $line, Int $width, Str $space = ' ') returns Str { our sub left-justify(Str $line, Int $width, Str $space = ' ') returns Str {
if $line.chars < $width { if $line.chars < $width {
return $line ~ (($space x ($width - $line.chars) / $space.chars)); return $line ~ (($space x ($width - $line.chars) / $space.chars));
} }


return $line.substr(0, $width); return $line.substr(0, $width);
} }


sub right-justify(Str $line, Int $width, Str $space = ' ') returns Str { our sub right-justify(Str $line, Int $width, Str $space = ' ') returns Str {
if $line.chars < $width { if $line.chars < $width {
return ($space x (($width - $line.chars) / $space.chars)) ~ $line; return ($space x (($width - $line.chars) / $space.chars)) ~ $line;
} }


return $line.substr($line.chars - $width, $width); return $line.substr($line.chars - $width, $width);
} }


sub centre-justify(Str $line, Int $width, Str $space = ' ') returns Str { our sub centre-justify(Str $line, Int $width, Str $space = ' ') returns Str {
if $line.chars < $width { if $line.chars < $width {
my Int $to-add = $width - $line.chars; my Int $to-add = $width - $line.chars;
my Int $before = $to-add div 2; my Int $before = $to-add div 2;
Expand All @@ -109,7 +109,7 @@ sub centre-justify(Str $line, Int $width, Str $space = ' ') returns Str {
return $line.substr(0, $width); return $line.substr(0, $width);
} }


sub full-justify(Str $line, Int $width, Str $space = ' ') returns Str { our sub full-justify(Str $line, Int $width, Str $space = ' ') returns Str {
# TODO need a justify algorithm # TODO need a justify algorithm
# for now, do something entirely unsatisfactory # for now, do something entirely unsatisfactory
if $line.chars < $width { if $line.chars < $width {
Expand All @@ -118,11 +118,8 @@ sub full-justify(Str $line, Int $width, Str $space = ' ') returns Str {
my $words = @words.elems; my $words = @words.elems;
my @spaces = $space xx ($words - 1); my @spaces = $space xx ($words - 1);


# RAKUDO: reduce meta-op not in master post-ng my $words-width = [+] @words.map: *.chars;
#my $words-width = [+] @words.map: *.chars; my $spaces-width = [+] @spaces.map: *.chars;
my $words-width = @words.map({.chars}).reduce(&infix:<+>);
#my $spaces-width = [+] @spaces.map: *.chars;
my $spaces-width = @spaces.map({.chars}).reduce(&infix:<+>);


my $act-space = 0; my $act-space = 0;
while $words-width + $spaces-width < $width while $words-width + $spaces-width < $width
Expand Down
1 change: 0 additions & 1 deletion t/02-fieldobjects.t
@@ -1,4 +1,3 @@
#
use v6; use v6;
use Test; use Test;


Expand Down
42 changes: 21 additions & 21 deletions t/06-form.t
Expand Up @@ -5,43 +5,43 @@ use Form;


plan 21; plan 21;


ok(Form::form('a') eq "a\n", "Literal"); ok(form('a') eq "a\n", "Literal");
ok(Form::form('{<<}', 'a') eq "a \n", "Single left-line field"); ok(form('{<<}', 'a') eq "a \n", "Single left-line field");
ok(Form::form('{<<}a', 'a') eq "a a\n", "Single left-line field with literal after"); ok(form('{<<}a', 'a') eq "a a\n", "Single left-line field with literal after");
ok(Form::form('{>>}a', 'a') eq " aa\n", "Single right-line field with literal after"); ok(form('{>>}a', 'a') eq " aa\n", "Single right-line field with literal after");
ok(Form::form('{>>><<}{<<<}', 'a', 'b') eq " a b \n", "Single centred-line field with single left-line field after"); ok(form('{>>><<}{<<<}', 'a', 'b') eq " a b \n", "Single centred-line field with single left-line field after");
ok(Form::form('{<<}', 'a', '{>>}', 'b') eq "a \n b\n", "Two fields"); ok(form('{<<}', 'a', '{>>}', 'b') eq "a \n b\n", "Two fields");
ok(Form::form( ok(form(
'+----+', '+----+',
'|{<<}|', 'aa', '|{<<}|', 'aa',
'+----+' '+----+'
) eq "+----+\n|aa |\n+----+\n", "Two literals, one field"); ) eq "+----+\n|aa |\n+----+\n", "Two literals, one field");
dies_ok(-> { Form::form('{<<}{>>}', 'a') }, "Insufficient arguments"); dies_ok(-> { form('{<<}{>>}', 'a') }, "Insufficient arguments");
ok(Form::form('{<<<<<}', "The quick brown fox jumps over the lazy dog") eq "The \n", "Line field overflow"); ok(form('{<<<<<}', "The quick brown fox jumps over the lazy dog") eq "The \n", "Line field overflow");
# TODO: reformat these as here-documents for neatness - when Rakudo supports them # TODO: reformat these as here-documents for neatness - when Rakudo supports them
ok(Form::form('{[[[[[}', "The quick brown fox jumps over the lazy dog") eq "The \nquick \nbrown \nfox \njumps \nover \nthe \nlazy \ndog \n", "Block field overflow"); ok(form('{[[[[[}', "The quick brown fox jumps over the lazy dog") eq "The \nquick \nbrown \nfox \njumps \nover \nthe \nlazy \ndog \n", "Block field overflow");
ok( ok(
Form::form( form(
'{[[[[[[[[} {]]]]]]]]}', '{[[[[[[[[} {]]]]]]]]}',
"The quick brown fox", "jumps over the lazy dog" "The quick brown fox", "jumps over the lazy dog"
) )
eq eq
"The quick jumps over\nbrown fox the lazy\n dog\n", "The quick jumps over\nbrown fox the lazy\n dog\n",
"Multiple block overflow" "Multiple block overflow"
); );
ok(Form::form('{""}', "Boo\nYah") eq "Boo \nYah \n", "Literal block field"); ok(form('{""}', "Boo\nYah") eq "Boo \nYah \n", "Literal block field");


dies_ok({Form::form('{<<<<}')}, 'Too few arguments'); dies_ok({form('{<<<<}')}, 'Too few arguments');


# time for some numbers # time for some numbers


ok(Form::form('{>>.<}', 456.78) eq "456.78\n", "Simple numeric field"); ok(form('{>>.<}', 456.78) eq "456.78\n", "Simple numeric field");
ok(Form::form('{>>.<}', 56.7) eq " 56.7 \n", "Non-full simple numeric field"); ok(form('{>>.<}', 56.7) eq " 56.7 \n", "Non-full simple numeric field");
ok(Form::form('{>>.<}', 4567.89) eq "567.89\n", "Left-side overflow numeric field"); ok(form('{>>.<}', 4567.89) eq "567.89\n", "Left-side overflow numeric field");
ok(Form::form('{>>.<}', 56.789) eq " 56.78\n", "Right-side overflow numeric field"); ok(form('{>>.<}', 56.789) eq " 56.78\n", "Right-side overflow numeric field");


# Mixed numbers and text # Mixed numbers and text
ok(Form::form( ok(form(
'{[[[[[[} {>.<<<}', '{[[[[[[} {>.<<<}',
"Six short people went down to the sea", 6.78 "Six short people went down to the sea", 6.78
) )
Expand All @@ -53,7 +53,7 @@ ok(Form::form(
# Multiple numbers # Multiple numbers
my @nums = (4.5, 5.6, 6.78, 9.101); my @nums = (4.5, 5.6, 6.78, 9.101);
ok( ok(
Form::form( form(
'{>>.<<}', \@nums '{>>.<<}', \@nums
) )
eq eq
Expand All @@ -64,7 +64,7 @@ ok(
# Multiple strings # Multiple strings
my @strings = <one two three four>; my @strings = <one two three four>;
ok( ok(
Form::form( form(
'{>>>>>>}', '{>>>>>>}',
\@strings \@strings
) )
Expand All @@ -76,7 +76,7 @@ ok(
# Both! # Both!


ok( ok(
Form::form( form(
'{>>>>>>}|{>.<<}', '{>>>>>>}|{>.<<}',
\@strings, \@nums \@strings, \@nums
) )
Expand Down

0 comments on commit aac6d97

Please sign in to comment.