Skip to content

Commit

Permalink
feat: add support for anlysis content by content
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 4, 2023
1 parent e274524 commit fb61da8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
package org.unimesh.eval.picker.bs

import chapi.domain.core.CodeDataStruct
import org.archguard.scanner.analyser.count.FileJob
import org.archguard.scanner.analyser.count.LanguageSummary
import org.archguard.scanner.analyser.count.LanguageWorker
import java.nio.file.Path

class EstimateAnalyser(val data: List<CodeDataStruct>) {
class EstimateAnalyser() {
companion object {
private var instance: EstimateAnalyser? = null
fun getInstance(): EstimateAnalyser {
if (instance == null) {
instance = EstimateAnalyser()
}

return instance!!
}
}

private var languageWorker = LanguageWorker()

fun analysisByPath(path: Path): LanguageSummary? {
val file = path.toFile()
val content = file.readText()

return analysisByContent(content, file.name)
}

fun analysisByContent(content: String, filename: String): LanguageSummary? {
val fileContent = content.toByteArray()
val fileJob = FileJob(
language = "Go",
content = fileContent,
filename = filename,
bytes = fileContent.size.toLong(),
)


val countStats = languageWorker.countStats(fileJob) ?: return null

return LanguageSummary(
name = countStats.language,
lines = countStats.lines,
code = countStats.code,
blank = countStats.blank,
comment = countStats.comment,
complexity = countStats.complexity,
weightedComplexity = countStats.weightedComplexity,
bytes = countStats.bytes,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.unimesh.eval.picker.bs;

import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.test.assertEquals

class EstimateAnalyserTest {

@Test
fun should_return_null_when_analysis_by_content_with_empty_content() {
// given
val content = """type BSDataStruct struct {
core_domain.CodeDataStruct
Functions []BSFunction
DataStructBS ClassBadSmellInfo
}"""
val file = File("sample.txt").canonicalPath

// when
val summary = EstimateAnalyser.getInstance().analysisByContent(content, file)!!

// then
println(summary)
assertEquals("Go", summary.name)
}
}

0 comments on commit fb61da8

Please sign in to comment.