#!/usr/bin/env ruby
require_relative 'FormGenerator'
require_relative 'AnalyzedClass'
class IndexHTMLFile
def initialize(formGenerator)
@formGenerator = formGenerator
@indexHTMLString = ""
end
def beginWriting
@indexHTML = File.new("#{@formGenerator.analysisDirectory}/index.html", "w+")
end
def addAnalysisItem (analyzedClass, analysisFile)
htmlFileName = analysisFile.name
htmlFileName = htmlFileName.gsub(".swift", "")
htmlFileName = htmlFileName.gsub("./", "")
@indexHTMLString += "
"
@indexHTMLString += "\n"
@indexHTMLString += "\n#{htmlFileName}.swift"
@indexHTMLString += "\n | "
numFunctions, numFunctionsTested, percFunctions = funcStats analyzedClass
color = colorForValidLinePercentage percFunctions
@indexHTMLString += "\n"
@indexHTMLString += "\n#{percFunctions}% (#{numFunctionsTested}/#{numFunctions})"
@indexHTMLString += "\n | "
numLinesTested = analyzedClass.numberOfLinesTested
numLines = analyzedClass.numberOfLines
percLines = ((Float(numLinesTested) / Float(numLines)) * 100).round
color = colorForValidLinePercentage percLines
@indexHTMLString += "\n"
@indexHTMLString += "\n#{percLines}% (#{numLinesTested}/#{numLines})"
@indexHTMLString += "\n | "
end
def funcStats (analyzedClass)
numFunctionsTested = analyzedClass.testedFunctions.count
numFunctions = analyzedClass.functions.count
if numFunctionsTested == 0 or numFunctions == 0
return 0, 0, 0
end
percFunctions = ((Float(numFunctionsTested) / Float(numFunctions)) * 100).round
return numFunctions, numFunctionsTested, percFunctions
end
def addCoverageTable
@indexHTML.puts "OVERALL COVERAGE
"
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts "class | "
@indexHTML.puts "function | "
@indexHTML.puts "lines | "
@indexHTML.puts "
"
@indexHTML.puts ""
percClassesTested = ((Float(@formGenerator.classesTested) / Float(@formGenerator.numClasses)) * 100).round
color = colorForValidLinePercentage percClassesTested
@indexHTML.puts ""
@indexHTML.puts "#{percClassesTested}% (#{@formGenerator.classesTested}/#{@formGenerator.numClasses})"
@indexHTML.puts " | "
numLinesTested, numFunctionsTested = @formGenerator.functionsTested
percFunctionsTested = ((Float(numFunctionsTested) / Float(@formGenerator.numFunc)) * 100).round
color = colorForValidLinePercentage percFunctionsTested
@indexHTML.puts ""
@indexHTML.puts "#{percFunctionsTested}% (#{numFunctionsTested}/#{@formGenerator.numFunc})"
@indexHTML.puts " | "
percLinesTested = ((Float(numLinesTested) / Float(@formGenerator.totalLines)) * 100).round
color = colorForValidLinePercentage percLinesTested
@indexHTML.puts ""
@indexHTML.puts "#{percLinesTested}% (#{numLinesTested}/#{@formGenerator.totalLines})"
@indexHTML.puts " | "
@indexHTML.puts "
"
@indexHTML.puts "
"
end
def addTotalStatsTable
@indexHTML.puts "OVERALL STATS
"
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts "Total files:"
@indexHTML.puts " | "
@indexHTML.puts ""
@indexHTML.puts "#{@formGenerator.numProjectFiles}"
@indexHTML.puts " | "
@indexHTML.puts "
"
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts "Total test files:"
@indexHTML.puts " | "
@indexHTML.puts ""
@indexHTML.puts "#{@formGenerator.numTestFiles}"
@indexHTML.puts " | "
@indexHTML.puts "
"
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts "Total classes:"
@indexHTML.puts " | "
@indexHTML.puts ""
@indexHTML.puts "#{@formGenerator.numClasses}"
@indexHTML.puts " | "
@indexHTML.puts "
"
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts "Total functions:"
@indexHTML.puts " | "
@indexHTML.puts ""
@indexHTML.puts "#{@formGenerator.numFunc}"
@indexHTML.puts " | "
@indexHTML.puts "
"
@indexHTML.puts "
"
end
def addAnalysisFilesTable
@indexHTML.puts "COVERAGE BY FILES
"
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts "File | "
@indexHTML.puts "Functions | "
@indexHTML.puts "Lines | "
@indexHTML.puts "
"
@indexHTML.puts @indexHTMLString
@indexHTML.puts "
"
end
def colorForValidLinePercentage (percentage)
if percentage >= 90
return "#52CC52" # Green
end
if percentage >= 65 and percentage < 90
return "yellow"
end
if percentage < 65
return "#FF4D4D" # Red
end
end
def endWriting
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts ""
@indexHTML.puts ""
addCoverageTable
addTotalStatsTable
addAnalysisFilesTable
@indexHTML.puts ""
@indexHTML.close()
end
end