Skip to content

Commit

Permalink
Add initial tests for MoonPhase
Browse files Browse the repository at this point in the history
Before moving on to CoverImage, finish writing tests for MoonPhase.
Figured out how to do UI testing using Selenium 2.0 and
Selenium::Remote::Driver.  Just need to make that work with Test::Mojo;
@kraih and @tempire hints about rolling a daemon on my own instead of
using Test::Mojo->test_server and using Test::Fork to do blocking IO.
  • Loading branch information
zakame committed Aug 27, 2011
1 parent 588e904 commit e5d3532
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
8 changes: 2 additions & 6 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ following:
difficult as the practice calls for enhancing the app using
JavaScript, so I need to learn of ways to do proper testing...

*** TODO Figure out how to test JavaScript-enhanced webapps from Perl

** Getting Started

To run this webapp, you need Mojolicious as well as a bunch of modules
Expand All @@ -39,8 +37,8 @@ listed in the Makefile.PL; install them as needed.
You may run the webapp using Mojo::Server::Morbo with the following
command:

$ morbo script/mojoten
Server available at http://127.0.0.1:3000
: $ morbo script/mojoten
: Server available at http://127.0.0.1:3000

** The Models

Expand All @@ -61,6 +59,4 @@ of the page, as well as being able to handle bad input correctly (by
bailing out in case of bad input via Try::Tiny.) Still needs test
scripts though, at least for the Perl parts.

**** TODO Write tests for MoonPhase

*** TODO CoverImages: get some product cover images from Amazon
35 changes: 35 additions & 0 deletions t/controller/moonphase.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 22;
use Test::Mojo;
use Model::MoonPhase qw(illumination is_waxing);

BEGIN { use_ok 'Mojoten' or BAIL_OUT "Mojoten is broken" }

my $t = Test::Mojo->new('Mojoten');
$t->get_ok('/moonphase')->status_is(200)->content_like(qr/Moon Phase/i);

my $time = "42 days ago at 7am";

# default output (HTML)
$t->post_form_ok('/moonphase/check', {time => $time})->status_is(200)
->content_type_like(qr#text/html#)->element_exists('div.success')
->content_like(qr/.+full/);
$t->post_form_ok('/moonphase/check', {time => 'fail'})->status_is(200)
->content_type_like(qr#text/html#)->element_exists('div.error')
->content_like(qr/no date or time I can recognize!/);

# REST output (JSON)
my $json = {
time => $time,
illuminated => sprintf("%.1f", illumination($time) * 100),
phase => is_waxing($time) ? 'waxing' : 'waning'
};
$t->post_form_ok('/moonphase/check', {time => $time, format => 'json'})
->status_is(200)->content_type_is('application/json')
->json_content_is($json);
$t->post_form_ok('/moonphase/check', {time => 'fail', format => 'json'})
->status_is(200)->content_type_is('application/json')
->json_content_is(
{error => "What, that's no date or time I can recognize!"});
22 changes: 22 additions & 0 deletions t/model/moonphase.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env perl
use Modern::Perl;
use Astro::MoonPhase;
use Time::ParseDate;
use Test::More tests => 5;

BEGIN {
use_ok('Model::MoonPhase', qw(illumination is_waxing))
or BAIL_OUT "Can't see the moon in cloudy skies";
}

my $time = "42 days ago at 5pm";
is_deeply(
[Model::MoonPhase::moonphase($time)],
[phase(parsedate($time))],
"correct moon phase"
);

my @phase = Model::MoonPhase::moonphase($time);
is(illumination($time), $phase[1], "correct moon illumination");
is(Model::MoonPhase::age($time), $phase[2], "correct moon age");
is(is_waxing($time), $phase[2] < (29.53 / 2), "moon is/is not waxing");

0 comments on commit e5d3532

Please sign in to comment.