File: matchers.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (88 lines) | stat: -rw-r--r-- 1,957 bytes parent folder | download
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
# frozen_string_literal: true
# rubocop:todo all

RSpec::Matchers.define :be_int32 do |num|
  match do |actual|
    actual == [num].pack('l<')
  end
end

RSpec::Matchers.define :be_int64 do |num|
  match do |actual|
    actual == [num].pack('q<')
  end
end

RSpec::Matchers.define :be_int64_sequence do |array|
  match do |actual|
    actual == array.reduce(String.new) do |buffer, num|
      buffer << [num].pack('q<')
    end
  end
end

RSpec::Matchers.define :be_cstring do |string|
  match do |actual|
    actual == "#{string.dup.force_encoding(BSON::BINARY)}\0"
  end
end

RSpec::Matchers.define :be_bson do |hash|
  match do |actual|
    actual == hash.to_bson.to_s
  end
end

RSpec::Matchers.define :be_bson_sequence do |array|
  match do |actual|
    actual == array.map(&:to_bson).join
  end
end

RSpec::Matchers.define :be_ciphertext do
  match do |object|
    object.is_a?(BSON::Binary) && object.type == :ciphertext
  end
end


RSpec::Matchers.define :match_with_type do |event|
  match do |actual|
    Utils.match_with_type?(event, actual)
  end
end

RSpec::Matchers.define :be_uuid do
  match do |object|
    object.is_a?(BSON::Binary) && object.type == :uuid
  end
end

RSpec::Matchers.define :take_longer_than do |min_expected_time|
  match do |proc|
    start_time = Mongo::Utils.monotonic_time
    proc.call
    (Mongo::Utils.monotonic_time - start_time).should > min_expected_time
  end
end

RSpec::Matchers.define :take_shorter_than do |min_expected_time|
  match do |proc|
    start_time = Mongo::Utils.monotonic_time
    proc.call
    (Mongo::Utils.monotonic_time - start_time).should < min_expected_time
  end
end

RSpec::Matchers.define :be_explain_output do
  match do |actual|
    Hash === actual && (
      actual.key?('queryPlanner') ||
      actual.key?('allPlans')
    )
  end

  failure_message do |actual|
    "expected that #{actual} is explain output: is a hash with either allPlans or queryPlanner keys present"
  end
end