Class: AhnnotateUpToDateMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ahnnotate/rspec.rb

Overview

This RSpec matcher only works for Rails. Here’s a quick usage example:

require "rails_helper"
require "ahnnotate/rspec"

RSpec.describe Ahnnotate do
  it { is_expected.to be_up_to_date }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



12
13
14
# File 'lib/ahnnotate/rspec.rb', line 12

def actual
  @actual
end

#expectedObject (readonly)

Returns the value of attribute expected.



13
14
15
# File 'lib/ahnnotate/rspec.rb', line 13

def expected
  @expected
end

Instance Method Details

#descriptionObject



41
42
43
# File 'lib/ahnnotate/rspec.rb', line 41

def description
  "be up to date (you may need to run `rails db:test:prepare`)"
end

#diffable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ahnnotate/rspec.rb', line 15

def diffable?
  true
end

#failure_messageObject



45
46
47
# File 'lib/ahnnotate/rspec.rb', line 45

def failure_message
  "expected Ahnnotate to be up to date"
end

#failure_message_when_negatedObject



49
50
51
# File 'lib/ahnnotate/rspec.rb', line 49

def failure_message_when_negated
  "expected Ahnnotate not to be up to date"
end

#matches?(_) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ahnnotate/rspec.rb', line 19

def matches?(_)
  options = Ahnnotate::Options.new(fix: false)
  config = Ahnnotate::Config.load(root: Rails.root)
  main = Ahnnotate::Function::Main.new(Rails.root, options, config)

  main.call

  writes = main.vfs.instance_variable_get(:@driver).changes

  format = proc do |vfs_hash|
    vfs_hash
      .map { |path, contents| "~~~ #{path} ~~~\n#{contents}" }
      .join("\n")
  end

  expected = writes.map { |path, _content| [path, File.read(path)] }.to_h
  @expected = format.call(expected)
  @actual = format.call(writes)

  @actual == @expected
end