From bb50b299de382395bd2ff7099ae1f32db3fd7a7a Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 4 Apr 2020 20:37:11 +1300 Subject: Add test plan for Random --- test/test_random.t | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 test/test_random.t (limited to 'test/test_random.t') diff --git a/test/test_random.t b/test/test_random.t new file mode 100755 index 0000000..fc30336 --- /dev/null +++ b/test/test_random.t @@ -0,0 +1,72 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More tests => 10; +use Mock::CommandRegistry; + +use Plugin::Random; + +my $cr = new Mock::CommandRegistry; +my $register = sub { $cr->register(@_); }; +Plugin::Random->configure($register, undef, undef, undef); + +my %ctx = ( + irc => undef, + logger => undef, + who => undef, + where => undef, + ided => undef, + rest => undef, + no_reenter => undef, + args => undef +); + +# Test that all expected commands were registered +{ + for my $cmd (qw/shuffle choose/) { + my $is_registered = $cr->is_registered_to_owner("Plugin::Random", $cmd); + ok($is_registered, "registered command $cmd"); + } +} + +# Test that `choose` works on a singleton input +{ + my %ctx_m = %ctx; + $ctx_m{rest} = "singleton"; + my $result = $cr->run_owned("Plugin::Random", "choose", %ctx_m); + is($result, "singleton", "choose on singleton is the singleton"); +} + +# Test that `choose` chooses returns something from the input +{ + my %ctx_m = %ctx; + $ctx_m{rest} = "foo bar baz qux"; + my @items = split / /, $ctx_m{rest}; + my $result = $cr->run_owned("Plugin::Random", "choose", %ctx_m); + my $in_input = grep {$_ eq $result} @items; + ok($in_input, "chosen value was given as an option"); +} + +# Test that `shuffle` on singleton is that same singleton +{ + my %ctx_m = %ctx; + $ctx_m{rest} = "singleton"; + my $result = $cr->run_owned("Plugin::Random", "shuffle", %ctx_m); + is($result, "singleton", "shuffled singleton is the same"); +} + +# Test that `shuffle` on list has all the same elements. No more or less +{ + my %ctx_m = %ctx; + $ctx_m{rest} = "foo bar baz qux"; + my @expected_items = split / /, $ctx_m{rest}; + my $result = $cr->run_owned("Plugin::Random", "shuffle", %ctx_m); + my @results = split / /, $result; + + for my $item (@expected_items) { + ok((grep {$_ eq $item} @results), "$item carried over"); + } + + is(@results, @expected_items, "shuffled list same length as expected"); +} -- cgit v1.1