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 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
|
RSpec.describe "a matcher defined using the matcher DSL" do
def question?
:answer
end
def ok
"ok"
end
it "supports calling custom matchers from within other custom matchers" do
RSpec::Matchers.define :be_ok do
match { |actual| actual == ok }
end
RSpec::Matchers.define :be_well do
match { |actual| expect(actual).to be_ok }
end
expect(ok).to be_well
end
it "has access to methods available in the scope of the example" do
RSpec::Matchers.define(:matcher_a) {}
expect(matcher_a.question?).to eq(:answer)
end
it "raises when method is missing from local scope as well as matcher" do
RSpec::Matchers.define(:matcher_b) {}
expect { matcher_b.i_dont_exist }.to raise_error(NameError)
end
if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
it 'supports the use of required keyword arguments in definition block' do
RSpec::Matchers.define(:match_required_kw) do |bar:|
match { expect(actual).to eq bar }
end
expect(1).to match_required_kw(bar: 1)
end
def kw(a:)
a
end
it "supports the use of required keyword arguments on methods" do
RSpec::Matchers.define(:matcher_required_kw_on_method) {}
expect(matcher_required_kw_on_method.kw(a: 1)).to eq(1)
end
CODE
end
if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
it 'supports the use of optional keyword arguments in definition block' do
RSpec::Matchers.define(:match_optional_kw) do |bar: nil|
match { expect(actual).to eq bar }
end
expect(1).to match_optional_kw(bar: 1)
end
def optional_kw(a: nil)
a
end
it "supports the use of optional keyword arguments on methods" do
RSpec::Matchers.define(:matcher_optional_kw_on_method) {}
expect(matcher_optional_kw_on_method.optional_kw(a: 1)).to eq(1)
end
CODE
end
it "clears user instance variables between invocations" do
RSpec::Matchers.define(:be_just_like) do |expected|
match do |actual|
@foo ||= expected
@foo == actual
end
end
expect(3).to be_just_like(3)
expect(4).to be_just_like(4)
end
describe '#block_arg' do
before(:context) do
RSpec::Matchers.define :be_lazily_equal_to do
match { actual == block_arg.call }
description do
"be lazily equal to #{block_arg.call}"
end
end
end
it "it is used in a passing condition" do
expect(1).to be_lazily_equal_to { 1 }
end
it "it is used in a failing condition" do
expect { expect(1).to be_lazily_equal_to { 2 } }.to fail_with(/be lazily equal to 2/)
end
end
it "warns when passing block to the block of define", :if => (RUBY_VERSION.to_f > 1.8) do
expect(RSpec).to receive(:warning).with(/be_warning.*a_block.*block_arg/)
RSpec::Matchers.define :be_warning do |&a_block|
match { a_block }
end
end
describe "#respond_to?" do
it "returns true for methods in example scope" do
RSpec::Matchers.define(:matcher_c) {}
expect(matcher_c).to respond_to(:question?)
end
it "returns false for methods not defined in matcher or example scope" do
RSpec::Matchers.define(:matcher_d) {}
expect(matcher_d).not_to respond_to(:i_dont_exist)
end
end
end
class UnexpectedError < StandardError; end
module MatcherHelperModule
def self.included(base)
base.module_exec do
def included_method; end
end
end
def self.extended(base)
base.instance_exec do
def extended_method; end
end
end
def greeting
"Hello, World"
end
end
module RSpec::Matchers::DSL
RSpec.describe "#alias_matcher" do
describe "an alias matcher defined in the current scope" do
alias_matcher :be_untrue_in_this_scope, :be_falsy
it "is available only in the current scope" do
expect(false).to be_untrue_in_this_scope
end
end
describe "an aliased matcher defined in another scope" do
it "is not available in the current scope" do
expect {
expect(false).to be_untrue_in_this_scope
}.to fail_with("expected false to respond to `untrue_in_this_scope?`")
end
end
end
RSpec.describe "#define_negated_matcher" do
describe "a negated matcher defined in the current scope" do
define_negated_matcher :be_untrue_in_this_scope, :be_truthy
it "is available only in the current scope" do
expect(false).to be_untrue_in_this_scope
end
end
describe "a negated matcher defined in another scope" do
it "is not available in the current scope" do
expect {
expect(false).to be_untrue_in_this_scope
}.to fail_with("expected false to respond to `untrue_in_this_scope?`")
end
end
end
RSpec.describe Matcher do
def new_matcher(name, *expected, &block)
RSpec::Matchers::DSL::Matcher.new(name, block, self, *expected)
end
it_behaves_like "an RSpec value matcher", :valid_value => 1, :invalid_value => 2 do
let(:matcher) do
new_matcher(:equal_to_1) do
match { |v| v == 1 }
end
end
end
it "can be stored aside and used later" do
# Supports using rspec-expectation matchers as argument matchers in
# rspec-mocks.
RSpec::Matchers.define :example_matcher do |expected|
match do |actual|
actual == expected
end
end
m1 = example_matcher(1)
m2 = example_matcher(2)
expect(m1.matches?(1)).to be_truthy
expect(m2.matches?(2)).to be_truthy
end
context 'using deprecated APIs' do
before { allow_deprecation }
describe "failure_message_for_should" do
let(:matcher) do
new_matcher(:foo) do
match { false }
failure_message_for_should { "failed" }
end
end
line = __LINE__ - 3
it 'defines the failure message for a positive expectation' do
expect {
expect(nil).to matcher
}.to fail_with("failed")
end
it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /failure_message_for_should/)
matcher
end
end
describe "failure_message_for_should_not" do
let(:matcher) do
new_matcher(:foo) do
match { true }
failure_message_for_should_not { "failed" }
end
end
line = __LINE__ - 3
it 'defines the failure message for a negative expectation' do
expect {
expect(nil).not_to matcher
}.to fail_with("failed")
end
it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /failure_message_for_should_not/)
matcher
end
end
describe "match_for_should" do
let(:matcher) do
new_matcher(:foo) do
match_for_should { |arg| arg }
end
end
line = __LINE__ - 3
it 'defines the positive expectation match logic' do
expect(true).to matcher
expect { expect(false).to matcher }.to fail_with(/foo/)
end
it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /match_for_should/)
matcher
end
end
describe "match_for_should_not" do
let(:matcher) do
new_matcher(:foo) do
match_for_should_not { |arg| !arg }
end
end
line = __LINE__ - 3
it 'defines the positive expectation match logic' do
expect(false).not_to matcher
expect { expect(true).not_to matcher }.to fail_with(/foo/)
end
it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /match_for_should_not/)
matcher
end
end
end
context "with an included module" do
let(:matcher) do
new_matcher(:be_a_greeting) do
include MatcherHelperModule
match { |actual| actual == greeting }
end
end
it "has access to the module's methods" do
matcher.matches?("Hello, World")
end
it "runs the module's included hook" do
expect(matcher).to respond_to(:included_method)
end
it "does not run the module's extended hook" do
expect(matcher).not_to respond_to(:extended_method)
end
it 'allows multiple modules to be included at once' do
m = new_matcher(:multiple_modules) do
include Enumerable
include Comparable
end
expect(m).to be_a(Enumerable)
expect(m).to be_a(Comparable)
end
end
context "without overrides" do
let(:matcher) do
new_matcher(:be_a_multiple_of, 3) do |multiple|
match do |actual|
actual % multiple == 0
end
end
end
it "provides a default description" do
expect(matcher.description).to eq "be a multiple of 3"
end
it "provides a default positive expectation failure message" do
expect { expect(8).to matcher }.to fail_with 'expected 8 to be a multiple of 3'
end
it "provides a default negative expectation failure message" do
expect { expect(9).to_not matcher }.to fail_with 'expected 9 not to be a multiple of 3'
end
end
context "without overrides with chained matchers" do
let(:matcher) do
new_matcher(:be_bigger_than, 5) do |five|
match do |to_match|
(to_match > five) && smaller_than_ceiling?(to_match) && divisible_by_divisor?(to_match)
end
match_when_negated do |to_match|
(to_match <= five) || greater_than_ceiling(to_match) && not_divisible_by_divisor?(to_match)
end
chain :and_smaller_than do |ceiling|
@ceiling = ceiling
end
chain :and_divisible_by do |divisor|
@divisor = divisor
end
private
def smaller_than_ceiling?(to_match)
to_match < @ceiling
end
def greater_than_ceiling(to_match)
to_match >= @ceiling
end
def divisible_by_divisor?(to_match)
@divisor % to_match == 0
end
def not_divisible_by_divisor?(to_match)
@divisor % to_match != 0
end
end
end
context "when the matchers are chained" do
include_context "isolate include_chain_clauses_in_custom_matcher_descriptions"
context "without include_chain_clauses_in_custom_matcher_descriptions configured" do
before { RSpec::Expectations.configuration.include_chain_clauses_in_custom_matcher_descriptions = false }
let(:match) { matcher.and_smaller_than(10).and_divisible_by(3) }
it "provides a default description that does not include any of the chained matchers' descriptions" do
expect(match.description).to eq 'be bigger than 5'
end
it "provides a default positive expectation failure message that does not include any of the chained matchers' descriptions" do
expect { expect(8).to match }.to fail_with 'expected 8 to be bigger than 5'
end
it "provides a default negative expectation failure message that does not include the any of the chained matchers's descriptions" do
expect { expect(9).to_not match }.to fail_with 'expected 9 not to be bigger than 5'
end
end
context "with include_chain_clauses_in_custom_matcher_descriptions configured to be true" do
before do
expect(RSpec::Expectations.configuration.include_chain_clauses_in_custom_matcher_descriptions?).to be true
end
it "provides a default description that includes the chained matchers' descriptions in they were used" do
expect(matcher.and_divisible_by(3).and_smaller_than(29).and_smaller_than(20).and_divisible_by(5).description).to \
eq 'be bigger than 5 and divisible by 3 and smaller than 29 and smaller than 20 and divisible by 5'
end
it "provides a default positive expectation failure message that includes the chained matchers' failures" do
expect { expect(30).to matcher.and_smaller_than(29).and_divisible_by(3) }.to \
fail_with 'expected 30 to be bigger than 5 and smaller than 29 and divisible by 3'
end
it "provides a default negative expectation failure message that includes the chained matchers' failures" do
expect { expect(21).to_not matcher.and_smaller_than(29).and_divisible_by(3) }.to \
fail_with 'expected 21 not to be bigger than 5 and smaller than 29 and divisible by 3'
end
end
it 'only decides if to include the chained clauses at the time description is invoked' do
matcher.and_divisible_by(3)
expect {
RSpec::Expectations.configuration.include_chain_clauses_in_custom_matcher_descriptions = false
}.to change { matcher.description }.
from('be bigger than 5 and divisible by 3').
to('be bigger than 5')
end
end
end
context "with separate match logic for positive and negative expectations" do
let(:matcher) do
new_matcher(:to_be_composed_of, 7, 11) do |a, b|
match do |actual|
actual == a * b
end
match_when_negated do |actual|
actual == a + b
end
end
end
it "invokes the match block for #matches?" do
expect(matcher.matches?(77)).to be_truthy
expect(matcher.matches?(18)).to be_falsey
end
it "invokes the match_when_negated block for #does_not_match?" do
expect(matcher.does_not_match?(77)).to be_falsey
expect(matcher.does_not_match?(18)).to be_truthy
end
it "provides a default failure message for negative expectations" do
matcher.does_not_match?(77)
expect(matcher.failure_message_when_negated).to eq "expected 77 not to to be composed of 7 and 11"
end
it 'can access helper methods from `match_when_negated`' do
matcher = new_matcher(:be_foo) do
def foo
:foo
end
match_when_negated do |actual|
actual != foo
end
end
expect(matcher.does_not_match?(:bar)).to be true
end
end
it "allows helper methods to be defined with #define_method to have access to matcher parameters" do
matcher = new_matcher(:name, 3, 4) do |a, b|
define_method(:sum) { a + b }
end
expect(matcher.sum).to eq 7
end
it "is not diffable by default" do
matcher = new_matcher(:name) {}
expect(matcher).not_to be_diffable
end
it "is diffable when told to be" do
matcher = new_matcher(:name) { diffable }
expect(matcher).to be_diffable
end
it 'handles multiline string diffs' do
actual = "LINE1\nline2\n"
expected = "line1\nline2\n"
matcher = new_matcher(:custom_match, expected) do
match { |act| act == expected }
diffable
end
diff = nil
begin
allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
expect(actual).to matcher
rescue RSpec::Expectations::ExpectationNotMetError => e
diff = e.message.sub(/\A.*Diff:/m, "Diff:").gsub(/^\s*/, '')
end
if Diff::LCS::VERSION.to_f < 1.4
expected_diff = "Diff:\n@@ -1,3 +1,3 @@\n-line1\n+LINE1\nline2\n"
else
expected_diff = "Diff:\n@@ -1 +1 @@\n-line1\n+LINE1\n"
end
expect(diff).to eq expected_diff
end
it 'does not confuse the diffability of different matchers' do
# Necessary to guard against a regression that involved
# using a class variable to store the diffable state,
# which had the side effect of causing all custom matchers
# to share that state
m1 = new_matcher(:m1) { diffable }
m2 = new_matcher(:m2) {}
m3 = new_matcher(:m3) { diffable }
expect(m1).to be_diffable
expect(m2).not_to be_diffable
expect(m3).to be_diffable
end
it "provides expected" do
matcher = new_matcher(:name, "expected string") {}
expect(matcher.expected).to eq 'expected string'
end
it "provides expected when there is more than one argument" do
matcher = new_matcher(:name, "expected string", "another arg") {}
expect(matcher.expected).to eq ['expected string', "another arg"]
end
it "provides expected_as_array which returns an array regardless of expected" do
matcher = new_matcher(:name, "expected string") {}
expect(matcher.expected_as_array).to eq ['expected string']
matcher = new_matcher(:name, "expected\nstring") {}
expect(matcher.expected_as_array).to eq ["expected\nstring"]
matcher = new_matcher(:name, "expected string", "another arg") {}
expect(matcher.expected_as_array).to eq ['expected string', "another arg"]
end
it "provides actual when `match` is used" do
matcher = new_matcher(:name, 'expected string') do
match { |actual| }
end
matcher.matches?('actual string')
expect(matcher.actual).to eq 'actual string'
end
it "provides actual when the `match` block accepts splat args" do
matcher = new_matcher(:actual) do
match { |*actual| actual == [5] }
end
expect(matcher.matches?(5)).to be true
expect(matcher.matches?(4)).to be false
end
it 'allows an early `return` to be used from a `match` block' do
matcher = new_matcher(:with_return, 5) do |expected|
match { |actual| return true if expected == actual }
end
expect(matcher.matches?(5)).to be true
expect(matcher.matches?(4)).to be_falsey
end
it 'provides actual when `match_unless_raises` is used' do
matcher = new_matcher(:name, 'expected string') do
match_unless_raises(SyntaxError) { |actual| }
end
matcher.matches?('actual string')
expect(matcher.actual).to eq 'actual string'
end
it 'allows an early `return` to be used from a `match_unless_raises` block' do
matcher = new_matcher(:with_return) do
match_unless_raises(ArgumentError) do |actual|
return actual if [true, false].include?(actual)
raise ArgumentError
end
end
expect(matcher.matches?(true)).to be true
# It should match even if it returns false, because no error was raised.
expect(matcher.matches?(false)).to be true
expect(matcher.matches?(4)).to be_falsey
end
it 'provides actual when `match_when_negated` is used' do
matcher = new_matcher(:name, 'expected string') do
match_when_negated { |actual| }
end
matcher.does_not_match?('actual string')
expect(matcher.actual).to eq 'actual string'
end
it 'allows an early `return` to be used from a `match_when_negated` block' do
matcher = new_matcher(:with_return, 5) do |expected|
match_when_negated { |actual| return true if expected != actual }
end
expect(matcher.does_not_match?(5)).to be_falsey
expect(matcher.does_not_match?(4)).to be true
end
context "wrapping another expectation in a `match` block" do
context "with a positive expectation" do
let(:matcher) do
new_matcher(:name, "value") do |expected|
match do |actual|
expect(actual).to eq expected
end
end
end
specify "`match?` returns true if the wrapped expectation passes" do
expect(matcher.matches?('value')).to be_truthy
end
specify "`match?` returns false if the wrapped expectation fails" do
expect(matcher.matches?('other value')).to be_falsey
end
end
context "with a negative expectation" do
let(:matcher) do
new_matcher(:name, "purposely_the_same") do |expected|
match do |actual|
expect(actual).not_to eq expected
end
end
end
specify "`match?` returns true if the wrapped expectation passes" do
expect(matcher.matches?('purposely_different')).to be_truthy
end
specify "`match?` returns false if the wrapped expectation fails" do
expect(matcher.matches?('purposely_the_same')).to be_falsey
end
end
it "can use the `include` matcher from a `match` block" do
RSpec::Matchers.define(:descend_from) do |mod|
match do |klass|
expect(klass.ancestors).to include(mod)
end
end
expect(Integer).to descend_from(Object)
expect(Integer).not_to descend_from(Array)
expect {
expect(Integer).to descend_from(Array)
}.to fail_with(/expected Integer to descend from Array/)
expect {
expect(Integer).not_to descend_from(Object)
}.to fail_with(/expected Integer not to descend from Object/)
end
it "can use the `match` matcher from a `match` block" do
RSpec::Matchers.define(:be_a_phone_number_string) do
match do |string|
expect(string).to match(/\A\d{3}\-\d{3}\-\d{4}\z/)
end
end
expect("206-123-1234").to be_a_phone_number_string
expect("foo").not_to be_a_phone_number_string
expect {
expect("foo").to be_a_phone_number_string
}.to fail_with(/expected "foo" to be a phone number string/)
expect {
expect("206-123-1234").not_to be_a_phone_number_string
}.to fail_with(/expected "206-123-1234" not to be a phone number string/)
end
context "when used within an `aggregate_failures` block" do
it 'does not aggregate the inner expectation failure' do
use_an_internal_expectation = new_matcher(:use_an_internal_expectation) do
match do |actual|
expect(actual).to end_with "z"
end
end
expect {
aggregate_failures do
expect(1).to be_even
expect("foo").to use_an_internal_expectation
end
}.to fail do |error|
expect(error).to have_attributes(:failures => [
an_object_having_attributes(:message => "expected `1.even?` to return true, got false"),
an_object_having_attributes(:message => 'expected "foo" to use an internal expectation')
])
end
end
it 'does not aggregate the inner expectation failure (negation)' do
use_an_internal_expectation = new_matcher(:use_an_internal_expectation) do
match_when_negated do |actual|
expect(actual).not_to end_with "o"
end
end
expect {
aggregate_failures do
expect(1).to be_even
expect("foo").not_to use_an_internal_expectation
end
}.to fail do |error|
expect(error).to have_attributes(:failures => [
an_object_having_attributes(:message => "expected `1.even?` to return true, got false"),
an_object_having_attributes(:message => 'expected "foo" not to use an internal expectation')
])
end
end
it 'still raises the expectation failure internally in case the matcher relies upon rescuing the error' do
error_rescued = false
rescue_failure = new_matcher(:rescue_failure) do
match do |actual|
begin
expect(actual).to eq(2)
rescue RSpec::Expectations::ExpectationNotMetError
error_rescued = true
end
end
end
begin
aggregate_failures do
expect(1).to rescue_failure
end
rescue RSpec::Expectations::ExpectationNotMetError # rubocop:disable Lint/SuppressedException
end
expect(error_rescued).to be true
end
end
end
context "wrapping another expectation in a `match_when_negated` block" do
context "with a positive expectation" do
let(:matcher) do
new_matcher(:name, "purposely_the_same") do |expected|
match_when_negated do |actual|
expect(actual).to eq expected
end
end
end
specify "`does_not_match?` returns true if the wrapped expectation passes" do
expect(matcher.does_not_match?('purposely_the_same')).to be_truthy
end
specify "`does_not_match?` returns false if the wrapped expectation fails" do
expect(matcher.does_not_match?('purposely_different')).to be_falsey
end
end
context "with a negative expectation" do
let(:matcher) do
new_matcher(:name, "value") do |expected|
match_when_negated do |actual|
expect(actual).not_to eq expected
end
end
end
specify "`does_not_match?` returns true if the wrapped expectation passes" do
expect(matcher.does_not_match?('other value')).to be_truthy
end
specify "`does_not_match?` returns false if the wrapped expectation fails" do
expect(matcher.does_not_match?('value')).to be_falsey
end
end
end
context "with overrides" do
let(:matcher) do
new_matcher(:be_boolean, true) do |boolean|
match do |actual|
actual
end
description do |actual|
"be the boolean #{boolean} (actual was #{actual})"
end
failure_message do |actual|
"expected #{actual} to be the boolean #{boolean}"
end
failure_message_when_negated do |actual|
"expected #{actual} not to be the boolean #{boolean}"
end
end
end
it "does not hide result of match block when true" do
expect(matcher.matches?(true)).to be_truthy
end
it "does not hide result of match block when false" do
expect(matcher.matches?(false)).to be_falsey
end
it "overrides the description (which yields `actual`)" do
matcher.matches?(true)
expect(matcher.description).to eq "be the boolean true (actual was true)"
end
it "overrides the failure message for positive expectations" do
matcher.matches?(false)
expect(matcher.failure_message).to eq "expected false to be the boolean true"
end
it "overrides the failure message for negative expectations" do
matcher.matches?(true)
expect(matcher.failure_message_when_negated).to eq "expected true not to be the boolean true"
end
it 'can access helper methods from `description`' do
matcher = new_matcher(:desc) do
def subdesc() "sub description" end
description { "Desc (#{subdesc})" }
end
expect(matcher.description).to eq("Desc (sub description)")
end
it 'can access helper methods from `failure_message`' do
matcher = new_matcher(:positive_failure_message) do
def helper() "helper" end
failure_message { helper }
end
expect(matcher.failure_message).to eq("helper")
end
it 'can access helper methods from `failure_message_when_negated`' do
matcher = new_matcher(:negative_failure_message) do
def helper() "helper" end
failure_message_when_negated { helper }
end
expect(matcher.failure_message_when_negated).to eq("helper")
end
it 'can exit early with a `return` from `description` just like in a method' do
matcher = new_matcher(:desc) do
description { return "Desc" }
end
expect(matcher.description).to eq("Desc")
end
it 'can exit early with a `return` from `failure_message` just like in a method' do
matcher = new_matcher(:positive_failure_message) do
failure_message { return "msg" }
end
expect(matcher.failure_message).to eq("msg")
end
it 'can exit early with a `return` from `failure_message_when_negated` just like in a method' do
matcher = new_matcher(:negative_failure_message) do
failure_message_when_negated { return "msg" }
end
expect(matcher.failure_message_when_negated).to eq("msg")
end
end
context "with description override and chained matcher" do
context "by default" do
let(:matcher) do
new_matcher(:be_even) do
match do |to_match|
to_match.even? && (to_match % @divisible_by == 0)
end
chain :and_divisible_by do |divisible_by|
@divisible_by = divisible_by
end
description { super() + " and divisible by #{@divisible_by}" }
end
end
context "with include_chain_clauses_in_custom_matcher_descriptions configured to false" do
include_context "isolate include_chain_clauses_in_custom_matcher_descriptions"
before { RSpec::Expectations.configuration.include_chain_clauses_in_custom_matcher_descriptions = false }
it "provides a default description that does not include any of the chained matchers' descriptions" do
expect(matcher.and_divisible_by(10).description).to eq 'be even and divisible by 10'
end
end
context "with include_chain_clauses_in_custom_matcher_descriptions configured to true" do
it "provides a default description that does includes the chained matchers' descriptions" do
expect(matcher.and_divisible_by(10).description).to eq 'be even and divisible by 10 and divisible by 10'
end
end
end
end
context "matching blocks" do
it 'cannot match blocks by default' do
matcher = new_matcher(:foo) { match { true } }
expect(3).to matcher
expect {
expect { 3 }.to matcher
}.to fail_with(/must pass an argument/)
end
it 'can match blocks if it declares `supports_block_expectations`' do
matcher = new_matcher(:foo) do
match { true }
supports_block_expectations
end
expect(3).to matcher
expect { 3 }.to matcher
end
it 'will not swallow expectation errors from blocks when told to' do
matcher = new_matcher(:foo) do
match(:notify_expectation_failures => true) do |actual|
actual.call
true
end
supports_block_expectations
end
expect {
expect { raise RSpec::Expectations::ExpectationNotMetError.new('original') }.to matcher
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /original/)
end
end
context "matching blocks when negated" do
it 'cannot match blocks by default' do
matcher = new_matcher(:foo) { match_when_negated { true } }
expect(3).to_not matcher
expect {
expect { 3 }.to_not matcher
}.to fail_with(/must pass an argument/)
end
it 'can match blocks if it declares `supports_block_expectations`' do
matcher = new_matcher(:foo) do
match_when_negated { true }
supports_block_expectations
end
expect(3).to_not matcher
expect { 3 }.to_not matcher
end
it 'will not swallow expectation errors from blocks when told to' do
matcher = new_matcher(:foo) do
match_when_negated(:notify_expectation_failures => true) do |actual|
actual.call
true
end
supports_block_expectations
end
expect {
expect { raise RSpec::Expectations::ExpectationNotMetError.new('original') }.to_not matcher
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /original/)
end
end
context "#new" do
it "passes matches? arg to match block" do
matcher = new_matcher(:ignore) do
match do |actual|
actual == 5
end
end
expect(matcher.matches?(5)).to be_truthy
end
it "exposes arg submitted through #new to matcher block" do
matcher = new_matcher(:ignore, 4) do |expected|
match do |actual|
actual > expected
end
end
expect(matcher.matches?(5)).to be_truthy
end
end
context "with no args" do
let(:matcher) do
new_matcher(:matcher_name) do
match do |actual|
actual == 5
end
end
end
it "matches" do
expect(matcher.matches?(5)).to be_truthy
end
it "describes" do
expect(matcher.description).to eq "matcher name"
end
end
context "with 1 arg" do
let(:matcher) do
new_matcher(:matcher_name, 1) do |expected|
match do |actual|
actual == 5 && expected == 1
end
end
end
it "matches" do
expect(matcher.matches?(5)).to be_truthy
end
it "describes" do
expect(matcher.description).to eq "matcher name 1"
end
end
context "with multiple args" do
let(:matcher) do
new_matcher(:matcher_name, 1, 2, 3, 4) do |a, b, c, d|
match do |sum|
a + b + c + d == sum
end
end
end
it "matches" do
expect(matcher.matches?(10)).to be_truthy
end
it "describes" do
expect(matcher.description).to eq "matcher name 1, 2, 3, and 4"
end
end
it "supports helper methods" do
matcher = new_matcher(:be_similar_to, [1, 2, 3]) do |sample|
match do |actual|
similar?(sample, actual)
end
def similar?(a, b)
a.sort == b.sort
end
end
expect(matcher.matches?([2, 3, 1])).to be_truthy
end
it "supports fluent interface" do
matcher = new_matcher(:first_word) do
def second_word
self
end
end
expect(matcher.second_word).to eq matcher
end
it "treats method missing normally for undeclared methods" do
matcher = new_matcher(:ignore) {}
expect { matcher.non_existent_method }.to raise_error(NoMethodError)
end
it "has access to other matchers" do
matcher = new_matcher(:ignore, 3) do |expected|
match do |actual|
extend RSpec::Matchers
expect(actual).to eql(5 + expected)
end
end
expect(matcher.matches?(8)).to be_truthy
end
context 'when multiple instances of the same matcher are used in the same example' do
RSpec::Matchers.define(:be_like_a) do |expected|
match { |actual| actual == expected }
description { "be like a #{expected}" }
failure_message { "expected to be like a #{expected}" }
failure_message_when_negated { "expected not to be like a #{expected}" }
end
# Note: these bugs were only exposed when creating both instances
# first, then checking their descriptions/failure messages.
#
# That's why we eager-instantiate them here.
let!(:moose) { be_like_a("moose") }
let!(:horse) { be_like_a("horse") }
it 'allows them to use the expected value in the description' do
expect(horse.description).to eq("be like a horse")
expect(moose.description).to eq("be like a moose")
end
it 'allows them to use the expected value in the positive failure message' do
expect(moose.failure_message).to eq("expected to be like a moose")
expect(horse.failure_message).to eq("expected to be like a horse")
end
it 'allows them to use the expected value in the negative failure message' do
expect(moose.failure_message_when_negated).to eq("expected not to be like a moose")
expect(horse.failure_message_when_negated).to eq("expected not to be like a horse")
end
it 'allows them to match separately' do
expect("moose").to moose
expect("horse").to horse
expect("horse").not_to moose
expect("moose").not_to horse
end
end
describe "#match_unless_raises" do
context "with an assertion" do
mod = Module.new do
def assert_equal(a, b)
raise UnexpectedError.new("#{b} does not equal #{a}") unless a == b
end
end
let(:matcher) do
new_matcher(:equal, 4) do |expected|
include mod
match_unless_raises UnexpectedError do
assert_equal expected, actual
end
end
end
context "with passing assertion" do
it "passes" do
expect(matcher.matches?(4)).to be_truthy
end
end
context "with failing assertion" do
it "fails" do
expect(matcher.matches?(5)).to be_falsey
end
it "provides the raised exception" do
matcher.matches?(5)
expect(matcher.rescued_exception.message).to eq("5 does not equal 4")
end
end
end
context "with an unexpected error" do
it "raises the error" do
matcher = new_matcher(:foo, :bar) do |_expected|
match_unless_raises SyntaxError do |_actual|
raise "unexpected exception"
end
end
expect {
matcher.matches?(:bar)
}.to raise_error("unexpected exception")
end
end
context "without a specified error class" do
let(:matcher) do
new_matcher(:foo) do
match_unless_raises do |actual|
raise Exception unless actual == 5
end
end
end
it 'passes if no error is raised' do
expect(matcher.matches?(5)).to be true
end
it 'fails if an exception is raised' do
expect(matcher.matches?(4)).to be false
end
end
end
it "can define chainable methods" do
matcher = new_matcher(:name) do
chain(:expecting) do |expected_value|
@expected_value = expected_value
end
match { |actual| actual == @expected_value }
end
expect(matcher.expecting('value').matches?('value')).to be_truthy
expect(matcher.expecting('value').matches?('other value')).to be_falsey
end
it "can define chainable setters" do
matcher = new_matcher(:name) do
chain(:expecting, :expected_value)
match { |actual| actual == expected_value }
end
expect(matcher.expecting('value').matches?('value')).to be_truthy
expect(matcher.expecting('value').matches?('other value')).to be_falsey
end
it "can define chainable setters for several attributes" do
matcher = new_matcher(:name) do
chain(:expecting, :expected_value, :min_value, :max_value)
match { |actual| actual == expected_value && actual >= min_value && actual <= max_value }
end
expect(matcher.expecting('value', 'apple', 'zebra').matches?('value')).to be_truthy
expect(matcher.expecting('value', 'apple', 'zebra').matches?('other value')).to be_falsey
expect(matcher.expecting('other value', 'parrot', 'zebra').matches?('other value')).to be_falsey
end
it "raises when neither a `chain` block nor attribute name is provided" do
expect do
new_matcher(:name) do
chain(:expecting)
end
end.to raise_error(ArgumentError)
end
it "raises when both a `chain` block and attribute name are provided" do
expect do
new_matcher(:name) do
chain(:expecting, :expected_value) do |expected_value|
@expected_value = expected_value
end
end
end.to raise_error(ArgumentError)
end
it 'can use an early return from a `chain` block' do
matcher = new_matcher(:name) do
chain(:expecting) do |expected_value|
@expected_value = expected_value
return
end
match { |actual| actual == @expected_value }
end
expect(matcher.expecting('value').matches?('value')).to be_truthy
expect(matcher.expecting('value').matches?('other value')).to be_falsey
end
it 'allows chainable methods to accept blocks' do
matcher = new_matcher(:name) do
chain(:for_block) { |&b| @block = b }
match { |value| @block.call == value }
end
expect(matcher.for_block { 5 }.matches?(5)).to be true
expect(matcher.for_block { 3 }.matches?(4)).to be false
end
it "prevents name collisions on chainable methods from different matchers" do
m1 = new_matcher(:m1) { chain(:foo) { raise "foo in m1" } }
m2 = new_matcher(:m2) { chain(:foo) { raise "foo in m2" } }
expect { m1.foo }.to raise_error("foo in m1")
expect { m2.foo }.to raise_error("foo in m2")
end
context "defined using the dsl" do
def a_method_in_the_example
"method defined in the example"
end
it "can access methods in the running example" do |example|
RSpec::Matchers.define(:__access_running_example) do
match do |_actual|
a_method_in_the_example == "method defined in the example"
end
end
expect(example).to __access_running_example
end
it 'can get a method object for methods in the running example', :if => (RUBY_VERSION.to_f > 1.8) do
matcher = new_matcher(:get_method_object) {}
method = matcher.method(:a_method_in_the_example)
expect(method.call).to eq("method defined in the example")
end
it 'indicates that it responds to a method from the running example' do
matcher = new_matcher(:respond_to) {}
expect(matcher).to respond_to(:a_method_in_the_example)
expect(matcher).not_to respond_to(:a_method_not_in_the_example)
end
it "raises NoMethodError for methods not in the running_example" do |example|
RSpec::Matchers.define(:__raise_no_method_error) do
match do |_actual|
self.a_method_not_in_the_example == "method defined in the example" # rubocop:disable Style/RedundantSelf RuboCop bug, should disappear on version update
end
end
expected_msg = "RSpec::Matchers::DSL::Matcher"
expected_msg = "#{expected_msg} __raise_no_method_error" unless rbx? || RUBY_VERSION.to_f > 3.2
expect {
expect(example).to __raise_no_method_error
}.to raise_error(NoMethodError, /#{expected_msg}/)
end
def rbx?
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
end
end
end
end
|