File: spec_helper.rb

package info (click to toggle)
ruby-redis-namespace 1.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: ruby: 1,579; makefile: 10; sh: 4
file content (79 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download | duplicates (2)
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

require 'rspec'
require 'rspec/its'
require 'redis'
require 'logger'
require 'rspec/its'

$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'redis/namespace'

if Redis.respond_to?(:exists_returns_integer=)
  Redis.exists_returns_integer = true
end

module Helper
  def capture_stderr(io = nil)
    require 'stringio'
    io ||= StringIO.new
    begin
      original, $stderr = $stderr, io
      yield
    rescue Redis::CommandError
      # ignore Redis::CommandError for test and
      # return captured messages
      $stderr.string.chomp
    ensure
      $stderr = original
    end
  end

  def with_env(env = {})
    backup_env = ENV.to_hash.dup
    ENV.update(env)

    yield
  ensure
    ENV.replace(backup_env)
  end

  def silent
    verbose, $VERBOSE = $VERBOSE, false

    begin
      yield
    ensure
      $VERBOSE = verbose
    end
  end

  def with_external_encoding(encoding)
    original_encoding = Encoding.default_external

    begin
      silent { Encoding.default_external = Encoding.find(encoding) }
      yield
    ensure
      silent { Encoding.default_external = original_encoding }
    end
  end

  def try_encoding(encoding, &block)
    if defined?(Encoding)
      with_external_encoding(encoding, &block)
    else
      yield
    end
  end
end

RSpec.configure do |c|
  c.include Helper
end

RSpec::Matchers.define :have_key do |expected|
  match do |redis|
    redis.exists(expected)
  end
end