1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
|
require 'spec_helper'
require 'extlib/hook'
describe Extlib::Hook do
before do
@module = Module.new do
def greet; greetings_from_module; end;
end
@class = Class.new do
include Extlib::Hook
def hookable; end;
def self.clakable; end;
def ambiguous; hi_mom!; end;
def self.ambiguous; hi_dad!; end;
end
@another_class = Class.new do
include Extlib::Hook
end
@other = Class.new do
include Extlib::Hook
def hookable; end
def self.clakable; end;
end
@class.register_instance_hooks :hookable
@class.register_class_hooks :clakable
end
#
# Specs out how hookable methods are registered
#
describe "explicit hookable method registration" do
describe "for class methods" do
it "shouldn't confuse instance method hooks and class method hooks" do
@class.register_instance_hooks :ambiguous
@class.register_class_hooks :ambiguous
@class.should_receive(:hi_dad!)
@class.ambiguous
end
it "should be able to register multiple hookable methods at once" do
%w(method_one method_two method_three).each do |method|
@another_class.class_eval %(def self.#{method}; end;)
end
@another_class.register_class_hooks :method_one, :method_two, :method_three
@another_class.class_hooks.should have_key(:method_one)
@another_class.class_hooks.should have_key(:method_two)
@another_class.class_hooks.should have_key(:method_three)
end
it "should not allow a method that does not exist to be registered as hookable" do
lambda { @another_class.register_class_hooks :method_one }.should raise_error(ArgumentError)
end
it "should allow hooks to be registered on methods from module extensions" do
@class.extend(@module)
@class.register_class_hooks :greet
@class.class_hooks[:greet].should_not be_nil
end
it "should allow modules to register hooks in the self.extended method" do
@module.class_eval do
def self.extended(base)
base.register_class_hooks :greet
end
end
@class.extend(@module)
@class.class_hooks[:greet].should_not be_nil
end
it "should be able to register protected methods as hooks" do
@class.class_eval %{protected; def self.protected_hookable; end;}
lambda { @class.register_class_hooks(:protected_hookable) }.should_not raise_error(ArgumentError)
end
it "should not be able to register private methods as hooks" do
@class.class_eval %{class << self; private; def private_hookable; end; end;}
lambda { @class.register_class_hooks(:private_hookable) }.should raise_error(ArgumentError)
end
it "should allow advising methods ending in ? or !" do
@class.class_eval do
def self.hookable!; two!; end;
def self.hookable?; three!; end;
register_class_hooks :hookable!, :hookable?
end
@class.before_class_method(:hookable!) { one! }
@class.after_class_method(:hookable?) { four! }
@class.should_receive(:one!).once.ordered
@class.should_receive(:two!).once.ordered
@class.should_receive(:three!).once.ordered
@class.should_receive(:four!).once.ordered
@class.hookable!
@class.hookable?
end
it "should allow hooking methods ending in ?, ! or = with method hooks" do
@class.class_eval do
def self.before_hookable!; one!; end;
def self.hookable!; two!; end;
def self.hookable?; three!; end;
def self.after_hookable?; four!; end;
register_class_hooks :hookable!, :hookable?
end
@class.before_class_method(:hookable!, :before_hookable!)
@class.after_class_method(:hookable?, :after_hookable?)
@class.should_receive(:one!).once.ordered
@class.should_receive(:two!).once.ordered
@class.should_receive(:three!).once.ordered
@class.should_receive(:four!).once.ordered
@class.hookable!
@class.hookable?
end
it "should allow hooking methods that have single character names" do
@class.class_eval do
def self.a; end;
def self.b; end;
end
@class.before_class_method(:a) { omg! }
@class.before_class_method(:b) { hi2u! }
@class.should_receive(:omg!).once.ordered
@class.should_receive(:hi2u!).once.ordered
@class.a
@class.b
end
end
describe "for instance methods" do
it "shouldn't confuse instance method hooks and class method hooks" do
@class.register_instance_hooks :ambiguous
@class.register_class_hooks :ambiguous
inst = @class.new
inst.should_receive(:hi_mom!)
inst.ambiguous
end
it "should be able to register multiple hookable methods at once" do
%w(method_one method_two method_three).each do |method|
@another_class.send(:define_method, method) {}
end
@another_class.register_instance_hooks :method_one, :method_two, :method_three
@another_class.instance_hooks.should have_key(:method_one)
@another_class.instance_hooks.should have_key(:method_two)
@another_class.instance_hooks.should have_key(:method_three)
end
it "should not allow a method that does not exist to be registered as hookable" do
lambda { @another_class.register_instance_hooks :method_one }.should raise_error(ArgumentError)
end
it "should allow hooks to be registered on included module methods" do
@class.send(:include, @module)
@class.register_instance_hooks :greet
@class.instance_hooks[:greet].should_not be_nil
end
it "should allow modules to register hooks in the self.included method" do
@module.class_eval do
def self.included(base)
base.register_instance_hooks :greet
end
end
@class.send(:include, @module)
@class.instance_hooks[:greet].should_not be_nil
end
it "should be able to register protected methods as hooks" do
@class.class_eval %{protected; def protected_hookable; end;}
lambda { @class.register_instance_hooks(:protected_hookable) }.should_not raise_error(ArgumentError)
end
it "should not be able to register private methods as hooks" do
@class.class_eval %{private; def private_hookable; end;}
lambda { @class.register_instance_hooks(:private_hookable) }.should raise_error(ArgumentError)
end
it "should allow hooking methods ending in ? or ! with block hooks" do
@class.class_eval do
def hookable!; two!; end;
def hookable?; three!; end;
register_instance_hooks :hookable!, :hookable?
end
@class.before(:hookable!) { one! }
@class.after(:hookable?) { four! }
inst = @class.new
inst.should_receive(:one!).once.ordered
inst.should_receive(:two!).once.ordered
inst.should_receive(:three!).once.ordered
inst.should_receive(:four!).once.ordered
inst.hookable!
inst.hookable?
end
it "should allow hooking methods ending in ?, ! or = with method hooks" do
@class.class_eval do
def before_hookable(val); one!; end;
def hookable=(val); two!; end;
def hookable?; three!; end;
def after_hookable?; four!; end;
register_instance_hooks :hookable=, :hookable?
end
@class.before(:hookable=, :before_hookable)
@class.after(:hookable?, :after_hookable?)
inst = @class.new
inst.should_receive(:one!).once.ordered
inst.should_receive(:two!).once.ordered
inst.should_receive(:three!).once.ordered
inst.should_receive(:four!).once.ordered
inst.hookable = 'hello'
inst.hookable?
end
it "should allow hooking methods that have single character names" do
@class.class_eval do
def a; end;
def b; end;
end
@class.before(:a) { omg! }
@class.before(:b) { hi2u! }
inst = @class.new
inst.should_receive(:omg!).once.ordered
inst.should_receive(:hi2u!).once.ordered
inst.a
inst.b
end
end
end
describe "implicit hookable method registration" do
describe "for class methods" do
it "should implicitly register the method as hookable" do
@class.class_eval %{def self.implicit_hook; end;}
@class.before_class_method(:implicit_hook) { hello }
@class.should_receive(:hello)
@class.implicit_hook
end
end
describe "for instance methods" do
it "should implicitly register the method as hookable" do
@class.class_eval %{def implicit_hook; end;}
@class.before(:implicit_hook) { hello }
inst = @class.new
inst.should_receive(:hello)
inst.implicit_hook
end
it 'should not overwrite methods included by modules after the hook is declared' do
my_module = Module.new do
# Just another module
@another_module = Module.new do
def some_method; "Hello " + super; end;
end
def some_method; "world"; end;
def self.included(base)
base.before(:some_method, :a_method)
base.send(:include, @another_module)
end
end
@class.class_eval { include my_module }
inst = @class.new
inst.should_receive(:a_method)
inst.some_method.should == "Hello world"
end
end
end
describe "hook method registration" do
describe "for class methods" do
it "should complain when only one argument is passed" do
lambda { @class.before_class_method(:clakable) }.should raise_error(ArgumentError)
lambda { @class.after_class_method(:clakable) }.should raise_error(ArgumentError)
end
it "should complain when target_method is not a symbol" do
lambda { @class.before_class_method("clakable", :ambiguous) }.should raise_error(ArgumentError)
lambda { @class.after_class_method("clakable", :ambiguous) }.should raise_error(ArgumentError)
end
it "should complain when method_sym is not a symbol" do
lambda { @class.before_class_method(:clakable, "ambiguous") }.should raise_error(ArgumentError)
lambda { @class.after_class_method(:clakable, "ambiguous") }.should raise_error(ArgumentError)
end
it "should not allow methods ending in = to be hooks" do
lambda { @class.before_class_method(:clakable, :annoying=) }.should raise_error(ArgumentError)
lambda { @class.after_class_method(:clakable, :annoying=) }.should raise_error(ArgumentError)
end
end
describe "for instance methods" do
it "should complain when only one argument is passed" do
lambda { @class.before(:hookable) }.should raise_error(ArgumentError)
lambda { @class.after(:hookable) }.should raise_error(ArgumentError)
end
it "should complain when target_method is not a symbol" do
lambda { @class.before("hookable", :ambiguous) }.should raise_error(ArgumentError)
lambda { @class.after("hookable", :ambiguous) }.should raise_error(ArgumentError)
end
it "should complain when method_sym is not a symbol" do
lambda { @class.before(:hookable, "ambiguous") }.should raise_error(ArgumentError)
lambda { @class.after(:hookable, "ambiguous") }.should raise_error(ArgumentError)
end
it "should not allow methods ending in = to be hooks" do
lambda { @class.before(:hookable, :annoying=) }.should raise_error(ArgumentError)
lambda { @class.after(:hookable, :annoying=) }.should raise_error(ArgumentError)
end
end
end
#
# Specs out how hook methods / blocks are invoked when there is no inheritance
# involved
#
describe "hook invocation without inheritance" do
describe "for class methods" do
it 'should run an advice block' do
@class.before_class_method(:clakable) { hi_mom! }
@class.should_receive(:hi_mom!)
@class.clakable
end
it 'should run an advice method' do
@class.class_eval %{def self.before_method; hi_mom!; end;}
@class.before_class_method(:clakable, :before_method)
@class.should_receive(:hi_mom!)
@class.clakable
end
it "should not pass any of the hookable method's arguments if the hook block does not accept arguments" do
@class.class_eval do
def self.method_with_args(one, two, three); end;
before_class_method(:method_with_args) { hi_mom! }
end
@class.should_receive(:hi_mom!)
@class.method_with_args(1, 2, 3)
end
it "should not pass any of the hookable method's arguments if the hook method does not accept arguments" do
@class.class_eval do
def self.method_with_args(one, two, three); end;
def self.before_method_with_args; hi_mom!; end;
before_class_method(:method_with_args, :before_method_with_args)
end
@class.should_receive(:hi_mom!)
@class.method_with_args(1, 2, 3)
end
it "should not pass any of the hookable method's arguments if the hook is declared after it is registered and does not accept arguments" do
@class.class_eval do
def self.method_with_args(one, two, three); end;
before_class_method(:method_with_args, :before_method_with_args)
def self.before_method_with_args; hi_mom!; end;
end
@class.should_receive(:hi_mom!)
@class.method_with_args(1, 2, 3)
end
it "should not pass any of the hookable method's arguments if the hook has been re-defined not to accept arguments" do
@class.class_eval do
def self.method_with_args(one, two, three); end;
def self.before_method_with_args(one, two, three); hi_mom!; end;
before_class_method(:method_with_args, :before_method_with_args)
orig_verbose, $VERBOSE = $VERBOSE, false
def self.before_method_with_args; hi_dad!; end;
$VERBOSE = orig_verbose
end
@class.should_not_receive(:hi_mom1)
@class.should_receive(:hi_dad!)
@class.method_with_args(1, 2, 3)
end
it 'should pass the hookable method arguments to the hook method if the hook method takes arguments' do
@class.class_eval do
def self.hook_this(word, lol); end;
register_class_hooks(:hook_this)
def self.before_hook_this(word, lol); hi_mom!(word, lol); end;
before_class_method(:hook_this, :before_hook_this)
end
@class.should_receive(:hi_mom!).with("omg", "hi2u")
@class.hook_this("omg", "hi2u")
end
it "should pass the hookable method arguments to the hook block if the hook block takes arguments" do
@class.class_eval do
def self.method_with_args(word, lol); end;
before_class_method(:method_with_args) { |one, two| hi_mom!(one, two) }
end
@class.should_receive(:hi_mom!).with('omg', 'hi2u')
@class.method_with_args('omg', 'hi2u')
end
it 'should pass the hookable method arguments to the hook method if the hook method was re-defined to accept arguments' do
@class.class_eval do
def self.method_with_args(word, lol); end;
def self.before_method_with_args; hi_mom!; end;
before_class_method(:method_with_args, :before_method_with_args)
orig_verbose, $VERBOSE = $VERBOSE, false
def self.before_method_with_args(word, lol); hi_dad!(word, lol); end;
$VERBOSE = orig_verbose
end
@class.should_not_receive(:hi_mom!)
@class.should_receive(:hi_dad!).with("omg", "hi2u")
@class.method_with_args("omg", "hi2u")
end
it 'should work with glob arguments (or whatever you call em)' do
@class.class_eval do
def self.hook_this(*args); end;
def self.before_hook_this(*args); hi_mom!(*args); end;
before_class_method(:hook_this, :before_hook_this)
end
@class.should_receive(:hi_mom!).with("omg", "hi2u", "lolercoaster")
@class.hook_this("omg", "hi2u", "lolercoaster")
end
it 'should allow the use of before and after together' do
@class.class_eval %{def self.before_hook; first!; end;}
@class.before_class_method(:clakable, :before_hook)
@class.after_class_method(:clakable) { last! }
@class.should_receive(:first!).once.ordered
@class.should_receive(:last!).once.ordered
@class.clakable
end
it 'should be able to use private methods as hooks' do
@class.class_eval do
class << self
private
def nike; doit!; end;
end
before_class_method(:clakable, :nike)
end
@class.should_receive(:doit!)
@class.clakable
end
end
describe "for instance methods" do
it 'should run an advice block' do
@class.before(:hookable) { hi_mom! }
inst = @class.new
inst.should_receive(:hi_mom!)
inst.hookable
end
it 'should run an advice method' do
@class.send(:define_method, :before_method) { hi_mom! }
@class.before(:hookable, :before_method)
inst = @class.new
inst.should_receive(:hi_mom!)
inst.hookable
end
it "should not pass any of the hookable method's arguments if the hook block does not accept arguments" do
@class.class_eval do
def method_with_args(one, two, three); end;
before(:method_with_args) { hi_mom! }
end
inst = @class.new
inst.should_receive(:hi_mom!)
inst.method_with_args(1, 2, 3)
end
it "should not pass any of the hookable method's arguments if the hook method does not accept arguments" do
@class.class_eval do
def method_with_args(one, two, three); end;
def before_method_with_args; hi_mom!; end;
before(:method_with_args, :before_method_with_args)
end
inst = @class.new
inst.should_receive(:hi_mom!)
inst.method_with_args(1, 2, 3)
end
it "should not pass any of the hookable method's arguments if the hook is declared after it is registered and does not accept arguments" do
@class.class_eval do
def method_with_args(one, two, three); end;
before(:method_with_args, :before_method_with_args)
def before_method_with_args; hi_mom!; end;
end
inst = @class.new
inst.should_receive(:hi_mom!)
inst.method_with_args(1, 2, 3)
end
it "should not pass any of the hookable method's arguments if the hook has been re-defined not to accept arguments" do
@class.class_eval do
def method_with_args(one, two, three); end;
def before_method_with_args(one, two, three); hi_mom!; end;
before(:method_with_args, :before_method_with_args)
orig_verbose, $VERBOSE = $VERBOSE, false
def before_method_with_args; hi_dad!; end;
$VERBOSE = orig_verbose
end
inst = @class.new
inst.should_not_receive(:hi_mom1)
inst.should_receive(:hi_dad!)
inst.method_with_args(1, 2, 3)
end
it 'should pass the hookable method arguments to the hook method if the hook method takes arguments' do
@class.class_eval do
def method_with_args(word, lol); end;
def before_method_with_args(one, two); hi_mom!(one, two); end;
before(:method_with_args, :before_method_with_args)
end
inst = @class.new
inst.should_receive(:hi_mom!).with("omg", "hi2u")
inst.method_with_args("omg", "hi2u")
end
it "should pass the hookable method arguments to the hook block if the hook block takes arguments" do
@class.class_eval do
def method_with_args(word, lol); end;
before(:method_with_args) { |one, two| hi_mom!(one, two) }
end
inst = @class.new
inst.should_receive(:hi_mom!).with('omg', 'hi2u')
inst.method_with_args('omg', 'hi2u')
end
it 'should pass the hookable method arguments to the hook method if the hook method was re-defined to accept arguments' do
@class.class_eval do
def method_with_args(word, lol); end;
def before_method_with_args; hi_mom!; end;
before(:method_with_args, :before_method_with_args)
orig_verbose, $VERBOSE = $VERBOSE, false
def before_method_with_args(word, lol); hi_dad!(word, lol); end;
$VERBOSE = orig_verbose
end
inst = @class.new
inst.should_not_receive(:hi_mom!)
inst.should_receive(:hi_dad!).with("omg", "hi2u")
inst.method_with_args("omg", "hi2u")
end
it "should not pass the method return value to the after hook if the method does not take arguments" do
@class.class_eval do
def method_with_ret_val; 'hello'; end;
def after_method_with_ret_val; hi_mom!; end;
after(:method_with_ret_val, :after_method_with_ret_val)
end
inst = @class.new
inst.should_receive(:hi_mom!)
inst.method_with_ret_val
end
it 'should work with glob arguments (or whatever you call em)' do
@class.class_eval do
def hook_this(*args); end;
def before_hook_this(*args); hi_mom!(*args) end;
before(:hook_this, :before_hook_this)
end
inst = @class.new
inst.should_receive(:hi_mom!).with("omg", "hi2u", "lolercoaster")
inst.hook_this("omg", "hi2u", "lolercoaster")
end
it 'should allow the use of before and after together' do
@class.class_eval %{def after_hook; last!; end;}
@class.before(:hookable) { first! }
@class.after(:hookable, :after_hook)
inst = @class.new
inst.should_receive(:first!).once.ordered
inst.should_receive(:last!).once.ordered
inst.hookable
end
it 'should be able to use private methods as hooks' do
@class.class_eval %{private; def nike; doit!; end;}
@class.before(:hookable, :nike)
inst = @class.new
inst.should_receive(:doit!)
inst.hookable
end
end
end
describe "hook invocation with class inheritance" do
describe "for class methods" do
it 'should run an advice block when the class is inherited' do
@class.before_class_method(:clakable) { hi_mom! }
@child = Class.new(@class)
@child.should_receive(:hi_mom!)
@child.clakable
end
it 'should run an advice block on child class when hook is registered in parent after inheritance' do
@child = Class.new(@class)
@class.before_class_method(:clakable) { hi_mom! }
@child.should_receive(:hi_mom!)
@child.clakable
end
it 'should be able to declare advice methods in child classes' do
@class.class_eval %{def self.before_method; hi_dad!; end;}
@class.before_class_method(:clakable, :before_method)
@child = Class.new(@class) do
def self.child; hi_mom!; end;
before_class_method(:clakable, :child)
end
@child.should_receive(:hi_dad!).once.ordered
@child.should_receive(:hi_mom!).once.ordered
@child.clakable
end
it "should not execute hooks added in the child classes when in the parent class" do
@child = Class.new(@class) { def self.child; hi_mom!; end; }
@child.before_class_method(:clakable, :child)
@class.should_not_receive(:hi_mom!)
@class.clakable
end
it 'should not call the hook stack if the hookable method is overwritten and does not call super' do
@class.before_class_method(:clakable) { hi_mom! }
@child = Class.new(@class) do
def self.clakable; end;
end
@child.should_not_receive(:hi_mom!)
@child.clakable
end
it 'should not call hooks defined in the child class for a hookable method in a parent if the child overwrites the hookable method without calling super' do
@child = Class.new(@class) do
before_class_method(:clakable) { hi_mom! }
def self.clakable; end;
end
@child.should_not_receive(:hi_mom!)
@child.clakable
end
it 'should not call hooks defined in child class even if hook method exists in parent' do
@class.class_eval %{def self.hello_world; hello_world!; end;}
@child = Class.new(@class) do
before_class_method(:clakable, :hello_world)
end
@class.should_not_receive(:hello_world!)
@class.clakable
end
end
describe "for instance methods" do
it 'should run an advice block when the class is inherited' do
@inherited_class = Class.new(@class)
@class.before(:hookable) { hi_dad! }
inst = @inherited_class.new
inst.should_receive(:hi_dad!)
inst.hookable
end
it 'should run an advice block on child class when hook is registered in parent after inheritance' do
@child = Class.new(@class)
@class.before(:hookable) { hi_mom! }
inst = @child.new
inst.should_receive(:hi_mom!)
inst.hookable
end
it 'should be able to declare advice methods in child classes' do
@class.send(:define_method, :before_method) { hi_dad! }
@class.before(:hookable, :before_method)
@child = Class.new(@class) do
def child; hi_mom!; end;
before :hookable, :child
end
inst = @child.new
inst.should_receive(:hi_dad!).once.ordered
inst.should_receive(:hi_mom!).once.ordered
inst.hookable
end
it "should not execute hooks added in the child classes when in parent class" do
@child = Class.new(@class)
@child.send(:define_method, :child) { hi_mom! }
@child.before(:hookable, :child)
inst = @class.new
inst.should_not_receive(:hi_mom!)
inst.hookable
end
it 'should not call the hook stack if the hookable method is overwritten and does not call super' do
@class.before(:hookable) { hi_mom! }
@child = Class.new(@class) do
def hookable; end;
end
inst = @child.new
inst.should_not_receive(:hi_mom!)
inst.hookable
end
it 'should not call hooks defined in the child class for a hookable method in a parent if the child overwrites the hookable method without calling super' do
@child = Class.new(@class) do
before(:hookable) { hi_mom! }
def hookable; end;
end
inst = @child.new
inst.should_not_receive(:hi_mom!)
inst.hookable
end
it 'should not call hooks defined in child class even if hook method exists in parent' do
@class.send(:define_method, :hello_world) { hello_world! }
@child = Class.new(@class) do
before(:hookable, :hello_world)
end
inst = @class.new
inst.should_not_receive(:hello_world!)
inst.hookable
end
it 'should call different hooks in different children when they are defined there' do
@class.send(:define_method, :hello_world) {}
@child1 = Class.new(@class) do
before(:hello_world){ hi_dad! }
end
@child2 = Class.new(@class) do
before(:hello_world){ hi_mom! }
end
@child3 = Class.new(@child1) do
before(:hello_world){ hi_grandma! }
end
inst1 = @child1.new
inst2 = @child2.new
inst3 = @child3.new
inst1.should_receive(:hi_dad!).once
inst2.should_receive(:hi_mom!).once
inst3.should_receive(:hi_dad!).once
inst3.should_receive(:hi_grandma!).once
inst1.hello_world
inst2.hello_world
inst3.hello_world
end
end
end
describe "hook invocation with module inclusions / extensions" do
describe "for class methods" do
it "should not overwrite methods included by extensions after the hook is declared" do
@module.class_eval do
@another_module = Module.new do
def greet; greetings_from_another_module; super; end;
end
def self.extended(base)
base.before_class_method(:clakable, :greet)
base.extend(@another_module)
end
end
@class.extend(@module)
@class.should_receive(:greetings_from_another_module).once.ordered
@class.should_receive(:greetings_from_module).once.ordered
@class.clakable
end
end
describe "for instance methods" do
it 'should not overwrite methods included by modules after the hook is declared' do
@module.class_eval do
@another_module = Module.new do
def greet; greetings_from_another_module; super; end;
end
def self.included(base)
base.before(:hookable, :greet)
base.send(:include, @another_module)
end
end
@class.send(:include, @module)
inst = @class.new
inst.should_receive(:greetings_from_another_module).once.ordered
inst.should_receive(:greetings_from_module).once.ordered
inst.hookable
end
end
end
describe "hook invocation with unrelated classes" do
describe "for class methods" do
it "should not execute hooks registered in an unrelated class" do
@class.before_class_method(:clakable) { hi_mom! }
@other.should_not_receive(:hi_mom!)
@other.clakable
end
end
describe "for instance methods" do
it "should not execute hooks registered in an unrelated class" do
@class.before(:hookable) { hi_mom! }
inst = @other.new
inst.should_not_receive(:hi_mom!)
inst.hookable
end
end
end
describe "using before hook" do
describe "for class methods" do
it 'should run the advice before the advised method' do
@class.class_eval %{def self.hook_me; second!; end;}
@class.register_class_hooks(:hook_me)
@class.before_class_method(:hook_me, :first!)
@class.should_receive(:first!).ordered
@class.should_receive(:second!).ordered
@class.hook_me
end
it 'should execute all advices once in order' do
@class.before_class_method(:clakable, :hook_1)
@class.before_class_method(:clakable, :hook_2)
@class.before_class_method(:clakable, :hook_3)
@class.should_receive(:hook_1).once.ordered
@class.should_receive(:hook_2).once.ordered
@class.should_receive(:hook_3).once.ordered
@class.clakable
end
end
describe "for instance methods" do
it 'should run the advice before the advised method' do
@class.class_eval %{
def hook_me; second!; end;
}
@class.register_instance_hooks(:hook_me)
@class.before(:hook_me, :first!)
inst = @class.new
inst.should_receive(:first!).ordered
inst.should_receive(:second!).ordered
inst.hook_me
end
it 'should execute all advices once in order' do
@class.before(:hookable, :hook_1)
@class.before(:hookable, :hook_2)
@class.before(:hookable, :hook_3)
inst = @class.new
inst.should_receive(:hook_1).once.ordered
inst.should_receive(:hook_2).once.ordered
inst.should_receive(:hook_3).once.ordered
inst.hookable
end
end
end
describe 'using after hook' do
describe "for class methods" do
it 'should run the advice after the advised method' do
@class.class_eval %{def self.hook_me; first!; end;}
@class.register_class_hooks(:hook_me)
@class.after_class_method(:hook_me, :second!)
@class.should_receive(:first!).ordered
@class.should_receive(:second!).ordered
@class.hook_me
end
it 'should execute all advices once in order' do
@class.after_class_method(:clakable, :hook_1)
@class.after_class_method(:clakable, :hook_2)
@class.after_class_method(:clakable, :hook_3)
@class.should_receive(:hook_1).once.ordered
@class.should_receive(:hook_2).once.ordered
@class.should_receive(:hook_3).once.ordered
@class.clakable
end
it "the advised method should still return its normal value" do
@class.class_eval %{def self.hello; "hello world"; end;}
@class.register_class_hooks(:hello)
@class.after_class_method(:hello) { "BAM" }
@class.hello.should == "hello world"
end
it "should pass the return value to a hook method" do
@class.class_eval do
def self.with_return_val; 'hello'; end;
def self.after_with_return_val(retval); retval.should == 'hello'; end;
after_class_method(:with_return_val, :after_with_return_val)
end
@class.with_return_val
end
it "should pass the return value to a hook block" do
@class.class_eval do
def self.with_return_val; 'hello'; end;
after_class_method(:with_return_val) { |ret| ret.should == 'hello' }
end
@class.with_return_val
end
it "should pass the return value and method arguments to a hook block" do
@class.class_eval do
def self.with_args_and_return_val(world); 'hello'; end;
after_class_method(:with_args_and_return_val) do |hello, world|
hello.should == "hello"
world.should == "world"
end
end
@class.with_args_and_return_val('world')
end
end
describe "for instance methods" do
it 'should run the advice after the advised method' do
@class.class_eval %{def hook_me; first!; end;}
@class.register_instance_hooks(:hook_me)
@class.after(:hook_me, :second!)
inst = @class.new
inst.should_receive(:first!).ordered
inst.should_receive(:second!).ordered
inst.hook_me
end
it 'should execute all advices once in order' do
@class.after(:hookable, :hook_1)
@class.after(:hookable, :hook_2)
@class.after(:hookable, :hook_3)
inst = @class.new
inst.should_receive(:hook_1).once.ordered
inst.should_receive(:hook_2).once.ordered
inst.should_receive(:hook_3).once.ordered
inst.hookable
end
it "the advised method should still return its normal value" do
@class.class_eval %{def hello; "hello world"; end;}
@class.register_instance_hooks(:hello)
@class.after(:hello) { "BAM" }
@class.new.hello.should == "hello world"
end
it "should return nil if an after hook throws :halt without a return value" do
@class.class_eval %{def with_value; "hello"; end;}
@class.register_instance_hooks(:with_value)
@class.after(:with_value) { throw :halt }
@class.new.with_value.should be_nil
end
it "should pass the return value to a hook method" do
@class.class_eval do
def with_return_val; 'hello'; end;
def after_with_return_val(retval); retval.should == 'hello'; end;
after(:with_return_val, :after_with_return_val)
end
@class.new.with_return_val
end
it "should pass the return value to a hook block" do
@class.class_eval do
def with_return_val; 'hello'; end;
after(:with_return_val) { |ret| ret.should == 'hello' }
end
@class.new.with_return_val
end
it "should pass the return value and method arguments to a hook block" do
@class.class_eval do
def with_args_and_return_val(world); 'hello'; end;
after(:with_args_and_return_val) do |hello, world|
hello.should == "hello"
world.should == "world"
end
end
@class.new.with_args_and_return_val('world')
end
end
end
describe 'aborting' do
describe "for class methods" do
it "should catch :halt from a before hook and abort the advised method" do
@class.class_eval %{def self.no_love; love_me!; end;}
@class.register_class_hooks :no_love
@class.before_class_method(:no_love) { maybe! }
@class.before_class_method(:no_love) { throw :halt }
@class.before_class_method(:no_love) { what_about_me? }
@class.should_receive(:maybe!)
@class.should_not_receive(:what_about_me?)
@class.should_not_receive(:love_me!)
lambda { @class.no_love }.should_not throw_symbol(:halt)
end
it "should not run after hooks if a before hook throws :halt" do
@class.before_class_method(:clakable) { throw :halt }
@class.after_class_method(:clakable) { bam! }
@class.should_not_receive(:bam!)
lambda { @class.clakable }.should_not throw_symbol(:halt)
end
it "should return nil from the hookable method if a before hook throws :halt" do
@class.class_eval %{def self.with_value; "hello"; end;}
@class.register_class_hooks(:with_value)
@class.before_class_method(:with_value) { throw :halt }
@class.with_value.should be_nil
end
it "should catch :halt from an after hook and cease the advice" do
@class.after_class_method(:clakable) { throw :halt }
@class.after_class_method(:clakable) { never_see_me! }
@class.should_not_receive(:never_see_me!)
lambda { @class.clakable }.should_not throw_symbol(:halt)
end
it "should return nil if an after hook throws :halt without a return value" do
@class.class_eval %{def self.with_value; "hello"; end;}
@class.register_class_hooks(:with_value)
@class.after_class_method(:with_value) { throw :halt }
@class.with_value.should be_nil
end
end
describe "for instance methods" do
it "should catch :halt from a before hook and abort the advised method" do
@class.class_eval %{def no_love; love_me!; end;}
@class.register_instance_hooks :no_love
@class.before(:no_love) { maybe! }
@class.before(:no_love) { throw :halt }
@class.before(:no_love) { what_about_me? }
inst = @class.new
inst.should_receive(:maybe!)
inst.should_not_receive(:what_about_me?)
inst.should_not_receive(:love_me!)
lambda { inst.no_love }.should_not throw_symbol(:halt)
end
it "should not run after hooks if a before hook throws :halt" do
@class.before(:hookable) { throw :halt }
@class.after(:hookable) { bam! }
inst = @class.new
inst.should_not_receive(:bam!)
lambda { inst.hookable }.should_not throw_symbol(:halt)
end
it "should return nil from the hookable method if a before hook throws :halt" do
@class.class_eval %{def with_value; "hello"; end;}
@class.register_instance_hooks(:with_value)
@class.before(:with_value) { throw :halt }
@class.new.with_value.should be_nil
end
it "should catch :halt from an after hook and cease the advice" do
@class.after(:hookable) { throw :halt }
@class.after(:hookable) { never_see_me! }
inst = @class.new
inst.should_not_receive(:never_see_me!)
lambda { inst.hookable }.should_not throw_symbol(:halt)
end
end
end
describe 'aborting with return values' do
describe "for class methods" do
it "should be able to abort from a before hook with a return value" do
@class.before_class_method(:clakable) { throw :halt, 'omg' }
@class.clakable.should == 'omg'
end
it "should be able to abort from an after hook with a return value" do
@class.after_class_method(:clakable) { throw :halt, 'omg' }
@class.clakable.should == 'omg'
end
end
describe "for instance methods" do
it "should be able to abort from a before hook with a return value" do
@class.before(:hookable) { throw :halt, 'omg' }
inst = @class.new
inst.hookable.should == 'omg'
end
it "should be able to abort from an after hook with a return value" do
@class.after(:hookable) { throw :halt, 'omg' }
inst = @class.new
inst.hookable.should == 'omg'
end
end
end
describe "helper methods" do
it 'should generate the correct argument signature' do
@class.class_eval do
def some_method(a, b, c)
[a, b, c]
end
def yet_another(a, *heh)
[a, *heh]
end
end
@class.args_for(@class.instance_method(:hookable)).should == "&block"
@class.args_for(@class.instance_method(:some_method)).should == "_1, _2, _3, &block"
@class.args_for(@class.instance_method(:yet_another)).should == "_1, *args, &block"
end
end
end
|