Skip to content

Commit 237bb93

Browse files
Undinrrevenantt
authored andcommitted
T: test diff values for failed comparison tests
1 parent 2822ee4 commit 237bb93

File tree

3 files changed

+135
-48
lines changed

3 files changed

+135
-48
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Use of this source code is governed by the MIT license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
package org.rustSlowTests.cargo.runconfig.test
7+
8+
import com.intellij.execution.testframework.stacktrace.DiffHyperlink
9+
import org.intellij.lang.annotations.Language
10+
11+
class CargoTestDiffTest : CargoTestRunnerTestBase() {
12+
13+
fun `test int diff`() = doAvailableTest("""
14+
assert_eq!(1, 2);
15+
""", "1", "2")
16+
17+
fun `test char diff`() = doAvailableTest("""
18+
assert_eq!('a', 'c');
19+
""", "a", "c")
20+
21+
fun `test string diff`() = doAvailableTest("""
22+
assert_eq!("aaa", "bbb");
23+
""", "aaa", "bbb")
24+
25+
fun `test multiline string diff`() = doAvailableTest("""
26+
assert_eq!("a\naa", "bbb");
27+
""", "a\naa", "bbb")
28+
29+
fun `test assert with additional message`() = doAvailableTest("""
30+
assert_eq!(1, 2, "`1` != `2`");
31+
""", "1", "2")
32+
33+
fun `test no diff`() = doUnavailableTest("""
34+
assert!("aaa" != "aaa");
35+
""")
36+
37+
fun `test no diff for assert_ne`() = doUnavailableTest("""
38+
assert_ne!(123, 123);
39+
""")
40+
41+
private fun doAvailableTest(@Language("Rust") testFnText: String, expected: String, actual: String) {
42+
val diff = getDiff(testFnText) ?: error("Diff should be not null")
43+
assertEquals("Some text", expected, diff.left)
44+
assertEquals(actual, diff.right)
45+
}
46+
47+
private fun doUnavailableTest(@Language("Rust") testFnText: String) {
48+
val diff = getDiff(testFnText)
49+
assertNull(diff)
50+
}
51+
52+
private fun getDiff(testFnText: String): DiffHyperlink? {
53+
val testProject = buildProject {
54+
toml("Cargo.toml", """
55+
[package]
56+
name = "sandbox"
57+
version = "0.1.0"
58+
authors = []
59+
""")
60+
61+
dir("src") {
62+
rust("main.rs", """
63+
#[test]
64+
fn test() {/*caret*/
65+
$testFnText
66+
}
67+
""")
68+
}
69+
}
70+
myFixture.configureFromTempProjectFile(testProject.fileWithCaret)
71+
val configuration = createTestRunConfigurationFromContext()
72+
val testNode = executeAndGetTestRoot(configuration).findTestByName("sandbox::test")
73+
assertFalse("Test should fail", testNode.isPassed)
74+
return testNode.diffViewerProvider
75+
}
76+
}

src/test/kotlin/org/rustSlowTests/cargo/runconfig/test/TestRunnerTest.kt renamed to src/test/kotlin/org/rustSlowTests/cargo/runconfig/test/CargoTestRunnerTest.kt

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@
55

66
package org.rustSlowTests.cargo.runconfig.test
77

8-
import com.intellij.execution.configurations.RunConfiguration
9-
import com.intellij.execution.testframework.TestProxyRoot
108
import com.intellij.execution.testframework.sm.runner.SMTestProxy
11-
import com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView
12-
import com.intellij.openapi.util.Disposer
13-
import com.intellij.util.ui.UIUtil
14-
import org.rust.stdext.removeLast
15-
import org.rustSlowTests.cargo.runconfig.RunConfigurationTestBase
169

