Skip to content

Commit 1dfc45d

Browse files
committed
Introduce sectionsTestMap loading to load only existing testMaps
1 parent 93ee5af commit 1dfc45d

File tree

5 files changed

+116
-45
lines changed

5 files changed

+116
-45
lines changed

web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/SectionTestMap.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.jetbrains.kotlin.spec.entity
2+
3+
import kotlinx.serialization.json.JsonElement
4+
5+
sealed class TestsLoadingInfo() {
6+
class Tests(val json: JsonElement)
7+
class Sections(val json: JsonElement)
8+
}

web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/GithubTestsLoader.kt

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import js.externals.jquery.JQueryXHR
55
import js.externals.jquery.`$`
66
import kotlinx.serialization.json.Json
77
import kotlinx.serialization.json.JsonConfiguration
8-
import org.jetbrains.kotlin.spec.entity.SectionTestMap
8+
import org.jetbrains.kotlin.spec.entity.TestsLoadingInfo
99
import org.jetbrains.kotlin.spec.entity.SpecSection
1010
import org.jetbrains.kotlin.spec.entity.test.SpecTest
1111
import org.jetbrains.kotlin.spec.entity.test.TestPlace
@@ -25,8 +25,13 @@ interface GithubTestsLoader {
2525
private const val LINKED_SPEC_TESTS_FOLDER = "linked"
2626
private const val HELPERS_FOLDER = "helpers"
2727

28+
private const val SECTIONS_MAP_FILENAME = "sectionsMap.json"
29+
private const val TESTS_MAP_FILENAME = "testsMap.json"
30+
2831
const val DEFAULT_BRANCH = "spec-tests"
2932

33+
protected val testAreasToLoad = TestArea.values()
34+
3035
fun getBranch() = window.localStorage.getItem("spec-tests-branch") ?: DEFAULT_BRANCH
3136

3237
fun loadHelperFromRawGithub(fileName: String, testArea: TestArea): Promise<String> {
@@ -54,28 +59,61 @@ interface GithubTestsLoader {
5459

5560

5661
fun loadTestMapFileFromRawGithub(
62+
mainSectionName: String,
5763
path: String,
5864
testType: TestOrigin,
59-
testAreasToLoad: List<TestArea>
60-
): Promise<Map<TestArea, SectionTestMap>> = Promise { resolve, _ ->
61-
val resultMap = mutableMapOf<TestArea, SectionTestMap>()
65+
sectionsMapByTestArea: Map<TestArea, TestsLoadingInfo.Sections>
66+
): Promise<Map<TestArea, TestsLoadingInfo.Tests>> = Promise { resolve, _ ->
67+
val resultMap = mutableMapOf<TestArea, TestsLoadingInfo.Tests>()
68+
val loadableTestAreas: MutableSet<TestArea> = mutableSetOf()
69+
testAreasToLoad.forEach {
70+
if (sectionsMapByTestArea.isTestsMapExists(testArea = it, requestedMainSection = mainSectionName, requestedSubsectionPath = path)) {
71+
loadableTestAreas.add(it)
72+
}
73+
}
6274
`$`.`when`(
63-
*(testAreasToLoad.associateWith {
64-
`$`.ajax(getFullTestMapPath(testType, it, path), jQueryAjaxSettings { })
75+
*(loadableTestAreas.associateWith {
76+
`$`.ajax(getFullTestMapPath(testType, it, mainSectionName, path), jQueryAjaxSettings { })
6577
.then({ response: Any?, _: Any ->
66-
resultMap[it] = SectionTestMap(parseJsonText(response.toString()))
78+
resultMap[it] = TestsLoadingInfo.Tests(parseJsonText(response.toString()))
6779
})
6880
}.values.toTypedArray())
6981
).then({ _: Any?, _: Any -> resolve(resultMap) }, { resolve(resultMap) })
7082
}
7183

72-
private fun getFullTestMapPath(testOrigin: TestOrigin, testArea: TestArea, path: String) =
84+
private fun Map<TestArea, TestsLoadingInfo.Sections>.isTestsMapExists(testArea: TestArea, requestedMainSection: String, requestedSubsectionPath: String): Boolean {
85+
val subsectionsArray = this[testArea]?.json?.jsonObject?.get(requestedMainSection) ?: return false
86+
subsectionsArray.jsonArray.forEach { jsonElement ->
87+
if (jsonElement.primitive.content == requestedSubsectionPath)
88+
return true
89+
}
90+
return false
91+
}
92+
93+
private fun getFullTestMapPath(testOrigin: TestOrigin, testArea: TestArea, mainSectionName: String, path: String) =
7394
when (testOrigin) {
74-
TestOrigin.SPEC_TEST -> "{1}/{2}/{3}/{4}/{5}/{6}"
75-
.format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, LINKED_SPEC_TESTS_FOLDER, path)
76-
TestOrigin.IMPLEMENTATION_TEST -> "{1}/{2}/{3}".format(RAW_GITHUB_URL, getBranch(), path)
95+
TestOrigin.SPEC_TEST -> "{1}/{2}/{3}/{4}/{5}/{6}/{7}/{8}"
96+
.format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, LINKED_SPEC_TESTS_FOLDER, mainSectionName, path, TESTS_MAP_FILENAME)
97+
TestOrigin.IMPLEMENTATION_TEST -> "{1}/{2}/{3}/{4}/{5}".format(RAW_GITHUB_URL, getBranch(), mainSectionName, path, TESTS_MAP_FILENAME)
7798
}
7899

100+
101+
fun loadSectionsMapFileFromRawGithub(): Promise<Map<TestArea, TestsLoadingInfo.Sections>> = Promise { resolve, _ ->
102+
val resultMap = mutableMapOf<TestArea, TestsLoadingInfo.Sections>()
103+
`$`.`when`(
104+
*(testAreasToLoad.asList().associateWith {
105+
`$`.ajax(getFullSectionsMapPath(it), jQueryAjaxSettings { })
106+
.then({ response: Any?, _: Any ->
107+
resultMap[it] = TestsLoadingInfo.Sections(parseJsonText(response.toString()))
108+
})
109+
}.values.toTypedArray())
110+
).then({ _: Any?, _: Any -> resolve(resultMap) }, { resolve(resultMap) })
111+
}
112+
113+
private fun getFullSectionsMapPath(testArea: TestArea) = "{1}/{2}/{3}/{4}/{5}/{6}"
114+
.format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, LINKED_SPEC_TESTS_FOLDER, SECTIONS_MAP_FILENAME)
115+
116+
79117
private fun getFullHelperPath(testArea: TestArea, helperFile: String) =
80118
"{1}/{2}/{3}/{4}/{5}/{6}"
81119
.format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, HELPERS_FOLDER, helperFile)
@@ -97,5 +135,5 @@ interface GithubTestsLoader {
97135

98136
}
99137

100-
fun loadTestFiles(sectionName: String, sectionsPath: List<String>, testAreasToLoad: Array<TestArea>): Promise<Promise<SpecSection>>
101-
}
138+
fun loadTestFiles(sectionName: String, mainSectionName: String, sectionsPath: List<String>, sectionsMapsByTestArea: Map<TestArea, TestsLoadingInfo.Sections>): Promise<Promise<SpecSection>>
139+
}
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
package org.jetbrains.kotlin.spec.loader
22

