use warnings;
use Module::Load;
use Moose::Role;
+use TryCatch;
requires 'throw';
Accessor for the primary language of the tradition. Must correspond to one
of the Text::Tradition::Language::* modules in this package.
+=begin testing
+
+use Test::Warn;
+use TryCatch;
+use_ok( 'Text::Tradition' ); # with Language
+
+# Test setting and recovering language
+my $t = Text::Tradition->new( input => 'Self', file => 't/data/legendfrag.xml' );
+warning_like { $t->language( 'Klingon' ); } qr/^Cannot load language/,
+ "Got expected warning for setting of unsupported language";
+$t->language( 'English' );
+is( $t->language, 'English', "Successfully set supported language" );
+
+# Test bad attempt to lemmatize - proper lemmatization is tested separately
+my $bt = Text::Tradition->new( input => 'Self', file => 't/data/besoin.xml' );
+try {
+ $bt->lemmatize;
+ ok( 0, "Failed to throw error on lemmatizing without language" );
+} catch( Text::Tradition::Error $e ) {
+ is( $e->message, "Please set a language to lemmatize a tradition",
+ "Got correct error thrown for lemmatization without set language" );
+} catch {
+ ok( 0, "Unexpected error on bad lemmatization attempt" );
+}
+
+=end testing
+
=cut
has 'language' => (
if( @_ && $_[0] ne 'Default' ) {
# We are trying to set the language; check that the corresponding
# module exists.
- eval "require Text::Tradition::Language::".$_[0];
- if( $@ ) {
- throw( "Cannot load language module for @_: $@" );
+ try {
+ load( "Text::Tradition::Language::".$_[0] );
+ } catch ( $e ) {
+ warn( "Cannot load language module for @_: $e" );
}
}
};
sub lemmatize {
my $self = shift;
unless( $self->has_language ) {
- throw( "Please set a language to lemmatize a tradition" );
+ $self->throw( "Please set a language to lemmatize a tradition" );
}
my $mod = "Text::Tradition::Language::" . $self->language;
- load( $mod );
+ try {
+ load( $mod );
+ } catch ( $e ) {
+ $self->throw( "Cannot load language module for " . $self->language );
+ }
+ $self->throw( "Language module $mod has no lemmatize function" )
+ unless $mod->can( 'lemmatize' );
$mod->can( 'lemmatize' )->( $self );
}
--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+use Test::More 'no_plan';
+$| = 1;
+
+
+
+# =begin testing
+{
+use Test::Warn;
+use TryCatch;
+use_ok( 'Text::Tradition' ); # with Language
+
+# Test setting and recovering language
+my $t = Text::Tradition->new( input => 'Self', file => 't/data/legendfrag.xml' );
+warning_like { $t->language( 'Klingon' ); } qr/^Cannot load language/,
+ "Got expected warning for setting of unsupported language";
+$t->language( 'English' );
+is( $t->language, 'English', "Successfully set supported language" );
+
+# Test bad attempt to lemmatize - proper lemmatization is tested separately
+my $bt = Text::Tradition->new( input => 'Self', file => 't/data/besoin.xml' );
+try {
+ $bt->lemmatize;
+ ok( 0, "Failed to throw error on lemmatizing without language" );
+} catch( Text::Tradition::Error $e ) {
+ is( $e->message, "Please set a language to lemmatize a tradition",
+ "Got correct error thrown for lemmatization without set language" );
+} catch {
+ ok( 0, "Unexpected error on bad lemmatization attempt" );
+}
+}
+
+
+
+
+1;