Skip to content

Instantly share code, notes, and snippets.

@samuraisam
Last active December 18, 2015 17:29
Show Gist options
  • Save samuraisam/5819387 to your computer and use it in GitHub Desktop.
Save samuraisam/5819387 to your computer and use it in GitHub Desktop.
class PB::SubMsg {
}
class PB::Option {
has Str $.name;
has $.constant;
has PB::SubMsg $.sub-message;
method new(Str :$name, :$constant?, PB::SubMsg :$sub-message?) {
if (!$constant.defined && !$sub-message.defined) || ($constant.defined && $sub-message.defined) {
die "either constant OR sub-message must be provided";
}
self.bless(*, name => $name, constant => $constant, sub-message => $sub-message);
}
method gist() {
"<Option {$.name}={$.constant.defined ?? $.constant !! 'Any'}>"
}
}
multi infix:<eq>(PB::Option $a, PB::Option $b) is export {
# say "$a = $b";
return
[&&] ($a.name eq $b.name),
($a.constant // Nil) eq ($b.constant // Nil),
($a.sub-message // Nil) eq ($b.sub-message // Nil);
}
class PB::Field {
has Str $.label;
has Str $.type;
has Str $.name;
has Int $.number;
has Array[PB::Option] @.options;
method new(Str :$label, Str :$type, Str :$name, Int :$number, :@options) {
if $label.defined && $type.defined && $name.defined && $number.defined {
self.bless(*, name => $name, label => $label, type => $type, number => $number, options => @options);
} else {
die "label, type, name and number are all required for PB::Field";
}
}
method gist() {
"<Field {$.name}={$.number} opts=[{@.options>>.gist}]>"
}
}
multi infix:<eq>(PB::Field $a, PB::Field $b) is export {
# say 'comparing options: ', $a.options Zeq $b.options;
return
[&&] ($a.label eq $b.label),
($a.type eq $b.type),
($a.name eq $b.name),
($a.number eq $b.number),
[&&](($a.options // []) Zeq ($b.options // []));
}
use Test;
my $field = PB::Field.new(name=>'name', label=>'required', type=>'int32', number=>1);
my $field2 = PB::Field.new(name=>'name', label=>'required', type=>'int32', number=>1);
my $fopt = PB::Option.new(name=>'default', constant=>0);
my $fopt2 = PB::Option.new(name=>'default', constant=>0);
ok [&&]([$fopt] Zeq [$fopt2]), 'option equality sanity test';
my @optarr = $fopt;
my @optarr2 = $fopt2;
ok [&&](@optarr Zeq @optarr2), 'option array @variable sanity test';
my $field-with-opts = $field.clone(options=>[$fopt]);
my $field-with-opts2 = $field2.clone(options=>[$fopt2]);
ok [&&]($field-with-opts.options Zeq $field-with-opts2.options), 'option array .@attribute sanity test (using =>[] arg syntax)';
my $field-with-opts3 = $field.clone(:options($fopt));
my $field-with-opts4 = $field2.clone(:options($fopt2));
ok [&&]($field-with-opts3.options Zeq $field-with-opts4.options), 'option array .@attribute sanity test (using :() arg syntax)';
my $field-with-opts5 = $field.clone(options => ($fopt));
my $field-with-opts6 = $field2.clone(options => ($fopt2));
ok [&&]($field-with-opts5.options Zeq $field-with-opts6.options), 'option array .@attribute sanity test (using =>()) arg syntax)';
ok ($field.clone(options=>[$fopt]) eq $field2.clone(options=>[$fopt2]).options), 'field equality with same options';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment