Add test for previously discovered bug.

This commit is contained in:
sergiotarxz 2024-06-27 12:12:50 +02:00
parent b7c23a5199
commit cf9aaf3541
6 changed files with 61 additions and 9 deletions

View File

@ -22,5 +22,10 @@ my $build = Module::Build->new(
'Pango' => 0,
'Crypt::URandom' => 0,
},
test_requires => {
'Test::MockModule' => 0,
'Test::Most' => 0,
'Test::MockObject' => 0,
}
);
$build->create_build_script;

View File

@ -14,7 +14,7 @@ use YAML::PP;
use JapaChar::DB;
use JapaChar::Characters;
use Pango;
use Crypt::URandom qw( urandom );
use JapaChar::Random;
use Glib::IO;
@ -58,9 +58,7 @@ sub _new_challenge($self, $window, $type = undef) {
$self->_create_main_menu($window);
return;
}
my $rng = urandom(4);
$rng = unpack 'L', $rng;
$rng = ($rng % 100) + 1;
my $rng = JapaChar::Random->new->get(1, 100);
if ($rng > 50) {
$self->_new_challenge_romanji($window, $type);
return;

View File

@ -10,6 +10,8 @@ use Path::Tiny;
use JSON;
use Data::Dumper;
use JapaChar::Random;
my $option_populated = 'populated_basic_characters';
require JapaChar::DB;
require JapaChar::Schema;
@ -68,7 +70,7 @@ sub get_4_incorrect_answers( $self, $char ) {
return \@bad_answers;
}
sub next_review_char( $self, $type = undef ) {
sub _next_review_char( $self, $type = undef ) {
my $basic_character_resultset =
JapaChar::Schema->Schema->resultset('BasicCharacter');
my @chars = $basic_character_resultset->search(
@ -90,22 +92,23 @@ sub next_review_char( $self, $type = undef ) {
}
sub next_char( $self, $type = undef ) {
my $next_review = $self->next_review_char($type);
my $next_learning = $self->next_learning_char($type);
my $next_review = $self->_next_review_char($type);
my $next_learning = $self->_next_learning_char($type);
if ( !defined $next_review ) {
return $next_learning;
}
if ( !defined $next_learning) {
return $next_review;
}
my $rng = int( rand(100) ) + 1;
my $rng = JapaChar::Random->new->get(1, 100);
if ( $rng > 20 ) {
return $next_learning;
}
return $next_review;
}
sub next_learning_char( $self, $type = undef ) {
sub _next_learning_char( $self, $type = undef ) {
$self->populate_basic_characters;
my $basic_character_resultset =
JapaChar::Schema->Schema->resultset('BasicCharacter');

18
lib/JapaChar/Random.pm Normal file
View File

@ -0,0 +1,18 @@
package JapaChar::Random;
use v5.38.2;
use strict;
use warnings;
use Moo;
use Crypt::URandom qw( urandom );
sub get($min = 1, $max = 100) {
my $rng = urandom(4);
$rng = unpack 'L', $rng;
$rng = ($rng % $max) + $min;
return $rng;
}
1;

28
t/01-characters.t Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env perl
use v5.38.2;
use strict;
use warnings;
use Test::Most tests => 2;
use Test::MockModule;
use Path::Tiny;
use File::Basename;
use lib dirname(dirname(__FILE__)).'/lib';
use JapaChar::DB;
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');
});
ok defined(JapaChar::Characters->new->next_char), 'The next char is defined.';
}

Binary file not shown.