<commit_before>require_relative '../../lib/flexibility'

path = File.expand_path( '../../lib/flexibility.rb', File.dirname(__FILE__) ) 

# mask puts for examples
STRIO = StringIO.new
def puts(msg)
  STRIO.puts msg
end

describe "lib/flexibility.rb examples" do

  # find all comment blocks and parse out the code blocks
  comment_blocks = []
  last_spaces = nil
  last_index  = nil
  code_blocks = nil
  last_mode   = nil
  File.read( path ).each_line.with_index do |line, index|
    next unless line =~ /^( *)#( *)(.*)/
    spaces = $1.length
    indent = $2.length
    comment = $3

    unless spaces == last_spaces and index == last_index + 1
      code_blocks = []
      comment_blocks << { description: comment.sub(/\.*$/, '...'), code_blocks: code_blocks }
      last_mode = nil
    end

    last_spaces = spaces
    last_index  = index

    next unless indent >= 5
    line.sub! '#', ' '

    mode = if comment =~ /^irb> /
             :test
           elsif not [ :output, :test ].include? last_mode
             :eval
           elsif comment =~ /^=> /
             :result
           else
             :output
           end

    case mode
    when :eval
      unless last_mode == :eval
        code_blocks.push( lineno: index+1, lines: [] )
      end
      code_blocks.last[:lines] << line + "\n"
    when :test
      code_blocks.push( lineno: index+1, lines: [line.sub('irb>', '    ')], output: [] )
    when :output
      code_blocks.last[:output] << comment + "\n"
    when :result
      code_blocks.last.merge!( result_lineno: index+1, result: line.sub('=>', '  ') )
    end
    last_mode = mode
  end

  # create a spec out of each comment block that contains code, verifying that
  # the irb results and output are as described
  comment_blocks.each do |block|
    next if block[:code_blocks].empty?

    it block[:description] do
      b = Module.new.module_eval { binding }

      block[:code_blocks].each do |code|
        actual_result = eval( code[:lines].join, b, path, code[:lineno] )
        STRIO.rewind
        actual_output = STRIO.read
        STRIO.rewind
        STRIO.truncate 0

        if code[:result]
          expected_result = eval( code[:result], b, path, code[:result_lineno] )
          eval(%!expect( actual_result ).to eq( expected_result )!, binding, path, code[:lineno] )
        end

        if code[:output]
          expected_output = code[:output].join
          eval( %!expect( actual_output ).to eq( expected_output )!, binding, path, code[:lineno] )
        end
      end
    end
  end
end<commit_msg>making sure different specs use different namespaces<commit_after>require_relative '../../lib/flexibility'

path = File.expand_path( '../../lib/flexibility.rb', File.dirname(__FILE__) ) 

# mask puts for examples
STRIO = StringIO.new
def puts(msg)
  STRIO.puts msg
end

describe "lib/flexibility.rb examples" do

  # find all comment blocks and parse out the code blocks
  comment_blocks = []
  last_spaces = nil
  last_index  = nil
  code_blocks = nil
  last_mode   = nil
  File.read( path ).each_line.with_index do |line, index|
    next unless line =~ /^( *)#( *)(.*)/
    spaces = $1.length
    indent = $2.length
    comment = $3

    unless spaces == last_spaces and index == last_index + 1
      code_blocks = []
      comment_blocks << { description: comment.sub(/\.*$/, '...'), code_blocks: code_blocks }
      last_mode = nil
    end

    last_spaces = spaces
    last_index  = index

    next unless indent >= 5
    line.sub! '#', ' '

    mode = if comment =~ /^irb> /
             :test
           elsif not [ :output, :test ].include? last_mode
             :eval
           elsif comment =~ /^=> /
             :result
           else
             :output
           end

    case mode
    when :eval
      unless last_mode == :eval
        code_blocks.push( lineno: index+1, lines: [] )
      end
      code_blocks.last[:lines] << line + "\n"
    when :test
      code_blocks.push( lineno: index+1, lines: [line.sub('irb>', '    ')], output: [] )
    when :output
      code_blocks.last[:output] << comment + "\n"
    when :result
      code_blocks.last.merge!( result_lineno: index+1, result: line.sub('=>', '  ') )
    end
    last_mode = mode
  end

  # create a spec out of each comment block that contains code, verifying that
  # the irb results and output are as described
  comment_blocks.each do |block|
    next if block[:code_blocks].empty?

    it block[:description] do
      b = eval( "module #{block[:description].gsub(/^\W+|\W+$/,'').gsub(/\W+/,'_').upcase!} ; binding ;  end" )

      block[:code_blocks].each do |code|
        actual_result = eval( code[:lines].join, b, path, code[:lineno] )
        STRIO.rewind
        actual_output = STRIO.read
        STRIO.rewind
        STRIO.truncate 0

        if code[:result]
          expected_result = eval( code[:result], b, path, code[:result_lineno] )
          eval(%!expect( actual_result ).to eq( expected_result )!, binding, path, code[:lineno] )
        end

        if code[:output]
          expected_output = code[:output].join
          eval( %!expect( actual_output ).to eq( expected_output )!, binding, path, code[:lineno] )
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13104374520337683, 'fraction_numerical': 0.0034535686876438986, 'mean_word_length': 2.3119440914866582, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '6060316', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1623, 'n_words': 448}