Skip to content

Commit

Permalink
clearer testing of frame push/pop
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Nov 21, 2010
1 parent 4c60a24 commit e42e9d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/Global/Context.pm
Expand Up @@ -2,6 +2,7 @@ use strict;
use warnings;
package Global::Context;

use Carp ();
use Global::Context::Env::Basic;
use Global::Context::StackFrame::Trivial;

Expand Down Expand Up @@ -34,7 +35,7 @@ sub _export_context_glob {

sub ctx_init {
my ($arg) = @_;
confess("context has already been initialized") if $Object;
Carp::confess("context has already been initialized") if $Object;

$Object = Global::Context::Env::Basic->new($arg)->with_pushed_frame(
Global::Context::StackFrame::Trivial->new({
Expand Down
27 changes: 21 additions & 6 deletions t/simple.t
Expand Up @@ -7,6 +7,7 @@ use Global::Context::AuthToken::Basic;
use Global::Context::Terminal::Basic;

use Test::More;
use Test::Fatal;

ctx_init({
terminal => Global::Context::Terminal::Basic->new({ uri => 'ip://1.2.3.4' }),
Expand All @@ -16,23 +17,37 @@ ctx_init({
}),
});

like(
exception { ctx_init({}) },
qr/already/,
"we can't ctx_init twice",
);

{
my @frames = map {; $_->as_string } $Context->stack->frames;
is(@frames, 1);
like($frames[0], qr/^context initialized/);
is(@frames, 1, "there's one frame, to start with");
like($frames[0], qr/^context initialized/, '...it is the ctx_init frame');
}

{
local $Context = ctx_push("eat some pie");
my @frames = map {; $_->as_string } $Context->stack->frames;
is(@frames, 1);
is($frames[0], 'eat some pie');
is(@frames, 1, 'after pushing a frame, the first is gone');
is($frames[0], 'eat some pie', '...only our new frame remains');

{
local $Context = ctx_push("drink some coffee");
my @frames = map {; $_->as_string } $Context->stack->frames;
is(@frames, 2, 'after pushing another frame, we have two frames');
is($frames[0], 'eat some pie', '...0th frame is what we expect');
is($frames[1], 'drink some coffee', '...1st frame is what we expect');
}
}

{
my @frames = map {; $_->as_string } $Context->stack->frames;
is(@frames, 1);
like($frames[0], qr/^context initialized/);
is(@frames, 1, 'when we leave the frame-pushed context, still 1 stack left');
like($frames[0], qr/^context initialized/, '...the ctx_init frame again');
}

done_testing;

0 comments on commit e42e9d9

Please sign in to comment.