changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/matchers/include.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module Spec
2 module Matchers
3
4 class Include #:nodoc:
5
6 def initialize(*expecteds)
7 @expecteds = expecteds
8 end
9
10 def matches?(actual)
11 @actual = actual
12 @expecteds.each do |expected|
13 return false unless actual.include?(expected)
14 end
15 true
16 end
17
18 def failure_message
19 _message
20 end
21
22 def negative_failure_message
23 _message("not ")
24 end
25
26 def description
27 "include #{_pretty_print(@expecteds)}"
28 end
29
30 private
31 def _message(maybe_not="")
32 "expected #{@actual.inspect} #{maybe_not}to include #{_pretty_print(@expecteds)}"
33 end
34
35 def _pretty_print(array)
36 result = ""
37 array.each_with_index do |item, index|
38 if index < (array.length - 2)
39 result << "#{item.inspect}, "
40 elsif index < (array.length - 1)
41 result << "#{item.inspect} and "
42 else
43 result << "#{item.inspect}"
44 end
45 end
46 result
47 end
48 end
49
50 # :call-seq:
51 # should include(expected)
52 # should_not include(expected)
53 #
54 # Passes if actual includes expected. This works for
55 # collections and Strings. You can also pass in multiple args
56 # and it will only pass if all args are found in collection.
57 #
58 # == Examples
59 #
60 # [1,2,3].should include(3)
61 # [1,2,3].should include(2,3) #would pass
62 # [1,2,3].should include(2,3,4) #would fail
63 # [1,2,3].should_not include(4)
64 # "spread".should include("read")
65 # "spread".should_not include("red")
66 def include(*expected)
67 Matchers::Include.new(*expected)
68 end
69 end
70end