All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openbakery.coverage.model.SourceFile.groovy Maven / Gradle / Ivy

There is a newer version: 0.9.4
Show newest version
package org.openbakery.coverage.model

import org.apache.commons.io.FilenameUtils

import java.text.SimpleDateFormat

/**
 * Created by René Pirringer
 */
class SourceFile {

	String filename
	List sourceLines
	List methods = []

	SourceFile(List lines, String baseDirectory) {
		def currentMethod = null
		sourceLines = []

		lines.eachWithIndex { value, index ->

			if (value.startsWith("  ------------------") || value.startsWith("  |")) {
				return;
			}


			if (index == 0) {
				filename = parseFilename(value, baseDirectory)
			} else {
				SourceLine sourceLine = new SourceLine(value)
				if (currentMethod == null && sourceLine.hits != SourceLine.NOT_A_NUMBER) {
					currentMethod = new Function()
					methods << currentMethod
				} else if (currentMethod != null && sourceLine.hits == SourceLine.NOT_A_NUMBER) {
					currentMethod = null;
				}
				if (currentMethod) {
					currentMethod.sourceLines << sourceLine
				}
				sourceLines << sourceLine
			}
		}

	}

	String parseFilename(String line, String baseDirectory) {
		if (line.endsWith(":")) {
			return line[0..-2]  - baseDirectory
		}
		return line - baseDirectory
	}

	String getFileBasename() {
		String filenameEscaped = filename.replace("/", "_")
		return FilenameUtils.getBaseName(filenameEscaped)
	}

	ListgetSourceLinesCovered() {
		return sourceLines.findAll { it.hits != SourceLine.NOT_A_NUMBER }
	}

	long getLinesNumber() {
		return sourceLines.size()
	}

	long getLinesCovered() {
		return sourceLines.count { it.hits > 0 }
	}

	long getLinesNotCovered() {
		return sourceLines.count { it.hits == 0 }
	}

	long getLinesExecuted() {
		return getLinesCovered() + getLinesNotCovered()
	}

	double getCoverage() {
		return SourceLine.getCoverage(sourceLines)
	}

	String getCoverageInPercent() {
		return ((int)(getCoverage()*1000))/10
	}


	String getName() {
		return FilenameUtils.getName(filename)
	}

	Coverage getCoverageRate() {
		return getCoverageRate(getCoverage())
	}

	String getCurrentDate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat()
		return dateFormat.format(new Date())
	}

	static long getLinesCovered(List sourceFiles) {
		if (sourceFiles == null || sourceFiles.size() == 0) {
			return 0
		}
		return sourceFiles.sum { it.getLinesCovered()}
	}

	static long getLinesNotCovered(List sourceFiles) {
		if (sourceFiles == null || sourceFiles.size() == 0) {
			return 0
		}
		return sourceFiles.sum { it.getLinesNotCovered()}
	}

	static long getLinesExecuted(List sourceFiles) {
		if (sourceFiles == null || sourceFiles.size() == 0) {
			return 0
		}
		return sourceFiles.sum { it.getLinesExecuted()}
	}

	static long getTotalLines(List sourceFiles) {
		if (sourceFiles == null || sourceFiles.size() == 0) {
			return 0
		}
		return sourceFiles.sum {
			it.sourceLines.size()
		}
	}

	static double getCoverage(List sourceFiles) {
		if (sourceFiles == null || sourceFiles.size() == 0) {
			return 0
		}
		return getLinesCovered(sourceFiles) / getLinesExecuted(sourceFiles)
	}


	static Coverage getCoverageRate(double coverage) {
		if (coverage < 0.5) {
			return Coverage.Poor
		}
		if (coverage > 0.75) {
			return Coverage.Good
		}
		return Coverage.Ok
	}

	@Override
	public String toString() {
		return "SourceFile{" +
						"filename='" + filename + '\'' +
						'}';
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy