From fca9e9c6331d4b39efd7091e47ae527c9147e36f Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 8 Jan 2008 20:43:39 +0000 Subject: [PATCH] Add a method that returns the regexp used to validate generated GUIDs. This should prevent the regexp from proliferating all through tests and code. Also, add a test for the method, which just checks that it returns a regexp. --- lib/WebGUI/Session/Id.pm | 16 +++++++++++++++- t/Session/Id.t | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/WebGUI/Session/Id.pm b/lib/WebGUI/Session/Id.pm index 63c82f225..b43959d40 100644 --- a/lib/WebGUI/Session/Id.pm +++ b/lib/WebGUI/Session/Id.pm @@ -20,6 +20,7 @@ use Digest::MD5; use Time::HiRes qw( gettimeofday usleep ); use MIME::Base64; +my $idValidator = qr/^[A-Za-z0-9_-]{22}$/; =head1 NAME @@ -55,6 +56,19 @@ sub DESTROY { } +#------------------------------------------------------------------- + +=head2 getValidator + +Get the regular expression used to validate generated GUIDs. This is just to prevent +regular expressions from being duplicated all over the place. + +=cut + +sub getValidator { + return $idValidator; +} + #------------------------------------------------------------------- =head2 generate @@ -137,7 +151,7 @@ Returns true if $idString is a valid WebGUI guid. sub valid { my ($self, $idString) = @_; - return $idString =~ m/^[A-Za-z0-9_-]{22}$/; + return $idString =~ m/$idValidator/; } diff --git a/t/Session/Id.t b/t/Session/Id.t index 43a87f4e6..49cd2d153 100644 --- a/t/Session/Id.t +++ b/t/Session/Id.t @@ -53,7 +53,7 @@ my @testSets = ( my $session = WebGUI::Test->session; -plan tests => scalar(@testSets) + 4; +plan tests => scalar(@testSets) + 5; # generate my $generateId = $session->id->generate(); @@ -79,3 +79,6 @@ foreach my $testSet (@testSets) { # is($session->id->toHex('wjabZsKOb7kBBSiO3bQwzA'), 'c2369b66c28e6fb90105288eddb430cc', 'toHex works'); + +my $re = $session->id->getValidator; +is( ref $re, 'Regexp', 'getValidator returns a regexp object');