ePages 6.11.0 - DE_EPAGES/Core/API/TestParameter.pm

Package DE_EPAGES::Core::API::TestParameter

Use this module to test function parameters. Different functions are available for testing different types of parameters. Range checks are available where appropriate.
All functions throw an error if any test condition fails.

Example
sub SetProductQuantity {
    my ($Basket, $ProductNo, $Quantity) = @_;
    TestObject( 'Basket', $Basket, 'DE_EPAGES::Order::API::Object::Basket' );
    TestString( 'ProductNo', $ProductNo );
    TestInteger( 'Quantity', $Quantity, 1, 100 );
    ...
}
@EXPORT_OK
TestInteger
TestFloat
TestString
TestDateTime
TestHash
TestArray
TestLoop
TestObject
TestPackage
TestIpAddress
TestEMailAddress
CheckEMailAddrFormat
ExtractAddrSpec
TestMultipleEMailAddresses
TestUrl
TestDomain
CheckDomainFormat

Functions

CheckDomainFormat
CheckEMailAddrFormat
ExtractAddrSpec
TestArray
TestDateTime
TestDomain
TestEMailAddress
TestFloat
TestHash
TestInteger
TestIpAddress
TestLoop
TestMultipleEMailAddresses
TestObject
TestPackage
TestString
TestUrl

CheckDomainFormat

Tests if the parameter $Domain is a valid domain name according to RFC1035. To test international domain names use DE_EPAGES::Core::API::IDN::IsValidIDN instead.

Syntax
$ErrorCode = CheckDomainFormat($Domain);
Example
$ErrorCode = CheckDomainFormat('epages.de'); # ok
$ErrorCode = CheckDomainFormat('müller.de'); # fail
Input
$Domain (string)
parameter for test
Output
$ErrorCode (string)
error code (or nothing if success)

CheckEMailAddrFormat

