From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <p.hufnagl@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 9814999B9A
 for <pve-devel@lists.proxmox.com>; Thu, 16 Nov 2023 16:18:07 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 7B19C1687C
 for <pve-devel@lists.proxmox.com>; Thu, 16 Nov 2023 16:18:07 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Thu, 16 Nov 2023 16:18:06 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 780444393D
 for <pve-devel@lists.proxmox.com>; Thu, 16 Nov 2023 16:18:06 +0100 (CET)
Message-ID: <14ca3cd4-9ba4-424f-a994-50efa4e8d7c0@proxmox.com>
Date: Thu, 16 Nov 2023 16:18:05 +0100
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Content-Language: en-US
To: pve-devel@lists.proxmox.com
References: <20231116135546.3408028-1-d.csapak@proxmox.com>
 <20231116135546.3408028-3-d.csapak@proxmox.com>
From: Philipp Hufnagl <p.hufnagl@proxmox.com>
In-Reply-To: <20231116135546.3408028-3-d.csapak@proxmox.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.054 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: Re: [pve-devel] [PATCH common v3 2/5] tools: add is_deeply
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 16 Nov 2023 15:18:07 -0000



On 11/16/23 14:55, Dominik Csapak wrote:
> to compare nested hashes/lists and scalar values recursively.
> Also includes some tests
> 
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
> new in v3, split out from section config
> changes to the is_deeply function:
> * incorporate thomas suggestions
> * fix sub call style
> * add comment to make it clear `ref` never returns undef
> 
>  src/PVE/Tools.pm        |  31 +++++++++
>  test/Makefile           |   1 +
>  test/is_deeply_tests.pl | 142 ++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 174 insertions(+)
>  create mode 100755 test/is_deeply_tests.pl
> 
> diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
> index b3af2c6..766c809 100644
> --- a/src/PVE/Tools.pm
> +++ b/src/PVE/Tools.pm
> @@ -2150,4 +2150,35 @@ sub get_file_hash {
>      return lc($digest);
>  }
>  
> +# compare two perl variables recursively, so this works for scalars, nested
> +# hashes and nested arrays
> +sub is_deeply {
> +    my ($a, $b) = @_;
> +
> +    return 0 if defined($a) != defined($b);
> +    return 1 if !defined($a); # both are undef
> +
> +    my ($ref_a, $ref_b) = (ref($a), ref($b));
> +
> +    # scalar case
> +    return 0 if !$ref_a && !$ref_b && "$a" ne "$b";
> +
> +    # different types, ok because ref never returns undef, only empty string
> +    return 0 if $ref_a ne $ref_b;
> +
> +    if ($ref_a eq 'HASH') {
> +	return 0 if scalar(keys $a->%*) != scalar(keys $b->%*);
> +	for my $opt (keys $a->%*) {
> +	    return 0 if !is_deeply($a->{$opt}, $b->{$opt});
> +	}
> +    } elsif ($ref_a eq 'ARRAY') {
> +	return 0 if scalar($a->@*) != scalar($b->@*);
> +	for (my $i = 0; $i < $a->@*; $i++) {
> +	    return 0 if !is_deeply($a->[$i], $b->[$i]);
> +	}
> +    }
> +
> +    return 1;
> +}
> +
>  1;
> diff --git a/test/Makefile b/test/Makefile
> index 82f40ab..b0de1a5 100644
> --- a/test/Makefile
> +++ b/test/Makefile
> @@ -6,6 +6,7 @@ TESTS = lock_file.test			\
>  	format_test.test		\
>  	section_config_test.test	\
>  	api_parameter_test.test		\
> +	is_deeply_test.test		\

I think the name is incorrect here. Did you meen "is_deeply_tests"
with 's' at the end?
>  
>  all:
>  
> diff --git a/test/is_deeply_tests.pl b/test/is_deeply_tests.pl
> new file mode 100755
> index 0000000..f546b36
> --- /dev/null
> +++ b/test/is_deeply_tests.pl
> @@ -0,0 +1,142 @@
> +#!/usr/bin/perl
> +
> +use lib '../src';
> +
> +use strict;
> +use warnings;
> +
> +use Test::More;
> +use PVE::Tools;
> +
> +my $tests = [
> +    {
> +	name => 'both undef',
> +	a => undef,
> +	b => undef,
> +	expected => 1,
> +    },
> +    {
> +	name => 'empty string',
> +	a => '',
> +	b => '',
> +	expected => 1,
> +    },
> +    {
> +	name => 'empty string and undef',
> +	a => '',
> +	b => undef,
> +	expected => 0,
> +    },
> +    {
> +	name => '0 and undef',
> +	a => 0,
> +	b => undef,
> +	expected => 0,
> +    },
> +    {
> +	name => 'equal strings',
> +	a => 'test',
> +	b => 'test',
> +	expected => 1,
> +    },
> +    {
> +	name => 'unequal strings',
> +	a => 'test',
> +	b => 'tost',
> +	expected => 0,
> +    },
> +    {
> +	name => 'equal numerics',
> +	a => 42,
> +	b => 42,
> +	expected => 1,
> +    },
> +    {
> +	name => 'unequal numerics',
> +	a => 42,
> +	b => 420,
> +	expected => 0,
> +    },
> +    {
> +	name => 'equal arrays',
> +	a => ['foo', 'bar'],
> +	b => ['foo', 'bar'],
> +	expected => 1,
> +    },
> +    {
> +	name => 'equal empty arrays',
> +	a => [],
> +	b => [],
> +	expected => 1,
> +    },
> +    {
> +	name => 'unequal arrays',
> +	a => ['foo', 'bar'],
> +	b => ['bar', 'foo'],
> +	expected => 0,
> +    },
> +    {
> +	name => 'equal empty hashes',
> +	a => { },
> +	b => { },
> +	expected => 1,
> +    },
> +    {
> +	name => 'equal hashes',
> +	a => { foo => 'bar' },
> +	b => { foo => 'bar' },
> +	expected => 1,
> +    },
> +    {
> +	name => 'unequal hashes',
> +	a => { foo => 'bar' },
> +	b => { bar => 'foo' },
> +	expected => 0,
> +    },
> +    {
> +	name => 'equal nested hashes',
> +	a => {
> +	    foo => 'bar',
> +	    bar => 1,
> +	    list => ['foo', 'bar'],
> +	    properties => {
> +		baz => 'boo',
> +	    },
> +	},
> +	b => {
> +	    foo => 'bar',
> +	    bar => 1,
> +	    list => ['foo', 'bar'],
> +	    properties => {
> +		baz => 'boo',
> +	    },
> +	},
> +	expected => 1,
> +    },
> +    {
> +	name => 'unequal nested hashes',
> +	a => {
> +	    foo => 'bar',
> +	    bar => 1,
> +	    list => ['foo', 'bar'],
> +	    properties => {
> +		baz => 'boo',
> +	    },
> +	},
> +	b => {
> +	    foo => 'bar',
> +	    bar => 1,
> +	    list => ['foo', 'bar'],
> +	    properties => {
> +		baz => undef,
> +	    },
> +	},
> +	expected => 0,
> +    },
> +];
> +
> +for my $test ($tests->@*) {
> +    is (PVE::Tools::is_deeply($test->{a}, $test->{b}), $test->{expected}, $test->{name});
> +}
> +
> +done_testing();