<commit_before>/*
 * Use of this source code is governed by the MIT license that can be
 * found in the LICENSE file.
 */

package org.rust.lang.core.stubs

import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileVisitor
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.StubBasedPsiElement
import com.intellij.psi.SyntaxTraverser
import com.intellij.psi.impl.DebugUtil
import com.intellij.psi.impl.source.tree.LazyParseableElement
import com.intellij.psi.impl.source.tree.TreeUtil
import com.intellij.util.LocalTimeCounter
import junit.framework.TestCase
import org.rust.RsTestBase
import org.rust.lang.RsFileType
import org.rust.lang.core.psi.*
import org.rust.lang.core.psi.ext.descendantsOfType
import org.rustPerformanceTests.fullyRefreshDirectoryInUnitTests

/**
 * Base class for tests to check that block elements are not parsed if they don't contain stub elements
 */
abstract class RsLazyBlockStubCreationTestBase : RsTestBase() {

    protected fun checkRustFiles(directory: VirtualFile, ignored: Collection<String>) {
        val files = collectRustFiles(directory, ignored)
            .mapNotNull { it.toInMemoryPsiFile() as? RsFile }

        var numBlocks = 0
        var numParsedBlocks = 0

        for (psi in files) {
            parseFile(psi)
            val blocks = SyntaxTraverser.psiTraverser(psi).expand { it !is RsBlock }.filterIsInstance<RsBlock>()

            blocks.forEach {
                check(it.parent !is RsFunction || !it.isParsed) {
                    "Expected NOT parsed block: `${it.text}`"
                }
            }

            val stubBuilder = RsFileStub.Type.builder
            val stub1 = stubBuilder.buildStubTree(psi)

            for (block in blocks) {
                if (block.parent !is RsFunction) continue
                numBlocks++
                val skipChildProcessing = stubBuilder.skipChildProcessingWhenBuildingStubs(block.node.treeParent, block.node)
                val blockIsParsed = block.isParsed

                check(skipChildProcessing == !blockIsParsed) {<commit_msg>T: print file url in RsLazyBlockStubCreationTestBase<commit_after>/*
 * Use of this source code is governed by the MIT license that can be
 * found in the LICENSE file.
 */

package org.rust.lang.core.stubs

import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileVisitor
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.StubBasedPsiElement
import com.intellij.psi.SyntaxTraverser
import com.intellij.psi.impl.DebugUtil
import com.intellij.psi.impl.source.tree.LazyParseableElement
import com.intellij.psi.impl.source.tree.TreeUtil
import com.intellij.util.LocalTimeCounter
import junit.framework.TestCase
import org.rust.RsTestBase
import org.rust.lang.RsFileType
import org.rust.lang.core.psi.*
import org.rust.lang.core.psi.ext.descendantsOfType
import org.rustPerformanceTests.fullyRefreshDirectoryInUnitTests

/**
 * Base class for tests to check that block elements are not parsed if they don't contain stub elements
 */
abstract class RsLazyBlockStubCreationTestBase : RsTestBase() {

    protected fun checkRustFiles(directory: VirtualFile, ignored: Collection<String>) {
        val files = collectRustFiles(directory, ignored)

        var numBlocks = 0
        var numParsedBlocks = 0

        for (file in files) {
            fun check(value: Boolean, lazyMessage: () -> Any) {
                kotlin.check(value) {
                    "file: $file\n${lazyMessage()}"
                }
            }

            val psi = file.toInMemoryPsiFile() as? RsFile ?: continue
            parseFile(psi)
            val blocks = SyntaxTraverser.psiTraverser(psi).expand { it !is RsBlock }.filterIsInstance<RsBlock>()

            blocks.forEach {
                check(it.parent !is RsFunction || !it.isParsed) {
                    "Expected NOT parsed block: `${it.text}`"
                }
            }

            val stubBuilder = RsFileStub.Type.builder
            val stub1 = stubBuilder.buildStubTree(psi)

            for (block in blocks) {
                if (block.parent !is RsFunction) continue
                numBlocks++
                val skipChildProcessing = stubBuilder.skipChildProcessingWhenBuildingStubs(block.node.treeParent, block.node)
                val blockIsParsed = block.isParsed

                check(skipChildProcessing == !blockIsParsed) {
{'fraction_non_alphanumeric': 0.08535254311285745, 'fraction_numerical': 0.0013097576948264572, 'mean_word_length': 2.8666666666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4760361', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1298, 'n_words': 305}