Chandra

 view release on metacpan or  search on metacpan

t/88_app_xs.t  view on Meta::CPAN


# --- stores ---
{
    my $app = mock_app();
    my $named = $app->store('named');
    isa_ok($named, 'Chandra::Store', 'a named store');
    is($app->store('named'), $named, 'and is kept');
    local $ENV{HOME} = $dir;
    my $real = Chandra::App->new(title => 'Test App');
    my $own = $real->store;
    like($own->path, qr{/test_app/store\.json$}, 'an unnamed store takes its name from the title');
    is($real->store, $own, 'and is kept too');

    my $bare = bare_app();
    like($bare->store->path, qr{/chandra/store\.json$}, 'with no title the store is chandra');
    my $odd = Chandra::App->new(title => '!!My App--Title 2!!');
    like($odd->store->path, qr{/my_app--title_2/store\.json$}, 'punctuation runs become single underscores, hyphens stay, and the ends are trimmed');
    my $dies = Chandra::App->new(title => 'Dies');
    {
        no warnings 'redefine';
        local *Chandra::App::title = sub { die "no title\n" };
        like($dies->store->path, qr{/chandra/store\.json$}, 'a title that dies gives chandra');
    }
}

# --- windows ---
{
    my $app = mock_app();
    is($app->window_count, 0, 'no windows yet');
    is_deeply([ $app->windows ], [], 'and none listed');
    ok(!defined $app->window_by_id('w1'), 'and none found');
    my $w = $app->open_window(title => 'First', id => 'w1', width => 200, height => 100);
    isa_ok($w, 'Chandra::Window', 'open_window');
    is($app->window_count, 1, 'one window');
    is($app->window_by_id('w1'), $w, 'found by id');
    ok(!defined $app->window_by_id('nope'), 'an unknown id is undef');
    push @{ $app->{_windows} }, undef, 'string', { noid => 1 };
    is_deeply([ grep { defined } $app->windows ], [ $w, 'string', { noid => 1 } ], 'windows skips the undef entry');
    ok(!defined $app->window_by_id('nope'), 'the odd entries do not match an id');
    is($app->on_close(sub { }), $app, 'on_close returns the app');
    $app->open_window(title => 'Second');
    is($app->window_count, 5, 'a second window is added to the list');
    $w->close;
}

# --- assets, clipboard, drag and drop, context menus ---
{
    my $app = mock_app();
    eval { $app->assets };
    like($@, qr/assets\(\) requires a root directory/, 'assets needs a root the first time');
    my $assets = $app->assets($dir, prefix => '/static');
    isa_ok($assets, 'Chandra::Assets', 'assets with a root');
    is($app->assets, $assets, 'and is kept');
    is($app->clipboard, 'Chandra::Clipboard', 'clipboard names its class');
    my $dd = $app->drag_drop;
    isa_ok($dd, 'Chandra::DragDrop', 'drag_drop');
    is($app->drag_drop, $dd, 'and is kept');
    is($app->on_file_drop(sub { }), $app, 'on_file_drop returns the app');
    is($app->drop_zone('#z', sub { }), $app, 'drop_zone returns the app');
    my $cm = $app->context_menu_instance;
    isa_ok($cm, 'Chandra::ContextMenu', 'context_menu_instance');
    is($app->context_menu_instance, $cm, 'and is kept');
    is($app->context_menu('.a', sub { [] }), $app, 'a context menu with a callback');
    is($app->context_menu('.b', [ { label => 'x', action => sub { } } ]), $app, 'one with items');
    is($app->context_menu('.c', 'neither'), $app, 'and one with neither attaches statically');
}

# --- splash ---
{
    my $app = mock_app();
    my @init;
    my $s = $app->splash(title => 'Loading', init => sub { push @init, $_[0] });
    isa_ok($s, 'Chandra::Splash', 'splash');
    is($init[0], $s, 'the init callback got the splash');
    @warned = ();
    $app->splash(title => 'Dies', init => sub { die "init boom\n" });
    like($warned[0], qr/splash init callback died: init boom/, 'a dying init warns');
    ok(defined $app->splash(title => 'Plain', init => 'not code'), 'an init that is not code is ignored');
    {
        no warnings 'redefine';
        local *Chandra::Splash::new = sub { undef };
        ok(!defined $app->splash(title => 'None'), 'no splash object gives undef');
    }
}

# --- a hole in a depends list is an unknown extension, checked when the
# table is next resolved, so this comes last ---
{
    my $app = mock_app();
    my $holes = []; $#$holes = 1; $holes->[0] = 'extone';
    eval { $app->extend_bridge('exthole', 'window.exthole = 1;', depends => $holes) };
    eval { $app->_inject_post_content_js(0) };
    like($@, qr/depends on unknown extension ''/, 'a hole in the depends list is an unknown extension when the scripts are generated');
}

done_testing;



( run in 1.835 second using v1.01-cache-2.11-cpan-60884a8df76 )