#!/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"); }