From f7c9686f61fbf8e6c32b229734b8763ef4cf8325 Mon Sep 17 00:00:00 2001 From: bernhard Date: Sat, 14 Dec 2024 14:57:00 +0100 Subject: [PATCH] Issue #3984: require Test::Differences for testing 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 --- bin/otobo.CheckModules.pl | 22 +++- cpanfile | 6 ++ cpanfile.docker | 3 + scripts/test/DynamicField/EditFieldRender.t | 109 ++++++++++++-------- 4 files changed, 90 insertions(+), 50 deletions(-) diff --git a/bin/otobo.CheckModules.pl b/bin/otobo.CheckModules.pl index b2e5c238ab..4a969751d0 100755 --- a/bin/otobo.CheckModules.pl +++ b/bin/otobo.CheckModules.pl @@ -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 = ( @@ -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. diff --git a/cpanfile b/cpanfile index 1dd3a3dab7..536dee377a 100644 --- a/cpanfile +++ b/cpanfile @@ -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 { @@ -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 { diff --git a/cpanfile.docker b/cpanfile.docker index 2ec88f5833..92a69cb7c6 100644 --- a/cpanfile.docker +++ b/cpanfile.docker @@ -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 { diff --git a/scripts/test/DynamicField/EditFieldRender.t b/scripts/test/DynamicField/EditFieldRender.t index 0022fa9dc7..b4938b606d 100644 --- a/scripts/test/DynamicField/EditFieldRender.t +++ b/scripts/test/DynamicField/EditFieldRender.t @@ -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 @@ -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;