]> git.proxmox.com Git - pve-common.git/blame - test/format_test.pl
extract PVE::Format from PVE::CLIFormatter for reuse
[pve-common.git] / test / format_test.pl
CommitLineData
e37d5924
DJ
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib '../src';
7use PVE::JSONSchema;
57b33852 8use PVE::CLIFormatter;
e37d5924
DJ
9
10use Test::More;
11use Test::MockModule;
12
13my $valid_configids = [
14 'aa', 'a0', 'a_', 'a-', 'a-a', 'a'x100, 'Aa', 'AA',
15];
16my $invalid_configids = [
17 'a', 'a+', '1a', '_a', '-a', '+a', 'A',
18];
19
20my $noerr = 1; # easier to test
21foreach my $id (@$valid_configids) {
22 is(PVE::JSONSchema::pve_verify_configid($id, $noerr), $id, 'valid configid');
23}
24foreach my $id (@$invalid_configids) {
25 is(PVE::JSONSchema::pve_verify_configid($id, $noerr), undef, 'invalid configid');
26}
27
57b33852
SR
28# test some string rendering
29my $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", 0, undef, ""],
35 ["duration", 40, undef, "40s"],
36 ["duration", 60, undef, "1m"],
37 ["duration", 110, undef, "1m 50s"],
38 ["duration", 7*24*3829*2, undef, "2w 21h 22m 24s"],
39 ["fraction_as_percentage", 0.412, undef, "41.20%"],
40 ["bytes", 0, undef, "0.00 B"],
41 ["bytes", 1023, 4, "1023.0000 B"],
42 ["bytes", 1024, undef, "1.00 KiB"],
43 ["bytes", 1024*1024*123 + 1024*300, 1, "123.3 MiB"],
44 ["bytes", 1024*1024*1024*1024*4 + 1024*1024*2048*8, undef, "4.02 TiB"],
45];
46
47foreach my $data (@$render_data) {
48 my ($renderer_name, $p1, $p2, $expected) = @$data;
49 my $renderer = PVE::JSONSchema::get_renderer($renderer_name);
50 my $actual = $renderer->($p1, $p2);
51 is($actual, $expected, "string format '$renderer_name'");
52}
53
54done_testing();