I've just delivered a project for which the user needs to be able to contextually define which days of the week are considered "weekdays" and which are "weekend". The obvious (to me, at least) solution was to store a 7-character string (Sun-Sat) encoding "weekday" as 1 and "weekend" as 0 for each day of the week. The relevant sections of code being:
use constant WEEKDAY => 1; use constant WEEKEND => 0; ... sub is_weekday { my ($self, $date) = @_; my $dow = (localtime ($date || time))[6]; return substr $self->{weekdays}, $dow, 1 ? WEEKDAY : WEEKEND; }
and, to test this:
my %params = ( ... weekdays => '0011111', # Note that this sets Sun/Mon = weekend, Tue-Sat = weekd +ay ... ); ... is_deeply($obj, \%params, 'account type created with params'); is($obj->is_weekday(UnixDate(ParseDate('last sunday'), "%s")), 0, 'Sunday is weekend'); is($obj->is_weekday(UnixDate(ParseDate('last tuesday'), "%s")), 1, 'Tuesday is weekday');
This works perfectly on my development system. This also works perfectly on the first-line testing system which I installed the code on myself. However, the client installed this himself to a third system and is getting the following test failures:
root@system:/some/path# prove account_type.t account_type....NOK 27 # Failed test 'Sunday is weekend' # in account_type.t at line 69. # got: '1' # expected: '0' account_type....NOK 28 # Failed test 'Tuesday is weekday' # in account_type.t at line 71. # got: '0' # expected: '1'
(There are additional failed tests, but they are direct results of confusing weekends for weekdays and vice-versa. They do not appear likely to be due to separate bugs.)

The immediately preceding is_deeply should verify that the content of $obj->{weekdays} is '0011111', yet is_weekday is apparently interpreting the string's first character as true and the third character as false. Any ideas as to what I'm missing or why this might run correctly on the two machines I've touched while failing on a third? (I'm trying to get access to the machine where it's failing so that I can investigate more directly, but have not yet been able to arrange it.)

Edit: Added comment clarifying that the test is using a nonstandard concept of what's a weekday and what isn't, since that's caused a little confusion for those trying to help. Sorry about that.


In reply to Inscrutable test failure by dsheroh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.