Module: ExAequo::RSpex::Helpers::SubjectHelpers

Defined in:
lib/ex_aequo/rspex/helpers/subject_helpers.rb,
lib/ex_aequo/rspex/helpers/subject_helpers/match_helper.rb

Defined Under Namespace

Modules: MatchHelper

Instance Method Summary collapse

Instance Method Details

#it_eq(value, &blk) ⇒ Object Also known as: it_equals



9
10
11
12
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 9

def it_eq(value, &blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).to eq(value)
end

#it_fails_spec(message = nil, &blk) ⇒ Object



15
16
17
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 15

def it_fails_spec(message=nil, &blk)
  it_raises(RSpec::Expectations::ExpectationNotMetError, message, &blk)
end

#it_has_ivar(name, value, &blk) ⇒ Object



19
20
21
22
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 19

def it_has_ivar(name, value, &blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual.instance_variable_get("@#{name}")).to eq(value)
end

#it_has_same_lines(expected, &blk) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 55

def it_has_same_lines(expected, &blk)
  actual = blk ? instance_eval(&blk) : subject

  actual = actual.lines(chomp: true) if String === actual
  expected = expected.lines(chomp: true) if String === expected

  actual.each.with_index do |line, idx|
    expect(line:, idx:).to eq(line: expected[idx], idx:)
  end

  missing = expected.drop(actual.length)
  missing.each do
    expect(it).to be_nil, "#{it} is missing in actual result"
  end
end

#it_is(predicate, *args, &blk) ⇒ Object



24
25
26
27
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 24

def it_is(predicate, *args, &blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).to send("be_#{predicate}", *args)
end

#it_is_between(lower, higher, &blk) ⇒ Object



29
30
31
32
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 29

def it_is_between(lower, higher, &blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).to be_between(lower, higher)
end

#it_is_false(&blk) ⇒ Object



34
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 34

def it_is_false(&blk) = it_equals(false, &blk)

#it_is_falsy(&blk) ⇒ Object



36
37
38
39
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 36

def it_is_falsy(&blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).to be_falsy
end

#it_is_nil(&blk) ⇒ Object



41
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 41

def it_is_nil(&blk) = it_equals(nil, &blk)

#it_is_not(predicate, *args, &blk) ⇒ Object



50
51
52
53
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 50

def it_is_not(predicate, *args, &blk) 
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).not_to send("be_#{predicate}", *args)
end

#it_is_true(&blk) ⇒ Object



43
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 43

def it_is_true(&blk) = it_equals(true, &blk)

#it_is_truthy(&blk) ⇒ Object



45
46
47
48
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 45

def it_is_truthy(&blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).to be_truthy
end

#it_matches(matcher, &blk) ⇒ Object



71
72
73
74
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 71

def it_matches(matcher, &blk)
  actual = blk ? instance_eval(&blk) : subject
  expect(actual).to match(matcher)
end

#it_matches_output(*matches, to: :stdout, &blk) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 76

def it_matches_output(*matches, to: :stdout, &blk)
  match = MatchHelper.make_line_matchers(*matches.flatten)
  case to
  when :stdout
    expect{ instance_eval(&blk) }.to output(match).to_stdout
  when :stderr
    expect{ instance_eval(&blk) }.to output(match).to_stderr
  else
    raise ArgumentError, "to: must be :stdout or :stderr, but was: #{to.inspect}"
  end
end

#it_puts(lines, to: $stdout, &blk) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 93

def it_puts(lines, to: $stdout, &blk)
  lines = lines.dup
  idx = 0
  expect(to).to receive(:puts).with(anything).exactly(lines.count).times do |passed_in|
    idx += 1
    next if passed_in == lines[idx.pred]

    msg = "line number #{idx}: expected: #{lines[idx.pred].inspect}, was: #{passed_in.inspect}"
    raise RSpec::Expectations::ExpectationNotMetError,msg
  end
  blk ? instance_eval(&blk) : subject
end

#it_raises(exception, message = nil, &blk) ⇒ Object



88
89
90
91
# File 'lib/ex_aequo/rspex/helpers/subject_helpers.rb', line 88

def it_raises(exception, message=nil, &blk)
  expect { blk ? instance_eval(&blk) : subject }
    .to raise_error(exception, message)
end