Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the soft-fail tests to work with recent room versions #1149

Merged
merged 6 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/SyTest/Assertions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ our @EXPORT_OK = qw(
assert_ok
assert_eq
assert_deeply_eq
assert_elements_eq

assert_json_object
assert_json_keys
Expand Down Expand Up @@ -135,6 +136,55 @@ sub assert_deeply_eq
_assert_deeply_eq( $got, $want, undef, $name );
}

=head2 assert_elements_eq

assert_elements_eq( $got, $want, $name )

Fails the test if $got is not an array, or if the entries in $got are
not the same as those in $want (ignoring ordering).

Only a shallow comparison is made between elements (ie, if they are references,
they must be refs to exactly the same object).

=cut

sub assert_elements_eq
{
my ( $got, $want, $name ) = @_;

if( ref $got ne "ARRAY" ) {
croak "Expected an array for $name but got ${\ pp $got }";
}

# for quick lookup, build a hash of the entries in $got
my %got_elts = map { $_ => 1 } @$got;

# go through the entries in $want, checking if they are in %got_elts
# and crossing them off if so.
my @missing_elts;
foreach my $want_elt ( @$want ) {
if( exists $got_elts{ $want_elt } ) {
delete $got_elts{ $want_elt };
} else {
push @missing_elts, $want_elt;
}
}

my @msgs;
if( @missing_elts ) {
push @msgs, "Missing values: ${\ pp( @missing_elts ) }."
}

# any elements left in %got_elts must be absent from $want.
if( %got_elts ) {
push @msgs, "Extra values: ${\ pp( keys %got_elts ) }.";
}

if( @msgs ) {
croak "Mismatch for $name: ". join(" ", @msgs);
}
}

=head2 assert_json_object

assert_json_object( $obj )
Expand Down
Loading