Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reorganized tests, added more Text modifiers.

  • Loading branch information...
commit b3ab14f92de5edac5b4549a55587f8e58378fa99 1 parent 9dcac97
@supernovus authored
View
14 README
@@ -82,7 +82,19 @@ there are only a couple available, with a subset of the modifiers):
Sets marked with * have been partially implemented and are included with
the current version of Flower. Sets marked with ** are fully implemented.
-The URI set is not planned for inclusion (feel free to write it if you need it.)
+The interfaces to the modifiers included with Flower may not be compatible
+with the ones from Petal. For instance in Petal::Utils, you could call
+<div tal:replace="substr: $string 2 5 1">
+whereas with Flower::Utils::Text you would use:
+<div tal:replace="substr: 2,5,1 string">
+
+Full documentation for the usage of Flower and the Flower::Utils modifiers
+will be included in the docs/ folder in an upcoming release, until then
+there are comments in the libraries, and test files in t/ that show the
+proper usage.
+
+The URI set from Petal::Utils is not planned for inclusion,
+feel free to write it if you need it.
I'm sure new exciting libraries will be made adding onto these.
= Author: Timothy Totten
View
53 lib/Flower/Utils/Text.pm
@@ -8,17 +8,70 @@ our sub all() {
lowercase => &lower,
lower => &lower,
'lc' => &lower,
+ 'ucfirst' => &uc_first,
+ uc_first => &uc_first,
+ 'substr' => &sub_string,
+ 'printf' => &print_formatted,
+ 'sprintf' => &print_formatted,
};
return %modifiers;
}
+## Usage: uc: varname
our sub upper ($parent, $query, *%opts) {
my $result = $parent.query($query);
return $parent.process-query($result.uc, |%opts);
}
+## Usage: lc: varname
our sub lower ($parent, $query, *%opts) {
my $result = $parent.query($query);
return $parent.process-query($result.lc, |%opts);
}
+## Usage: ucfirst: varname
+our sub uc_first ($parent, $query, *%opts) {
+ my $result = $parent.query($query);
+ return $parent.process-query($result.ucfirst, |%opts);
+}
+
+## Usage: substr: opts string/variable
+## Opts: 1[,2][,3]
+## where 1 is the offset to start at,
+## 2 is the number of characters to keep,
+## and if 3 is true add an ellipsis (...) to the end.
+## E.g.: <div tal:content="substr: 3,5 'theendoftheworld'"/>
+## Returns: <div>endof</div>
+our sub sub_string ($parent, $query, *%opts) {
+ my ($params, $subquery) = $query.split(/\s+/, 2);
+ my @params = $params.split(',');
+ my $text = $parent.query($subquery);
+ if defined $text {
+ my $substr = $text.substr(|@params[0..1]);
+ if @params.elems gt 2 && @params[2] {
+ $substr ~= '...';
+ }
+ return $parent.process-query($substr, |%opts);
+ }
+}
+
+## Usage: printf: 'format' varname/path
+## The format needs to be surrounded by ' ' marks.
+## E.g.: <div tal:content="printf: '$%0.2f' '2.5'"/>
+## Returns: <div>$2.50</div>
+our sub print_formatted ($parent, $query, *%opts) {
+ my regex stringparam { ^ \'(.*?)\' \s+ }
+ if ($query ~~ /<stringparam=&stringparam>/) {
+ my $format = $/<stringparam>[0];
+ my $subquery = $query.subst(/<&stringparam>/, '');
+ my $text = $parent.query($subquery);
+ if $text {
+ my $formatted = sprintf($format, $text);
+ return $parent.process-query($formatted, |%opts);
+ }
+ }
+ else {
+ return $parent.process-query($parent.query($query), |%opts);
+ }
+}
+
View
13 t/03-custom-modifiers.t
@@ -4,20 +4,23 @@
## Add a separate set of tests for Flower::Utils::Text, and one
## for all modifiers in the DefaultModifiers set.
-BEGIN { @*INC.unshift: './lib' }
+BEGIN {
+ @*INC.unshift: './lib'; ## Standard lib.
+ @*INC.unshift: './t/lib'; ## Test libraries.
+}
use Test;
use Flower;
-use Flower::Utils::Text;
+use Example::Modifiers;
plan 1;
my $xml = '<?xml version="1.0"?>';
-my $template = '<test><upper tal:content="uc:string:A test of ${name}, in uppercase."/></test>';
+my $template = '<test><woah tal:replace="woah:crazy"/></test>';
my $flower = Flower.new(:template($template));
-$flower.add-modifiers(Flower::Utils::Text::all());
+$flower.add-modifiers(Example::Modifiers::all());
-is $flower.parse(name => 'Flower'), $xml~'<test><upper>A TEST OF FLOWER, IN UPPERCASE.</upper></test>', 'string: and custom :uc modifiers';
+is $flower.parse(crazy => 'hello world'), $xml~'<test>Woah, hello world, that\'s awesome!</test>', 'custom modifiers';
View
0  t/07-escape.t → t/05-escape.t
File renamed without changes
View
0  t/06-debug-modifier.t → t/06-utils-debug.t
File renamed without changes
View
0  t/05-perl-modifier.t → t/07-utils-perl.t
File renamed without changes
View
60 t/09-utils-text.t
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl6
+
+BEGIN { @*INC.unshift: './lib' }
+
+use Test;
+use Flower;
+
+plan 6;
+
+my $xml = '<?xml version="1.0"?>';
+
+## test 1
+
+my $template = '<test><upper tal:content="uc:string:A test of ${name}, in uppercase."/></test>';
+my $flower = Flower.new(:template($template));
+
+$flower.load-modifiers('Text');
+
+is $flower.parse(name => 'Flower'), $xml~'<test><upper>A TEST OF FLOWER, IN UPPERCASE.</upper></test>', 'uc: modifier';
+
+## test 2
+
+$template = '<test><lower tal:content="lc:string:I AM NOT YELLING"/></test>';
+
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test><lower>i am not yelling</lower></test>', 'lc: modifier';
+
+## test 3
+
+$template = '<test><ucfirst tal:replace="ucfirst:string:bob"/></test>';
+
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test>Bob</test>', 'ucfirst: modifier';
+
+## test 4
+
+$template = '<test><substr tal:replace="substr:3,5 \'theendoftheworld\'"/></test>';
+
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test>endof</test>', 'substr: modifier';
+
+## test 5
+
+$template = '<test><substr tal:replace="substr:3,5,1 \'theendoftheworld\'"/></test>';
+
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test>endof...</test>', 'substr: modifier with ellipsis';
+
+## test 6
+
+$template = '<test><printf tal:replace="printf: \'$%0.2f\' \'2.5\'"/></test>';
+
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test>$2.50</test>', 'printf: modifier';
+
View
15 t/lib/Example/Modifiers.pm
@@ -0,0 +1,15 @@
+module Example::Modifiers;
+
+our sub all() {
+ my %modifiers = {
+ woah => &woahize,
+ };
+ return %modifiers;
+}
+
+our sub woahize ($parent, $query, *%opts) {
+ my $result = $parent.query($query);
+ my $woah = "Woah, $result, that's awesome!";
+ return $parent.process-query($woah, |%opts);
+}
+

0 comments on commit b3ab14f

Please sign in to comment.
Something went wrong with that request. Please try again.