diff --git a/web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/SectionTestMap.kt b/web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/SectionTestMap.kt deleted file mode 100644 index e0880118d..000000000 --- a/web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/SectionTestMap.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.jetbrains.kotlin.spec.entity - -import kotlinx.serialization.json.JsonElement - -class SectionTestMap(val sectionTestMap: JsonElement) \ No newline at end of file diff --git a/web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/TestsLoadingInfo.kt b/web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/TestsLoadingInfo.kt new file mode 100644 index 000000000..b12468110 --- /dev/null +++ b/web/src/main/kotlin/org/jetbrains/kotlin/spec/entity/TestsLoadingInfo.kt @@ -0,0 +1,8 @@ +package org.jetbrains.kotlin.spec.entity + +import kotlinx.serialization.json.JsonElement + +sealed class TestsLoadingInfo() { + class Tests(val json: JsonElement) + class Sections(val json: JsonElement) +} \ No newline at end of file diff --git a/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/GithubTestsLoader.kt b/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/GithubTestsLoader.kt index 985cc74ff..860fab143 100644 --- a/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/GithubTestsLoader.kt +++ b/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/GithubTestsLoader.kt @@ -5,7 +5,7 @@ import js.externals.jquery.JQueryXHR import js.externals.jquery.`$` import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration -import org.jetbrains.kotlin.spec.entity.SectionTestMap +import org.jetbrains.kotlin.spec.entity.TestsLoadingInfo import org.jetbrains.kotlin.spec.entity.SpecSection import org.jetbrains.kotlin.spec.entity.test.SpecTest import org.jetbrains.kotlin.spec.entity.test.TestPlace @@ -25,8 +25,13 @@ interface GithubTestsLoader { private const val LINKED_SPEC_TESTS_FOLDER = "linked" private const val HELPERS_FOLDER = "helpers" + private const val SECTIONS_MAP_FILENAME = "sectionsMap.json" + private const val TESTS_MAP_FILENAME = "testsMap.json" + const val DEFAULT_BRANCH = "spec-tests" + protected val testAreasToLoad = TestArea.values() + fun getBranch() = window.localStorage.getItem("spec-tests-branch") ?: DEFAULT_BRANCH fun loadHelperFromRawGithub(fileName: String, testArea: TestArea): Promise { @@ -54,28 +59,61 @@ interface GithubTestsLoader { fun loadTestMapFileFromRawGithub( + mainSectionName: String, path: String, testType: TestOrigin, - testAreasToLoad: List - ): Promise> = Promise { resolve, _ -> - val resultMap = mutableMapOf() + sectionsMapByTestArea: Map + ): Promise> = Promise { resolve, _ -> + val resultMap = mutableMapOf() + val loadableTestAreas: MutableSet = mutableSetOf() + testAreasToLoad.forEach { + if (sectionsMapByTestArea.isTestsMapExists(testArea = it, requestedMainSection = mainSectionName, requestedSubsectionPath = path)) { + loadableTestAreas.add(it) + } + } `$`.`when`( - *(testAreasToLoad.associateWith { - `$`.ajax(getFullTestMapPath(testType, it, path), jQueryAjaxSettings { }) + *(loadableTestAreas.associateWith { + `$`.ajax(getFullTestMapPath(testType, it, mainSectionName, path), jQueryAjaxSettings { }) .then({ response: Any?, _: Any -> - resultMap[it] = SectionTestMap(parseJsonText(response.toString())) + resultMap[it] = TestsLoadingInfo.Tests(parseJsonText(response.toString())) }) }.values.toTypedArray()) ).then({ _: Any?, _: Any -> resolve(resultMap) }, { resolve(resultMap) }) } - private fun getFullTestMapPath(testOrigin: TestOrigin, testArea: TestArea, path: String) = + private fun Map.isTestsMapExists(testArea: TestArea, requestedMainSection: String, requestedSubsectionPath: String): Boolean { + val subsectionsArray = this[testArea]?.json?.jsonObject?.get(requestedMainSection) ?: return false + subsectionsArray.jsonArray.forEach { jsonElement -> + if (jsonElement.primitive.content == requestedSubsectionPath) + return true + } + return false + } + + private fun getFullTestMapPath(testOrigin: TestOrigin, testArea: TestArea, mainSectionName: String, path: String) = when (testOrigin) { - TestOrigin.SPEC_TEST -> "{1}/{2}/{3}/{4}/{5}/{6}" - .format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, LINKED_SPEC_TESTS_FOLDER, path) - TestOrigin.IMPLEMENTATION_TEST -> "{1}/{2}/{3}".format(RAW_GITHUB_URL, getBranch(), path) + TestOrigin.SPEC_TEST -> "{1}/{2}/{3}/{4}/{5}/{6}/{7}/{8}" + .format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, LINKED_SPEC_TESTS_FOLDER, mainSectionName, path, TESTS_MAP_FILENAME) + TestOrigin.IMPLEMENTATION_TEST -> "{1}/{2}/{3}/{4}/{5}".format(RAW_GITHUB_URL, getBranch(), mainSectionName, path, TESTS_MAP_FILENAME) } + + fun loadSectionsMapFileFromRawGithub(): Promise> = Promise { resolve, _ -> + val resultMap = mutableMapOf() + `$`.`when`( + *(testAreasToLoad.asList().associateWith { + `$`.ajax(getFullSectionsMapPath(it), jQueryAjaxSettings { }) + .then({ response: Any?, _: Any -> + resultMap[it] = TestsLoadingInfo.Sections(parseJsonText(response.toString())) + }) + }.values.toTypedArray()) + ).then({ _: Any?, _: Any -> resolve(resultMap) }, { resolve(resultMap) }) + } + + private fun getFullSectionsMapPath(testArea: TestArea) = "{1}/{2}/{3}/{4}/{5}/{6}" + .format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, LINKED_SPEC_TESTS_FOLDER, SECTIONS_MAP_FILENAME) + + private fun getFullHelperPath(testArea: TestArea, helperFile: String) = "{1}/{2}/{3}/{4}/{5}/{6}" .format(RAW_GITHUB_URL, getBranch(), SPEC_TEST_DATA_PATH, testArea.path, HELPERS_FOLDER, helperFile) @@ -97,5 +135,5 @@ interface GithubTestsLoader { } - fun loadTestFiles(sectionName: String, sectionsPath: List, testAreasToLoad: Array): Promise> -} \ No newline at end of file + fun loadTestFiles(sectionName: String, mainSectionName: String, sectionsPath: List, sectionsMapsByTestArea: Map): Promise> +} diff --git a/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/LoaderByTestsMapFile.kt b/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/LoaderByTestsMapFile.kt index 89b92a614..b0cdb8249 100644 --- a/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/LoaderByTestsMapFile.kt +++ b/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/LoaderByTestsMapFile.kt @@ -1,31 +1,37 @@ package org.jetbrains.kotlin.spec.loader -import org.jetbrains.kotlin.spec.entity.SectionTestMap import org.jetbrains.kotlin.spec.entity.SpecSection +import org.jetbrains.kotlin.spec.entity.TestsLoadingInfo import org.jetbrains.kotlin.spec.entity.test.SpecTest import org.jetbrains.kotlin.spec.entity.test.TestPlace import org.jetbrains.kotlin.spec.entity.test.parameters.TestInfo import org.jetbrains.kotlin.spec.entity.test.parameters.TestType import org.jetbrains.kotlin.spec.entity.test.parameters.testArea.TestArea +import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.loadSectionsMapFileFromRawGithub import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.loadTestFileFromRawGithub import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.loadTestMapFileFromRawGithub +import org.jetbrains.kotlin.spec.loader.GithubTestsLoader.Companion.testAreasToLoad import kotlin.js.Promise class LoaderByTestsMapFile : GithubTestsLoader { - private val testsMapFilename = "testsMap.json" - private fun loadTestsMapFile(sectionsPath: String, testAreasToLoad: Array - ) = loadTestMapFileFromRawGithub( - path = "$sectionsPath/$testsMapFilename", - testType = GithubTestsLoader.TestOrigin.SPEC_TEST, - testAreasToLoad = testAreasToLoad.asList() - ) + private fun loadTestsMapFile(mainSectionName: String, sectionsPath: String, sectionsMapByTestArea: Map + ): Promise> { + return loadTestMapFileFromRawGithub( + mainSectionName = mainSectionName, + path = sectionsPath, + testType = GithubTestsLoader.TestOrigin.SPEC_TEST, + sectionsMapByTestArea = sectionsMapByTestArea + ) + } + + private fun loadSectionsMapFile() = loadSectionsMapFileFromRawGithub() - private fun getPromisesForTestFilesFromTestMap(testsMapSection: SectionTestMap?, testArea: TestArea): Array> { + private fun getPromisesForTestFilesFromTestMap(testsMap: TestsLoadingInfo.Tests?, testArea: TestArea): Array> { val promises = mutableListOf>() - val testsMap = testsMapSection?.sectionTestMap ?: return promises.toTypedArray() + val testsMap = testsMap?.json ?: return promises.toTypedArray() for ((paragraph, testsByParagraphs) in testsMap.jsonObject) { for ((testType, testsByTypes) in testsByParagraphs.jsonObject) { @@ -43,22 +49,32 @@ class LoaderByTestsMapFile : GithubTestsLoader { } - override fun loadTestFiles(sectionName: String, sectionsPath: List, testAreasToLoad: Array - ) = loadTestsMapFile(sectionsPath.joinToString("/") + "/" + sectionName, testAreasToLoad) - .then { sectionTestMaps -> + override fun loadTestFiles(sectionToLoadName: String, + mainSectionPath: String, + sectionsPath: List, + sectionsMapsByTestArea: Map + ) = loadTestsMapFile(mainSectionName = mainSectionPath, + sectionsPath = when { + mainSectionPath == sectionToLoadName && sectionsPath.isEmpty() -> ""; + sectionsPath.isNotEmpty() -> sectionsPath.joinToString("/") + "/" + sectionToLoadName; + else -> sectionToLoadName + }, + sectionsMapByTestArea = sectionsMapsByTestArea) + .then { testsMapsByTestArea -> val resultMap = mutableMapOf>() Promise.all(testAreasToLoad.asList() - .associateWith { getPromiseForTests(it, sectionTestMaps, resultMap) } + .associateWith { getPromiseForTests(it, testsMapsByTestArea, resultMap) } .values.toTypedArray() ).then { SpecSection(resultMap) } } + fun loadSectionsMapFiles() = loadSectionsMapFile() private fun getPromiseForTests( testArea: TestArea, - sectionTestMaps: Map, + testMaps: Map, mapOfTests: MutableMap> ) = Promise.all( - getPromisesForTestFilesFromTestMap(sectionTestMaps[testArea], testArea)) + getPromisesForTestFilesFromTestMap(testMaps[testArea], testArea)) .then { mapOfTests[testArea] = it.toList() } } \ No newline at end of file diff --git a/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/SpecTestsLoader.kt b/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/SpecTestsLoader.kt index 4b507c092..e798113d8 100644 --- a/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/SpecTestsLoader.kt +++ b/web/src/main/kotlin/org/jetbrains/kotlin/spec/loader/SpecTestsLoader.kt @@ -2,6 +2,7 @@ package org.jetbrains.kotlin.spec.loader import js.externals.jquery.JQuery import js.externals.jquery.`$` +import org.jetbrains.kotlin.spec.entity.TestsLoadingInfo import org.jetbrains.kotlin.spec.entity.SpecSection import org.jetbrains.kotlin.spec.entity.test.parameters.testArea.TestArea import org.jetbrains.kotlin.spec.utils.format @@ -165,15 +166,24 @@ class SpecTestsLoader { private var originalSectionName: String? = null private var numberSectionsLoaded = 0 + fun onTestsLoadingLinkClick(link: JQuery) { + loader.loadSectionsMapFiles() + .then { sectionsMapsByTestArea -> + loadTests(link, sectionsMapsByTestArea) + } + } + + private fun loadTests(link: JQuery, sectionsMapsByTestArea: Map) { val section = link.parent("h2, h3, h4, h5") val paragraphsInfo = getParagraphsInfo(section) val nestedSections = getNestedSections(section) - val sectionName = section.attr("id") - val sectionsPath = mutableListOf(getParentSectionName(section, "h2")) + val sectionToLoadName = section.attr("id") + val sectionsPath: MutableList = mutableListOf() + val mainSectionsPath = getParentSectionName(section, "h2") if (originalSectionName == null) { - originalSectionName = sectionName + originalSectionName = sectionToLoadName numberSectionsLoaded = 1 } @@ -186,24 +196,28 @@ class SpecTestsLoader { sectionsPath.add(getParentSectionName(section, "h4")) } - loader.loadTestFiles(sectionName, sectionsPath, TestArea.values()) + loader.loadTestFiles( + sectionToLoadName = sectionToLoadName, + mainSectionPath = mainSectionsPath, + sectionsPath = sectionsPath, + sectionsMapsByTestArea = sectionsMapsByTestArea) .then { sectionTestSet -> if (paragraphsInfo != null) - parseTestFiles(sectionTestSet, sectionName, sectionsPath, paragraphsInfo) + parseTestFiles(sectionTestSet, sectionToLoadName, sectionsPath, paragraphsInfo) link.html(getButtonToLoadTests(link, true)) - if (originalSectionName == sectionName) { + if (originalSectionName == sectionToLoadName) { section.nextAll(".paragraph.with-tests").first().get()[0].scrollIntoView() originalSectionName = null - sectionPrevLoaded = sectionName + sectionPrevLoaded = sectionToLoadName } }.catch { numberSectionsLoaded-- - if (originalSectionName == sectionName) { + if (originalSectionName == sectionToLoadName) { originalSectionName = null - sectionPrevLoaded = sectionName + sectionPrevLoaded = sectionToLoadName } if (numberSectionsLoaded == 0) { window.alert(notLoadedTestsText.format(sectionPrevLoaded)) @@ -213,7 +227,7 @@ class SpecTestsLoader { nestedSections.forEach { sectionId -> numberSectionsLoaded++ - `$`("#${sectionId.replace(".", """\\.""")} .load-tests").click() + loadTests(`$`("#${sectionId.replace(".", """\\.""")} .load-tests").click(), sectionsMapsByTestArea) } } -} +} \ No newline at end of file