1#!/usr/bin/perl -w
2
3use strict;
4use warnings;
5
6use constant DEBUG => $ENV{DEBUG};
7use if DEBUG, 'Smart::Comments';
8
9use Test::More;
10use Finance::Quote;
11use Scalar::Util qw(looks_like_number);
12
13if (not $ENV{ONLINE_TEST}) {
14    plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
15}
16
17my %valid    = ('STN' => 'Stantec Inc.',
18                'BCE' => 'BCE Inc.',
19                'BMO' => 'Bank of Montreal'
20               );
21my @invalid  = ('BOGUS');
22my @symbols  = (keys %valid, @invalid);
23
24my $method   = 'tmx';    # Name of the target method for testing
25
26my %check    = (# Tests are called with (value_to_test, symbol, quote_hash_reference)
27                'success'  => sub {$_[0] == 1},
28                'name'     => sub {$_[0] eq $valid{$_[1]}},
29                'year_range' => sub {$_[0] =~ /[0-9.]+ - [0-9.]+/},
30                'exchange' => sub {$_[0] eq 'Toronto Stock Exchange'},
31                'symbol'   => sub {$_[0] =~ /^$_[1](:CA)?$/},
32                'high'   => sub {looks_like_number($_[0])},
33                'low'   => sub {looks_like_number($_[0])},
34                'open'   => sub {looks_like_number($_[0])},
35                'close'   => sub {looks_like_number($_[0])},
36               );
37my $q        = Finance::Quote->new();
38
39plan tests => 1 + %check*%valid + @invalid;
40
41my %quotes = $q->fetch($method, @symbols);
42ok(%quotes);
43
44### [<now>] quotes: %quotes
45
46foreach my $symbol (keys %valid) {
47  while (my ($key, $lambda) = each %check) {
48    ok($lambda->($quotes{$symbol, $key}, $symbol, \%quotes), "$key -> " . (defined $quotes{$symbol, $key} ? $quotes{$symbol, $key} : '<undefined>'));
49  }
50}
51
52foreach my $symbol (@invalid) {
53  ok((not $quotes{'BOGUS', 'success'}), 'failed as expected');
54}
55
56