CGI-Test

 view release on metacpan or  search on metacpan

lib/CGI/Test/Form.pm  view on Meta::CPAN

#
# Widget extraction routine: traverse the <FORM> tree and create an instance
# of CGI::Test::Form::Widget per encountered widget.  The dynamic type depends
# on the widget type, e.g. a button creates a CGI::Test::Form::Widget::Button
# object.
#
# Widgets are also sorted by type, and stored as object attribute:
#
#   buttons         all buttons
#	inputs        	text area, text fields, password fields
#	menus		    popup menus
#	radios		  	radio buttons
#	checkboxes	  	all checkboxes
#	hidden          all hidden fields
#	widgets         all widgets, whatever their type.
#
# The special attribute `radio_groups' is only built when there is at least
# one radio button.
#
# Although we extract ALL the widgets, caller is only interested in a
# specific list, given in $which.  Therefore, returns a list ref on that

lib/CGI/Test/Form/Widget.pm  view on Meta::CPAN

CGI::Test::Form::Widget - Ancestor of all form widget classes

=head1 SYNOPSIS

 # Deferred class, only heirs can be created

=head1 DESCRIPTION

The C<CGI::Test::Form::Widget> class is deferred.
It is an abstract representation of a <FORM> widget, i.e. a graphical control
element like a popup menu or a submit button.

Here is an outline of the class hierarchy tree, with the leading
C<CGI::Test::Form::> string stripped for readability, and a trailing C<*>
indicating deferred classes:

    Widget*
    . Widget::Box*
    . . Widget::Box::Check
    . . Widget::Box::Radio
    . Widget::Button*

lib/CGI/Test/Form/Widget.pm  view on Meta::CPAN


=over 4

=item C<form>

The C<CGI::Test::Form> to which this widget belongs.

=item C<gui_type>

A human readable description of the widget, as it would appear on a GUI,
like "popup menu" or "radio button".  Meant for logging only, not to
determine the object type.

=item C<name>

The CGI parameter name.

=item C<value>

The current CGI parameter value.

lib/CGI/Test/Form/Widget.pm  view on Meta::CPAN


Returns true for hidden fields, which have no graphical representation
by definition.

=item C<is_input>

Returns true for all input fields, where the user can type text.

=item C<is_menu>

Returns true for popup menus and scrolling lists.

=back

=head2 Miscellaneous Features

Although documented, those features are more targetted for internal use...

=over 4

=item C<delete>

lib/CGI/Test/Form/Widget/Menu.pm  view on Meta::CPAN

#
#  Copyright (c) 2001, Raphael Manfredi
#
#  You may redistribute only under the terms of the Artistic License,
#  as specified in the README file that comes with the distribution.
#

use Carp;

#
# This class models a FORM menu (either a popup or a scrollable list).
#

use base qw(CGI::Test::Form::Widget);

use Storable qw(dclone);

############################################################
#
# ->_parse_options
#

lib/CGI/Test/Form/Widget/Menu.pm  view on Meta::CPAN

        push @$values, $value;
        $known->{$value}++;    # help them spot dups
        if ($is_selected)
        {
            $selected->{$value}++;
            $count++;
        }
    }

    #
    # A popup menu needs to have at least one item selected.  We're the
    # user agent, and we get to choose which item we'll select implicitely.
    # Use the first listed value, if any.
    #

    if ($count == 0 && $this->is_popup() && @$values)
    {
        my $first = $values->[ 0 ];
        $selected->{$first}++;
        $count++;
        warn "implicitely selecting OPTION '%s' for SELECT NAME=\"%s\"",
          $first, $this->name();
    }

    $this->{selected_count} = $count;

lib/CGI/Test/Form/Widget/Menu.pm  view on Meta::CPAN

sub is_menu
{
    return 1;
}

#
# Predicates for menus
#

############################################################
sub is_popup
{
    confess "deferred";
}

############################################################
#
# ->is_selected
#
# Checks whether given value is selected.
#

lib/CGI/Test/Form/Widget/Menu.pm  view on Meta::CPAN


CGI::Test::Form::Widget::Menu - Abstract representation of a menu

=head1 SYNOPSIS

 # Inherits from CGI::Test::Form::Widget

=head1 DESCRIPTION

This class is the abstract representation of a menu from which one can choose
one or several items, i.e. either a popup menu or a scrollable list
(with possibly multiple selections).

There is an interface to query the selected items, get at the presented
labels and associated values, and naturally C<select()> or C<unselect()>
items.

=head1 INTERFACE

The interface is the same as the one described in L<CGI::Test::Form::Widget>,
with the following additions:

lib/CGI/Test/Form/Widget/Menu.pm  view on Meta::CPAN


=item C<select> I<value>

Mark the option I<value> as selected.  If C<multiple> is false, any
previously selected value is automatically unselected.

Note that this takes a I<value>, not a I<label>.

=item C<unselect> I<value>

Unselect an option I<value>.  It is not possible to do that on a popup
menu: you must C<select> another item to unselect any previously selected one.

=back

=head2  Menu Probing

=over 4

=item C<is_selected> I<value>

Test whether an option I<value> is currently selected or not.  This is
not testing a label, but a value, which is what the script will get back
eventually: labels are there for human consumption only.

