Skip to content

Commit

Permalink
Synergy::SwitchBox: add as_sentences for English explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Oct 22, 2023
1 parent 83eb487 commit 6f7861a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
66 changes: 64 additions & 2 deletions lib/Synergy/SwitchBox.pm
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,78 @@ package Synergy::SwitchBox::Error {
}

# These are super weird but let's not just drop them on the floor.
push @structs, { type => 'undef-name' } if $errors{undef_name};
push @structs, { type => 'empty-switch' } if $errors{empty_switch};
push @structs, { type => 'undef_name' } if $errors{undef_name};
push @structs, { type => 'empty_switch' } if $errors{empty_switch};

return @structs;
}

sub as_sentences ($self) {
my sub andlist ($set) {
my @words = sort keys %$set;
$words[-1] = "and $words[-1]" if @words > 2;
join q{, }, @words;
}

my @structs = $self->as_structs;
return unless @structs;

my %switch_value;
my %switch_multi;
my %switch_novalue;
my %switch_unknown;
my %switch_misc;
my %other;

STRUCT: for my $struct (@structs) {
if (defined(my $switch = $struct->{switch})) {
if ($struct->{type} eq 'value') { $switch_value{$switch} = 1 }
elsif ($struct->{type} eq 'multi') { $switch_multi{$switch} = 1 }
elsif ($struct->{type} eq 'novalue') { $switch_novalue{$switch} = 1 }
elsif ($struct->{type} eq 'unknown') { $switch_unknown{$switch} = 1 }
else { $switch_misc{$switch} = 1 }

next STRUCT;
}

if ($struct->{undef_name}) { $other{undef_name} = 1 }
elsif ($struct->{empty_switch}) { $other{empty_switch} = 1 }
else { $other{misc} = 1 }
}

my @sentences;
if (keys %switch_value) {
push @sentences,
"These switches had invalid values: " . andlist(\%switch_value) . ".";
}

if (keys %switch_novalue) {
push @sentences,
"These switches need to be given a value, but weren't: " . andlist(\%switch_novalue) . ".";
}

if (keys %switch_multi) {
push @sentences,
"These switches can only be given once, but were given multiple times: " . andlist(\%switch_multi) . ".";
}

if (keys %switch_unknown) {
push @sentences,
"There was something inexplicably wrong with these switches: " . andlist(\%switch_multi) . ".";
}

if (%other) {
push @sentences, "Some weird thing happened that probably shouldn't.";
}

return @sentences;
}

no Moose;
}

package Synergy::SwitchBox::Set {
# This package exists only so that the generated packages can subclass it.
use Moose;

no Moose;
Expand Down
26 changes: 19 additions & 7 deletions t/lib/Test/SwitchBox.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,25 @@ sub errors_ok ($self, $str, $want, $desc) {

isa_ok($error, 'Synergy::SwitchBox::Error', 'result of handle_switches');

my @structs = $error->as_structs;

cmp_deeply(
\@structs,
bag(@$want),
"got the expected error structs",
);
if ($want->{structs}) {
my @structs = $error->as_structs;

cmp_deeply(
\@structs,
bag($want->{structs}->@*),
"got the expected error structs",
);
}

if ($want->{sentences}) {
my @sentences = $error->as_sentences;

cmp_deeply(
\@sentences,
bag($want->{sentences}->@*),
"got the expected error sentences",
);
}
};
}

Expand Down
40 changes: 26 additions & 14 deletions t/switchbox.t
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ $box->switches_ok(

$box->errors_ok(
"/name Dana Cassidy /age unknown /unexpected inquisition",
[
{ switch => 'age', type => 'value' },
{ switch => 'unexpected', type => 'unknown' },
],
{
structs => [
{ switch => 'age', type => 'value' },
{ switch => 'unexpected', type => 'unknown' },
],
},
"simple example with errors",
);

$box->errors_ok(
"/age 17 /age unknown /age 19",
[
{ switch => 'age', type => 'value' },
{ switch => 'age', type => 'multi' },
],
{
structs => [
{ switch => 'age', type => 'value' },
{ switch => 'age', type => 'multi' },
],
sentences => [
"These switches had invalid values: age.",
"These switches can only be given once, but were given multiple times: age.",
],
},
"simple example with type errors, /age x3",
);

Expand All @@ -64,17 +72,21 @@ $box->errors_ok(
# enough. -- rjbs, 2023-10-22
$box->errors_ok(
"/age 17 unknown 19",
[
{ switch => 'age', type => 'value' },
],
{
structs => [
{ switch => 'age', type => 'value' },
],
},
"simple example with type errors, /age with 3 values",
);

$box->errors_ok(
"/age",
[
{ switch => 'age', type => 'novalue' },
],
{
structs => [
{ switch => 'age', type => 'novalue' },
],
},
"non-bool switch with no value",
);

Expand Down

0 comments on commit 6f7861a

Please sign in to comment.