class Tryouts

def parse path

def parse path
  #debug "Loading #{path}"
  lines = File.readlines path
  skip_ahead = 0
  batch = TestBatch.new path, lines
  lines.size.times do |idx|
    skip_ahead -= 1 and next if skip_ahead > 0
    line = lines[idx].chomp
    #debug('%-4d %s' % [idx, line])
    if expectation? line
      offset = 0
      exps = Section.new(path, idx+1)
      exps << line.chomp
      while (idx+offset < lines.size)
        offset += 1
        this_line = lines[idx+offset]
        break if ignore?(this_line)
        if expectation?(this_line)
          exps << this_line.chomp
          skip_ahead += 1
        end
        exps.last += 1
      end
      
      offset = 0
      buffer, desc = Section.new(path), Section.new(path)
      test = Section.new(path, idx)  # test start the line before the exp. 
      blank_buffer = Section.new(path)
      while (idx-offset >= 0)
        offset += 1
        this_line = lines[idx-offset].chomp
        buffer.unshift this_line if ignore?(this_line)
        if comment?(this_line)
          buffer.unshift this_line 
        end
        if test?(this_line)
          test.unshift(*buffer) && buffer.clear
          test.unshift this_line
        end
        if test_begin?(this_line)
          while test_begin?(lines[idx-(offset+1)].chomp)
            offset += 1
            buffer.unshift lines[idx-offset].chomp
          end
        end
        if test_begin?(this_line) || idx-offset == 0 || expectation?(this_line)
          adjust = expectation?(this_line) ? 2 : 1
          test.first = idx-offset+buffer.size+adjust
          desc.unshift *buffer
          desc.last = test.first-1
          desc.first = desc.last-desc.size+1
          # remove empty lines between the description 
          # and the previous expectation
          while !desc.empty? && desc[0].empty? 
            desc.shift
            desc.first += 1
          end
          break 
        end
      end
      
      batch << TestCase.new(desc, test, exps)
    end
  end
  
  batch
end