<commit_before><%
# STRATEGIES (maybe some of these should be pulled into their own gems? they seem generally useful)
MountainBerryFields::Test::Strategy.register :install_dep, Class.new {
  include MountainBerryFields::Test::Strategy

  def initialize(install_string)
    @prompt, @gem_command, @install_command, @dependency_name = install_string.split
    @gemspec = Gem::Specification.load 'mountain_berry_fields.gemspec'
  end

  def pass?
    !failure_message
  end

  def failure_message
    return %'prompt marker is "$", not #{@prompt.inspect}' unless @prompt == '$'
    return %'gem command is "gem", not #{@gem_command.inspect}' unless @gem_command == 'gem'
    return %'install command is "install", not #{@install_command.inspect}' unless @install_command == 'install'
    return %'development dependencies are #{development_dependencies.inspect} WTF is #{@dependency_name.inspect}?' unless is_dependency?
  end

  def development_dependencies
    @gemspec.development_dependencies.map &:name
  end

  def is_dependency?
    development_dependencies.include? @dependency_name
  end
}

# MountainBerryFields::Test::Strategy.register
%>

# ReadmeTester

Tests code in readme files, generates the readme if they are successful.

## Usage

When you have a file with embedded code samples, rename it to include a .mountain_berry_fields suffix.
Then wrap test statements around the code samples. I've written two testing strategies: rspec and magic_comments.
You can create your own without much effort.


### Code samples with magic comments

You will need to
`<% test('dep magic_comments', with: :install_dep) { %>$ gem install mountain_berry_fields-magic_comments<% } %>`
for this to work.
<commit_msg>Progress on the readme, but hung up on a bug<commit_after><%
# STRATEGIES (maybe some of these should be pulled into their own gems? they seem generally useful)
MountainBerryFields::Test::Strategy.register :install_dep, Class.new {
  include MountainBerryFields::Test::Strategy

  def initialize(install_string)
    @prompt, @gem_command, @install_command, @dependency_name = install_string.split
    @gemspec = Gem::Specification.load 'mountain_berry_fields.gemspec'
  end

  def pass?
    !failure_message
  end

  def failure_message
    return %'prompt marker is "$", not #{@prompt.inspect}' unless @prompt == '$'
    return %'gem command is "gem", not #{@gem_command.inspect}' unless @gem_command == 'gem'
    return %'install command is "install", not #{@install_command.inspect}' unless @install_command == 'install'
    return %'development dependencies are #{development_dependencies.inspect} WTF is #{@dependency_name.inspect}?' unless is_dependency?
  end

  def development_dependencies
    @gemspec.development_dependencies.map &:name
  end

  def is_dependency?
    development_dependencies.include? @dependency_name
  end
}


MountainBerryFields::Test::Strategy.register  :mbf_example, Class.new {
  include MountainBerryFields::Test::Strategy

  attr_accessor :input_filename, :input_code, :command_line_invocation, :output_filename, :output_code, :expected_failure

  def pass?
    parse
    happy_path && sad_path
  end

  def failure_message
    @failure_message || ''
  end

  def happy_path
    require 'tmpdir'
    require 'open3'

    happy_lib_code = '
    <%% setup do %>
      MyLibName = Struct.new :data do
        def result
          "some cool result"
        end
      end
    <%% end %>
    '

puts "TO WRITE: #{happy_lib_code + input_code}"

    Dir.mktmpdir 'happy_path' do |dir|
      Dir.chdir dir do
        File.write input_filename, happy_lib_code + input_code
#        o, e, s = Open3.capture3 command_line_invocation
#puts "FILE: #{File.read input_filename}"
#puts "OUTPUT: #{o.inspect}"
#puts "ERROR: #{e.inspect}"
#puts "STATUS: #{s.inspect}"
#        @failure_message ||= e
#        s.success?
      end
    end
  end

def sad_path
end

  # all this parsing is overly simple and a bit fragile, but good enough
  def parse
    results = code_to_test.split(/^(?=\S)/)
    parse_setup      results.shift
    parse_happy_path results.shift
    parse_sad_path   results.shift
  end

  def parse_setup(raw_setup)
    lines = raw_setup.lines.to_a
    self.input_filename = lines.shift[/`(.*?)`/, 1]
    lines.shift
    lines.pop
    self.input_code = lines.join.gsub(/^ {4}/, '')
  end

  def parse_happy_path(raw_happy_path)
    lines = raw_happy_path.lines.to_a
    first_line = lines.shift
    self.command_line_invocation, self.output_filename = first_line.scan(/`[^`]*`/).map { |text| text[1...-1] }
    command_line_invocation.sub! /^\$\s+/, ''
    lines.shift
    lines.pop
    self.output_code = lines.join.gsub(/^ {4}/, '')
  end

  def parse_sad_path(raw_sad_path)
    self.expected_failure = raw_sad_path.lines.drop(2).join.gsub(/^ {4}/, '')
  end
}
%>

# ReadmeTester

Tests code in readme files, generates the readme if they are successful.

## Usage

When you have a file with embedded code samples, rename it to include a .mountain_berry_fields suffix.
Then wrap test statements around the code samples. I've written two testing strategies: rspec and magic_comments.
You can create your own without much effort.


### Code samples with magic comments

You will need to
`<% test('dep magic_comments', with: :install_dep) { %>$ gem install mountain_berry_fields-magic_comments<% } %>`
for this to work.

<% test 'show magic comments', with: :mbf_example do %>
The file `Readme.mountain_berry_fields.md`

    # MyLibName

        <%% test 'an example', with: :magic_comments do %>
        MyLibName.new('some data').result # => "some cool result"
        <%% end %>

Run `$ mountain_berry_fields Readme.mountain_berry_fields.md` and it will generate `Readme.md`

    # MyLibName

        MyLibName.new('some data').result # => "some cool result"

If at some point, you change your lib to not do that cool thing, then it will not generate the file.  Instead it will give you an error message:

    FAILURE: an example
    Expected: MyLibName.new('some data').result # => "some cool result"
    Actual:   MyLibName.new('some data').result # => "some unexpected result"
<% end %>

Now you can be confident that your code is still legit.
{'fraction_non_alphanumeric': 0.1075234097513723, 'fraction_numerical': 0.0016144656118824668, 'mean_word_length': 3.979903536977492, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '497210', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1876, 'n_words': 575}