#!/usr/bin/env ruby require_relative 'IndexHTMLFile' require_relative 'AnalysisHTMLFile' class FormGenerator def initialize (outputDirectoryOpt, testedFunctionsMap, analyzedClasses, numProjectFiles, numTestFiles) @analyzedClasses = analyzedClasses @testedFunctionsMap = testedFunctionsMap @totalLines = 0 @numClasses = 0 @numFunc = 0 @numProjectFiles = numProjectFiles @numTestFiles = numTestFiles @analysisDirectory = "SwiftCodeCoverage" if outputDirectoryOpt != nil if outputDirectoryOpt[-1,1] == "/" outputDirectoryOpt = outputDirectoryOpt[0..-2] end puts outputDirectoryOpt @analysisDirectory = "#{outputDirectoryOpt}/#{@analysisDirectory}" end @filesDir = "AnalysisFiles" end def generateForm createProjectStats printResultsToCommandLine # Start creating HTML puts puts "Generating form..." puts "#{@analysisDirectory}" if File.directory?(@analysisDirectory) FileUtils.rm_rf(@analysisDirectory) end Dir.mkdir(@analysisDirectory) indexHTML = IndexHTMLFile.new(self) indexHTML.beginWriting for analyzedClass in @analyzedClasses analysisFile = AnalysisHTMLFile.new("#{@analysisDirectory}/#{@filesDir}", analyzedClass) analysisFile.writeFile indexHTML.addAnalysisItem analyzedClass, analysisFile end indexHTML.endWriting puts "Finished generating form at #{@analysisDirectory}" # Finish HTML end def createProjectStats for analyzedClass in @analyzedClasses @numClasses += 1 @numFunc += analyzedClass.functions.count @totalLines += analyzedClass.numberOfLines end end def functionsTested numLines = 0 numFunctions = 0 for analyzedClass in @analyzedClasses for function in analyzedClass.testedFunctions numLines += function.numLines numFunctions += 1 end end return numLines, numFunctions end def classesTested numClasses = 0 for analyzedClass in @analyzedClasses if analyzedClass.testedFunctions.count > 0 numClasses += 1 end end return numClasses end def printResultsToCommandLine puts puts "Total classes: #{@numClasses}" puts "Total functions: #{@numFunc}" puts "Total lines: #{@totalLines}" puts numLinesTested, numFunctionsTested = functionsTested puts "Total functions Tested: #{numFunctionsTested}" puts "Total lines Tested: #{numLinesTested}" percFunc = (numFunctionsTested / Float(@numFunc) * 100).round percLines = (numLinesTested / Float(@totalLines) * 100).round puts "Function coverage: #{percFunc}%" puts "Line coverage: #{percLines}%" end def analyzedClasses @analyzedClasses end def testedFunctionsMap @testedFunctionsMap end def totalLines @totalLines end def numClasses @numClasses end def numFunc @numFunc end def analysisDirectory @analysisDirectory end def numProjectFiles @numProjectFiles end def numTestFiles @numTestFiles end end