<commit_before>package org.rust.lang.core.completion

import com.intellij.psi.PsiElement
import org.intellij.lang.annotations.Language
import org.rust.lang.RustTestCaseBase

abstract class RustCompletionTestBase : RustTestCaseBase() {

    protected fun checkSingleCompletionByFile() = checkByFile {
        executeSoloCompletion()
    }

    protected fun checkSingleCompletion(target: String, @Language("Rust") code: String) {
        InlineFile(code).withCaret()
        val variants = myFixture.completeBasic()
        check(variants == null) {
            "Expected a single completion, but got ${variants.size}\n" + "${variants.toList()}"
        }
        val shift = if (target.endsWith("()")) 3 else 1
        val element = myFixture.file.findElementAt(myFixture.caretOffset - shift)!!
        check(element.correspondsToText(target)) {
            "Wrong completion, expected `$target`, but got `${element.text}`"
        }
    }

    protected fun checkNoCompletion(@Language("Rust") code: String) {
        InlineFile(code).withCaret()
        val variants = myFixture.completeBasic()
        checkNotNull(variants) {
            val element = myFixture.file.findElementAt(myFixture.caretOffset - 1)
            "Expected zero completions, but one completion was auto inserted: `${element?.text}`."
        }
        check(variants.isEmpty()) {
            "Expected zero completions, got ${variants.size}."
        }
    }

    protected fun executeSoloCompletion() {
        val variants = myFixture.completeBasic()
        if (variants != null) {
            error("Expected a single completion, but got ${variants.size}\n" +
                "${variants.toList()}")
        }
    }

    private fun PsiElement.correspondsToText(target: String): Boolean {
        return when {
            text == target -> true
            text.length > target.length -> false
            parent != null -> parent.correspondsToText(target)
            else -> false
        }
    }
}<commit_msg>(T): More robust tests for autocompletion<commit_after>package org.rust.lang.core.completion

import com.intellij.psi.PsiElement
import org.intellij.lang.annotations.Language
import org.rust.lang.RustTestCaseBase

abstract class RustCompletionTestBase : RustTestCaseBase() {

    protected fun checkSingleCompletionByFile() = checkByFile {
        executeSoloCompletion()
    }

    protected fun checkSingleCompletion(target: String, @Language("Rust") code: String) {
        InlineFile(code).withCaret()
        val variants = myFixture.completeBasic()
        check(variants == null) {
            "Expected a single completion, but got ${variants.size}\n" + "${variants.toList()}"
        }
        val fnName = target.substringBeforeLast("()").substringAfterLast("::").substringAfterLast(".")
        val shift = if (target.endsWith("()")) 3 else 1
        val element = myFixture.file.findElementAt(myFixture.caretOffset - shift)!!
        check(element.text == fnName && element.correspondsToText(target)) {
            "Wrong completion, expected `$target`, but got `${element.text}`"
        }
    }

    protected fun checkNoCompletion(@Language("Rust") code: String) {
        InlineFile(code).withCaret()
        val variants = myFixture.completeBasic()
        checkNotNull(variants) {
            val element = myFixture.file.findElementAt(myFixture.caretOffset - 1)
            "Expected zero completions, but one completion was auto inserted: `${element?.text}`."
        }
        check(variants.isEmpty()) {
            "Expected zero completions, got ${variants.size}."
        }
    }

    protected fun executeSoloCompletion() {
        val variants = myFixture.completeBasic()
        if (variants != null) {
            error("Expected a single completion, but got ${variants.size}\n" +
                "${variants.toList()}")
        }
    }

    private fun PsiElement.correspondsToText(target: String): Boolean = when {
            text == target -> true
            text.length > target.length -> false
            parent != null -> parent.correspondsToText(target)
            else -> false
        }
}
{'fraction_non_alphanumeric': 0.11784675072744907, 'fraction_numerical': 0.001454898157129001, 'mean_word_length': 2.653675819309123, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 13, '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': '6649053', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1122, 'n_words': 253}