Commits

Toby Inkster committed e7b21dd

some basic tests using Ask::Callback

Comments (0)

Files changed (2)

+use strict;
+use warnings;
+use Test::More;
+
+use Ask;
+
+my @input;
+my @output;
+
+sub flush_buffers {
+	@input = @output = ();
+}
+
+my $ask = Ask->detect(
+	class           => 'Ask::Callback',
+	input_callback  => sub { shift @input },
+	output_callback => sub { push @output, $_[0] },
+);
+
+{
+	@input = 'Bob';
+	is(
+		$ask->entry(text => 'Bob, what is your name?'),
+		'Bob',
+	);
+	flush_buffers();
+}
+
+{
+	@input = 'y';
+	is(
+		!!$ask->question(text => 'Will this test pass?'),
+		!!1,
+	);
+	flush_buffers();
+}
+
+{
+	@input = qw( file1.txt file2.txt file3.txt file4.txt );
+	is(
+		$ask->file_selection(text => 'Enter "file1.txt"'),
+		'file1.txt',
+	);
+	is_deeply(
+		[ $ask->file_selection(
+			text     => 'Enter "file2.txt", "file3.txt" and "file4.txt"',
+			multiple => 1,
+		) ],
+		[ qw( file2.txt file3.txt file4.txt ) ],
+	);	
+	flush_buffers();
+}
+
+{
+	$ask->info(text => 'Argh!');
+	is(
+		$output[0],
+		'Argh!',
+	);
+	flush_buffers();
+}
+
+{
+	$ask->warning(text => 'Argh!');
+	is(
+		$output[0],
+		'WARNING: Argh!',
+	);
+	flush_buffers();
+}
+
+{
+	$ask->error(text => 'Argh!');
+	is(
+		$output[0],
+		'ERROR: Argh!',
+	);
+	flush_buffers();
+}
+
+done_testing;
+use strict;
+use warnings;
+use Test::More;
+
+use Ask;
+
+my @input;
+my @output;
+
+BEGIN {
+	package AskX::Method::Password;
+	use Moo::Role;
+	sub password {
+		my ($self, %o) = @_;
+		$o{hide_text} //= 1;
+		$o{text}      //= "please enter your password";
+		$self->entry(%o);
+	}
+};
+
+sub flush_buffers {
+	@input = @output = ();
+}
+
+my $ask = Ask->detect(
+	class           => 'Ask::Callback',
+	traits          => ['AskX::Method::Password'],
+	input_callback  => sub { shift @input },
+	output_callback => sub { push @output, $_[0] },
+);
+
+{
+	@input = 's3cr3t';
+	is(
+		$ask->password,
+		's3cr3t',
+	);
+	flush_buffers();
+}
+
+done_testing;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.