#!/usr/bin/env perl6 use v6; use Test; use JSON::Fast; use lib $?FILE.IO.dirname; use Phone; plan 14; my Version:D $version = v5; if Phone.^ver !~~ $version { warn "\nExercise version mismatch. Further tests may fail!" ~ "\nPhone is {Phone.^ver.gist}. " ~ "Test is {$version.gist}.\n"; } my $c-data = from-json $=pod.pop.contents; for @($c-data[0]) { if . { is clean-number(.), |.; } else { nok clean-number(.), .; } } =head2 Canonical Data =begin code { "exercise": "phone-number", "version": "1.4.0", "cases": [ { "description": "Cleanup user-entered phone numbers", "comments": [ " Returns the cleaned phone number if given number is valid, " , " else returns nil. Note that number is not formatted, " , " just a 10-digit number is returned. " ], "cases": [ { "description": "cleans the number", "property": "clean", "input": { "phrase": "(223) 456-7890" }, "expected": "2234567890" }, { "description": "cleans numbers with dots", "property": "clean", "input": { "phrase": "223.456.7890" }, "expected": "2234567890" }, { "description": "cleans numbers with multiple spaces", "property": "clean", "input": { "phrase": "223 456 7890 " }, "expected": "2234567890" }, { "description": "invalid when 9 digits", "property": "clean", "input": { "phrase": "123456789" }, "expected": null }, { "description": "invalid when 11 digits does not start with a 1", "property": "clean", "input": { "phrase": "22234567890" }, "expected": null }, { "description": "valid when 11 digits and starting with 1", "property": "clean", "input": { "phrase": "12234567890" }, "expected": "2234567890" }, { "description": "valid when 11 digits and starting with 1 even with punctuation", "property": "clean", "input": { "phrase": "+1 (223) 456-7890" }, "expected": "2234567890" }, { "description": "invalid when more than 11 digits", "property": "clean", "input": { "phrase": "321234567890" }, "expected": null }, { "description": "invalid with letters", "property": "clean", "input": { "phrase": "123-abc-7890" }, "expected": null }, { "description": "invalid with punctuations", "property": "clean", "input": { "phrase": "123-@:!-7890" }, "expected": null }, { "description": "invalid if area code starts with 0", "property": "clean", "input": { "phrase": "(023) 456-7890" }, "expected": null }, { "description": "invalid if area code starts with 1", "property": "clean", "input": { "phrase": "(123) 456-7890" }, "expected": null }, { "description": "invalid if exchange code starts with 0", "property": "clean", "input": { "phrase": "(223) 056-7890" }, "expected": null }, { "description": "invalid if exchange code starts with 1", "property": "clean", "input": { "phrase": "(223) 156-7890" }, "expected": null } ] } ] } =end code