diff options
Diffstat (limited to 't')
-rw-r--r-- | t/01-grammar.t | 61 | ||||
-rw-r--r-- | t/02-actions.t.t | 77 | ||||
-rw-r--r-- | t/03-instantiation.t | 94 | ||||
-rw-r--r-- | t/04-stringification.t | 38 |
4 files changed, 270 insertions, 0 deletions
diff --git a/t/01-grammar.t b/t/01-grammar.t new file mode 100644 index 0000000..a84bf86 --- /dev/null +++ b/t/01-grammar.t @@ -0,0 +1,61 @@ +#! /usr/bin/env perl6 + +use v6.d; + +use URL::Grammar; +use Test; + +plan 2; + +subtest "https://www.tyil.nl", { + plan 8; + + my $match = URL::Grammar.parse("https://www.tyil.nl"); + + is ~$match<scheme>, "https", "Scheme is 'https'"; + nok $match<userinfo><username>, "URL contains no username"; + nok $match<userinfo><password>, "URL contains no password"; + is ~$match<host><hostname>, "www.tyil.nl", "Hostname is 'www.tyil.nl'"; + nok $match<host><port>, "URL contains no port"; + nok $match<path>, "URL contains no path"; + nok $match<query>, "URL contains no query"; + nok $match<fragment>, "URL contains no fragment"; +}; + +subtest "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", { + plan 8; + + my $match = URL::Grammar.parse("https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module"); + + is ~$match<scheme>, "https", "Scheme is 'https'"; + is ~$match<userinfo><username>, "tyil", "Username is 'tyil'"; + is ~$match<userinfo><password>, "donthackme", "Password is 'donthackme'"; + is ~$match<host><hostname>, "www.tyil.nl", "Hostname is 'www.tyil.nl'"; + is +$match<host><port>, 8443, "Port is 8443"; + is ~$match<fragment>, "module", "Fragment is 'module'"; + + subtest "path", { + plan 4; + + my @path = $match<path><part>; + + is @path.elems, 3, "Path consists of 3 parts"; + is @path[0], "a", "Part 0 is 'a'"; + is @path[1], "path", "Part 1 is 'path'"; + is @path[2], "part", "Part 2 is 'part'"; + } + + subtest "query", { + plan 5; + + my @query = $match<query><part>; + + is @query.elems, 2, "Query consists of 2 parts"; + is @query[0]<key>, "foo", "Part 0's key is 'foo'"; + is @query[0]<value>, "bar", "Part 0's value is 'bar'"; + is @query[1]<key>, "perl", "Part 1's key is 'perl'"; + is @query[1]<value>, "6", "Part 1's value is '6'"; + } +} + +# vim: ft=perl6 noet diff --git a/t/02-actions.t.t b/t/02-actions.t.t new file mode 100644 index 0000000..8eae227 --- /dev/null +++ b/t/02-actions.t.t @@ -0,0 +1,77 @@ +#! /usr/bin/env perl6 + +use v6.d; + +use URL::Grammar; +use URL::Grammar::Actions; + +use Test; + +plan 2; + +subtest "https://www.tyil.nl", { + plan 8; + + my %match = URL::Grammar.parse("https://www.tyil.nl", actions => URL::Grammar::Actions.new).made; + + is %match<scheme>, "https", "Scheme is 'https'"; + is %match<hostname>, "www.tyil.nl", "Hostname is 'www.tyil.nl'"; + + nok %match<username>, "URL contains no username"; + nok %match<password>, "URL contains no password"; + nok %match<port>, "URL contains no port"; + nok %match<path>, "URL contains no path"; + nok %match<query>, "URL contains no query"; + nok %match<fragment>, "URL contains no fragment"; +}; + +subtest "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", { + plan 8; + + my %match = URL::Grammar.parse( + "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", + actions => URL::Grammar::Actions.new, + ).made; + + is %match<scheme>, "https", "Scheme is 'https'"; + is %match<username>, "tyil", "Username is 'tyil'"; + is %match<password>, "donthackme", "Password is 'donthackme'"; + is %match<hostname>, "www.tyil.nl", "Hostname is 'www.tyil.nl'"; + is %match<port>, 8443, "Port is 8443"; + is %match<fragment>, "module", "Fragment is 'module'"; + + subtest "path", { + plan 4; + + my @path = %match<path>.list; + + is @path.elems, 3, "Path consists of 3 parts"; + is @path[0], "a", "Part 0 is 'a'"; + is @path[1], "path", "Part 1 is 'path'"; + is @path[2], "part", "Part 2 is 'part'"; + } + + subtest "query", { + plan 3; + + my %query = %match<query>.hash; + + is %query.elems, 2, "Query consists of 2 parts"; + + subtest "foo", { + plan 2; + + ok %query<foo>:exists, "Query has a key 'foo'"; + is %query<foo>, "bar", "Value for 'foo' is 'bar'"; + } + + subtest "perl", { + plan 2; + + ok %query<perl>:exists, "Query has a key 'perl'"; + is %query<perl>, "6", "Value for 'perl' is '6'"; + } + } +} + +# vim: ft=perl6 noet diff --git a/t/03-instantiation.t b/t/03-instantiation.t new file mode 100644 index 0000000..15b7efa --- /dev/null +++ b/t/03-instantiation.t @@ -0,0 +1,94 @@ +#! /usr/bin/env perl6 + +use v6.d; + +use Test; +use URL; + +plan 2; + +subtest "Object oriented", { + plan 2; + + subtest "https://www.tyil.nl", { + plan 8; + + my $url = URL.new( + scheme => "https", + hostname => "www.tyil.nl", + ); + + is $url.username, Str, "Username is empty"; + is $url.password, Str, "Password is emtpy"; + is $url.port, Int, "Port is empty"; + is $url.path, [], "Path is empty"; + is $url.query, {}, "Query is empty"; + is $url.fragment, Str, "Fragment is empty"; + + is $url.scheme, "https", "Scheme is 'https'"; + is $url.hostname, "www.tyil.nl", "Host is 'www.tyil.nl'"; + } + + subtest "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", { + plan 8; + + my $url = URL.new( + scheme => "https", + username => "tyil", + password => "donthackme", + hostname => "www.tyil.nl", + port => 8443, + path => ["a", "path", "part"], + query => { foo => "bar", perl => "6" }, + fragment => "module", + ); + + is $url.scheme, "https", "Scheme is https"; + is $url.username, "tyil", "Username is 'tyil'"; + is $url.password, "donthackme", "Password is 'donthackme'"; + is $url.hostname, "www.tyil.nl", "Host is www.tyil.nl"; + is $url.port, 8443, "Port is 8443"; + is $url.fragment, "module", "Fragment is 'module'"; + + subtest "path", { + plan 4; + + my @path = $url.path; + + is @path.elems, 3, "URL path part contains 3 elements"; + is @path[0], "a", "URL path part 0 is 'a'"; + is @path[1], "path", "URL path part 1 is 'path'"; + is @path[2], "part", "URL path part 2 is 'part'"; + } + + subtest "query", { + plan 3; + + my %query = $url.query; + + is %query.elems, 2, "URL query part contains 2 elements"; + is %query<foo>, "bar", "foo query is bar"; + is %query<perl>, "6", "perl query is 6"; + } + } +} + +subtest "Grammar", { + subtest "https://www.tyil.nl", { + plan 8; + + my $url = URL.new("https://www.tyil.nl"); + + is $url.username, Str, "Username is empty"; + is $url.password, Str, "Password is emtpy"; + is $url.port, Int, "Port is empty"; + is $url.path, [], "Path is empty"; + is $url.query, {}, "Query is empty"; + is $url.fragment, Str, "Fragment is empty"; + + is $url.scheme, "https", "Scheme is 'https'"; + is $url.hostname, "www.tyil.nl", "Host is 'www.tyil.nl'"; + } +} + +# vim: ft=perl6 noet diff --git a/t/04-stringification.t b/t/04-stringification.t new file mode 100644 index 0000000..4a50e5a --- /dev/null +++ b/t/04-stringification.t @@ -0,0 +1,38 @@ +#! /usr/bin/env perl6 + +use v6.d; + +use Test; +use URL; + +plan 2; + +subtest "https://www.tyil.nl", { + plan 4; + + my $url = URL.new( + scheme => "https", + hostname => "www.tyil.nl", + ); + + is $url.Str(:path), "", "Str(:path) = ''"; + is $url.Str(:query), "", "Str(:query) = ''"; + is $url.Str(:userinfo), "", "Str(:userinfo) = ''"; + is ~$url, "https://www.tyil.nl", "Str() = 'https://www.tyil.nl'"; +} + +subtest "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", { + plan 4; + + my $url = URL.new("https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module"); + + is $url.Str(:path), "a/path/part", "Str(:path) = 'a/path/part'"; + is $url.Str(:query), "foo=bar&perl=6", "Str(:query) = 'foo=bar&perl=6'"; + is $url.Str(:userinfo), "tyil:donthackme", "Str(:userinfo) = 'tyil:donthackme'"; + is ~$url, + "https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module", + "Str() = 'https://tyil:donthackme\@www.tyil.nl:8443/a/path/part?foo=bar&perl=6#module'" + ; +} + +# vim: ft=perl6 noet |