#!/usr/bin/env ruby class AbstractClassAnalyzer def initialize @curlyBraces = 0 @funcCurlyBraces = 0 @functionName = nil @funcLines = 0 end def updateCurlyBraceIndex (line) if line.include? "{" @curlyBraces = @curlyBraces + 1 end if line.include? "}" @curlyBraces = @curlyBraces - 1 end end def didReachEndOfClass @curlyBraces == 0 end def resetClassStats @curlyBraces = 0 end def updateFunctionCurlyBraceIndex (line) if line.include? "{" @funcCurlyBraces = @funcCurlyBraces + 1 end if line.include? "}" @funcCurlyBraces = @funcCurlyBraces - 1 end end def didReachEndOfFunction @funcCurlyBraces == 0 end def resetFunctionStats @funcCurlyBraces = 0 @funcLines = 0 @functionName = nil end end class AnalyzedClassGenerator < AbstractClassAnalyzer def generateAnalyzedClassesForFile (file) analyzedClasses = Array.new analyzedClass = AnalyzedClass.new f = File.open(file, "r") f.each_line do |line| tempClassName = classNameForLine line, analyzedClass.name if tempClassName != nil analyzedClass.name = tempClassName end if analyzedClass.name != nil if line.strip.length > 1 analyzedClass.numberOfLines += 1 end if analyzedClass.fileName == nil analyzedClass.fileName = file end updateCurlyBraceIndex line if line.include? "func " @functionName = line.slice((line.index("func ") + 5)..(line.index("(")-1)) end if @functionName != nil updateFunctionCurlyBraceIndex line if line.strip.length > 0 @funcLines = @funcLines + 1 end if didReachEndOfFunction analyzedClass.addFunction(@functionName, @funcLines) resetFunctionStats end end if (line.include? "var " or line.include? "let ") variable = Variable.new.variableForLine line analyzedClass.addVariable variable end if didReachEndOfClass analyzedClasses.push(analyzedClass) resetClassStats analyzedClass = AnalyzedClass.new end end end f.close return analyzedClasses end def classNameForLine (line, name) if LineDeterminator.new(line).isClass and name == nil if line.include? ":" return line.strip.slice(5..(line.strip.index(":")-1)) else strippedLine = line.gsub("class", "").strip return strippedLine.slice(0..(strippedLine.index(" ")-1)) end end return nil end end class AnalyzedTestClassGenerator def initialize (analyzedClasses, analyzedTestClasses, testCaseDirectory) @analyzedClasses = analyzedClasses @analyzedTestClasses = analyzedTestClasses @testCaseDirectory = testCaseDirectory end def generateAnalyzedTestClasses testedFunctionsMap = Hash.new curlyBraces = 0 count = 0 funcCurlyBraces = 0 funcLines = 0 name = nil testClass = nil Dir.glob("#{@testCaseDirectory}/**/*.swift") do |item| next if item == '.' or item == '..' f = File.open(item, "r") f.each_line do |line| tempName = AnalyzedClassGenerator.new.classNameForLine line, name if tempName != nil name = tempName end if name != nil if line.include? "{" curlyBraces = curlyBraces + 1 end if line.include? "}" curlyBraces = curlyBraces - 1 end if testClass == nil testClass = analyzedTestClassWithName name end variable = variableForLine testClass, line if variable for analyzedClass in @analyzedClasses if variable.type == analyzedClass.name for function in analyzedClass.functions if line.include? function.name # instance function analyzedClass.addTestedFunction function end end end end elsif line.strip.length > 9 for analyzedClass in @analyzedClasses if line.include? analyzedClass.name for function in analyzedClass.functions if line.include? function.name # Class function analyzedClass.addTestedFunction function end end end end end if curlyBraces == 0 testClass = nil name = nil curlyBraces = 0 end end end f.close end return testedFunctionsMap end def analyzedTestClassWithName (name) for testClass in @analyzedTestClasses if testClass.name == name.strip return testClass end end return nil end def variableForLine (testClass, line) for variable in testClass.variablesWithType if line.include? variable.name return variable end end return nil end end