From bc3a01aef9a58d5e22137dbcc38fec7abebfdb22 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 30 Apr 2013 16:10:03 +0200 Subject: [PATCH] Funktionen 'snakify' und 'camelify' nach SL::Util verschoben, gebugfixt, getestet --- SL/DB/Helper/Mappings.pm | 14 ++------------ SL/Util.pm | 31 ++++++++++++++++++++++++++++++- t/helper/camelify.t | 16 ++++++++++++++++ t/helper/snakify.t | 15 +++++++++++++++ 4 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 t/helper/camelify.t create mode 100644 t/helper/snakify.t diff --git a/SL/DB/Helper/Mappings.pm b/SL/DB/Helper/Mappings.pm index 35845a8ea..cc4d2abe1 100644 --- a/SL/DB/Helper/Mappings.pm +++ b/SL/DB/Helper/Mappings.pm @@ -3,6 +3,8 @@ package SL::DB::Helper::Mappings; use utf8; use strict; +use SL::Util qw(camelify); + require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names); @@ -157,18 +159,6 @@ sub db { die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?"; } -sub camelify { - my ($str) = @_; - $str =~ s/_+(.)/uc($1)/ge; - ucfirst $str; -} - -sub snakify { - my ($str) = @_; - $str =~ s/(? + +Returns C<$string> converted from underscore-style to +camel-case-style, e.g. for the string C it will +return C. + +L does the reverse. + +=item C + +Returns C<$string> converted from camel-case-style to +underscore-style, e.g. for the string C it will +return C. + +L does the reverse. + =back =head1 BUGS diff --git a/t/helper/camelify.t b/t/helper/camelify.t new file mode 100644 index 000000000..0b5f8e3c6 --- /dev/null +++ b/t/helper/camelify.t @@ -0,0 +1,16 @@ +use Test::More tests => 8; + +use strict; + +use lib 't'; + +use SL::Util qw(camelify); + +is(camelify('hello'), 'Hello', 'hello'); +is(camelify('hello_world'), 'HelloWorld', 'hello_world'); +is(camelify('hello_world_'), 'HelloWorld_', 'hello_world_'); +is(camelify('charlie_the_unicorn'), 'CharlieTheUnicorn', 'charlie_the_unicorn'); +is(camelify('_charlie_the_unicorn'), 'CharlieTheUnicorn', '_charlie_the_unicorn'); +is(camelify('hello__world'), 'HelloWorld', 'hello__world'); +is(camelify('hELLO'), 'HELLO', 'hELLO'); +is(camelify('hellO_worlD'), 'HellOWorlD', 'hellO_worlD'); diff --git a/t/helper/snakify.t b/t/helper/snakify.t new file mode 100644 index 000000000..2a301bf40 --- /dev/null +++ b/t/helper/snakify.t @@ -0,0 +1,15 @@ +use Test::More tests => 7; + +use strict; + +use lib 't'; + +use SL::Util qw(snakify); + +is(snakify('Hello'), 'hello', 'Hello'); +is(snakify('HelloWorld'), 'hello_world', 'helloWorld'); +is(snakify('HelloWorld_'), 'hello_world_', 'helloWorld_'); +is(snakify('charlieTheUnicorn'), 'charlie_the_unicorn', 'charlieTheUnicorn'); +is(snakify('_CharlieTheUnicorn'), '_charlie_the_unicorn', '_CharlieTheUnicorn'); +is(snakify('HEllo'), 'h_ello', 'HEllo'); +is(snakify('HELlo'), 'h_e_llo', 'HELlo'); -- 2.20.1