Skip to content

Commit

Permalink
Tests added, need to fix repeat with nested elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
supernovus committed Sep 17, 2010
1 parent 51cf1e5 commit 2bd1877
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/Flower.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ method !parse-elements ($xml is rw) {
loop (my $i=0; True; $i++) {
if $i == $xml.nodes.elems { last; }
my $element = $xml.nodes[$i];
say "Processing: " ~ $element ~ ' = ' ~ $element.WHAT;
if $element !~~ Exemel::Element { next; } # skip non-elements.
self!parse-element($element);
## Now we clean up removed elements, and insert replacements.
if ! defined $element {
$xml.nodes.splice($i--, 1);
}
elsif $element ~~ Array {
$xml.nodes.splice($i--, 1, |$element);
say "Splicing an array";
$xml.nodes.splice($i--, 1, |@($element));
}
else {
$xml.nodes[$i] = $element; # Ensure the node is updated.
Expand Down Expand Up @@ -192,6 +194,7 @@ method !parse-repeat ($xml is rw, $tag) {
for @($array) -> $item {
my $newxml = $xml.clone;
%!data{$attrib} = $item;
say "Processing repeated item";
self!parse-element($newxml);
@elements.push: $newxml;
}
Expand Down
18 changes: 0 additions & 18 deletions lib/test.p6

This file was deleted.

32 changes: 32 additions & 0 deletions t/01-basics.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env perl6

BEGIN { @*INC.push: './lib' }

use Test;
use Flower;

plan 5;

my $xml = '<?xml version="1.0"?>';

my $template = '<test><item petal:define="test my_test_var" petal:content="test"/></test>';
my $flower = Flower.new(:template($template));
is $flower.parse(my_test_var => 'Hello World'), $xml~'<test><item>Hello World</item></test>', 'petal:define and petal:content';

$template = '<test><replaced petal:replace="hello">This will be replaced</replaced></test>';
$flower = Flower.new(:template($template));
is $flower.parse(hello => 'Hello World'), $xml~'<test>Hello World</test>', 'petal:replace';

$template = '<test><true petal:condition="hello">This is true</true><false petal:condition="notreal">This is false</false><right petal:condition="true:hello"/><wrong petal:condition="false:hello"/></test>';
$flower = Flower.new(:template($template));
is $flower.parse(hello => 'Hello World'), $xml~'<test><true>This is true</true><right/></test>', 'petal:condition';

$template = '<test><div petal:omit-tag="">Good <b petal:omit-tag="hello">Day</b> Mate</div></test>';
#$template = '<test><div petal:omit-tag="string:1">Good Day Mate</div></test>';
$flower = Flower.new(:template($template));
is $flower.parse(hello => 'hello world'), $xml~'<test>Good Day Mate</test>', 'petal:omit-tag';

$template = '<test><attrib petal:attributes="hello hello; cya goodbye"/></test>';
$flower = Flower.new(:template($template));
is $flower.parse(hello => 'Hello World', goodbye => 'Goodbye Universe'), $xml~'<test><attrib hello="Hello World" cya="Goodbye Universe"/></test>', 'petal:attributes';

25 changes: 25 additions & 0 deletions t/02-repeat.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env perl6

BEGIN { @*INC.push: './lib' }

use Test;
use Flower;

plan 2;

my $xml = '<?xml version="1.0"?>';

my $template = '<test><item petal:repeat="item items" petal:attributes="alt item/alt" petal:content="item/content"/></test>';
my $flower = Flower.new(:template($template));
my @items = (
{ :alt<One>, :content<First> },
{ :alt<Two>, :content<Second> },
{ :alt<Three>, :content<Third> },
);

is $flower.parse(:items(@items)), $xml~'<test><item alt="One">First</item><item alt="Two">Second</item><item alt="Three">Third</item></test>', 'petal:repeat';

$template = '<test><div petal:repeat="item items" petal:omit-tag=""><tr><td petal:content="item/alt"/></tr><tr><td petal:content="item/content"/></tr></div></test>';
$flower = Flower.new(:template($template));
is $flower.parse(:items(@items)), $xml~'<test><tr><td>One</td><td>First</td></tr><tr><td>Two</td><td>Second</td></tr><tr><td>Three</td><td>Third</td></tr></test>', 'petal:repeat with nested elements and omit-tag';

23 changes: 23 additions & 0 deletions t/03-custom-modifiers.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env perl6

## TODO: Separate out the string: parsing from the uc: parsing.
## Add a separate set of tests for Flower::Utils::Text, and one
## for all modifiers in the DefaultModifiers set.

BEGIN { @*INC.push: './lib' }

use Test;
use Flower;
use Flower::Utils::Text;

plan 1;

my $xml = '<?xml version="1.0"?>';

my $template = '<test><upper petal:content="uc:string:A test of ${name}, in uppercase."/></test>';
my $flower = Flower.new(:template($template));

$flower.add-modifiers(Flower::Utils::Text::all());

is $flower.parse(name => 'Flower'), $xml~'<test><upper>A TEST OF FLOWER, IN UPPERCASE.</upper></test>', 'string: and custom :uc modifiers';

0 comments on commit 2bd1877

Please sign in to comment.