]> Dogcows Code - chaz/graphql-client/blob - t/cli.t
2fa501ceac917a4f2dc88bac39e0d127ea579208
[chaz/graphql-client] / t / cli.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use Test::Exception;
7 use Test::More;
8
9 use GraphQL::Client::CLI;
10
11 delete $ENV{GRAPHQL_CLIENT_OPTIONS};
12
13 subtest 'get_options' => sub {
14 my $expected = {
15 format => 'json:pretty',
16 help => undef,
17 manual => undef,
18 operation_name => undef,
19 outfile => undef,
20 query => 'bar',
21 transport => undef,
22 unpack => 0,
23 url => 'foo',
24 variables => undef,
25 version => undef,
26 };
27
28 my $r = GraphQL::Client::CLI->_get_options(qw{--url foo --query bar});
29 is_deeply($r, $expected, '--url and --query set options') or diag explain $r;
30
31 $r = GraphQL::Client::CLI->_get_options(qw{foo --query bar});
32 is_deeply($r, $expected, '--url is optional') or diag explain $r;
33
34 $r = GraphQL::Client::CLI->_get_options(qw{foo bar});
35 is_deeply($r, $expected, '--query is also optional') or diag explain $r;
36
37 {
38 local $ENV{GRAPHQL_CLIENT_OPTIONS} = '--url asdf --query "baz qux" --unpack';
39 local $expected->{query} = 'baz qux';
40 local $expected->{unpack} = 1;
41 $r = GraphQL::Client::CLI->_get_options(qw{--url foo});
42 is_deeply($r, $expected, 'options can come from GRAPHQL_CLIENT_OPTIONS') or diag explain $r;
43 }
44 };
45
46 subtest 'expand_vars' => sub {
47 my $r = GraphQL::Client::CLI::_expand_vars({
48 'foo.bar' => 'baz',
49 'foo.qux.muf' => 42,
50 'arr1[1].tut' => 'whatever',
51 'arr2[1][0].meh'=> 3.1415,
52 });
53 is_deeply($r, {
54 foo => {
55 bar => 'baz',
56 qux => {
57 muf => 42,
58 },
59 },
60 arr1 => [
61 undef,
62 {
63 tut => 'whatever',
64 }
65 ],
66 arr2 => [
67 undef,
68 [
69 {
70 meh => 3.1415,
71 },
72 ],
73 ],
74 }, 'expand all the vars') or diag explain $r;
75
76 throws_ok {
77 GraphQL::Client::CLI::_expand_vars({
78 'foo[0]' => 'baz',
79 'foo.bar' => 'muf',
80 });
81 } qr/^Conflicting keys/, 'throw if conflict between hash and array';
82
83 throws_ok {
84 GraphQL::Client::CLI::_expand_vars({
85 'foo' => 'baz',
86 'foo.bar' => 'muf',
87 });
88 } qr/^Conflicting keys/, 'throw if conflict between hash and scalar';
89 };
90
91 done_testing;
This page took 0.044755 seconds and 4 git commands to generate.