From a1f08b131529d7295678775997baadfe11944ff1 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 30 Oct 2020 12:09:36 +0200 Subject: [PATCH] Add a test for the oeis plugin. In the process I extracted a function. --- plugins/oeis.pm | 26 +++++++++++++++----------- t/oeis-plugin.t | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 t/oeis-plugin.t diff --git a/plugins/oeis.pm b/plugins/oeis.pm index 67d753c..dedf201 100644 --- a/plugins/oeis.pm +++ b/plugins/oeis.pm @@ -6,15 +6,13 @@ use CGI; use LWP::Simple; use WWW::Shorten 'TinyURL'; -sub { - my($said) = @_; - my $q = $said->{"body"}; +sub query_oeis { + my ($q)=@_; warn 1; if( $q =~ /^\s*(?:(?:help|wtf|\?|\:)\s*)?$/i ) { - print "see http://oeis.org/"; - return; + return([], ["see http://oeis.org/"]); } warn 2; my $uri = "http://oeis.org/search?q=" . CGI::escape($q)."&fmt=text"; @@ -24,8 +22,7 @@ sub { my $nrfound = $1; unless( /^%N (\S+) (.*)/m ) { - print "Reply from OEIS in unknown format 2"; - return; + return([ "Reply from OEIS in unknown format 2"],[]); } warn 3; my($anum, $title) = ($1, $2); @@ -35,21 +32,28 @@ sub { warn 3.5; if (1 == $nrfound) { my $outuri = sprintf "http://oeis.org/%s", $anum; - print sprintf "%s %.256s: %.512s", $outuri, $title, $elts; + return ([],[sprintf( "%s %.256s: %.512s", $outuri, $title, $elts)]); } else { my $outuri1 = "http://oeis.org/searchs?q=" . CGI::escape($q); warn 3.6; # my $outuri = makeashorterlink($outuri1) || $outuri1; - print sprintf "%s %.10s(1/%d) %.256s: %.512s", $outuri1, $anum, $nrfound, $title, $elts; + return([], [sprintf( "%s %.10s(1/%d) %.256s: %.512s", $outuri1, $anum, $nrfound, $title, $elts)]); } } elsif (/^no matches/mi) { - print "No matches found"; + return([], ["No matches found"]); warn 4 } else { warn 5; - print "Reply from OEIS in unknown format: $_"; + return([ "Reply from OEIS in unknown format: $_"], []); } } +sub { + my($said) = @_; + my $q = $said->{"body"}; + my ($err, $out) = query_oeis($q); + print "Error: @$err\n" if @$err; + print "@$out\n"; +} __DATA__ Search for a sequence in the On-Line Encyclopedia of Integer Sequences (http://tinyurl.com/2blo2w) Syntax, oeis 1,1,2,3,5 diff --git a/t/oeis-plugin.t b/t/oeis-plugin.t new file mode 100644 index 0000000..bcb17da --- /dev/null +++ b/t/oeis-plugin.t @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use Test::More tests => 1; +use lib '.'; +use plugins::oeis; + +# TEST +like( + (query_oeis("1,2,6,24"))[1][0], + qr#https?://oeis\.org/.*?Factorial numbers: n!#ms, + "factorials", +);