Skip to content

Commit 4bb7acc

Browse files
authored
feat: indentation fact (#110) (#154)
1 parent 23e383d commit 4bb7acc

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/main/kotlin/app/FactCodes.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ object FactCodes {
2323
val VARIABLE_NAMING_SNAKE_CASE = 0
2424
val VARIABLE_NAMING_CAMEL_CASE = 1
2525
val VARIABLE_NAMING_OTHER = 2
26+
val INDENTATION = 14
27+
val INDENTATION_TABS = 0
28+
val INDENTATION_SPACES = 1
2629
}

src/main/kotlin/app/hashers/FactHasher.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FactHasher(private val serverRepo: Repo = Repo(),
3333
private val fsLineNum = hashMapOf<String, Long>()
3434
private val fsLinesPerCommits = hashMapOf<String, Array<Int>>()
3535
private val fsVariableNaming = hashMapOf<String, Array<Int>>()
36+
private val fsIndentation = hashMapOf<String, Array<Int>>()
3637

3738
init {
3839
for (author in emails) {
@@ -47,6 +48,7 @@ class FactHasher(private val serverRepo: Repo = Repo(),
4748
// TODO(anatoly): Do the bin computations on the go.
4849
fsLinesPerCommits.put(author, Array(rehashes.size) {0})
4950
fsVariableNaming.put(author, Array(3) { 0 })
51+
fsIndentation.put(author, Array(2) { 0 })
5052
}
5153
}
5254

@@ -121,6 +123,11 @@ class FactHasher(private val serverRepo: Repo = Repo(),
121123
fsVariableNaming[email]!![FactCodes.VARIABLE_NAMING_OTHER] +=
122124
others
123125
}
126+
127+
fsIndentation[email]!![FactCodes.INDENTATION_SPACES] +=
128+
lines.count { it.isNotBlank() && it.startsWith(" ") && !it.contains("\t")}
129+
fsIndentation[email]!![FactCodes.INDENTATION_TABS] +=
130+
lines.count { it.startsWith("\t") }
124131
}
125132

126133
private fun createFacts(): List<Fact> {
@@ -139,6 +146,12 @@ class FactHasher(private val serverRepo: Repo = Repo(),
139146
fs.add(Fact(serverRepo, FactCodes.VARIABLE_NAMING, naming,
140147
count.toString(), author))
141148
}}
149+
fsIndentation[email]?.forEachIndexed { indentation, count ->
150+
if (count > 0) {
151+
fs.add(Fact(serverRepo, FactCodes.INDENTATION, indentation,
152+
count.toString(), author))
153+
}
154+
}
142155

143156
fs.add(Fact(serverRepo, FactCodes.REPO_DATE_START, 0,
144157
fsRepoDateStart[email].toString(), author))

src/test/kotlin/test/tests/hashers/FactHasherTest.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,46 @@ class FactHasherTest : Spek({
327327
testRepo.destroy()
328328
}
329329
}
330+
331+
given("commits for indentation facts") {
332+
val testRepo = TestRepo(repoPath + "indentation-facts")
333+
val emails = hashSetOf(authorEmail1)
334+
val mockApi = MockApi(mockRepo = repo)
335+
val facts = mockApi.receivedFacts
336+
337+
afterEachTest {
338+
facts.clear()
339+
}
340+
341+
val lines = listOf("\tdef test()", "\t\tdef fn()", "a b c d", " ", " def fn()")
342+
343+
it("sends facts") {
344+
for (i in 0..lines.size - 1) {
345+
val line = lines[i]
346+
val fileName = "file$i.txt"
347+
testRepo.createFile(fileName, listOf(line))
348+
testRepo.commit(message = "$line in $fileName", author = author1)
349+
}
350+
351+
val errors = mutableListOf<Throwable>()
352+
val observable = CommitCrawler.getObservable(testRepo.git, repo)
353+
val rehashes = (0..lines.size - 1).map { "r$it" }
354+
355+
FactHasher(repo, mockApi, rehashes, emails)
356+
.updateFromObservable(observable, { e -> errors.add(e) })
357+
if (errors.size > 0) {
358+
println(errors[0].message)
359+
}
360+
assertEquals(0, errors.size)
361+
362+
assertFactInt(FactCodes.INDENTATION,
363+
FactCodes.INDENTATION_TABS, 2, author1, facts)
364+
assertFactInt(FactCodes.INDENTATION,
365+
FactCodes.INDENTATION_SPACES, 1, author1, facts)
366+
}
367+
368+
afterGroup {
369+
testRepo.destroy()
370+
}
371+
}
330372
})

0 commit comments

Comments
 (0)