xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test2-Suite/lib/Test2/Compare/String.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1package Test2::Compare::String;
2use strict;
3use warnings;
4
5use Carp qw/confess/;
6
7use base 'Test2::Compare::Base';
8
9our $VERSION = '0.000162';
10
11use Test2::Util::HashBase qw/input/;
12
13# Overloads '!' for us.
14use Test2::Compare::Negatable;
15
16sub stringify_got { 1 }
17
18sub init {
19    my $self = shift;
20    confess "input must be defined for 'String' check"
21        unless defined $self->{+INPUT};
22
23    $self->SUPER::init(@_);
24}
25
26sub name {
27    my $self = shift;
28    my $in = $self->{+INPUT};
29    return "$in";
30}
31
32sub operator {
33    my $self = shift;
34
35    return '' unless @_;
36    my ($got) = @_;
37
38    return '' unless defined($got);
39
40    return 'ne' if $self->{+NEGATE};
41    return 'eq';
42}
43
44sub verify {
45    my $self = shift;
46    my %params = @_;
47    my ($got, $exists) = @params{qw/got exists/};
48
49    return 0 unless $exists;
50    return 0 unless defined $got;
51
52    my $input  = $self->{+INPUT};
53    my $negate = $self->{+NEGATE};
54
55    return "$input" ne "$got" if $negate;
56    return "$input" eq "$got";
57}
58
591;
60
61__END__
62
63=pod
64
65=encoding UTF-8
66
67=head1 NAME
68
69Test2::Compare::String - Compare two values as strings
70
71=head1 DESCRIPTION
72
73This is used to compare two items after they are stringified. You can also check
74that two strings are not equal.
75
76B<Note>: This will fail if the received value is undefined, it must be defined.
77
78=head1 SOURCE
79
80The source code repository for Test2-Suite can be found at
81F<https://github.com/Test-More/Test2-Suite/>.
82
83=head1 MAINTAINERS
84
85=over 4
86
87=item Chad Granum E<lt>exodist@cpan.orgE<gt>
88
89=back
90
91=head1 AUTHORS
92
93=over 4
94
95=item Chad Granum E<lt>exodist@cpan.orgE<gt>
96
97=back
98
99=head1 COPYRIGHT
100
101Copyright 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
102
103This program is free software; you can redistribute it and/or
104modify it under the same terms as Perl itself.
105
106See F<http://dev.perl.org/licenses/>
107
108=cut
109