Skip to content

Commit

Permalink
* added tests for list and more tests for xhtml
Browse files Browse the repository at this point in the history
* fixed a very minor bug in xhtml
  • Loading branch information
patrickmcsweeney committed Jan 3, 2016
1 parent a4d52de commit 3a3ff10
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 4 deletions.
2 changes: 1 addition & 1 deletion perl_lib/EPrints/XHTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ sub input_field
}
if( $noenter )
{
push @opts, onKeyPress => 'return EPJS_block_enter( event )';
push @opts, onkeypress => 'return EPJS_block_enter( event )';
}

return $self->{repository}->xml->create_element( "input",
Expand Down
156 changes: 156 additions & 0 deletions tests/30_list.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use strict;
use utf8;
use Test::More tests => 17;
use Test::MockObject;

BEGIN { use_ok( "EPrints" ); }
BEGIN { use_ok( "EPrints::Test" ); }

my $dummy_cache_id = 1234;

my $db = Test::MockObject->new();

$db->set_always("cache", $dummy_cache_id);
$db->set_true("drop_cache");
$db->mock("get_dataobjs", sub {
my ( $self, $dataset, @ids ) = @_;

my @objs = ();

foreach my $id (@ids)
{
my $obj = Test::MockObject->new();
$obj->set_always("id", $id);
push @objs, $obj;
}

return @objs;
});

my $repo = Test::MockObject->new();

my $dataset = Test::MockObject->new();

$repo->set_always("get_database", $db);

my $ids = [3,4,6,9];

my %params = ( session=>$repo, dataset=>$dataset, ids=>$ids);

my $list = EPrints::List->new(%params);

ok(defined($list), "list instantiated with basic parameters");

ok( $list->count == scalar @{$ids}, "count returns the correct number of elements" );

ok( array_refs_match($list->ids, $ids), "ids sub returns the same ids we instatiated with");

ok( $list->get_dataset() == $dataset, "get dataset returns the dataset we gave it");

my $dataobj = $list->item(3);
ok( $dataobj->id == $ids->[3], "item gets the correct item from the list" );

my @objects = $list->slice;

ok( objects_have_ids(\@objects, $ids), "slice with no arguements returns all ids" );

@objects = $list->slice( 1,2 );
my $slice_ids = [$ids->[1], $ids->[2]];

ok( objects_have_ids(\@objects, $slice_ids), "slice cuts the objects correctly" );

my $ctx = {count=>0};

$list->map( sub {
my ( $repo, $dataset, $dataobj, $ctx ) = @_;

if($dataobj->id)
{
$ctx->{count}++;
}
}, $ctx);

ok($ctx->{count} == scalar @{$ids}, "map is called once for each item in the list and the context is passed through");

ok( $list->get_cache_id == undef, "an uncached list should have undef cache_id");

$list->cache;

ok( $db->called("cache"), "list->cache calls db->cache");

ok( $list->get_cache_id == $dummy_cache_id, "cache_id is stored correctly after cache is called");

$list->dispose;

ok( $list->get_cache_id == undef, "dispose removes the cache id");

my $list = EPrints::List->new(%params);

my $ids2 = [$ids->[2],13,14,15];
my %params2 = ( session=>$repo, dataset=>$dataset, ids=>$ids2);
my $list2 = EPrints::List->new(%params2);
my $union = $list->union($list2);

my @union_ids = (@{$ids2});
shift @union_ids;
@union_ids = (@{$ids}, @union_ids);

ok( array_refs_match( [$union->ids], \@union_ids ), "union joins the ids and does not duplicate overlapping ids" );

my $intersect = $list->intersect($list2);

ok( array_refs_match( [$intersect->ids], [$ids->[2]] ), "intersect returns only the overlapping ids" );

my $remainder = $list->remainder($list2);

my @remainder_ids = @{$ids};
delete $remainder_ids[2];

ok( array_refs_match( [$remainder->ids], \@remainder_ids ), "remainder subtracts items in the parameter list from the calling list");





sub objects_have_ids
{
my ($objects, $ids) = @_;

for( my $i=0; $i < scalar @{$objects}; $i++)
{
if($ids->[$i] != $objects->[$i]->id)
{
return 0;
}
}

return 1;
}

sub array_refs_match
{
my ($arr1, $arr2) = @_;

my $str1 = join(@{$arr1},",");
my $str2 = join(@{$arr2},",");

return $str1 eq $str2;

}

#sub reorder
#sub export
#sub render_description

#sub union
#sub remainder
#sub intersect
#sub map
#sub get_dataset
#sub cache
#sub get_cache_id
#sub dispose
#sub count
#sub item
#sub slice
#sub ids
82 changes: 79 additions & 3 deletions tests/93_api_xml.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl

use Test::More tests => 17;
use Test::More tests => 39;

use strict;
use warnings;
Expand Down Expand Up @@ -33,6 +33,15 @@
ok(defined($doc) && $doc->documentElement->nodeName eq "root", "parse_string");
ok($xml->is($doc->documentElement, "Element"), "is type matches Element");

my $ele = $doc->documentElement->firstChild->nextSibling;
ok($ele->nodeName eq "ele", "child node named correctly");
ok($ele->hasAttribute("attr"), "child node has attribute attr");
ok($ele->getAttribute("attr") eq "foo", "attribute has value foo");

my $content = $ele->firstChild->nextSibling->firstChild->nodeValue;

ok($content eq "content", "text node at the bottom of the tree is created correctly");

my $node;

$node = $xml->create_element( "ele", attr => "foo" );
Expand All @@ -47,8 +56,6 @@

my $xhtml = $repo->xhtml;

$node = $xhtml->hidden_field( "foo", "bar" );
ok(defined($node) && $node->getAttribute( "name" ) eq "foo" && $node->getAttribute( "value" ) eq "bar" && $node->getAttribute( "type" ) eq "hidden", "xhtml hidden field");
$node = $xml->create_element( "html" );
$node->appendChild( $xml->create_element( "script", type => 'text/javascript' ) );
$node->appendChild( $xml->create_element( "div" ) );
Expand All @@ -72,6 +79,63 @@
$clone = $xml->clone_node( $node );
ok( !$clone->hasChildNodes, "shallow clone doesn't clone children" );

my $url = "http://foo.bar/save/";
my $method = "POST";
my $form = $xhtml->form($method, $url);

ok( defined($form) && $form->nodeName eq "form", "\$xhtml->form produces a form element" );

ok( $form->getAttribute("method") eq "post", "form has its method attribute set correctly" );

ok( $form->getAttribute("action") eq $url, "form has its action attribute set correctly" );

$node = $xhtml->hidden_field( "foo", "bar" );
ok(defined($node) && $node->getAttribute( "name" ) eq "foo" && $node->getAttribute( "value" ) eq "bar" && $node->getAttribute( "type" ) eq "hidden", "xhtml hidden field");

my $name="foo";
my $value="bar";
my $input = $xhtml->input_field($name, $value);

ok( defined($input) && $input->nodeName eq "input", "\$xhtml input_field returns an input element" );

ok( $input->getAttribute("name") eq $name, "input element has its name attribute set correctly" );

ok( $input->getAttribute("value") eq $value, "input element has its value attribute set correctly" );

my $type = "radio";
my %opts = ( "type"=>"radio", "noenter"=>1 );
my $input_with_opts = $xhtml->input_field( $name, $value, %opts );


ok( defined($input_with_opts) && $input_with_opts->nodeName eq "input", "\$xhtml input_field returns an input element" );

ok( $input_with_opts->getAttribute("name") eq $name, "input element has its name attribute set correctly" );

ok( $input_with_opts->getAttribute("value") eq $value, "input element has its value attribute set correctly" );

ok( $input_with_opts->getAttribute("type") eq $type, "input has its type set correctly" );

ok( $input_with_opts->hasAttribute("onkeypress"), "input has an onkeypress attibute which hopefully stops people using the enter key in the input" );

$value = "Running a big test with lots of words in the \n box and checking that everything still renders out correctly";

my $textarea = $xhtml->text_area_field($name, $value);

ok( defined($textarea) && $textarea->nodeName eq "textarea", "XHTML text_area_field returns a textarea" );

ok( $textarea->getAttribute("name") eq $name, "text has the name attribute set correctly" );

my $text_area_contents = $textarea->firstChild->toString;

ok( $text_area_contents eq $value, "textarea contains a text node with the correct value" );

my $data_element = $xhtml->data_element($name, $value, "foo" => "bar");

ok($data_element->nodeName eq $name, "data_element has correct name");
ok($data_element->firstChild->toString eq $value, "data_element has correct value");
ok($data_element->hasAttribute("foo"), "data_element has attribute foo");
ok($data_element->getAttribute("foo") eq "bar", "data_element attribute value bar");

$node = eval { $xhtml->tree([ # dl
[ "fruit", # dt
[ "apple", "orange", ], # ul {li, li}
Expand All @@ -93,3 +157,15 @@
) };

ok( defined $node && $node->toString =~ /leopard/, "XHTML::tree" );

#sub action_button
#sub action_icon
#sub data_element
#sub to_text_dump
#sub page
#sub tabs
#sub tree2
#sub action_list
#sub action_definition_list
#sub doc_type

0 comments on commit 3a3ff10

Please sign in to comment.