IO-K8s

 view release on metacpan or  search on metacpan

t/150_k188_yaml_string_quoting.t  view on Meta::CPAN

        api_version     => 'k188.example.com/v1',
        resource_plural => 'widgets';
    with 'IO::K8s::Role::Namespaced';

    k8s cond  => '+Test::K188::Cond';
    k8s phase => Str, { enum => [qw(True False Unknown)] };
}

my @WORDS = ( qw(
    True False yes No on OFF y N ~ Null 0x1F 0o14 012 1_000 190:20:30
    .inf .NaN 1e3 +1 true null 123
) );

my @SCHEMAS = (
    [ 'YAML1_1' => [qw( YAML1_1 )] ],
    [ 'Core'    => [qw( Core )] ],
    [ 'JSON'    => [qw( JSON )] ]
);

my $json = JSON::PP->new->canonical->allow_nonref->allow_blessed->convert_blessed;

t/150_k188_yaml_string_quoting.t  view on Meta::CPAN


my %words = map { ( 'w' . $_ => $WORDS[$_] ) } 0 .. $#WORDS;

my $k8s = IO::K8s->new;

# --------------------------------------------------------------------
subtest 'preconditions: the words really are non-strings to YAML 1.1' => sub {
    # Guards the test itself: if the reader stopped resolving these, the
    # assertions below would pass vacuously.
    my $y11 = [qw( YAML1_1 )];
    for my $w (qw( True False yes No on OFF y N ~ Null 0x1F 012 1_000 190:20:30 .inf .NaN +1 true null 123 )) {
        my $got = load_as( "a: $w\n", $y11 )->{a};
        isnt( $json->encode($got), $json->encode($w), 'bare ' . $w . ' is not a string under YAML 1.1' );
    }
    # Observed with YAML::PP 0.41: its YAML1_1 loader keeps 0o14 and 1e3 as
    # strings (they are YAML 1.2 Core words); the Core reader does resolve them.
    for my $w (qw( 0o14 1e3 )) {
        my $got = load_as( "a: $w\n", [qw( Core )] )->{a};
        isnt( $json->encode($got), $json->encode($w), 'bare ' . $w . ' is not a string under Core' );
    }
};

t/150_k188_yaml_string_quoting.t  view on Meta::CPAN


    my $cm = $k8s->new_object( 'ConfigMap',
        metadata => { name => 'k188', namespace => 'default', labels => { flag => 'yes' } },
        data     => { %words, count => '10' }
    );
    my $dir  = tempdir( CLEANUP => 1 );
    my $file = $dir . '/cm.yaml';
    $cm->save($file);

    # Report the Python type name of each value: Python's JSON output cannot
    # represent .inf/.NaN, and the type is what matters here.
    my $py = 'import yaml,json,sys; d=yaml.safe_load(open(sys.argv[1])); '
        . 'print(json.dumps({"data":{k:type(v).__name__ for k,v in d["data"].items()},"label":d["metadata"]["labels"]["flag"]}))';
    open my $ph, '-|', 'python3', '-c', $py, $file or die 'cannot run python3: ' . $!;
    my $out = do { local $/; <$ph> };
    close $ph;
    my $got = JSON::PP->new->canonical->decode($out);
    my @non_string = grep { $got->{data}{$_} ne 'str' } sort keys %{ $got->{data} };
    is_deeply( \@non_string, [], 'PyYAML reads every data value as a str' );
    is( $got->{label}, 'yes', 'PyYAML reads label yes as the string yes' );
};



( run in 1.161 second using v1.01-cache-2.11-cpan-60884a8df76 )