class Rspec::Usecases::Contents::BaseContent

BaseContent

Constants

EXTRACT_CONTENT_REX
METHOD_NAMES

Attributes

is_hr[RW]
metadata[RW]

metadata

source[RW]

source

title[RW]

title

type[RW]

:type

Public Class Methods

get_instance(example) click to toggle source
# File lib/rspec/usecases/contents/base_content.rb, line 47
def self.get_instance(example)
  type = example.metadata[:content_type].to_s

  begin
    klass = Module.const_get("Rspec::Usecases::Contents::#{type.capitalize}")
    klass.new(type, example)
  rescue NameError
    # TODO: Logging
    puts "UNKNOWN CONTENT TYPE: #{type}"
    nil
  rescue StandardError => e
    # TODO: Logging
    puts e
    nil
  end
end
new(type, example) click to toggle source
# File lib/rspec/usecases/contents/base_content.rb, line 64
def initialize(type, example)
  title = example.description.strip
  @title = title.start_with?('example at .') ? '' : title
  @type = type

  # May want to delegate this to an OpenStruct called options
  @is_hr = !!example.metadata[:hr]
end
parse(example) click to toggle source
# File lib/rspec/usecases/contents/base_content.rb, line 36
def self.parse(example)
  # return nil if example.description.nil?# || example.description.strip.length.zero?
  return nil if example.metadata[:content_type].nil?

  result = get_instance(example)

  result&.parse_block_source(example)

  result
end

Public Instance Methods

get_source(example) click to toggle source
# File lib/rspec/usecases/contents/base_content.rb, line 100
def get_source(example)
  if defined?(example.metadata) && defined?(example.metadata[:block]) && defined?(example.metadata[:block].source)
    example.metadata[:block].source.strip
  else
    ''
  end
end
parse_block_source(example) click to toggle source

Source code for rspec is living on the metadata.source location Have not written a test for this yet

# File lib/rspec/usecases/contents/base_content.rb, line 75
def parse_block_source(example)
  unless example.metadata[:source_override].nil?
    @source = example.metadata[:source_override]
    return
  end

  source = get_source(example)

  # NOTE: Need to investigate how RSpec deals with code, see:
  # https://github.com/rspec/rspec-core/blob/fe3084758857f0714f05ada44a18f1dfe9bf7a7e/spec/rspec/core/formatters/snippet_extractor_spec.rb
  # https://github.com/rspec/rspec-core/blob/fe3084758857f0714f05ada44a18f1dfe9bf7a7e/lib/rspec/core/formatters/html_formatter.rb
  segments = source.match(EXTRACT_CONTENT_REX)

  unless defined?(segments) && defined?(segments[:content])
    @source = ''
    return
  end
  @source = remove_wasted_indentation(segments[:content])
  @source
rescue StandardError => e
  puts 'Could not parse source'
  puts example.metadata
  puts e
end
remove_wasted_indentation(content) click to toggle source
# File lib/rspec/usecases/contents/base_content.rb, line 108
def remove_wasted_indentation(content)
  lines = content.lines

  whitespace = /^\s*/

  # find the small whitespace sequence
  # at beginning of line that is not \n or blank
  # and grab the smallest value
  indent = lines
           .map    { |l| l.match(whitespace).to_s }
           .reject { |s| ["\n", ''].include?(s) }
           .min_by(&:length)

  # remove the smallest indentation from beginning
  # of all lines, this is the wasted indentation
  rex_indent = /^#{indent}/

  lines.each { |l| l.gsub!(rex_indent, '') }

  # convert back to a content string
  lines.join.strip
end
to_h() click to toggle source
# File lib/rspec/usecases/contents/base_content.rb, line 131
def to_h
  {
    title: title,
    type: type,
    source: source,
    is_hr: is_hr
    # options: [
    #   is_hr: is_hr
    # ]
  }
end