Tests if the parameter $EMailAddress is an valid e-mail address according to RFC $Rfc (see http://tools.ietf.org/html/rfc5322 etc.). Use one of the following values for $Rfc: 2821 - Simple Mail Transfer Protocol; excludes contrary to RFC: folding white space, \n, comments in addr-spec, [IP in brackets] 2822 - Internet Message Format; excludes contrary to RFC: folding white space, \n, comments in addr-spec, [IP in brackets] 5321 - Simple Mail Transfer Protocol 5322 - Internet Message Format Default is RFC2822. *2 support angle-addr (<wolf@epages.de>); *1 do not.

Syntax
CheckEMailAddrFormat($EMailAddress, $Rfc, $Debug);
Example
CheckEMailAddrFormat('max.mustermann@epages.de');
Input
$EMailAddress (string)
parameter for test
$Rfc (string)
specification for test
$Debug (scalar)
if set prints debug output
Output
$ErrorMsg (string)
error string (or nothing if success)

ExtractAddrSpec

Shortens $Address from angle-addr to addr-spec according to $Rfc. (see http://tools.ietf.org/html/rfc5322) Use one of the following values for $Rfc: 2822 - as defined by Mail::Address->parse($$Address) 5322 - RFC5322 Internet Message Format Default is RFC2822.

Syntax
($ErrorCode, $ErrorMsg) = ExtractAddrSpec(\$Address, $Rfc);
Example
my $Address = '"Wolf Filss" <wfilss@epages.de>';
($ErrorCode, $ErrorMsg) = ExtractAddrSpec( \$Address );
die "Error $ErrorCode ($ErrorMsg)" if defined $ErrorCode;
print "Email: $Address"; # wfilss@epages.de
Input
$Address (string)
e-mail address
$Rfc (string)
(optional, default: 2822) RFC specification
Return
$ErrorCode (string)
error code
$ErrorMsg (string)
error text (optional)

TestArray

Tests if $aValue is an array reference.

Syntax
TestArray($Name, $aValue);
Example
TestArray( 'Lines', $aLines );
Input
$Name (string)
parameter name
$aValue (array ref)
parameter value

TestDateTime

Tests if a string parameter is defined and contains at least one non - whitespace character. The function currently does not check the actual date or time format. Instead it maps directly to TestString.

Syntax
TestDateTime($Name, $DateTime);
Example
TestDateTime('TestDate', '2004-04-05 18:09:10');
Input
$Name (string)
parameter name (used for the error message)
$DateTime (DateTime object)
parameter value

TestDomain

Tests if the parameter $Domain is a valid domain name according to RFC1035. This function accepts domain names starting with a digit, which would be forbidden according to RFC1035. To test international domain names use DE_EPAGES::Core::API::IDN::IsValidIDN instead.

Syntax
TestDomain($Name, $Domain);
Example
TestDomain('Domain', 'epages.de'); # ok
TestDomain('Domain', 'müller.de'); # fail
Input
$Name (string)
parameter name
$Domain (string)
parameter for test

TestEMailAddress

Tests if the parameter $EMailAddress is an valid e-mail address according to RFC $Rfc (see http://tools.ietf.org/html/rfc5322 etc.). Use one of the following values for $Rfc: 2821 - Simple Mail Transfer Protocol; excludes contrary to RFC: unquoted or folding white space, \n, comments in addr-spec, [IP in brackets] 2822 - Internet Message Format; excludes contrary to RFC: unquoted or folding white space, \n, comments in addr-spec, [IP in brackets] 5321 - Simple Mail Transfer Protocol 5322 - Internet Message Format Default is RFC2822. *2 support angle-addr (<wolf@epages.de>); *1 do not.

Syntax
TestEMailAddress($Name, $EMailAddress, $Rfc);
Example
TestEMailAddress('PrivateEMail', 'max.mustermann@epages.de');
Input
$Name (string)
parameter name
$EMailAddress (string)
parameter for test
$Rfc (string)
specification for test
$Debug (scalar)
if set prints debug output

TestFloat

Tests if the parameter $Float is a floating point number.

Syntax
TestFloat( $Name, $Float, $MinValue, $MaxValue);
Example
TestFloat('TestFloat', -1.12E-34);
Input
$Name (string)
parameter name
$Float (float)
parameter value
$MinValue (integer)
minimum value
$MaxValue (integer)
maximum value

TestHash

Tests if the parameter is a hash and tests if all parameters have correct values.

Syntax
$result = TestHash($Name, $hParameter, $hParameterKeys, $NonUsedKeys);
Example
TestHash('hParameter', $hParameter, {'Number'=>'Integer;NotNull'});
Input
$Name (string)
parameter name (used for the error message)
$hParameter (hash for test)
ref.hash
$hParameterKeys (keys to test)
ref.hash
$NonUsedKeys (test for unused keys)
bool (0/1)

TestInteger

Tests if the parameter $Integer is an integer number. Tests the value range ($MinValue..$MaxValue) if not defined use default range (-2147483648..2147483647)

Syntax
TestInteger($Name, $Integer);
TestInteger($Name, $Integer, $MinValue, $MaxValue);
Example
TestInteger('TestNumber', 1233);
Input
$Name (string)
parameter name
$Integer (integer)
parameter value
$MinValue (integer)
minimum value
$MaxValue (integer)
maximum value

TestIpAddress

Tests if the parameter $IpAddress is an IpAddress.

Syntax
TestIpAddress($Name, $IpAddress);
Example
TestIpAddress('ClientIP', '123.120.34.254');
Input
$Name (string)
parameter name
$IpAddress (string)
parameter for test

TestLoop

Tests if each item of the loop has the correct keys. Tests array with TestArray and each item with TestHash.

Syntax
$result = TestLoop($ParameterName, $rhParameterKeys, $rahParameter, $NonUsedKeys);
Input
$ParameterName (loop name)
string
$rhParameterKeys (keys for loop)
ref.hash
$rahParameter (loop for test)
ref.array.hash
$NonUsedKeys (test for unused keys
created 09.11.2001 tmueller
lastchanges 09.11.2001 tmueller)
bool (0/1)

TestMultipleEMailAddresses

Tests if the parameter $EMailAddresses contains only valid email addresses.

Syntax
TestMultipleEMailAddresses($Name, $EMailAddresses);
Example
TestMultipleEMailAddresses('PrivateEMailAddresses', 'max.mustermann@epages.de; maria.musterfrau@epages.de');
Input
$Name (string)
parameter name
$EMailAddresses (string)
parameter for test
$Rfc (string)
specification for test
$Debug (scalar)
if set prints debug output

TestObject

Tests if the parameter $Object is defined and is a blessed object. Also tests if $Object is an instance of the class $ClassPackage using UNIVERSAL::isa().

Syntax
TestObject($Name, $Object);
TestObject($Name, $Object, $ClassPackage);
Input
$Name (string)
parameter name
$Object (object)
parameter value
$ClassPackage (string)
(optional) class package name

TestPackage

Tests if the package exists in @INC.

Syntax
TestPackage($PackageName);
Example
TestPackage('SharedLib::Error');
Input
$PackageName (package name)
string

TestString

Tests if a string parameter is defined and contains at least one non-space character. Optionally checks the minimum and/or maximum string length in characters. Throws an error if the parameter is not correct.

Syntax
TestString($Name, $String );
TestString($Name, $String ,$MinLength );
TestString($Name, $String ,$MinLength, $MaxLength );
Example
TestString('Keyword', $Keyword);
TestString('Keyword', $Keyword, 1, 30);
Input
$Name (string)
parameter name (used for the error message)
$String (string)
test value
$MinLength (integer)
(optional) minimum string length
$MaxLength (integer)
(optional) maximum string length

TestUrl

Tests if the parameter $Url is a url.

Syntax
TestUrl($Name, $Url);
Example
TestUrl('SubmitUrl', 'http://www.epages.de/index.html');
Input
$Name (string)
parameter name
$Url (string)
parameter for test