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