3-
import org.jetbrains.kotlin.spec.entity.SectionTestMap
43
import org.jetbrains.kotlin.spec.entity.SpecSection
4+
import org.jetbrains.kotlin.spec.entity.TestsLoadingInfo
55
import org.jetbrains.kotlin.spec.entity.test.SpecTest
66
import org.jetbrains.kotlin.spec.entity.test.TestPlace
77
import org.jetbrains.kotlin.spec.entity.test.parameters.TestInfo
88
import org.jetbrains.kotlin.spec.entity.test.parameters.TestType
99
import org.jetbrains.kotlin.spec.entity.test.parameters.testArea.TestArea
10+
import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.loadSectionsMapFileFromRawGithub
1011
import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.loadTestFileFromRawGithub
1112
import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.loadTestMapFileFromRawGithub
13+
import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.testAreasToLoad
1214
import kotlin.js.Promise
1315

1416

1517
class LoaderByTestsMapFile : GithubTestsLoader {
16-
private val testsMapFilename = "testsMap.json"
1718

18-
private fun loadTestsMapFile(sectionsPath: String, testAreasToLoad: Array<TestArea>
19-
) = loadTestMapFileFromRawGithub(
20-
path = "$sectionsPath/$testsMapFilename",
21-
testType = GithubTestsLoader.TestOrigin.SPEC_TEST,
22-
testAreasToLoad = testAreasToLoad.asList()
23-
)
19+
private fun loadTestsMapFile(mainSectionName: String, sectionsPath: String, sectionsMapByTestArea: Map<TestArea, TestsLoadingInfo.Sections>
20+
): Promise<Map<TestArea, TestsLoadingInfo.Tests>> {
21+
return loadTestMapFileFromRawGithub(
22+
mainSectionName = mainSectionName,
23+
path = sectionsPath,
24+
testType = GithubTestsLoader.TestOrigin.SPEC_TEST,
25+
sectionsMapByTestArea = sectionsMapByTestArea
26+
)
27+
}
28+
29+
private fun loadSectionsMapFile() = loadSectionsMapFileFromRawGithub()
2430

2531

26-
private fun getPromisesForTestFilesFromTestMap(testsMapSection: SectionTestMap?, testArea: TestArea): Array<Promise<SpecTest>> {
32+
private fun getPromisesForTestFilesFromTestMap(testsMap: TestsLoadingInfo.Tests?, testArea: TestArea): Array<Promise<SpecTest>> {
2733
val promises = mutableListOf<Promise<SpecTest>>()
28-
val testsMap = testsMapSection?.sectionTestMap ?: return promises.toTypedArray()
34+
val testsMap = testsMap?.json ?: return promises.toTypedArray()
2935

3036
for ((paragraph, testsByParagraphs) in testsMap.jsonObject) {
3137
for ((testType, testsByTypes) in testsByParagraphs.jsonObject) {
@@ -43,22 +49,32 @@ class LoaderByTestsMapFile : GithubTestsLoader {
4349
}
4450

4551

46-
override fun loadTestFiles(sectionName: String, sectionsPath: List<String>, testAreasToLoad: Array<TestArea>
47-
) = loadTestsMapFile(sectionsPath.joinToString("/") + "/" + sectionName, testAreasToLoad)
48-
.then { sectionTestMaps ->
52+
override fun loadTestFiles(sectionToLoadName: String,
53+
mainSectionPath: String,
54+
sectionsPath: List<String>,
55+
sectionsMapsByTestArea: Map<TestArea, TestsLoadingInfo.Sections>
56+
) = loadTestsMapFile(mainSectionName = mainSectionPath,
57+
sectionsPath = when {
58+
mainSectionPath == sectionToLoadName && sectionsPath.isEmpty() -> "";
59+
sectionsPath.isNotEmpty() -> sectionsPath.joinToString("/") + "/" + sectionToLoadName;
60+
else -> sectionToLoadName
61+
},
62+
sectionsMapByTestArea = sectionsMapsByTestArea)
63+
.then { testsMapsByTestArea ->
4964
val resultMap = mutableMapOf<TestArea, List<SpecTest>>()
5065
Promise.all(testAreasToLoad.asList()
51-
.associateWith { getPromiseForTests(it, sectionTestMaps, resultMap) }
66+
.associateWith { getPromiseForTests(it, testsMapsByTestArea, resultMap) }
5267
.values.toTypedArray()
5368
).then { SpecSection(resultMap) }
5469
}
5570

71+
fun loadSectionsMapFiles() = loadSectionsMapFile()
5672

5773
private fun getPromiseForTests(
5874
testArea: TestArea,
59-
sectionTestMaps: Map<TestArea, SectionTestMap>,
75+
testMaps: Map<TestArea, TestsLoadingInfo.Tests>,
6076
mapOfTests: MutableMap<TestArea, List<SpecTest>>
6177
) = Promise.all(
62-
getPromisesForTestFilesFromTestMap(sectionTestMaps[testArea], testArea))
78+
getPromisesForTestFilesFromTestMap(testMaps[testArea], testArea))
6379
.then { mapOfTests[testArea] = it.toList() }
6480
}

web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/SpecTestsLoader.kt

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlin.spec.loader
22

33
import js.externals.jquery.JQuery
44
import js.externals.jquery.`$`
5+
import org.jetbrains.kotlin.spec.entity.TestsLoadingInfo
56
import org.jetbrains.kotlin.spec.entity.SpecSection
67
import org.jetbrains.kotlin.spec.entity.test.parameters.testArea.TestArea
78
import org.jetbrains.kotlin.spec.utils.format
@@ -165,15 +166,24 @@ class SpecTestsLoader {
165166
private var originalSectionName: String? = null
166167
private var numberSectionsLoaded = 0
167168

169+
168170
fun onTestsLoadingLinkClick(link: JQuery) {
171+
loader.loadSectionsMapFiles()
172+
.then { sectionsMapsByTestArea ->
173+
loadTests(link, sectionsMapsByTestArea)
174+
}
175+
}
176+
177+
private fun loadTests(link: JQuery, sectionsMapsByTestArea: Map<TestArea, TestsLoadingInfo.Sections>) {
169178
val section = link.parent("h2, h3, h4, h5")
170179
val paragraphsInfo = getParagraphsInfo(section)
171180
val nestedSections = getNestedSections(section)
172-
val sectionName = section.attr("id")
173-
val sectionsPath = mutableListOf(getParentSectionName(section, "h2"))
181+
val sectionToLoadName = section.attr("id")
182+
val sectionsPath: MutableList<String> = mutableListOf()
183+
val mainSectionsPath = getParentSectionName(section, "h2")
174184

175185
if (originalSectionName == null) {
176-
originalSectionName = sectionName
186+
originalSectionName = sectionToLoadName
177187
numberSectionsLoaded = 1
178188
}
179189

@@ -186,24 +196,28 @@ class SpecTestsLoader {
186196
sectionsPath.add(getParentSectionName(section, "h4"))
187197
}
188198

189-
loader.loadTestFiles(sectionName, sectionsPath, TestArea.values())
199+
loader.loadTestFiles(
200+
sectionToLoadName = sectionToLoadName,
201+
mainSectionPath = mainSectionsPath,
202+
sectionsPath = sectionsPath,
203+
sectionsMapsByTestArea = sectionsMapsByTestArea)
190204
.then { sectionTestSet ->
191205

192206
if (paragraphsInfo != null)
193-
parseTestFiles(sectionTestSet, sectionName, sectionsPath, paragraphsInfo)
207+
parseTestFiles(sectionTestSet, sectionToLoadName, sectionsPath, paragraphsInfo)
194208

195209
link.html(getButtonToLoadTests(link, true))
196210

197-
if (originalSectionName == sectionName) {
211+
if (originalSectionName == sectionToLoadName) {
198212
section.nextAll(".paragraph.with-tests").first().get()[0].scrollIntoView()
199213
originalSectionName = null
200-
sectionPrevLoaded = sectionName
214+
sectionPrevLoaded = sectionToLoadName
201215
}
202216
}.catch {
203217
numberSectionsLoaded--
204-
if (originalSectionName == sectionName) {
218+
if (originalSectionName == sectionToLoadName) {
205219
originalSectionName = null
206-
sectionPrevLoaded = sectionName
220+
sectionPrevLoaded = sectionToLoadName
207221
}
208222
if (numberSectionsLoaded == 0) {
209223
window.alert(notLoadedTestsText.format(sectionPrevLoaded))
@@ -213,7 +227,7 @@ class SpecTestsLoader {
213227

214228
nestedSections.forEach { sectionId ->
215229
numberSectionsLoaded++
216-
`$`("#${sectionId.replace(".", """\\.""")} .load-tests").click()
230+
loadTests(`$`("#${sectionId.replace(".", """\\.""")} .load-tests").click(), sectionsMapsByTestArea)
217231
}
218232
}
219-
}
233+
}

0 commit comments

Comments
 (0)