Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loader sectionsmap #62

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> {
Expand Down Expand Up @@ -54,28 +59,61 @@ interface GithubTestsLoader {


fun loadTestMapFileFromRawGithub(
mainSectionName: String,
path: String,
testType: TestOrigin,
testAreasToLoad: List<TestArea>
): Promise<Map<TestArea, SectionTestMap>> = Promise { resolve, _ ->
val resultMap = mutableMapOf<TestArea, SectionTestMap>()
sectionsMapByTestArea: Map<TestArea, TestsLoadingInfo.Sections>
): Promise<Map<TestArea, TestsLoadingInfo.Tests>> = Promise { resolve, _ ->
val resultMap = mutableMapOf<TestArea, TestsLoadingInfo.Tests>()
val loadableTestAreas: MutableSet<TestArea> = 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<TestArea, TestsLoadingInfo.Sections>.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<Map<TestArea, TestsLoadingInfo.Sections>> = Promise { resolve, _ ->
val resultMap = mutableMapOf<TestArea, TestsLoadingInfo.Sections>()
`$`.`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)
Expand All @@ -97,5 +135,5 @@ interface GithubTestsLoader {

}

fun loadTestFiles(sectionName: String, sectionsPath: List<String>, testAreasToLoad: Array<TestArea>): Promise<Promise<SpecSection>>
}
fun loadTestFiles(sectionName: String, mainSectionName: String, sectionsPath: List<String>, sectionsMapsByTestArea: Map<TestArea, TestsLoadingInfo.Sections>): Promise<Promise<SpecSection>>
}
Original file line number Diff line number Diff line change
@@ -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<TestArea>
) = loadTestMapFileFromRawGithub(
path = "$sectionsPath/$testsMapFilename",
testType = GithubTestsLoader.TestOrigin.SPEC_TEST,
testAreasToLoad = testAreasToLoad.asList()
)
private fun loadTestsMapFile(mainSectionName: String, sectionsPath: String, sectionsMapByTestArea: Map<TestArea, TestsLoadingInfo.Sections>
): Promise<Map<TestArea, TestsLoadingInfo.Tests>> {
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<Promise<SpecTest>> {
private fun getPromisesForTestFilesFromTestMap(testsMap: TestsLoadingInfo.Tests?, testArea: TestArea): Array<Promise<SpecTest>> {
val promises = mutableListOf<Promise<SpecTest>>()
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) {
Expand All @@ -43,22 +49,32 @@ class LoaderByTestsMapFile : GithubTestsLoader {
}


override fun loadTestFiles(sectionName: String, sectionsPath: List<String>, testAreasToLoad: Array<TestArea>
) = loadTestsMapFile(sectionsPath.joinToString("/") + "/" + sectionName, testAreasToLoad)
.then { sectionTestMaps ->
override fun loadTestFiles(sectionToLoadName: String,
mainSectionPath: String,
sectionsPath: List<String>,
sectionsMapsByTestArea: Map<TestArea, TestsLoadingInfo.Sections>
) = loadTestsMapFile(mainSectionName = mainSectionPath,
sectionsPath = when {
mainSectionPath == sectionToLoadName && sectionsPath.isEmpty() -> "";
sectionsPath.isNotEmpty() -> sectionsPath.joinToString("/") + "/" + sectionToLoadName;
else -> sectionToLoadName
},
sectionsMapByTestArea = sectionsMapsByTestArea)
.then { testsMapsByTestArea ->
val resultMap = mutableMapOf<TestArea, List<SpecTest>>()
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<TestArea, SectionTestMap>,
testMaps: Map<TestArea, TestsLoadingInfo.Tests>,
mapOfTests: MutableMap<TestArea, List<SpecTest>>
) = Promise.all(
getPromisesForTestFilesFromTestMap(sectionTestMaps[testArea], testArea))
getPromisesForTestFilesFromTestMap(testMaps[testArea], testArea))
.then { mapOfTests[testArea] = it.toList() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TestArea, TestsLoadingInfo.Sections>) {
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<String> = mutableListOf()
val mainSectionsPath = getParentSectionName(section, "h2")

if (originalSectionName == null) {
originalSectionName = sectionName
originalSectionName = sectionToLoadName
numberSectionsLoaded = 1
}

Expand All @@ -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))
Expand All @@ -213,7 +227,7 @@ class SpecTestsLoader {

nestedSections.forEach { sectionId ->
numberSectionsLoaded++
`$`("#${sectionId.replace(".", """\\.""")} .load-tests").click()
loadTests(`$`("#${sectionId.replace(".", """\\.""")} .load-tests").click(), sectionsMapsByTestArea)
}
}
}
}