Class: Insta::RSpec::Matchers::MatchInlineSnapshot

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers::Composable
Defined in:
lib/insta/rspec/matchers.rb,
sig/insta/rspec/matchers.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected = nil, serializer: nil, redact: nil) ⇒ MatchInlineSnapshot

: (String?, ?serializer: Symbol?, ?redact: Hash[String, untyped]?) -> void

Parameters:

  • (String, nil)
  • serializer: (Symbol, nil) (defaults to: nil)
  • redact: (Hash[String, untyped], nil) (defaults to: nil)


148
149
150
151
152
153
# File 'lib/insta/rspec/matchers.rb', line 148

def initialize(expected = nil, serializer: nil, redact: nil)
  @expected = expected
  @serializer = serializer
  @redact = redact
  @failure_message = nil
end

Instance Attribute Details

#failure_messageString? (readonly)

: String?

Returns:

  • (String, nil)


145
146
147
# File 'lib/insta/rspec/matchers.rb', line 145

def failure_message
  @failure_message
end

Instance Method Details

#descriptionString

: () -> String

Returns:

  • (String)


180
181
182
# File 'lib/insta/rspec/matchers.rb', line 180

def description
  "match inline snapshot"
end

#handle_inline_mismatch(file, line, content, expected_normalized) ⇒ Boolean

: (String, Integer, String, String) -> bool

Parameters:

  • (String)
  • (Integer)
  • (String)
  • (String)

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/insta/rspec/matchers.rb', line 202

def handle_inline_mismatch(file, line, content, expected_normalized)
  mode = Insta.configuration.resolved_update_mode

  case mode
  when :force
    Inline::PendingRegistry.add(file: file, line: line, content: content, type: :replace)

    true
  when :no
    @failure_message = Diff.failure_message(expected_normalized, content, "inline snapshot at #{file}:#{line}")

    false
  else
    Inline::PendingStore.add(file: file, line: line, content: content, old_content: expected_normalized,
                             type: :replace)

    if ENV["INSTA_FORCE_PASS"]
      true
    else
      @failure_message = Diff.failure_message(expected_normalized, content,
                                              "inline snapshot at #{file}:#{line}")

      false
    end
  end
end

#handle_new_inline_snapshot(file, line, content) ⇒ Boolean

: (String, Integer, String) -> bool

Parameters:

  • (String)
  • (Integer)
  • (String)

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/insta/rspec/matchers.rb', line 187

def handle_new_inline_snapshot(file, line, content)
  if Insta.configuration.new_snapshot == :review
    Inline::PendingStore.add(file: file, line: line, content: content, old_content: "", type: :insert)

    return true if ENV["INSTA_FORCE_PASS"]

    return record_failure("New inline snapshot pending review at #{file}:#{line}\nRun `bundle exec insta review` to accept.")
  end

  Inline::PendingRegistry.add(file: file, line: line, content: content, type: :insert)

  true
end

#matches?(actual) ⇒ Boolean

: (untyped) -> bool

Parameters:

  • (Object)

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/insta/rspec/matchers.rb', line 156

def matches?(actual)
  @actual = actual

  actual = Redaction::Applicator.apply(actual, @redact) if @redact
  serializer_name = @serializer || Insta.configuration.default_serializer
  serialized = Serializers.serialize(serializer_name, actual)
  content = SnapshotContent.normalize(serialized)

  location = (caller_locations || []).find { |location| location.path && !location.path.include?("lib/insta") && !location.path.include?("lib/rspec") }
  return record_failure("Could not determine caller location") unless location

  file = location.path
  line = location.lineno

  return record_failure("Could not determine source file") unless file
  return handle_new_inline_snapshot(file, line, content) if @expected.nil?

  expected_normalized = SnapshotContent.normalize(@expected.to_s)
  return true if content == expected_normalized

  handle_inline_mismatch(file, line, content, expected_normalized)
end

#record_failure(msg) ⇒ false

: (String) -> false

Parameters:

  • (String)

Returns:

  • (false)


230
231
232
233
234
# File 'lib/insta/rspec/matchers.rb', line 230

def record_failure(msg)
  @failure_message = msg

  false
end