From 825cd884fc16e96892ad1463d8b6e1f10ecab725 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Fri, 4 Feb 2022 22:10:33 +0100 Subject: [PATCH] Adding Telegram along some tests and a fix to add into @INC lib in tests. --- lib/DoctorAgustin/Telegram.pm | 32 ++++++++++++++ t/00001-config.t | 83 +++++++++++++++++++++++++++++++++++ t/00002-telegram.t | 22 ++++++++++ 3 files changed, 137 insertions(+) create mode 100644 lib/DoctorAgustin/Telegram.pm create mode 100644 t/00001-config.t create mode 100644 t/00002-telegram.t diff --git a/lib/DoctorAgustin/Telegram.pm b/lib/DoctorAgustin/Telegram.pm new file mode 100644 index 0000000..769fafe --- /dev/null +++ b/lib/DoctorAgustin/Telegram.pm @@ -0,0 +1,32 @@ +package DoctorAgustin::Telegram; + +use v5.30.0; + +use strict; +use warnings; + +use Types::Standard qw/Str/; +use Params::ValidationCompiler qw(validation_for); + +{ + my $validator = validation_for( + params => { + telegram_token => { type => Str }, + } + ); + + sub new { + my $class = shift; + my $self = bless {}, $class; + my %params = $validator->(@_); + my $telegram_token = $params{telegram_token}; + $self->{telegram_token} = $telegram_token; + return $self; + } +} + +sub _telegram_token { + my $self = shift; + return $self->{telegram_token}; +} +1; diff --git a/t/00001-config.t b/t/00001-config.t new file mode 100644 index 0000000..2c7be13 --- /dev/null +++ b/t/00001-config.t @@ -0,0 +1,83 @@ +#!/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 'DoctorAgustin::Config'; +} + +{ + ## GIVEN + my $result = 'hola'; + my $readline_mockmodule = Test::MockModule->new('Term::ReadLine'); + my $logger_mockmodule = Test::MockModule->new('DoctorAgustin::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 = DoctorAgustin::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('DoctorAgustin::Logger'); + my $readline_mockobject = Test::MockObject->new; + my $new_home = Path::Tiny->tempdir; + my $config_dir = qq($new_home/.config/doctoragustin); + 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 = DoctorAgustin::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'; + +} diff --git a/t/00002-telegram.t b/t/00002-telegram.t new file mode 100644 index 0000000..42b64f1 --- /dev/null +++ b/t/00002-telegram.t @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use v5.30.0; + +use strict; +use warnings; + +use Test::Most tests => 3; +use Test::MockModule; +use Test::MockObject; + +BEGIN { + unshift @INC, 'lib'; + use_ok 'DoctorAgustin::Telegram'; +} + +{ + my $result = 'hola'; + my $telegram = DoctorAgustin::Telegram->new (telegram_token => $result); + ok $telegram->isa('DoctorAgustin::Telegram'), 'Telegram object created successfully.'; + is $telegram->_telegram_token, $result, 'Telegram token setup was successful.'; +}