2024-06-27 12:12:50 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use v5.38.2;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2024-06-27 12:43:57 +02:00
|
|
|
use Test::Most tests => 5;
|
2024-06-27 12:12:50 +02:00
|
|
|
use Test::MockModule;
|
|
|
|
use Path::Tiny;
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
use lib dirname(dirname(__FILE__)).'/lib';
|
|
|
|
|
|
|
|
use JapaChar::DB;
|
2024-06-27 12:20:22 +02:00
|
|
|
use JapaChar::Random;
|
2024-06-27 12:43:57 +02:00
|
|
|
use JapaChar::Schema;
|
2024-06-27 12:12:50 +02:00
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
use_ok 'JapaChar::Characters';
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
my $mock_db = Test::MockModule->new('JapaChar::DB');
|
|
|
|
$mock_db->mock(_db_path => sub {
|
|
|
|
return path(__FILE__)->parent->child('all-learned-basic-characters.db');
|
|
|
|
});
|
2024-06-27 12:20:22 +02:00
|
|
|
my $mock_random = Test::MockModule->new('JapaChar::Random');
|
|
|
|
$mock_random->mock(get => sub {
|
|
|
|
return 100;
|
|
|
|
});
|
|
|
|
my $mock_characters = Test::MockModule->new('JapaChar::Characters');
|
2024-06-27 12:23:33 +02:00
|
|
|
my $next_review_char = undef;
|
2024-06-27 12:20:22 +02:00
|
|
|
$mock_characters->mock(_next_review_char => sub {
|
2024-06-27 12:23:33 +02:00
|
|
|
$next_review_char = $mock_characters->original('_next_review_char')->(@_);
|
|
|
|
return $next_review_char;
|
2024-06-27 12:20:22 +02:00
|
|
|
});
|
2024-06-27 12:23:33 +02:00
|
|
|
my $next_char = JapaChar::Characters->new->next_char;
|
|
|
|
ok defined($next_char), 'The next char is defined.';
|
2024-06-27 12:43:57 +02:00
|
|
|
is_deeply $next_char, $next_review_char, 'The next char is a review one when all characters are learned.';
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JapaChar::Schema::reset_schema();
|
|
|
|
JapaChar::DB::reset_dbh();
|
|
|
|
my $mock_db = Test::MockModule->new('JapaChar::DB');
|
|
|
|
my $tempdir = Path::Tiny->tempdir;
|
|
|
|
my $db_file = $tempdir->child('doesnt-exist.db');
|
|
|
|
$mock_db->mock(_db_path => sub {
|
|
|
|
return $db_file;
|
|
|
|
});
|
|
|
|
my $mock_random = Test::MockModule->new('JapaChar::Random');
|
|
|
|
$mock_random->mock(get => sub {
|
|
|
|
return 19;
|
|
|
|
});
|
|
|
|
my $mock_characters = Test::MockModule->new('JapaChar::Characters');
|
|
|
|
my $next_learning = undef;
|
|
|
|
$mock_characters->mock(_next_learning_char => sub {
|
|
|
|
$next_learning = $mock_characters->original('_next_learning_char')->(@_);
|
|
|
|
return $next_learning;
|
|
|
|
});
|
|
|
|
my $next_char = JapaChar::Characters->new->next_char;
|
|
|
|
ok defined($next_char), 'The next char is defined.';
|
|
|
|
is_deeply $next_char, $next_learning, 'The next char is a learning one when there is no progress.';
|
2024-06-27 12:12:50 +02:00
|
|
|
}
|