]> git.proxmox.com Git - pve-common.git/blob - test/format_test.pl
bump version to 8.1.2
[pve-common.git] / test / format_test.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib '../src';
7 use PVE::JSONSchema;
8 use PVE::CLIFormatter;
9
10 use Test::More;
11 use Test::MockModule;
12
13 my $valid_configids = [
14 'aa', 'a0', 'a_', 'a-', 'a-a', 'a'x100, 'Aa', 'AA',
15 ];
16 my $invalid_configids = [
17 'a', 'a+', '1a', '_a', '-a', '+a', 'A',
18 ];
19
20 my $noerr = 1; # easier to test
21 foreach my $id (@$valid_configids) {
22 is(PVE::JSONSchema::pve_verify_configid($id, $noerr), $id, 'valid configid');
23 }
24 foreach my $id (@$invalid_configids) {
25 is(PVE::JSONSchema::pve_verify_configid($id, $noerr), undef, 'invalid configid');
26 }
27
28 # test some string rendering
29 my $render_data = [
30 ["timestamp", 0, undef, "1970-01-01 01:00:00"],
31 ["timestamp", 1612776831, undef, "2021-02-08 10:33:51"],
32 ["timestamp_gmt", 0, undef, "1970-01-01 00:00:00"],
33 ["timestamp_gmt", 1612776831, undef, "2021-02-08 09:33:51"],
34 ["duration", undef, undef, "0s"],
35 ["duration", 0.3, undef, "0s"],
36 ["duration", 0, undef, "0s"],
37 ["duration", 40, undef, "40s"],
38 ["duration", 59.64432, undef, "1m"],
39 ["duration", 110, undef, "1m 50s"],
40 ["duration", 7*24*3829*2, undef, "2w 21h 22m 24s"],
41 ["fraction_as_percentage", 0.412, undef, "41.20%"],
42 ["bytes", 0, undef, "0.00 B"],
43 ["bytes", 1023, 4, "1023.0000 B"],
44 ["bytes", 1024, undef, "1.00 KiB"],
45 ["bytes", 1024*1024*123 + 1024*300, 1, "123.3 MiB"],
46 ["bytes", 1024*1024*1024*1024*4 + 1024*1024*2048*8, undef, "4.02 TiB"],
47 ];
48
49 foreach my $data (@$render_data) {
50 my ($renderer_name, $p1, $p2, $expected) = @$data;
51 my $renderer = PVE::JSONSchema::get_renderer($renderer_name);
52 my $actual = $renderer->($p1, $p2);
53 is($actual, $expected, "string format '$renderer_name'");
54 }
55
56 done_testing();