Class: Insta::RSpec::Matchers::MatchInlineSnapshot
- Inherits:
-
Object
- Object
- Insta::RSpec::Matchers::MatchInlineSnapshot
- Includes:
- RSpec::Matchers::Composable
- Defined in:
- lib/insta/rspec/matchers.rb,
sig/insta/rspec/matchers.rbs
Instance Attribute Summary collapse
-
#failure_message ⇒ String?
readonly
: String?.
Instance Method Summary collapse
-
#description ⇒ String
: () -> String.
-
#handle_inline_mismatch(file, line, content, expected_normalized) ⇒ Boolean
: (String, Integer, String, String) -> bool.
-
#handle_new_inline_snapshot(file, line, content) ⇒ Boolean
: (String, Integer, String) -> bool.
-
#initialize(expected = nil, serializer: nil, redact: nil) ⇒ MatchInlineSnapshot
constructor
: (String?, ?serializer: Symbol?, ?redact: Hash[String, untyped]?) -> void.
-
#matches?(actual) ⇒ Boolean
: (untyped) -> bool.
-
#record_failure(msg) ⇒ false
: (String) -> false.
Constructor Details
#initialize(expected = nil, serializer: nil, redact: nil) ⇒ MatchInlineSnapshot
: (String?, ?serializer: Symbol?, ?redact: Hash[String, untyped]?) -> void
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_message ⇒ String? (readonly)
: String?
145 146 147 |
# File 'lib/insta/rspec/matchers.rb', line 145 def @failure_message end |
Instance Method Details
#description ⇒ String
: () -> 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
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.(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.(expected_normalized, content, "inline snapshot at #{file}:#{line}") false end end end |
#handle_new_inline_snapshot(file, line, content) ⇒ Boolean
: (String, Integer, String) -> bool
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
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
230 231 232 233 234 |
# File 'lib/insta/rspec/matchers.rb', line 230 def record_failure(msg) @failure_message = msg false end |