Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

[SOLVED] Test::Deep regex on hash keys

by Zoffix (Acolyte)
on Dec 26, 2013 at 08:05 UTC ( #1068398=perlquestion: print w/replies, xml ) Need Help??

Zoffix has asked for the wisdom of the Perl Monks concerning the following question:

Solution: http://perlmonks.org/?node_id=1068407

---- Hey, I have a nested structure that I want to test. The problem that I'm having is I can't figure out how to tell Test::Deep to use a regex for hashref keys. In the example below, the "markets" hashref's keys can be any string, and ideally I want something like re('.') for the key.

EDIT: { There seems to be some confusion here. The only reason I said I wanted a re('.') for the key is because the key can be anything and I need to tell Test::Deep to inspect the value of that key. }

How do I tell it to do each hashref's value, akin to the array_each() for arrays? I'm not set in stone regarding a module, so if anyone has better suggestions to accomplish this, please tell me.
{ 'markets' => { re('.') => { 'primaryname' => re('.'), 'volume' => re('^[-+.\d]+$'), 'lasttradeprice' => re('^[-+.\d]+$'), 'marketid' => re('^\d+$'), 'secondarycode' => re('.'), 'primarycode' => re('.'), 'lasttradetime' => re('.'), 'label' => re('.'), 'secondaryname' => re('.'), 'buyorders' => array_each( { 'quantity' => re('^[-+.\d]+$'), 'price' => re('^[-+.\d]+$'), 'total' => re('^[-+.\d]+$'), }, ), 'sellorders' => array_each( { 'quantity' => re('^[-+.\d]+$'), 'price' => re('^[-+.\d]+$'), 'total' => re('^[-+.\d]+$'), }, ), 'recenttrades' => array_each( { 'quantity' => re('^[-+.\d]+$'), 'price' => re('^[-+.\d]+$'), 'total' => re('^[-+.\d]+$'), 'id' => re('.'), 'time' => re('.'), }, ), }, }, }

Replies are listed 'Best First'.
Re: Test::Deep regex on hash keys
by CountZero (Bishop) on Dec 26, 2013 at 09:06 UTC
    If you want "any string" then use /.+/ as the regex or </c>/.*/</c> if you want "any string, including the empty string". Of course that is very much the same as "anything goes" so one wonders why you even bother to check it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Test::Deep regex on hash keys
by LanX (Archbishop) on Dec 26, 2013 at 08:58 UTC
    Hash keys are always strings, you know that this test is useless?

    Could you plz show complete code using Test::Deep that fails?

    This is just a data structure, does it already fail?

    I suppose you need to explicitly use keys for such tests, you can't stuff a regex into a key slot cause they are stringyfied.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I know they're always strings. The problem is without specifying "any value for the key", I can't tell it to inspect the deeper structure of where it points. I don't have any code, because I don't know how to tell it to assume any string for the key is valid.
Re: Test::Deep regex on hash keys
by Zoffix (Acolyte) on Dec 26, 2013 at 09:44 UTC
    I actually just solved when I peeked at the Test::Deep's code. There's an undocumented hash_each() subroutine, so the code becomes:
    { 'markets' => hash_each( { 'primaryname' => re('.'), 'volume' => re('^[-+.\d]+$'), 'lasttradeprice' => re('^[-+.\d]+$'), 'marketid' => re('^\d+$'), 'secondarycode' => re('.'), 'primarycode' => re('.'), 'lasttradetime' => re('.'), 'label' => re('.'), 'secondaryname' => re('.'), 'buyorders' => array_each( { 'quantity' => re('^[-+.\d]+$'), 'price' => re('^[-+.\d]+$'), 'total' => re('^[-+.\d]+$'), }, ), 'sellorders' => array_each( { 'quantity' => re('^[-+.\d]+$'), 'price' => re('^[-+.\d]+$'), 'total' => re('^[-+.\d]+$'), }, ), 'recenttrades' => array_each( { 'quantity' => re('^[-+.\d]+$'), 'price' => re('^[-+.\d]+$'), 'total' => re('^[-+.\d]+$'), 'id' => re('.'), 'time' => re('.'), }, ), }, ), }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1068398]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2019-10-24 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?