DoctorKarma/t/00001-config.t

84 lines
2.1 KiB
Perl

#!/usr/bin/env perl
use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 5;
use Test::MockModule;
use Test::MockObject;
use Path::Tiny;
use JSON;
BEGIN {
unshift @INC, 'lib';
use_ok 'DoctorKarma::Config';
}
{
## GIVEN
my $result = 'hola';
my $readline_mockmodule = Test::MockModule->new('Term::ReadLine');
my $logger_mockmodule = Test::MockModule->new('DoctorKarma::Logger');
my $readline_mockobject = Test::MockObject->new;
my $new_home = Path::Tiny->tempdir;
local $ENV{HOME} = qq($new_home);
$readline_mockmodule->mock(
new => sub {
return $readline_mockobject;
}
);
$logger_mockmodule->mock( _log => sub { shift; shift; say STDERR shift } );
$readline_mockobject->mock(
readline => sub {
return $result;
}
);
## WHEN
lives_ok {
my $config = DoctorKarma::Config->new;
my $telegram_token = $config->telegram_token;
## THEN
is $telegram_token, $result,
'Telegram token is recovered ok when creating the config.';
}
'The configuration is got without dying';
}
{
## GIVEN
my $result = 'hola';
my $readline_mockmodule = Test::MockModule->new('Term::ReadLine');
my $logger_mockmodule = Test::MockModule->new('DoctorKarma::Logger');
my $readline_mockobject = Test::MockObject->new;
my $new_home = Path::Tiny->tempdir;
my $config_dir = qq($new_home/.config/doctorkarma);
my $config_file = qq($config_dir/config.json);
local $ENV{HOME} = qq($new_home);
path($config_dir)->mkpath;
path($config_file)->spew( encode_json( { telegram_token => $result } ) );
$logger_mockmodule->mock( _log => sub { shift; shift; say STDERR shift } );
lives_ok {
## WHEN
my $config = DoctorKarma::Config->new;
my $telegram_token = $config->telegram_token;
## THEN
is $telegram_token, $result,
'Telegram token is recovered ok when config exists.';
}
'The configuration is got without dying';
}