view testing/utp_1.1/buildxml.rb @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
line wrap: on
line source

#!/usr/bin/ruby

require 'ostruct'
require 'unittest'
require 'rexml/document'
include REXML

options = OpenStruct.new
options.outfile = ""
options.infile = ""
options.version = "unknown"

if ARGV[1]==nil then
  puts "Usage: buildxml.rb <inputfile> <outputfile>"
  exit
else
  options.infile = ARGV[0]
  options.outfile = ARGV[1]
end

class UnitTestProcessor  
  attr_accessor :ut
  
  def initialize
    @ut = UnitTest.new
    @results = []
  end
  
  def result=(newResult)
    testvalues = newResult.split(",")
    @ut.path = getPath(testvalues)
    @ut.title = getTitle(testvalues)
    @ut.code = getCode(testvalues)
    result = Result.new
    result.syntax = testvalues[1]
    result.algorithm = testvalues[2]
    result.date = Date.new(testvalues[4].to_i)
    result.number = testvalues[3].to_i
    @ut.results << result
  end

  # generates the path out of the test name
  def getPath(testvalues)
    parts = testvalues[0].split("_")
    folder = parts[1]
    parts.pop
    filename = parts.join("_") + ".m"
    "utps/" + folder + "/" + filename
  end
  
  # generates the title out of the test name
  def getTitle(testvalues)
    parts = testvalues[0].split("_")
    parts.pop
    parts.shift
    #parts[0].upcase!
    parts.join(" ")
  end
  
  # generates the title out of the test name
  def getCode(testvalues)
    parts = testvalues[0].split("_")
    parts.pop
    parts.join("_")
  end
    
  def parseFile
    begin
      if !File.exists? @ut.path then
        return
      end
      inFile = File.new(@ut.path)
      currentTest = SubTest.new
      while line = inFile.readline.chomp
        # TODO: strip HTML-Tags
        # remove trailing spaces
        line.rstrip!
        
        if line.match /[%]+[ ]+UTP_[0-9]+/ then
          currentTest = SubTest.new
          parts = line.split("_")
          currentTest.number = parts[1].to_i
        end
        
        if line.match(/[%]+[ ]+END[ ]+UTP_[0-9]+/) and currentTest.number > 0 then
          @ut.tests << currentTest
        end
        
        if line.match "</MethodDescription>" then writeTo = "none" end
        if line.match "</SyntaxDescription>" then writeTo = "none" end
        if line.match "</TestDescription>" then writeTo = "none" end
        if line.match "</AlgoDescription>" then writeTo = "none" end
        if line.match "</SyntaxCode>" then writeTo = "none" end
        if line.match "</AlgoCode>" then writeTo = "none" end
          
          
        
        case writeTo
          when "none" then
            a = 1
          when "algoCode" then
            currentTest.algoCode += line + "\n"
          when "algoDescription" then
            currentTest.algoDescription += removeComment line
          when "syntaxCode" then
            currentTest.syntaxCode += line + "\n"
          when "syntaxDescription" then
            currentTest.syntaxDescription += removeComment line
          when "methodDescription" then
            @ut.methodDescription += removeComment line
          when "testDescription" then
            currentTest.testDescription += removeComment line
        end
        
        if line.match "<MethodDescription>" then writeTo = "methodDescription" end
        if line.match "<SyntaxDescription>" then writeTo = "syntaxDescription" end
        if line.match "<TestDescription>" then writeTo = "testDescription" end
        if line.match "<AlgoDescription>" then writeTo = "algoDescription" end
        if line.match "<SyntaxCode>" then writeTo = "syntaxCode" end
        if line.match "<AlgoCode>" then writeTo = "algoCode" end
        
      end
    rescue EOFError
      inFile.close
    end
  end
  
  # The analysis of a single line of the .m file
  def removeComment(line)
    line.strip!
    while line.match /^%/
      # not elegant, but works in every realistic case (cut off at 100000 chars)
      line = line[1,100000]
      line.strip!
    end
    if line.length>0 then
      line = line + "<br />"
    end
    line
  end
  
  # Merge Results and Test for easier access
  def mergeTestsAndResults
    @ut.tests.each do |t|
      @ut.results.each do |r|
        if t.number == r.number then
          t.date = r.date
          t.syntaxResult = r.syntax.to_i
          t.algoResult = r.algorithm.to_i
          if t.syntaxResult==0 or t.algoResult==0 then
            @ut.tainted = true
          end
        end
      end
    end
  end
end

infile = File.open(options.infile, 'r');
outfile = File.open(options.outfile, 'w');

tests = []

begin
  while line = infile.readline.chomp
    testvalues = line.split(",")
    parts = testvalues[0].split("_")
    parts.pop
    tests << parts.join("_")
  end
rescue EOFError
  infile.close
end

tests = tests.uniq

# For testing: tests fet only for the first test
#tests = []
#tests << "utp_ao_join_fsdata"
utps = []
puts "Collecting data ..."
tests.each do |t|
  inFile = File.open(options.infile, 'r')
  utp = UnitTestProcessor.new
  begin
    while line = inFile.readline.chomp
      if line.match t then
        utp.result = line
      end
    end
  rescue EOFError
    inFile.close
  end
  utps << utp
end

unittests = []

i=1

utps.each do |u|
  puts "Parsing file " + i.to_s + " of " + utps.length.to_s + " (" + u.ut.path + ") ..."
  i += 1
  u.parseFile
  u.mergeTestsAndResults
  unittests << u.ut
end

#if(options.outfile HERE MATCHES DAT) then
  Marshal.dump(unittests, outfile)
#elseif(optione.outfile. HERE MATCHES XML then
#  doc = Document.new()
#  root = Element.new("Report")
#  root.add_attribute 'date', Time.now.to_s
#  root.add_attribute 'version', options.version
#  unittests.each do |u|
#    testEl = Element.new("
#  end
#end
outfile.close