From c53d91dd209b74e794fed8aea3da1fa2e58ace12 Mon Sep 17 00:00:00 2001 From: Roy Storey Date: Mon, 26 Oct 2020 21:41:35 +1300 Subject: [PATCH] subtests in mojo/parameters.t --- t/mojo/parameters.t | 400 +++++++++++++++++++++++--------------------- 1 file changed, 211 insertions(+), 189 deletions(-) diff --git a/t/mojo/parameters.t b/t/mojo/parameters.t index 7cd5f2ff28..48743c0e3e 100644 --- a/t/mojo/parameters.t +++ b/t/mojo/parameters.t @@ -3,194 +3,216 @@ use Mojo::Base -strict; use Test::More; use Mojo::Parameters; -# Basic functionality -my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23'); -my $params2 = Mojo::Parameters->new('x', 1, 'y', 2); -is $params->to_string, 'foo=b%3Bar&baz=23', 'right format'; -is $params2->to_string, 'x=1&y=2', 'right format'; -is $params->to_string, 'foo=b%3Bar&baz=23', 'right format'; -is_deeply $params->pairs, ['foo', 'b;ar', 'baz', 23], 'right structure'; -$params->append(a => 4, a => 5, b => 6, b => 7); -is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7', 'right format'; -push @$params, c => 'f;oo'; -is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo', 'right format'; -is $params->remove('a')->to_string, 'foo=b%3Bar&baz=23&b=6&b=7&c=f%3Boo', 'right format'; - -# Clone -my $clone = $params->clone; -is "$params", "$clone", 'equal parameters'; -push @$clone, c => 9; -isnt "$params", "$clone", 'unequal parameters'; - -# Merge -$params = Mojo::Parameters->new('foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo'); -$params->merge($params2); -is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=2', 'right format'; -is $params2->to_string, 'x=1&y=2', 'right format'; -is $params->merge(baz => undef)->to_string, 'foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=2', 'right format'; -is $params->merge(y => 3, z => [4, 5])->to_string, 'foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=3&z=4&z=5', - 'right format'; -is $params->merge(Mojo::Parameters->new(z => 6))->to_string, 'foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=3&z=6', - 'right format'; - -# Merge (instances) -$params = Mojo::Parameters->new->merge(Mojo::Parameters->new(foo => [123, 456])); -is_deeply $params->to_hash, {foo => [123, 456]}, 'right structure'; -$params = Mojo::Parameters->new(foo => 321)->merge(Mojo::Parameters->new(foo => [123, 456])); -is_deeply $params->to_hash, {foo => [123, 456]}, 'right structure'; -$params = Mojo::Parameters->new(bar => 321)->merge(Mojo::Parameters->new(foo => [123, 456])); -is_deeply $params->to_hash, {foo => [123, 456], bar => 321}, 'right structure'; - -# Param -$params = Mojo::Parameters->new('foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=3&z=6'); -is_deeply $params->param('foo'), 'b;ar', 'right structure'; -is_deeply $params->every_param('foo'), ['b;ar'], 'right structure'; -is_deeply $params->every_param('a'), [4, 5], 'right structure'; -$params->param(foo => 'bar'); -is_deeply [$params->param('foo')], ['bar'], 'right structure'; -is_deeply $params->param(foo => qw(baz yada))->every_param('foo'), [qw(baz yada)], 'right structure'; -is_deeply $params->names, [qw(a b c foo x y z)], 'right structure'; - -# Append -$params = Mojo::Parameters->new('q=1'); -$params->append(a => 4, a => 5, b => 6, b => 7, c => undef, c => []); -is_deeply $params->to_hash, {a => [4, 5], b => [6, 7], q => 1}, 'right structure'; -is_deeply $params->names, [qw(a b q)], 'right structure'; -$params = Mojo::Parameters->new(foo => '', bar => 'bar'); -is $params->to_string, 'foo=&bar=bar', 'right format'; -$params = Mojo::Parameters->new(bar => 'bar', foo => ''); -is $params->to_string, 'bar=bar&foo=', 'right format'; -is $params->append($params2)->to_string, 'bar=bar&foo=&x=1&y=2', 'right format'; -is $params2->to_string, 'x=1&y=2', 'right format'; - -# "0" -$params = Mojo::Parameters->new(0 => 0); -is $params->param(0), 0, 'right value'; -is_deeply $params->every_param(0), [0], 'right value'; -is_deeply $params->every_param('foo'), [], 'no values'; -is $params->to_string, '0=0', 'right format'; -$params = Mojo::Parameters->new($params->to_string); -is $params->param(0), 0, 'right value'; -is_deeply $params->every_param(0), [0], 'right value'; -is $params->to_hash->{0}, 0, 'right value'; -is_deeply $params->to_hash, {0 => 0}, 'right structure'; -is $params->to_string, '0=0', 'right format'; - -# Semicolon -$params = Mojo::Parameters->new('foo=bar;baz'); -is $params->to_string, 'foo=bar;baz', 'right format'; -is_deeply $params->pairs, [foo => 'bar;baz'], 'right structure'; -is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure'; -is $params->to_string, 'foo=bar%3Bbaz', 'right format'; -$params = Mojo::Parameters->new($params->to_string); -is_deeply $params->pairs, [foo => 'bar;baz'], 'right structure'; -is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure'; -is $params->to_string, 'foo=bar%3Bbaz', 'right format'; - -# Reconstruction -$params = Mojo::Parameters->new('foo=bar&baz=23'); -is "$params", 'foo=bar&baz=23', 'right format'; -$params = Mojo::Parameters->new('foo=bar;baz=23'); -is "$params", 'foo=bar;baz=23', 'right format'; - -# Empty params -$params = Mojo::Parameters->new('c='); -is $params->to_hash->{c}, '', 'right value'; -is_deeply $params->to_hash, {c => ''}, 'right structure'; -$params = Mojo::Parameters->new('c=&c=&d'); -is_deeply $params->to_hash->{c}, ['', ''], 'right values'; -is $params->to_hash->{d}, '', 'right value'; -is_deeply $params->to_hash, {c => ['', ''], d => ''}, 'right structure'; -$params = Mojo::Parameters->new('c&d=0&e='); -is $params->to_hash->{c}, '', 'right value'; -is $params->to_hash->{d}, 0, 'right value'; -is $params->to_hash->{e}, '', 'right value'; -is_deeply $params->to_hash, {c => '', d => 0, e => ''}, 'right structure'; - -# "+" -$params = Mojo::Parameters->new('foo=%2B'); -is $params->param('foo'), '+', 'right value'; -is_deeply $params->to_hash, {foo => '+'}, 'right structure'; -$params->param('foo ' => ' bar'); -is $params->to_string, 'foo=%2B&foo+=+bar', 'right format'; -$params->remove('foo '); -is_deeply $params->to_hash, {foo => '+'}, 'right structure'; -$params->append('1 2', '3+3'); -is $params->param('1 2'), '3+3', 'right value'; -is_deeply $params->to_hash, {foo => '+', '1 2' => '3+3'}, 'right structure'; -$params = Mojo::Parameters->new('a=works+too'); -is "$params", 'a=works+too', 'right format'; -is_deeply $params->to_hash, {a => 'works too'}, 'right structure'; -is $params->param('a'), 'works too', 'right value'; -is "$params", 'a=works+too', 'right format'; - -# Array values -$params = Mojo::Parameters->new; -$params->append(foo => [qw(bar baz)], bar => [qw(bas test)], a => 'b'); -is_deeply $params->every_param('foo'), [qw(bar baz)], 'right values'; -is $params->param('a'), 'b', 'right value'; -is_deeply $params->every_param('bar'), [qw(bas test)], 'right values'; -is_deeply $params->to_hash, {foo => ['bar', 'baz'], a => 'b', bar => ['bas', 'test']}, 'right structure'; -$params = Mojo::Parameters->new(foo => ['ba;r', 'b;az']); -is_deeply $params->to_hash, {foo => ['ba;r', 'b;az']}, 'right structure'; -$params->append(foo => ['bar'], foo => ['baz', 'yada']); -is_deeply $params->to_hash, {foo => ['ba;r', 'b;az', 'bar', 'baz', 'yada']}, 'right structure'; -is $params->param('foo'), 'yada', 'right value'; -is_deeply $params->every_param('foo'), [qw(ba;r b;az bar baz yada)], 'right values'; -$params = Mojo::Parameters->new(foo => ['ba;r', 'b;az'], bar => 23); -is_deeply $params->to_hash, {foo => ['ba;r', 'b;az'], bar => 23}, 'right structure'; -is $params->param('foo'), 'b;az', 'right value'; -is_deeply $params->every_param('foo'), [qw(ba;r b;az)], 'right values'; -$params = Mojo::Parameters->new; -is $params->param(foo => ['ba;r', 'baz'])->to_string, 'foo=ba%3Br&foo=baz', 'right format'; - -# Unicode -$params = Mojo::Parameters->new; -$params->parse('input=say%20%22%C2%AB~%22;'); -is_deeply $params->pairs, ['input', 'say "«~";'], 'right structure'; -is $params->param('input'), 'say "«~";', 'right value'; -is "$params", 'input=say+%22%C2%AB%7E%22%3B', 'right result'; -$params = Mojo::Parameters->new('♥=☃'); -is_deeply $params->pairs, ['♥', '☃'], 'right structure'; -is $params->param('♥'), '☃', 'right value'; -is "$params", '%E2%99%A5=%E2%98%83', 'right result'; -$params = Mojo::Parameters->new('%E2%99%A5=%E2%98%83'); -is_deeply $params->pairs, ['♥', '☃'], 'right structure'; -is $params->param('♥'), '☃', 'right value'; -is "$params", '%E2%99%A5=%E2%98%83', 'right result'; - -# Reparse -$params = Mojo::Parameters->new('foo=bar&baz=23'); -$params->parse('foo=bar&baz=23'); -is "$params", 'foo=bar&baz=23', 'right result'; - -# Replace -$params = Mojo::Parameters->new('a=1&b=2'); -$params->pairs([a => 2, b => 3]); -is $params->to_string, 'a=2&b=3', 'right result'; -is $params->pairs([])->to_string, '', 'right result'; - -# Query string -$params = Mojo::Parameters->new('AZaz09-._~%!$&\'()*+,;=:@/?'); -is "$params", 'AZaz09-._~%!$&\'()*+,;=:@/?', 'right result'; -$params = Mojo::Parameters->new('foo{}bar'); -is "$params", 'foo%7B%7Dbar', 'right result'; - -# "%" -$params = Mojo::Parameters->new; -$params->param('%foo%' => '%'); -is "$params", '%25foo%25=%25', 'right result'; - -# Special characters -$params = Mojo::Parameters->new('!$\'()*,:@/foo?=!$\'()*,:@/?&bar=23'); -is $params->param('!$\'()*,:@/foo?'), '!$\'()*,:@/?', 'right value'; -is $params->param('bar'), 23, 'right value'; -is "$params", '%21%24%27%28%29*%2C%3A%40%2Ffoo%3F=' . '%21%24%27%28%29*%2C%3A%40%2F%3F&bar=23', 'right result'; - -# No charset -$params = Mojo::Parameters->new('%E5=%E4')->charset(undef); -is $params->param("\xe5"), "\xe4", 'right value'; -is "$params", '%E5=%E4', 'right result'; -is $params->clone->to_string, '%E5=%E4', 'right result'; +subtest 'Basic functionality' => sub { + my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23'); + my $params2 = Mojo::Parameters->new('x', 1, 'y', 2); + is $params->to_string, 'foo=b%3Bar&baz=23', 'right format'; + is $params2->to_string, 'x=1&y=2', 'right format'; + is $params->to_string, 'foo=b%3Bar&baz=23', 'right format'; + is_deeply $params->pairs, ['foo', 'b;ar', 'baz', 23], 'right structure'; + $params->append(a => 4, a => 5, b => 6, b => 7); + is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7', 'right format'; + push @$params, c => 'f;oo'; + is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo', 'right format'; + is $params->remove('a')->to_string, 'foo=b%3Bar&baz=23&b=6&b=7&c=f%3Boo', 'right format'; +}; + +subtest 'Clone' => sub { + my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23'); + my $clone = $params->clone; + is "$params", "$clone", 'equal parameters'; + push @$clone, c => 9; + isnt "$params", "$clone", 'unequal parameters'; +}; + +subtest 'Merge' => sub { + my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo'); + my $params2 = Mojo::Parameters->new('x', 1, 'y', 2); + $params->merge($params2); + is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=2', 'right format'; + is $params2->to_string, 'x=1&y=2', 'right format'; + is $params->merge(baz => undef)->to_string, 'foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=2', 'right format'; + is $params->merge(y => 3, z => [4, 5])->to_string, 'foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=3&z=4&z=5', + 'right format'; + is $params->merge(Mojo::Parameters->new(z => 6))->to_string, 'foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=3&z=6', + 'right format'; +}; + +subtest 'Merge (instances)' => sub { + my $params = Mojo::Parameters->new->merge(Mojo::Parameters->new(foo => [123, 456])); + is_deeply $params->to_hash, {foo => [123, 456]}, 'right structure'; + $params = Mojo::Parameters->new(foo => 321)->merge(Mojo::Parameters->new(foo => [123, 456])); + is_deeply $params->to_hash, {foo => [123, 456]}, 'right structure'; + $params = Mojo::Parameters->new(bar => 321)->merge(Mojo::Parameters->new(foo => [123, 456])); + is_deeply $params->to_hash, {foo => [123, 456], bar => 321}, 'right structure'; +}; + +subtest 'Param' => sub { + my $params = Mojo::Parameters->new('foo=b%3Bar&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=3&z=6'); + is_deeply $params->param('foo'), 'b;ar', 'right structure'; + is_deeply $params->every_param('foo'), ['b;ar'], 'right structure'; + is_deeply $params->every_param('a'), [4, 5], 'right structure'; + $params->param(foo => 'bar'); + is_deeply [$params->param('foo')], ['bar'], 'right structure'; + is_deeply $params->param(foo => qw(baz yada))->every_param('foo'), [qw(baz yada)], 'right structure'; + is_deeply $params->names, [qw(a b c foo x y z)], 'right structure'; +}; + +subtest 'Append' => sub { + my $params = Mojo::Parameters->new('q=1'); + my $params2 = Mojo::Parameters->new('x', 1, 'y', 2); + $params->append(a => 4, a => 5, b => 6, b => 7, c => undef, c => []); + is_deeply $params->to_hash, {a => [4, 5], b => [6, 7], q => 1}, 'right structure'; + is_deeply $params->names, [qw(a b q)], 'right structure'; + $params = Mojo::Parameters->new(foo => '', bar => 'bar'); + is $params->to_string, 'foo=&bar=bar', 'right format'; + $params = Mojo::Parameters->new(bar => 'bar', foo => ''); + is $params->to_string, 'bar=bar&foo=', 'right format'; + is $params->append($params2)->to_string, 'bar=bar&foo=&x=1&y=2', 'right format'; + is $params2->to_string, 'x=1&y=2', 'right format'; +}; + +subtest '"0"' => sub { + my $params = Mojo::Parameters->new(0 => 0); + is $params->param(0), 0, 'right value'; + is_deeply $params->every_param(0), [0], 'right value'; + is_deeply $params->every_param('foo'), [], 'no values'; + is $params->to_string, '0=0', 'right format'; + $params = Mojo::Parameters->new($params->to_string); + is $params->param(0), 0, 'right value'; + is_deeply $params->every_param(0), [0], 'right value'; + is $params->to_hash->{0}, 0, 'right value'; + is_deeply $params->to_hash, {0 => 0}, 'right structure'; + is $params->to_string, '0=0', 'right format'; +}; + +subtest 'Semicolon' => sub { + my $params = Mojo::Parameters->new('foo=bar;baz'); + is $params->to_string, 'foo=bar;baz', 'right format'; + is_deeply $params->pairs, [foo => 'bar;baz'], 'right structure'; + is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure'; + is $params->to_string, 'foo=bar%3Bbaz', 'right format'; + $params = Mojo::Parameters->new($params->to_string); + is_deeply $params->pairs, [foo => 'bar;baz'], 'right structure'; + is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure'; + is $params->to_string, 'foo=bar%3Bbaz', 'right format'; +}; + +subtest 'Reconstruction' => sub { + my $params = Mojo::Parameters->new('foo=bar&baz=23'); + is "$params", 'foo=bar&baz=23', 'right format'; + $params = Mojo::Parameters->new('foo=bar;baz=23'); + is "$params", 'foo=bar;baz=23', 'right format'; +}; + +subtest 'Empty params' => sub { + my $params = Mojo::Parameters->new('c='); + is $params->to_hash->{c}, '', 'right value'; + is_deeply $params->to_hash, {c => ''}, 'right structure'; + $params = Mojo::Parameters->new('c=&c=&d'); + is_deeply $params->to_hash->{c}, ['', ''], 'right values'; + is $params->to_hash->{d}, '', 'right value'; + is_deeply $params->to_hash, {c => ['', ''], d => ''}, 'right structure'; + $params = Mojo::Parameters->new('c&d=0&e='); + is $params->to_hash->{c}, '', 'right value'; + is $params->to_hash->{d}, 0, 'right value'; + is $params->to_hash->{e}, '', 'right value'; + is_deeply $params->to_hash, {c => '', d => 0, e => ''}, 'right structure'; +}; + +subtest '"+"' => sub { + my $params = Mojo::Parameters->new('foo=%2B'); + is $params->param('foo'), '+', 'right value'; + is_deeply $params->to_hash, {foo => '+'}, 'right structure'; + $params->param('foo ' => ' bar'); + is $params->to_string, 'foo=%2B&foo+=+bar', 'right format'; + $params->remove('foo '); + is_deeply $params->to_hash, {foo => '+'}, 'right structure'; + $params->append('1 2', '3+3'); + is $params->param('1 2'), '3+3', 'right value'; + is_deeply $params->to_hash, {foo => '+', '1 2' => '3+3'}, 'right structure'; + $params = Mojo::Parameters->new('a=works+too'); + is "$params", 'a=works+too', 'right format'; + is_deeply $params->to_hash, {a => 'works too'}, 'right structure'; + is $params->param('a'), 'works too', 'right value'; + is "$params", 'a=works+too', 'right format'; +}; + +subtest 'Array values' => sub { + my $params = Mojo::Parameters->new; + $params->append(foo => [qw(bar baz)], bar => [qw(bas test)], a => 'b'); + is_deeply $params->every_param('foo'), [qw(bar baz)], 'right values'; + is $params->param('a'), 'b', 'right value'; + is_deeply $params->every_param('bar'), [qw(bas test)], 'right values'; + is_deeply $params->to_hash, {foo => ['bar', 'baz'], a => 'b', bar => ['bas', 'test']}, 'right structure'; + $params = Mojo::Parameters->new(foo => ['ba;r', 'b;az']); + is_deeply $params->to_hash, {foo => ['ba;r', 'b;az']}, 'right structure'; + $params->append(foo => ['bar'], foo => ['baz', 'yada']); + is_deeply $params->to_hash, {foo => ['ba;r', 'b;az', 'bar', 'baz', 'yada']}, 'right structure'; + is $params->param('foo'), 'yada', 'right value'; + is_deeply $params->every_param('foo'), [qw(ba;r b;az bar baz yada)], 'right values'; + $params = Mojo::Parameters->new(foo => ['ba;r', 'b;az'], bar => 23); + is_deeply $params->to_hash, {foo => ['ba;r', 'b;az'], bar => 23}, 'right structure'; + is $params->param('foo'), 'b;az', 'right value'; + is_deeply $params->every_param('foo'), [qw(ba;r b;az)], 'right values'; + $params = Mojo::Parameters->new; + is $params->param(foo => ['ba;r', 'baz'])->to_string, 'foo=ba%3Br&foo=baz', 'right format'; +}; + +subtest 'Unicode' => sub { + my $params = Mojo::Parameters->new; + $params->parse('input=say%20%22%C2%AB~%22;'); + is_deeply $params->pairs, ['input', 'say "«~";'], 'right structure'; + is $params->param('input'), 'say "«~";', 'right value'; + is "$params", 'input=say+%22%C2%AB%7E%22%3B', 'right result'; + $params = Mojo::Parameters->new('♥=☃'); + is_deeply $params->pairs, ['♥', '☃'], 'right structure'; + is $params->param('♥'), '☃', 'right value'; + is "$params", '%E2%99%A5=%E2%98%83', 'right result'; + $params = Mojo::Parameters->new('%E2%99%A5=%E2%98%83'); + is_deeply $params->pairs, ['♥', '☃'], 'right structure'; + is $params->param('♥'), '☃', 'right value'; + is "$params", '%E2%99%A5=%E2%98%83', 'right result'; +}; + +subtest 'Reparse' => sub { + my $params = Mojo::Parameters->new('foo=bar&baz=23'); + $params->parse('foo=bar&baz=23'); + is "$params", 'foo=bar&baz=23', 'right result'; +}; + +subtest 'Replace' => sub { + my $params = Mojo::Parameters->new('a=1&b=2'); + $params->pairs([a => 2, b => 3]); + is $params->to_string, 'a=2&b=3', 'right result'; + is $params->pairs([])->to_string, '', 'right result'; +}; + +subtest 'Query string' => sub { + my $params = Mojo::Parameters->new('AZaz09-._~%!$&\'()*+,;=:@/?'); + is "$params", 'AZaz09-._~%!$&\'()*+,;=:@/?', 'right result'; + $params = Mojo::Parameters->new('foo{}bar'); + is "$params", 'foo%7B%7Dbar', 'right result'; +}; + +subtest '"%"' => sub { + my $params = Mojo::Parameters->new; + $params->param('%foo%' => '%'); + is "$params", '%25foo%25=%25', 'right result'; +}; + +subtest 'Special characters' => sub { + my $params = Mojo::Parameters->new('!$\'()*,:@/foo?=!$\'()*,:@/?&bar=23'); + is $params->param('!$\'()*,:@/foo?'), '!$\'()*,:@/?', 'right value'; + is $params->param('bar'), 23, 'right value'; + is "$params", '%21%24%27%28%29*%2C%3A%40%2Ffoo%3F=' . '%21%24%27%28%29*%2C%3A%40%2F%3F&bar=23', 'right result'; +}; + +subtest 'No charset' => sub { + my $params = Mojo::Parameters->new('%E5=%E4')->charset(undef); + is $params->param("\xe5"), "\xe4", 'right value'; + is "$params", '%E5=%E4', 'right result'; + is $params->clone->to_string, '%E5=%E4', 'right result'; +}; done_testing();