=back

=head2 Widget Classification Predicates

There is an additional predicate to distinguish between a popup menu (single
selection mandatory) from a scrolling list (multiple selection allowed, and
may select nothing).

=over 4

=item C<is_popup>

Returns I<true> for a popup menu.

=back

=head2 Miscellaneous Features

Although documented, those features are more targetted for
internal use...

=over 4

lib/CGI/Test/Form/Widget/Menu/List.pm  view on Meta::CPAN


sub gui_type
{
    "scrolling list"
}

#
# Defined predicates
#

sub is_popup
{
    return 0;
}

1;

=head1 NAME

CGI::Test::Form::Widget::Menu::List - A scrolling list menu

lib/CGI/Test/Form/Widget/Menu/Popup.pm  view on Meta::CPAN

#
#  Copyright (c) 2001, Raphael Manfredi
#
#  You may redistribute only under the terms of the Artistic License,
#  as specified in the README file that comes with the distribution.
#

use Carp;

#
# This class models a FORM popup menu.
#

use base qw(CGI::Test::Form::Widget::Menu);

#
# %attr
#
# Defines which HTML attributes we should look at within the node, and how
# to translate that into class attributes.
#

lib/CGI/Test/Form/Widget/Menu/Popup.pm  view on Meta::CPAN

    my ($node) = shift;
    $this->_parse_attr($node, \%attr);
    $this->_parse_options($node);
    return;
}

#
# ->set_selected		-- redefined
#
# Change "selected" status for a menu value.
# We can only "select" values from a popup, never unselect one.
#
sub set_selected
{
    my $this = shift;
    my ($value, $state) = @_;

    unless ($state)
    {
        carp "cannot unselect value \"%s\" from popup $this", $value;
        return;
    }

    return $this->SUPER::set_selected($value, $state);
}

#
# Attribute access
#

sub gui_type
{
    return "popup menu";
}

#
# Defined predicates
#

sub is_popup
{
    return 1;
}

1;

=head1 NAME

CGI::Test::Form::Widget::Menu::Popup - A popup menu

=head1 SYNOPSIS

 # Inherits from CGI::Test::Form::Widget::Menu
 # $form is a CGI::Test::Form

 my $action = $form->menu_by_name("action");
 $action->select("reboot");

=head1 DESCRIPTION

This class models a popup menu, from which one item at most may be selected,
and for which there is at least one item selected, i.e. where exactly one
item is chosen.

If no item was explicitely selected, C<CGI::Test> arbitrarily chooses the
first item in the popup (if not empty) and warns you via C<warn>.

=head1 INTERFACE

The interface is the same as the one described in
L<CGI::Test::Form::Widget::Menu>.

=head1 AUTHORS

The original author is Raphael Manfredi.

t/02_parsing.t  view on Meta::CPAN

for my $type ( qw/ inputs buttons menus checkboxes / ) {
    my $want = shift @wants;
    my $have = $form->$type;

    is @$have, $want, "Number of $type in form";
}

my $months = $form->menu_by_name("months");

ok defined $months, "Months menu defined";
ok !$months->is_popup, "Months menu is not popup";
is $months->selected_count, 1, "Months menu selected count";
is @{$months->option_values}, 12, "Months menu option values";
ok $months->is_selected("Jul"), "Months menu Jul is selected";
ok !$months->is_selected("Jan"), "Months menu Jan is not selected";

my $color = $form->menu_by_name("color");

ok  defined $color, "Color menu defined";
ok $color->is_popup, "Color menu is popup";
ok $color->is_selected("white"), "Color menu implicit selection";
is $color->selected_count, 1, "Color menu selected count";
is $color->option_values->[0], "white", "Color menu option value";
ok !$color->is_selected("black"), "Color menu black is not selected";

my @menus = $form->widgets_matching(sub { $_[0]->is_menu });

is @menus, 2, "Number of menus";

my @radio = $form->radios_named("title");

t/cgi/getform  view on Meta::CPAN

	-defaults	=> ['listening'],
) . br;

$content .= "New here: " . checkbox(
	-name		=> "new",
	-checked	=> 1,
	-value		=> "ON",
	-label		=> "click me",
) . br;

$content .= "Color: " . popup_menu(
	-name		=> "color",
	-values		=> [qw(white black green red blue)],
	-default	=> "white",
) . br;

$content .= "Note: " . textarea("note") . br;

$content .= "Prefers: " . scrolling_list(
	-name		=> "months",
	-values		=> [qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)],

t/cgi/getform.bat  view on Meta::CPAN

	-defaults	=> ['listening'],
) . br;

$content .= "New here: " . checkbox(
	-name		=> "new",
	-checked	=> 1,
	-value		=> "ON",
	-label		=> "click me",
) . br;

$content .= "Color: " . popup_menu(
	-name		=> "color",
	-values		=> [qw(white black green red blue)],
	-default	=> "white",
) . br;

$content .= "Note: " . textarea("note") . br;

$content .= "Prefers: " . scrolling_list(
	-name		=> "months",
	-values		=> [qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)],



( run in 1.642 second using v1.01-cache-2.11-cpan-364913b4093 )