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
|
# This class fakes some behavior of
# ActiveSupport::HashWithIndifferentAccess.
# It doesn't convert recursively.
class FakeHashWithIndifferentAccess < Hash
class << self
def from_hash(hsh)
new_hsh = new
hsh.each do |key, value|
new_hsh[key] = value
end
new_hsh
end
end
def [](key)
super(key.to_s)
end
def []=(key, value)
super(key.to_s, value)
end
def key?(key)
super(key.to_s)
end
def to_hash
new_hsh = ::Hash.new
each do |key, value|
new_hsh[key] = value
end
new_hsh
end
end
# Some custom hashes don't support fuzzy matching
class CaseInsensitiveHash < Hash
class << self
def from_hash(hsh)
new_hsh = new
hsh.each do |key, value|
new_hsh[key] = value
end
new_hsh
end
end
def include?(key)
super(key.downcase)
end
alias :key? :include?
end
RSpec.describe "#include matcher" do
include RSpec::Support::Spec::DiffHelpers
it "is diffable" do
expect(include("a")).to be_diffable
end
shared_examples_for "a Hash target" do
def build_target(hsh)
hsh
end
def use_string_keys_in_failure_message?
false
end
def convert_key(key)
use_string_keys_in_failure_message? ? "\"#{key}\"" : ":#{key}"
end
it 'passes if target has the expected as a key' do
expect(build_target(:key => 'value')).to include(:key)
end
it 'passes if target has the expected as a key fuzzily matched' do
expect(build_target('KEY' => 'value')).to include(match(/key/i))
end
it "fails if target does not include expected" do
failure_string = %(expected {#{convert_key(:key)} => "value"} to include :other)
expect {
expect(build_target(:key => 'value')).to include(:other)
}.to fail_matching(failure_string)
end
it "fails if target doesn't have a key and we expect nil" do
expect {
expect(build_target({})).to include(:something => nil)
}.to fail_matching("expected {} to include {:something => nil}")
end
it 'works even when an entry in the hash overrides #send' do
hash = build_target(:key => 'value')
def hash.send; :sent; end
expect(hash).to include(hash)
end
it 'provides a valid diff' do
allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
failure_string = if use_string_keys_in_failure_message?
dedent(<<-END)
|Diff:
|@@ -1,3 +1,3 @@
|-:bar => 3,
|-:foo => 1,
|+"bar" => 2,
|+"foo" => 1,
END
else
diff = dedent(<<-END)
|Diff:
|@@ #{one_line_header(3)} @@
|-:bar => 3,
|+:bar => 2,
END
diff << "\n :foo => 1,\n" if Diff::LCS::VERSION.to_f < 1.4
diff
end
expect {
expect(build_target(:foo => 1, :bar => 2)).to include(:foo => 1, :bar => 3)
}.to fail_including(failure_string)
end
it 'provides a valid diff for fuzzy matchers' do
allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
failure_string = if use_string_keys_in_failure_message?
dedent(<<-END)
|Diff:
|@@ -1,3 +1,3 @@
|-(match /FOO/i) => 1,
|-:bar => 3,
|+"bar" => 2,
|+"foo" => 1,
END
else
dedent(<<-END)
|Diff:
|@@ -1,3 +1,3 @@
|-(match /FOO/i) => 1,
|-:bar => 3,
|+:bar => 2,
|+:foo => 1,
END
end
expect {
expect(build_target(:foo => 1, :bar => 2)).to include(match(/FOO/i) => 1, :bar => 3)
}.to fail_including(failure_string)
end
it 'does not support count constraint' do
expect {
expect(build_target(:key => 'value')).to include(:other).once
}.to raise_error(NotImplementedError)
end
end
describe "expect(...).to include(with_one_arg)" do
it_behaves_like "an RSpec value matcher", :valid_value => [1, 2], :invalid_value => [1] do
let(:matcher) { include(2) }
end
context "for an object that does not respond to `include?`" do
it 'fails gracefully' do
expect {
expect(5).to include(1)
}.to fail_matching("expected 5 to include 1, but it does not respond to `include?`")
expect {
expect(5).to include(1).once
}.to fail_matching("expected 5 to include 1 once, but it does not respond to `include?`")
end
end
context "for an arbitrary object that responds to `include?`" do
it 'delegates to `include?`' do
container = double("Container")
allow(container).to receive(:include?) { |arg| arg == :stuff }
expect(container).to include(:stuff)
expect {
expect(container).to include(:space)
}.to fail_matching("to include :space")
end
end
context "for an arbitrary object that responds to `include?` and `to_hash`" do
it "delegates to `include?`" do
container = double("Container", :include? => true, :to_hash => { "foo" => "bar" })
expect(container).to receive(:include?).with("foo").and_return(true)
expect(container).to include("foo")
end
end
context "for a string target" do
it "passes if target includes expected" do
expect("abc").to include("a")
end
it "fails if target does not include expected" do
expect {
expect("abc").to include("d")
}.to fail_matching("expected \"abc\" to include \"d\"")
end
it "includes a diff when actual is multiline" do
expect {
expect("abc\ndef").to include("g")
}.to fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff")
end
it "includes a diff when actual is multiline and there are multiple expecteds" do
expect {
expect("abc\ndef").to include("g", "h")
}.to fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff")
end
it "does not diff when lines match but are not an exact match" do
expect {
expect(" foo\nbar\nbazz").to include("foo", "bar", "gaz")
}.to fail_with(a_string_not_matching(/Diff/i))
end
context "with exact count" do
it 'fails if the block yields wrong number of times' do
expect {
expect('foo bar foo').to include('foo').once
}.to fail_with(/expected "foo bar foo" to include "foo" once but it is included twice/)
end
it 'passes if the block yields the specified number of times' do
expect('fooo bar').to include('oo').once
expect('fooo bar').to include('o').thrice
expect('fooo ooo oo bar foo').to include('oo').exactly(4).times
end
end
context "with at_least count" do
it 'passes if the search term is included at least the number of times' do
expect('foo bar foo').to include('foo').at_least(2).times
expect('foo bar foo foo').to include('foo').at_least(:twice)
end
it 'fails if the search term is included too few times' do
expect {
expect('foo bar foo').to include('foo').at_least(:thrice)
}.to fail_with(/expected "foo bar foo" to include "foo" at least 3 times but it is included twice/)
end
end
context "with at_most count" do
it 'passes if the search term is included at most the number of times' do
expect('foo bar foo').to include('foo').at_most(2).times
expect('foo bar').to include('foo').at_most(:twice)
end
it 'fails if the search term is included too many times' do
expect {
expect('foo bar foo foo').to include('foo').at_most(:twice)
}.to fail_with(/expected "foo bar foo foo" to include "foo" at most twice but it is included 3 times/)
end
end
end
context "for an array target" do
it "passes if target includes expected" do
expect([1, 2, 3]).to include(3)
end
it "passes if target includes expected fuzzily matched" do
expect(["A", "B", "C"]).to include(match(/a/i))
end
it "fails if target does not include expected" do
expect {
expect([1, 2, 3]).to include(4)
}.to fail_matching("expected [1, 2, 3] to include 4")
end
it 'fails when given differing null doubles' do
dbl_1 = double.as_null_object
dbl_2 = double.as_null_object
expect {
expect([dbl_1]).to include(dbl_2)
}.to fail_matching("expected [#{dbl_1.inspect}] to include")
end
it 'passes when given the same null double' do
dbl = double.as_null_object
expect([dbl]).to include(dbl)
end
context "with exact count" do
it 'fails if the block yields wrong number of times' do
expect {
expect([1, 2, 1]).to include(1).once
}.to fail_with('expected [1, 2, 1] to include 1 once but it is included twice')
expect {
expect([10, 20, 30]).to include(a_value_within(2).of(17)).twice
}.to fail_with('expected [10, 20, 30] to include (a value within 2 of 17) twice but it is included 0 times')
end
it 'passes if the block yields the specified number of times' do
expect([1, 2, 1]).to include(1).twice
expect([10, 20, 30]).to include(a_value_within(5).of(17)).once
expect([{ 'a' => 1 }]).to include('a' => 1).once
end
end
context "with at_least count" do
it 'passes if the search term is included at least the number of times' do
expect([1, 2, 1]).to include(1).at_least(2).times
expect([1, 2, 1, 1]).to include(1).at_least(:twice)
end
it 'fails if the search term is included too few times' do
expect {
expect([1, 2, 1]).to include(1).at_least(:thrice)
}.to fail_with('expected [1, 2, 1] to include 1 at least 3 times but it is included twice')
end
end
context "with at_most count" do
it 'passes if the search term is included at most the number of times' do
expect([1, 2, 1]).to include(1).at_most(2).times
expect([1, 2]).to include(1).at_most(:twice)
end
it 'fails if the search term is included too many times' do
expect {
expect([1, 2, 1, 1]).to include(1).at_most(:twice)
}.to fail_with('expected [1, 2, 1, 1] to include 1 at most twice but it is included 3 times')
end
end
end
context "for a hash target" do
it_behaves_like "a Hash target"
end
context "for a target that subclasses Hash to treat string/symbol keys interchangeably, but returns a raw hash from #to_hash" do
it_behaves_like "a Hash target" do
undef :build_target # to prevent "method redefined" warning
def build_target(hsh)
FakeHashWithIndifferentAccess.from_hash(hsh)
end
undef :use_string_keys_in_failure_message? # to prevent "method redefined" warning
def use_string_keys_in_failure_message?
true
end
end
end
context "for a target that subclasses Hash to perform custom key checks like downcasing" do
it_behaves_like "a Hash target" do
undef :build_target # to prevent "method redefined" warning
def build_target(hsh)
CaseInsensitiveHash.from_hash(hsh)
end
end
end
context "for a target that can pass for a hash" do
def build_target(hsh)
PseudoHash.new(hsh)
end
around do |example|
in_sub_process_if_possible do
require 'delegate'
class PseudoHash < SimpleDelegator
end
example.run
end
end
it_behaves_like "a Hash target"
end
end
describe "expect(...).to include(with, multiple, args)" do
it "has a description" do
matcher = include("a")
expect(matcher.description).to eq("include \"a\"")
end
context "for a string target" do
it "passes if target includes all items" do
expect("a string").to include("str", "a")
end
it "fails if target does not include one of the items" do
expect {
expect("a string").to include("str", "a", "foo")
}.to fail_matching('expected "a string" to include "foo"')
end
it "fails if target does not include two of the items" do
expect {
expect("a string").to include("nope", "a", "nada", "str")
}.to fail_matching('expected "a string" to include "nope" and "nada"')
end
it "fails if target does not include many of the items" do
expect {
expect("a string").to include("nope", "a", "nada", "nein", "ing", "str")
}.to fail_matching('expected "a string" to include "nope", "nada", and "nein"')
end
end
context "for an array target" do
it "passes if target includes all items" do
expect([1, 2, 3]).to include(1, 2, 3)
end
it "passes if target includes all items fuzzily matched" do
expect(["A", "B", "C"]).to include(match(/b/i), "A")
end
it "fails if target does not include one of the items" do
expect {
expect([1, 2, 3]).to include(1, 2, 4)
}.to fail_matching("expected [1, 2, 3] to include 4")
end
it "fails if target does not include two of the items" do
expect {
expect([1, 2, 3]).to include(5, 1, 2, 4)
}.to fail_matching("expected [1, 2, 3] to include 5 and 4")
end
it "fails if target does not include many of the items" do
expect {
expect([1, 2, 3]).to include(5, 1, 6, 2, 4)
}.to fail_matching("expected [1, 2, 3] to include 5, 6, and 4")
end
it 'correctly diffs lists of hashes' do
allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
expect {
expect([
{ :number => 1 },
{ :number => 2 },
{ :number => 3 }
]).to include(
{ :number => 1 },
{ :number => 0 },
{ :number => 3 }
)
}.to fail_including(dedent(<<-END))
|Diff:
|@@ #{one_line_header} @@
|-[{:number=>1}, {:number=>0}, {:number=>3}]
|+[{:number=>1}, {:number=>2}, {:number=>3}]
END
end
end
context "for a hash target" do
it 'passes if target includes all items as keys' do
expect({ :key => 'value', :other => 'value' }).to include(:key, :other)
end
it "passes if target includes all items as keys fuzzily matched" do
expect({ "A" => "B", "C" => "D" }).to include(match(/c/i), "A")
end
it 'fails if target does not include one of the items as a key' do
expect {
expect({ :key => 'value', :this => 'that' }).to include(:key, :nope, :this)
}.to fail_with(%r|expected #{hash_inspect :key => "value", :this => "that"} to include :nope|)
end
it "fails if target does not include two of the items as keys" do
expect {
expect({ :key => 'value', :this => 'that' }).to include(:nada, :key, :nope, :this)
}.to fail_with(%r|expected #{hash_inspect :key => "value", :this => "that"} to include :nada and :nope|)
end
it "fails if target does not include many of the items as keys" do
expect {
expect({ :key => 'value', :this => 'that' }).to include(:nada, :key, :nope, :negative, :this)
}.to fail_with(%r|expected #{hash_inspect :key => "value", :this => "that"} to include :nada, :nope, and :negative|)
end
end
it 'does not implement count constraints' do
expect {
expect('').to include('foo', 'bar').once
}.to raise_error(NotImplementedError)
expect {
expect('').to include('foo', 'bar').at_least(:twice)
}.to raise_error(NotImplementedError)
expect {
expect('').to include('foo', 'bar').at_most(:twice)
}.to raise_error(NotImplementedError)
end
end
describe "expect(...).not_to include(expected)" do
context "for an object that does not respond to `include?`" do
it 'fails gracefully' do
expect {
expect(5).not_to include(1)
}.to fail_matching("expected 5 not to include 1, but it does not respond to `include?`")
end
end
context "for an arbitrary object that responds to `include?`" do
it 'delegates to `include?`' do
container = double("Container")
allow(container).to receive(:include?) { |arg| arg == :stuff }
expect(container).not_to include(:space)
expect {
expect(container).not_to include(:stuff)
}.to fail_matching("not to include :stuff")
end
end
context "for a string target" do
it "passes if target does not include expected" do
expect("abc").not_to include("d")
end
it "fails if target includes expected" do
expect {
expect("abc").not_to include("c")
}.to fail_with("expected \"abc\" not to include \"c\"")
end
context "with exact count" do
it 'passes if the block yields wrong number of times' do
expect('foo bar foo').not_to include('foo').once
end
it 'fails if the block yields the specified number of times' do
expect {
expect('fooo bar').not_to include('oo').once
}.to fail_with(/expected "fooo bar" not to include "oo" once but it is included once/)
end
end
context "with at_least count" do
it 'fails if the search term is included at least the number of times' do
expect {
expect('foo bar foo foo').not_to include('foo').at_least(:twice)
}.to fail_with(/expected "foo bar foo foo" not to include "foo" at least twice but it is included 3 times/)
end
it 'passes if the search term is included too few times' do
expect('foo bar foo').not_to include('foo').at_least(:thrice)
end
end
context "with at_most count" do
it 'fails if the search term is included at most the number of times' do
expect {
expect('foo bar').not_to include('foo').at_most(:twice)
}.to fail_with(/expected "foo bar" not to include "foo" at most twice but it is included once/)
end
it 'passes if the search term is included too many times' do
expect('foo bar foo foo').not_to include('foo').at_most(:twice)
end
end
end
context "for an array target" do
it "passes if target does not include expected" do
expect([1, 2, 3]).not_to include(4)
end
it "fails if target includes expected" do
expect {
expect([1, 2, 3]).not_to include(3)
}.to fail_with("expected [1, 2, 3] not to include 3")
end
it 'passes when given differing null doubles' do
expect([double.as_null_object]).not_to include(double.as_null_object)
end
it 'fails when given the same null double' do
dbl = double.as_null_object
expect {
expect([dbl]).not_to include(dbl)
}.to fail_matching("expected [#{dbl.inspect}] not to include")
end
end
context "for a hash target" do
it 'passes if target does not have the expected as a key' do
expect({ :other => 'value' }).not_to include(:key)
end
it "fails if target includes expected key" do
expect {
expect({ :key => 'value' }).not_to include(:key)
}.to fail_matching('expected {:key => "value"} not to include :key')
end
end
end
describe "expect(...).not_to include(with, multiple, args)" do
context "for a string target" do
it "passes if the target does not include any of the expected" do
expect("abc").not_to include("d", "e", "f")
end
it "fails if the target includes all of the expected" do
expect {
expect("abc").not_to include("c", "a")
}.to fail_with('expected "abc" not to include "c" and "a"')
end
it "fails if the target includes one (but not all) of the expected" do
expect {
expect("abc").not_to include("d", "a")
}.to fail_with('expected "abc" not to include "a"')
end
it "fails if the target includes two (but not all) of the expected" do
expect {
expect("abc").not_to include("d", "a", "b")
}.to fail_with('expected "abc" not to include "a" and "b"')
end
it "fails if the target includes many (but not all) of the expected" do
expect {
expect("abcd").not_to include("b", "d", "a", "f")
}.to fail_with('expected "abcd" not to include "b", "d", and "a"')
end
end
context "for a hash target" do
it "passes if it does not include any of the expected keys" do
expect({ :a => 1, :b => 2 }).not_to include(:c, :d)
end
it "fails if the target includes all of the expected keys" do
expect {
expect({ :a => 1, :b => 2 }).not_to include(:a, :b)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} not to include :a and :b|)
end
it "fails if the target includes one (but not all) of the expected keys" do
expect {
expect({ :a => 1, :b => 2 }).not_to include(:d, :b)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} not to include :b|)
end
it "fails if the target includes two (but not all) of the expected keys" do
expect {
expect({ :a => 1, :b => 2 }).not_to include(:a, :b, :c)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} not to include :a and :b|)
end
it "fails if the target includes many (but not all) of the expected keys" do
expect {
expect({ :a => 1, :b => 2, :c => 3 }).not_to include(:b, :a, :c, :f)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2, :c => 3} not to include :b, :a, and :c|)
end
end
context "for an array target" do
it "passes if the target does not include any of the expected" do
expect([1, 2, 3]).not_to include(4, 5, 6)
end
it "fails if the target includes all of the expected" do
expect {
expect([1, 2, 3]).not_to include(3, 1)
}.to fail_with('expected [1, 2, 3] not to include 3 and 1')
end
it "fails if the target includes one (but not all) of the expected" do
expect {
expect([1, 2, 3]).not_to include(4, 1)
}.to fail_with('expected [1, 2, 3] not to include 1')
end
it "fails if the target includes two (but not all) of the expected" do
expect {
expect([1, 2, 3]).not_to include(4, 1, 2)
}.to fail_with('expected [1, 2, 3] not to include 1 and 2')
end
it "fails if the target includes many (but not all) of the expected" do
expect {
expect([1, 2, 3]).not_to include(5, 4, 2, 1, 3)
}.to fail_with('expected [1, 2, 3] not to include 2, 1, and 3')
end
end
end
describe "expect(...).to include(:key => value)" do
context 'for a hash target' do
it "passes if target includes the key/value pair" do
expect({ :key => 'value' }).to include(:key => 'value')
end
it "passes if target includes the key/value pair among others" do
expect({ :key => 'value', :other => 'different' }).to include(:key => 'value')
end
it "passes if target includes the key/value pair fuzzily matched among others", :if => (RUBY_VERSION.to_f > 1.8) do
hsh = { :key => 'value', :other => 'different' }
expect(hsh).to include(match(/KEY/i) => 'value')
expect(FakeHashWithIndifferentAccess.from_hash(hsh)).to include(match(/KEY/i) => 'value')
expect(CaseInsensitiveHash.from_hash(hsh)).to include(match(/KEY/i) => 'value')
end
it "fails if target has a different value for key" do
expect {
expect({ :key => 'different' }).to include(:key => 'value')
}.to fail_matching('expected {:key => "different"} to include {:key => "value"}')
end
it "fails if target has a different key" do
expect {
expect({ :other => 'value' }).to include(:key => 'value')
}.to fail_matching('expected {:other => "value"} to include {:key => "value"}')
end
end
context 'for a non-hash target' do
it "fails if the target does not contain the given hash" do
expect {
expect(['a', 'b']).to include(:key => 'value')
}.to fail_matching('expected ["a", "b"] to include {:key => "value"}')
end
it "passes if the target contains the given hash" do
expect(['a', { :key => 'value' }]).to include(:key => 'value')
end
end
end
describe "expect(...).not_to include(:key => value)" do
context 'for a hash target' do
it "fails if target includes the key/value pair" do
expect {
expect({ :key => 'value' }).not_to include(:key => 'value')
}.to fail_matching('expected {:key => "value"} not to include {:key => "value"}')
end
it "fails if target includes the key/value pair among others" do
expect {
expect({ :key => 'value', :other => 'different' }).not_to include(:key => 'value')
}.to fail_with(%r|expected #{hash_inspect :key => "value", :other => "different"} not to include \{:key => "value"\}|)
end
it "passes if target has a different value for key" do
expect({ :key => 'different' }).not_to include(:key => 'value')
end
it "passes if target has a different key" do
expect({ :other => 'value' }).not_to include(:key => 'value')
end
end
context "for a non-hash target" do
it "passes if the target does not contain the given hash" do
expect(['a', 'b']).not_to include(:key => 'value')
end
it "fails if the target contains the given hash" do
expect {
expect(['a', { :key => 'value' }]).not_to include(:key => 'value')
}.to fail_matching('expected ["a", {:key => "value"}] not to include {:key => "value"}')
end
end
end
describe "expect(...).to include(:key1 => value1, :key2 => value2)" do
context 'for a hash target' do
it "passes if target includes the key/value pairs" do
expect({ :a => 1, :b => 2 }).to include(:b => 2, :a => 1)
end
it "passes if target includes the key/value pairs among others" do
expect({ :a => 1, :c => 3, :b => 2 }).to include(:b => 2, :a => 1)
end
it "fails if target has a different value for one of the keys" do
expect {
expect({ :a => 1, :b => 2 }).to include(:a => 2, :b => 2)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} to include #{hash_inspect :a => 2}|)
end
it "fails if target has a different value for both of the keys" do
expect {
expect({ :a => 1, :b => 1 }).to include(:a => 2, :b => 2)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 1} to include #{hash_inspect :a => 2, :b => 2}|)
end
it "fails if target lacks one of the keys" do
expect {
expect({ :a => 1, :b => 1 }).to include(:a => 1, :c => 1)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 1} to include #{hash_inspect :c => 1}|)
end
it "fails if target lacks both of the keys" do
expect {
expect({ :a => 1, :b => 1 }).to include(:c => 1, :d => 1)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 1} to include #{hash_inspect :c => 1, :d => 1}|)
end
it "fails if target lacks one of the keys and has a different value for another" do
expect {
expect({ :a => 1, :b => 2 }).to include(:c => 1, :b => 3)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} to include #{hash_inspect :c => 1, :b => 3}|)
end
end
context 'for a non-hash target' do
it "fails if the target does not contain the given hash" do
expect {
expect(['a', 'b']).to include(:a => 1, :b => 1)
}.to fail_with(%r|expected \["a", "b"\] to include #{hash_inspect :a => 1, :b => 1}|)
end
it "passes if the target contains the given hash" do
expect(['a', { :a => 1, :b => 2 }]).to include(:a => 1, :b => 2)
end
end
end
describe "expect(...).not_to include(:key1 => value1, :key2 => value2)" do
context 'for a hash target' do
it "fails if target includes the key/value pairs" do
expect {
expect({ :a => 1, :b => 2 }).not_to include(:a => 1, :b => 2)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} not to include #{hash_inspect :a => 1, :b => 2}|)
end
it "fails if target includes the key/value pairs among others" do
hash = { :a => 1, :b => 2, :c => 3 }
expect {
expect(hash).not_to include(:a => 1, :b => 2)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2, :c => 3} not to include #{hash_inspect :a => 1, :b => 2}|)
end
it "fails if target has a different value for one of the keys" do
expect {
expect({ :a => 1, :b => 2 }).not_to include(:a => 2, :b => 2)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 2} not to include #{hash_inspect :b => 2}|)
end
it "passes if target has a different value for both of the keys" do
expect({ :a => 1, :b => 1 }).not_to include(:a => 2, :b => 2)
end
it "fails if target lacks one of the keys" do
expect {
expect({ :a => 1, :b => 1 }).not_to include(:a => 1, :c => 1)
}.to fail_with(%r|expected #{hash_inspect :a => 1, :b => 1} not to include #{hash_inspect :a => 1}|)
end
it "passes if target lacks both of the keys" do
expect({ :a => 1, :b => 1 }).not_to include(:c => 1, :d => 1)
end
end
context 'for a non-hash target' do
it "passes if the target does not contain the given hash" do
expect(['a', 'b']).not_to include(:a => 1, :b => 1)
end
it "fails if the target contains the given hash" do
expect {
expect(['a', { :a => 1, :b => 2 }]).not_to include(:a => 1, :b => 2)
}.to fail_with(%r|expected \["a", #{hash_inspect :a => 1, :b => 2}\] not to include #{hash_inspect :a => 1, :b => 2}|)
end
end
end
describe "Composing matchers with `include`" do
RSpec::Matchers.define :a_string_containing do |expected|
match do |actual|
actual.include?(expected)
end
description do
"a string containing '#{expected}'"
end
end
describe "expect(array).to include(matcher)" do
it "passes when the matcher matches one of the values" do
expect([10, 20, 30]).to include( a_value_within(5).of(24) )
end
it 'provides a description' do
description = include( a_value_within(5).of(24) ).description
expect(description).to eq("include (a value within 5 of 24)")
end
it 'fails with a clear message when the matcher matches none of the values' do
expect {
expect([10, 30]).to include( a_value_within(5).of(24) )
}.to fail_with("expected [10, 30] to include (a value within 5 of 24)")
end
it 'works with comparison matchers' do
expect {
expect([100, 200]).to include(a_value < 90)
}.to fail_with("expected [100, 200] to include (a value < 90)")
expect([100, 200]).to include(a_value > 150)
end
it 'does not treat an object that only implements #matches? as a matcher' do
not_a_matcher = Struct.new(:value) do
def matches?(_)
fail "`matches?` should never be called"
end
end
expect([not_a_matcher.new("rspec.info")]).to include(not_a_matcher.new("rspec.info"))
expect {
expect([not_a_matcher.new("rspec.info")]).to include(not_a_matcher.new("foo.com"))
}.to fail_matching("expected [#{not_a_matcher.new("rspec.info").inspect}] to include")
end
end
describe "expect(array).to include(multiple, matcher, arguments)" do
it "passes if target includes items satisfying all matchers" do
expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing('oo'))
end
it "fails if target does not include an item satisfying any one of the items" do
expect {
expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc"))
}.to fail_matching("expected #{['foo', 'bar', 'baz'].inspect} to include (a string containing 'abc')")
end
end
describe "expect(hash).to include(key => matcher)" do
it "passes when the matcher matches" do
expect(:a => 12).to include(:a => a_value_within(3).of(10))
end
it 'provides a description' do
description = include(:a => a_value_within(3).of(10)).description
expect(description).to eq("include {:a => (a value within 3 of 10)}")
end
it "fails with a clear message when the matcher does not match" do
expect {
expect(:a => 15).to include(:a => a_value_within(3).of(10))
}.to fail_matching("expected {:a => 15} to include {:a => (a value within 3 of 10)}")
end
end
describe "expect(hash).to include(key_matcher)" do
it "passes when the matcher matches a key", :if => (RUBY_VERSION.to_f > 1.8) do
expect(:drink => "water", :food => "bread").to include(match(/foo/))
end
it 'provides a description' do
description = include(match(/foo/)).description
expect(description).to eq("include (match /foo/)")
end
it 'fails with a clear message when the matcher does not match', :if => (RUBY_VERSION.to_f > 1.8) do
expect {
expect(:drink => "water", :food => "bread").to include(match(/bar/))
}.to fail_matching('expected {:drink => "water", :food => "bread"} to include (match /bar/)')
end
end
describe "expect(hash).to include(key_matcher => value)" do
it "passes when the matcher matches a pair", :if => (RUBY_VERSION.to_f > 1.8) do
expect(:drink => "water", :food => "bread").to include(match(/foo/) => "bread")
end
it "passes when the matcher matches all pairs", :if => (RUBY_VERSION.to_f > 1.8) do
expect(:drink => "water", :food => "bread").to include(match(/foo/) => "bread", match(/ink/) => "water")
end
it "passes with a natural matcher", :if => (RUBY_VERSION.to_f > 1.8) do
expect(:drink => "water", :food => "bread").to include(/foo/ => "bread")
end
it "passes with a natural matcher", :if => (RUBY_VERSION.to_f > 1.8) do
expect(:drink => "water", :food => "bread").to include(/foo/ => /read/)
end
it 'provides a description' do
description = include(match(/foo/) => "bread").description
expect(description).to eq('include {(match /foo/) => "bread"}')
end
it 'fails with a clear message when the value does not match', :if => (RUBY_VERSION.to_f > 1.8) do
expect {
expect(:drink => "water", :food => "bread").to include(match(/foo/) => "meat")
}.to fail_matching('expected {:drink => "water", :food => "bread"} to include {(match /foo/) => "meat"}')
end
it 'fails with a clear message when the matcher does not match', :if => (RUBY_VERSION.to_f > 1.8) do
expect {
expect(:drink => "water", :food => "bread").to include(match(/bar/) => "bread")
}.to fail_matching('expected {:drink => "water", :food => "bread"} to include {(match /bar/) => "bread"}')
end
it 'fails with a clear message when several matchers do not match', :if => (RUBY_VERSION.to_f > 1.8) do
expect {
expect(:drink => "water", :food => "bread").to include(match(/bar/) => "bread", match(/baz/) => "water")
}.to fail_matching('expected {:drink => "water", :food => "bread"} to include {(match /bar/) => "bread", (match /baz/) => "water"}')
end
end
describe "expect(array).not_to include(multiple, matcher, arguments)" do
it "passes if none of the target values satisfies any of the matchers" do
expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("gh"), a_string_containing('de'))
end
it 'fails if all of the matchers are satisfied by one of the target values' do
expect {
expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('az'))
}.to fail_matching("expected #{['foo', 'bar', 'baz'].inspect} not to include (a string containing 'ar') and (a string containing 'az')")
end
it 'fails if the some (but not all) of the matchers are satisfied' do
expect {
expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('bz'))
}.to fail_matching("expected #{['foo', 'bar', 'baz'].inspect} not to include (a string containing 'ar')")
end
end
end
# `fail_including` uses the `include` matcher internally, and using a matcher
# to test itself is potentially problematic, so just for this spec file we
# use `fail_matching` instead, which converts to a regex instead.
def fail_matching(message)
raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/)
end
end
|