| 1 | |||
| 2 | use v5.14; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Syntax::Construct qw (package-block package-version); | ||
| 6 | |||
| 7 | package Test::YAFT::Attributes v1.0.4 { | ||
| 8 | use Attribute::Handlers; | ||
| 9 | |||
| 10 | require Ref::Util; | ||
| 11 | require Sub::Util; | ||
| 12 | |||
| 13 | my %attributes = ( | ||
| 14 | Assumption => { EXPORT => [qw [all assumptions asserts ]] }, | ||
| 15 | Expectation => { EXPORT => [qw [all expectations ]] }, | ||
| 16 | Foundation => { EXPORT_OK => [qw [all foundations plumbings]] }, | ||
| 17 | Util => { EXPORT => [qw [all utils helpers ]] }, | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub import { | ||
| 21 | my $caller = scalar caller; | ||
| 22 | my $target = __PACKAGE__; | ||
| 23 | |||
| 24 | for my $attribute (keys %attributes) { | ||
| 25 | eval qq ( | ||
| 26 | sub ${caller}::${attribute} : ATTR(CODE,BEGIN) { | ||
| 27 | goto &${target}::${attribute} | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | die qq (cannot install ${target} attribute ${attribute} into ${caller}: $@) | ||
| 32 | if $@ | ||
| 33 | ; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _push_unique_string (\@;@); | ||
| 38 | |||
| 39 | sub _build_coderef { | ||
| 40 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 41 | |||
| 42 | return | ||
| 43 | unless $data | ||
| 44 | ; | ||
| 45 | |||
| 46 | my ($builder, @arguments) = @$data; | ||
| 47 | |||
| 48 | if (Ref::Util::is_coderef ($builder)) { | ||
| 49 | return sub { $builder->(@arguments, @_) } | ||
| 50 | if @arguments | ||
| 51 | || _is_distinct_prototype ($referent, $builder) | ||
| 52 | ; | ||
| 53 | |||
| 54 | return $builder; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $file = qq ($builder.pm); | ||
| 58 | $file =~ s (::) (/)g; | ||
| 59 | |||
| 60 | return sub { | ||
| 61 | local ($@, $!, $^E); | ||
| 62 | require $file; | ||
| 63 | |||
| 64 | $builder->new (@arguments, @_); | ||
| 65 | }; | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | sub _register_expectation { | ||
| 70 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 71 | |||
| 72 | my $name = &_symbol_name; | ||
| 73 | my $coderef = &_symbol_code; | ||
| 74 | my $wrapper = sub { | ||
| 75 | my $expectation = $coderef->(@_); | ||
| 76 | |||
| 77 | Test::YAFT::Dumper->register_ref_builder ($expectation, $name, @_); | ||
| 78 | |||
| 79 | return $expectation; | ||
| 80 | }; | ||
| 81 | |||
| 82 | no warnings q (redefine); | ||
| 83 | *{$symbol} = _sync_prototype ($coderef => $wrapper); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _exported { | ||
| 87 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 88 | |||
| 89 | my $name = &_symbol_name; | ||
| 90 | my $config = $attributes{$attr}; | ||
| 91 | |||
| 92 | my ($where, $tags) = %$config; | ||
| 93 | |||
| 94 | no strict q (refs); | ||
| 95 | _push_unique_string @{qq (${package}::${where})}, $name; | ||
| 96 | _push_unique_string @{qq (${package}::EXPORT_OK)}, $name; | ||
| 97 | |||
| 98 | _push_unique_string @{ ${qq (${package}::EXPORT_TAGS)}{$_} //= [] }, $name | ||
| 99 | for @{ $tags // [] } | ||
| 100 | ; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _install_coderef { | ||
| 104 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 105 | |||
| 106 | return | ||
| 107 | unless my $coderef = &_build_coderef | ||
| 108 | ; | ||
| 109 | |||
| 110 | *{$symbol} = _sync_prototype ($referent => $coderef); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _is_distinct_prototype { | ||
| 114 | my ($target, $source) = @_; | ||
| 115 | |||
| 116 | my $lhs = Sub::Util::prototype ($target); | ||
| 117 | my $rhs = Sub::Util::prototype ($source); | ||
| 118 | |||
| 119 | return 0 | ||
| 120 | || (defined $lhs xor defined $rhs) | ||
| 121 | || (defined $lhs && $lhs ne $rhs) | ||
| 122 | ; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _push_unique_string (\@;@) { | ||
| 126 | my $push_into = shift; | ||
| 127 | |||
| 128 | my %exists; | ||
| 129 | @exists{@$push_into} = (); | ||
| 130 | |||
| 131 | push @$push_into, grep { ! exists $exists{$_} } @_; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _symbol_code { | ||
| 135 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 136 | |||
| 137 | *{$symbol}{CODE}; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _symbol_name { | ||
| 141 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 142 | |||
| 143 | *{$symbol}{NAME}; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _sync_prototype { | ||
| 147 | my ($referent, $coderef) = @_; | ||
| 148 | |||
| 149 | if (defined (my $prototype = Sub::Util::prototype ($referent))) { | ||
| 150 | Sub::Util::set_prototype $prototype => $coderef; | ||
| 151 | } | ||
| 152 | |||
| 153 | return $coderef; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub Assumption { | ||
| 157 | &_exported; | ||
| 158 | &_install_coderef; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub Expectation { | ||
| 162 | &_exported; | ||
| 163 | &_install_coderef; | ||
| 164 | &_register_expectation; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub Foundation { | ||
| 168 | &_exported; | ||
| 169 | &_install_coderef; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub Util { | ||
| 173 | &_exported; | ||
| 174 | &_install_coderef; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub From { | ||
| 178 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 179 | |||
| 180 | &_install_coderef; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub Cmp_Builder { | ||
| 184 | my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; | ||
| 185 | |||
| 186 | &_install_coderef; | ||
| 187 | } | ||
| 188 | |||
| 189 | 1; | ||
| 190 | } |