17-
class TestRunnerTest : RunConfigurationTestBase() {
10+
class CargoTestRunnerTest : CargoTestRunnerTestBase() {
1811

1912
fun `test test statuses`() {
2013
val testProject = buildProject {
@@ -265,13 +258,13 @@ class TestRunnerTest : RunConfigurationTestBase() {
265258
val configuration = createTestRunConfigurationFromContext()
266259
val root = executeAndGetTestRoot(configuration)
267260

268-
val test = findTestByName("tests::test", root)
261+
val test = root.findTestByName("tests::test")
269262
assertEquals("cargo:test://tests::test", test.locationUrl)
270263

271-
val mod = findTestByName("tests::test_mod", root)
264+
val mod = root.findTestByName("tests::test_mod")
272265
assertEquals("cargo:suite://tests::test_mod", mod.locationUrl)
273266

274-
val testInner = findTestByName("tests::test_mod::test", root)
267+
val testInner = root.findTestByName("tests::test_mod::test")
275268
assertEquals("cargo:test://tests::test_mod::test", testInner.locationUrl)
276269
}
277270

@@ -307,31 +300,18 @@ class TestRunnerTest : RunConfigurationTestBase() {
307300
val configuration = createTestRunConfigurationFromContext()
308301
val root = executeAndGetTestRoot(configuration)
309302

310-
val test1 = findTestByName("tests::test1", root)
303+
val test1 = root.findTestByName("tests::test1")
311304
check(test1.duration!! > 1000) { "The `test1` duration too short" }
312305

313-
val test2 = findTestByName("tests::test2", root)
306+
val test2 = root.findTestByName("tests::test2")
314307
check(test2.duration!! < 100) { "The `test2` duration too long" }
315308

316-
val mod = findTestByName("tests", root)
309+
val mod = root.findTestByName("tests")
317310
check(mod.duration!! == test1.duration!! + test2.duration!!) {
318311
"The `tests` mod duration is not the sum of durations of containing tests"
319312
}
320313
}
321314

322-
private fun executeAndGetTestRoot(configuration: RunConfiguration): SMTestProxy.SMRootTestProxy {
323-
val result = execute(configuration)
324-
val executionConsole = result.executionConsole as SMTRunnerConsoleView
325-
val testsRootNode = executionConsole.resultsViewer.testsRootNode
326-
with(result.processHandler) {
327-
startNotify()
328-
waitFor()
329-
}
330-
UIUtil.dispatchAllInvocationEvents()
331-
Disposer.dispose(executionConsole)
332-
return testsRootNode
333-
}
334-
335315
private fun checkTestTree(expectedFormattedTestTree: String) {
336316
val configuration = createTestRunConfigurationFromContext()
337317
val root = executeAndGetTestRoot(configuration)
@@ -363,26 +343,5 @@ class TestRunnerTest : RunConfigurationTestBase() {
363343
formatLevel(child, level + 1)
364344
}
365345
}
366-
367-
private fun findTestByName(testFullName: String, root: SMTestProxy.SMRootTestProxy): SMTestProxy {
368-
val fullNameBuffer = mutableListOf<String>()
369-
370-
fun find(test: SMTestProxy): SMTestProxy? {
371-
if (test !is TestProxyRoot) {
372-
fullNameBuffer.add(test.name)
373-
}
374-
if (testFullName == fullNameBuffer.joinToString("::")) return test
375-
for (child in test.children) {
376-
val result = find(child)
377-
if (result != null) return result
378-
}
379-
if (test !is TestProxyRoot) {
380-
fullNameBuffer.removeLast()
381-
}
382-
return null
383-
}
384-
385-
return checkNotNull(find(root)) { "Could not find the test" }
386-
}
387346
}
388347
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Use of this source code is governed by the MIT license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
package org.rustSlowTests.cargo.runconfig.test
7+
8+
import com.intellij.execution.configurations.RunConfiguration
9+
import com.intellij.execution.testframework.TestProxyRoot
10+
import com.intellij.execution.testframework.sm.runner.SMTestProxy
11+
import com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView
12+
import com.intellij.openapi.util.Disposer
13+
import com.intellij.util.ui.UIUtil
14+
import org.rust.stdext.removeLast
15+
import org.rustSlowTests.cargo.runconfig.RunConfigurationTestBase
16+
17+
abstract class CargoTestRunnerTestBase : RunConfigurationTestBase() {
18+
19+
protected fun executeAndGetTestRoot(configuration: RunConfiguration): SMTestProxy.SMRootTestProxy {
20+
val result = execute(configuration)
21+
val executionConsole = result.executionConsole as SMTRunnerConsoleView
22+
val testsRootNode = executionConsole.resultsViewer.testsRootNode
23+
with(result.processHandler) {
24+
startNotify()
25+
waitFor()
26+
}
27+
UIUtil.dispatchAllInvocationEvents()
28+
Disposer.dispose(executionConsole)
29+
return testsRootNode
30+
}
31+
32+
protected fun SMTestProxy.SMRootTestProxy.findTestByName(testFullName: String): SMTestProxy {
33+
val fullNameBuffer = mutableListOf<String>()
34+
35+
fun find(test: SMTestProxy): SMTestProxy? {
36+
if (test !is TestProxyRoot) {
37+
fullNameBuffer.add(test.name)
38+
}
39+
if (testFullName == fullNameBuffer.joinToString("::")) return test
40+
for (child in test.children) {
41+
val result = find(child)
42+
if (result != null) return result
43+
}
44+
if (test !is TestProxyRoot) {
45+
fullNameBuffer.removeLast()
46+
}
47+
return null
48+
}
49+
50+
return checkNotNull(find(this)) { "Could not find the test" }
51+
}
52+
}

0 commit comments

Comments
 (0)