Skip to content

Commit 009b24f

Browse files
asurkovanatolystansler
authored andcommitted
feat(tests): add lang heuristics testing (#278)
* feat(tests) - add lang heuristics testing * chore: style change * Add .gitattributes to exclude sampels from github stats
1 parent 5c38552 commit 009b24f

File tree

1,999 files changed

+537613
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,999 files changed

+537613
-1
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/test/resources/samples/* linguist-vendored

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ dependencies {
107107
compile 'org.slf4j:slf4j-nop:1.7.2'
108108
compile 'io.sentry:sentry:1.7.3'
109109

110+
testCompile 'commons-io:commons-io:2.6'
110111
testCompile 'org.jetbrains.kotlin:kotlin-test'
111112
testCompile 'org.jetbrains.spek:spek-api:1.1.5'
112113
testCompile 'org.junit.platform:junit-platform-runner:1.0.1'

src/main/kotlin/app/extractors/Heuristics.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ val Heuristics = mapOf<String, (List<String>) -> ExtractorInterface?>(
326326
else if (Regex("^\\s*;").matches(buf)) CommonExtractor("m")
327327
else if (Regex("\\*\\)$").matches(buf)) CommonExtractor("mathematica")
328328
else if (Regex("^\\s*%").matches(buf)) CommonExtractor("matlab")
329-
else if (Regex("^\\w+\\s*:\\s*module\\s*{").matches(buf)) CommonExtractor("limbo")
329+
else if (Regex("^\\w+\\s*:\\s*module\\s*\\{").matches(buf)) CommonExtractor("limbo")
330330
else CommonExtractor("wolframlanguage")
331331
},
332332
"makefile" to { _ ->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2018 Sourcerer Inc. All Rights Reserved.
2+
// Author: Alexander Surkov ([email protected])
3+
4+
package test.tests.extractors
5+
6+
// TODO(anatoly): Map all supported languages.
7+
val dirToLangMap = mapOf(
8+
"C" to "c",
9+
"C#" to "csharp",
10+
"C++" to "cpp",
11+
"CSS" to "css"
12+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2018 Sourcerer Inc. All Rights Reserved.
2+
// Author: Alexander Surkov ([email protected])
3+
4+
package test.tests.extractors
5+
6+
import app.Logger
7+
import app.model.DiffFile
8+
import app.model.DiffContent
9+
import app.extractors.*
10+
import org.apache.commons.io.FilenameUtils
11+
import org.eclipse.jgit.diff.DiffEntry.ChangeType
12+
import org.jetbrains.spek.api.Spek
13+
import org.jetbrains.spek.api.dsl.given
14+
import org.jetbrains.spek.api.dsl.it
15+
import java.io.File
16+
import kotlin.test.assertEquals
17+
18+
const val LANG_SAMPLES_PATH = "src/test/resources/samples/"
19+
20+
fun assertLang(file: File, expectedLang: String) {
21+
val diffFile = DiffFile(
22+
file.path,
23+
changeType = ChangeType.ADD,
24+
new = DiffContent(content = file.readLines())
25+
)
26+
Extractor().extract(listOf(diffFile))
27+
28+
val actualLang = diffFile.language
29+
30+
// TODO(anatoly): Verify all sample files from ignored list.
31+
// TODO(anatoly): Add support for all languages of samples.
32+
for (wc in ignoredSamplesWildcards) {
33+
if (FilenameUtils.wildcardMatchOnSystem(file.path, wc)) {
34+
Logger.debug { "-> File: ${file.absolutePath}. " +
35+
"Expected: <$expectedLang>, actual: <$actualLang>" }
36+
return
37+
}
38+
}
39+
40+
assertEquals(expectedLang, actualLang, "Unexpected lang for ${file.path}")
41+
}
42+
43+
class HeuristicsTest : Spek({
44+
given("heuristics test") {
45+
it("all language samples") {
46+
for (dir in File(LANG_SAMPLES_PATH).listFiles()) {
47+
val expectedLang = dirToLangMap.getOrDefault(dir.name, dir.name)
48+
for (file in dir.walkTopDown()) {
49+
if (file.isFile) assertLang(file, expectedLang)
50+
}
51+
}
52+
}
53+
}
54+
})

0 commit comments

Comments
 (0)