tracks/perl6/exercises/phone-number/phone-number.t in trackler-2.0.8.28 vs tracks/perl6/exercises/phone-number/phone-number.t in trackler-2.0.8.29
- old
+ new
@@ -1,85 +1,122 @@
#!/usr/bin/env perl6
use v6;
use Test;
+use lib my $dir = $?FILE.IO.dirname;
use JSON::Tiny;
-use lib $?FILE.IO.dirname;
-plan 12;
-my $module = %*ENV<EXERCISM> ?? 'Example' !! 'Phone';
-use-ok $module;
-require ::($module) <Phone>;
+my $exercise = 'Phone';
+my $version = v1;
+my $module = %*ENV<EXERCISM> ?? 'Example' !! $exercise;
+plan 13;
-my %cases;
-subtest 'number, area-code and pretty methods', {
- plan 3;
- ok Phone.can('number'), 'can Phone.number';
- ok Phone.can('area-code'), 'can Phone.area-code';
- ok Phone.can('pretty'), 'can Phone.pretty';
-} or fail 'Missing method(s).';
+use-ok $module or bail-out;
+require ::($module);
-for @(%cases<valid>) {
- my $phone = Phone.new(number => .<input>);
- my $msg = 'for ' ~ .<test>;
- is $phone.number, .<number>, "number $msg";
- is $phone.area-code, .<area-code>, "area-code $msg";
- is $phone.pretty, .<pretty>, "pretty $msg";
+if ::($exercise).^ver !~~ $version {
+ warn "\nExercise version mismatch. Further tests may fail!"
+ ~ "\n$exercise is $(::($exercise).^ver.gist). "
+ ~ "Test is $($version.gist).\n";
+ bail-out 'Example version must match test version.' if %*ENV<EXERCISM>;
}
-todo 'Optional Exception Tests' unless %*ENV<EXERCISM>; # Remove this line for invalid input tests
-subtest 'Throw exceptions for invalid input', {
- plan 5;
- throws-like {Phone.new(number => .<input>)}, Exception, .<test> for @(%cases<invalid>);
+my @subs;
+BEGIN { @subs = <&clean-number> };
+subtest 'Subroutine(s)', {
+ plan 1;
+ eval-lives-ok "use $module; ::('$_').defined or die '$_ is not defined.'", $_ for @subs;
+} or bail-out 'All subroutines must be defined and exported.';
+require ::($module) @subs.eager;
+
+for @(my $c-data.<cases>.[0].<cases>) {
+ if .<expected> {
+ is clean-number(.<phrase>), |.<expected description>;
+ } else {
+ nok clean-number(.<phrase>), .<description>;
+ }
}
+if %*ENV<EXERCISM> && (my $c-data-file = "$dir/../../x-common/exercises/{$dir.IO.basename}/canonical-data.json".IO.resolve) ~~ :f {
+ is-deeply $c-data, from-json($c-data-file.slurp), 'canonical-data'
+} else { skip }
+
done-testing;
INIT {
- %cases := from-json 「
+ $c-data := from-json 「
{
- "valid": [
+ "exercise": "phone-number",
+ "version": "1.0.2",
+ "cases": [
{
- "input": 1234567890,
- "number": "1234567890",
- "area-code": "123",
- "pretty": "(123) 456-7890",
- "test": "10 digit integer"
- },
- {
- "input": "+1 (234) 555-6789",
- "number": "2345556789",
- "area-code": "234",
- "pretty": "(234) 555-6789",
- "test": "11 digit formatted number"
- },
- {
- "input": "1.379.555.2468",
- "number": "3795552468",
- "area-code": "379",
- "pretty": "(379) 555-2468",
- "test": "11 digit number containing separators"
- }
- ],
- "invalid": [
- {
- "input": "",
- "test": "empty input"
- },
- {
- "input": "13579",
- "test": "5 digit number"
- },
- {
- "input": "123456789011",
- "test": "12 digit number"
- },
- {
- "input": "+2 (468) 555-1379",
- "test": "11 digit number not beginning with 1"
- },
- {
- "input": "phone number",
- "test": "not a number"
+ "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",
+ "phrase": "(123) 456-7890",
+ "expected": "1234567890"
+ },
+ {
+ "description": "cleans numbers with dots",
+ "property": "clean",
+ "phrase": "123.456.7890",
+ "expected": "1234567890"
+ },
+ {
+ "description": "cleans numbers with multiple spaces",
+ "property": "clean",
+ "phrase": "123 456 7890 ",
+ "expected": "1234567890"
+ },
+ {
+ "description": "invalid when 9 digits",
+ "property": "clean",
+ "phrase": "123456789",
+ "expected": null
+ },
+ {
+ "description": "invalid when 11 digits does not start with a 1",
+ "property": "clean",
+ "phrase": "21234567890",
+ "expected": null
+ },
+ {
+ "description": "valid when 11 digits and starting with 1",
+ "property": "clean",
+ "phrase": "11234567890",
+ "expected": "1234567890"
+ },
+ {
+ "description": "invalid when more than 11 digits",
+ "property": "clean",
+ "phrase": "321234567890",
+ "expected": null
+ },
+ {
+ "description": "invalid with letters",
+ "property": "clean",
+ "phrase": "123-abc-7890",
+ "expected": null
+ },
+ {
+ "description": "invalid with punctuations",
+ "property": "clean",
+ "phrase": "123-@:!-7890",
+ "expected": null
+ },
+ {
+ "description": "invalid with right number of digits but letters mixed in",
+ "property": "clean",
+ "phrase": "1a2b3c4d5e6f7g8h9i0j",
+ "expected": null
+ }
+ ]
}
]
}
」
}