Skip to content

Commit

Permalink
Issue #3984: require Test::Differences for testing
Browse files Browse the repository at this point in the history
Use Test::Differences in EditFieldRender.t.
Two workarounds were needed:
- load Test::Simple so that test counters are emitted
- compare the UTF-8 encoded strings, as otherwise there would be no line splitting
  • Loading branch information
bschmalhofer committed Dec 14, 2024
1 parent b05bfbc commit f7c9686
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 50 deletions.
22 changes: 17 additions & 5 deletions bin/otobo.CheckModules.pl
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,22 @@ =head1 DESCRIPTION
use utf8;

use File::Basename qw(dirname);
use FindBin qw($RealBin);
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';
use lib dirname($RealBin) . '/Custom';

# core modules
use Getopt::Long qw(GetOptions);
use Term::ANSIColor qw(color);
use Pod::Usage qw(pod2usage);
use Getopt::Long qw(GetOptions);
use Term::ANSIColor qw(color);
use Pod::Usage qw(pod2usage);
use Module::Metadata 1.000031 ();
use CPAN::Meta::Requirements 2.140 ();

# CPAN modules

# OTOBO modules
use Kernel::System::Environment ();
use Kernel::System::Environment ();
use Kernel::System::VariableCheck qw(IsHashRefWithData IsArrayRefWithData);

my %InstTypeToCMD = (
Expand Down Expand Up @@ -1052,6 +1052,18 @@ =head1 DESCRIPTION
ports => undef,
},
},
{
Module => 'Test::Differences',
Features => ['devel:test'],
VersionRequired => '>= 0.64', # 0.64 was released in 2015
Comment => 'show diff when comparing strings',
InstTypes => {
aptget => undef,
emerge => undef,
zypper => undef,
ports => undef,
},
},
);

# Sanity check.
Expand Down
6 changes: 6 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ feature 'devel:test', 'Modules for running the test suite' => sub {
# testing PSGI apps and URLs
requires 'Test2::Tools::HTTP';

# show diff when comparing strings
requires 'Test::Differences', '>= 0.64';

};

feature 'div:bcrypt', 'Support for feature div:bcrypt' => sub {
Expand Down Expand Up @@ -337,6 +340,9 @@ feature 'optional', 'Support for feature optional' => sub {
# testing PSGI apps and URLs
requires 'Test2::Tools::HTTP';

# show diff when comparing strings
requires 'Test::Differences', '>= 0.64';

};

feature 'performance:csv', 'Support for feature performance:csv' => sub {
Expand Down
3 changes: 3 additions & 0 deletions cpanfile.docker
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ requires 'Unicode::Collate';
# testing PSGI apps and URLs
requires 'Test2::Tools::HTTP';

# show diff when comparing strings
requires 'Test::Differences', '>= 0.64';

# };

# feature 'div:bcrypt', 'Support for feature div:bcrypt' => sub {
Expand Down
109 changes: 64 additions & 45 deletions scripts/test/DynamicField/EditFieldRender.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ use strict;
use warnings;
use utf8;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::MockTime qw(:all);
use Kernel::System::UnitTest::RegisterDriver;
# core modules
use Encode qw(encode); # workaround for Test::Differences

use vars (qw($Self));

use Kernel::Output::HTML::Layout;
# CPAN modules
use Test2::V0;
use Test::Simple; # workaround for Test::Differences
use Test::Differences qw(unified_diff eq_or_diff);

# OTOBO modules
use Kernel::System::UnitTest::MockTime qw(FixedTimeSet);
use Kernel::System::UnitTest::RegisterOM; # Set up $Kernel::OM
use Kernel::Output::HTML::Layout ();
use Kernel::System::VariableCheck qw(:all);

# get needed objects
Expand Down Expand Up @@ -3341,55 +3345,70 @@ EOF
},
);

# declare that eq_or_diff() should emit an unified diff
unified_diff();

# execute tests
for my $Test (@Tests) {

my $FieldHTML;
subtest $Test->{Name} => sub {

if ( IsHashRefWithData( $Test->{Config} ) ) {
my %Config = %{ $Test->{Config} };
my $FieldHTML;

if ( IsHashRefWithData( $Test->{Config}->{CGIParam} ) ) {
if ( IsHashRefWithData( $Test->{Config} ) ) {
my %Config = $Test->{Config}->%*;

# create a new CGI object to simulate a web request
my $WebRequest = CGI->new( $Test->{Config}->{CGIParam} );
if ( IsHashRefWithData( $Test->{Config}->{CGIParam} ) ) {

my $LocalParamObject = Kernel::System::Web::Request->new(
WebRequest => $WebRequest,
);
# create a new CGI object to simulate a web request
my $WebRequest = CGI->new( $Test->{Config}->{CGIParam} );

%Config = (
%Config,
ParamObject => $LocalParamObject,
);
}
$FieldHTML = $DFBackendObject->EditFieldRender(%Config);
}
else {
$FieldHTML = $DFBackendObject->EditFieldRender( %{ $Test->{Config} } );
}
if ( $Test->{Success} ) {
my $LocalParamObject = Kernel::System::Web::Request->new(
WebRequest => $WebRequest,
);

# Heredocs always have the newline, even if it is not expected
if ( $FieldHTML->{Field} !~ m{\n$} ) {
chomp $Test->{ExpectedResults}->{Field};
%Config = (
%Config,
ParamObject => $LocalParamObject,
);
}
$FieldHTML = $DFBackendObject->EditFieldRender(%Config);
}
else {
$FieldHTML = $DFBackendObject->EditFieldRender( %{ $Test->{Config} } );
}

$Self->IsDeeply(
$FieldHTML,
$Test->{ExpectedResults},
"$Test->{Name} | EditFieldRender()",
);
}
else {
$Self->Is(
$FieldHTML,
undef,
"$Test->{Name} | EditFieldRender() (should be undef)",
);
}
}
if ( $Test->{Success} ) {

# we don't need any cleanup
# Heredocs always have the newline, even if it is not expected
if ( $FieldHTML->{Field} !~ m{\n$} ) {
chomp $Test->{ExpectedResults}->{Field};
}

is(
$FieldHTML,
{
Field => T(),
Label => T(),
},
"EditFieldRender() gave the expected structure",
);

for my $Key (qw(Field Label)) {

# make sure to compare bytes, as otherwise there is a problem with splitting the string
# see https://github.com/DrHyde/perl-modules-Test-Differences/issues/30
eq_or_diff(
encode( 'UTF-8', $FieldHTML->{$Key} ),
encode( 'UTF-8', $Test->{ExpectedResults}->{$Key} ),
"EditFieldRender() gave the expected content for $Key",
);
}
}
else {
ok( !defined $FieldHTML, 'EditFieldRender failed, as expected' );
}
};
}

$Self->DoneTesting();
done_testing;

0 comments on commit f7c9686

Please sign in to comment.