|
1 | 1 | package org.elm.ide.actions |
2 | 2 |
|
| 3 | +import com.intellij.notification.Notification |
| 4 | +import com.intellij.notification.Notifications |
3 | 5 | import com.intellij.openapi.actionSystem.CommonDataKeys |
4 | 6 | import com.intellij.openapi.command.undo.UndoManager |
5 | 7 | import com.intellij.openapi.fileEditor.FileDocumentManager |
6 | 8 | import com.intellij.openapi.fileEditor.FileEditorManager |
| 9 | +import com.intellij.openapi.util.Ref |
7 | 10 | import com.intellij.openapi.vfs.VirtualFile |
8 | 11 | import com.intellij.testFramework.MapDataContext |
9 | 12 | import com.intellij.testFramework.TestActionEvent |
@@ -55,6 +58,36 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() { |
55 | 58 | TestCase.assertEquals(expected, document.text) |
56 | 59 | } |
57 | 60 |
|
| 61 | + @Test |
| 62 | + fun `test elm-format action on a file with syntax errors`() { |
| 63 | + val originalCode = """ |
| 64 | + module Main exposing (f) |
| 65 | +
|
| 66 | +
|
| 67 | + f x = |
| 68 | + """.trimIndent() |
| 69 | + |
| 70 | + |
| 71 | + buildProject { |
| 72 | + project("elm.json", manifestElm19) |
| 73 | + dir("src") { |
| 74 | + elm("Main.elm", originalCode) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + val file = myFixture.configureFromTempProjectFile("src/Main.elm").virtualFile |
| 79 | + val document = FileDocumentManager.getInstance().getDocument(file)!! |
| 80 | + |
| 81 | + val ref = connectToBusAndGetNotificationRef() |
| 82 | + reformat(file) |
| 83 | + |
| 84 | + TestCase.assertEquals(originalCode, document.text) |
| 85 | + |
| 86 | + TestCase.assertEquals("elm-format encountered syntax errors that it could not fix", ref.get().content) |
| 87 | + TestCase.assertEquals(1, ref.get().actions.size) |
| 88 | + TestCase.assertEquals("Show Errors", ref.get().actions.first().templatePresentation.text) |
| 89 | + } |
| 90 | + |
58 | 91 | @Test |
59 | 92 | fun `test elm-format action shouldn't be active on non-elm files`() { |
60 | 93 | buildProject { |
@@ -116,6 +149,16 @@ class ElmExternalFormatActionTest : ElmWorkspaceTestBase() { |
116 | 149 | return Pair(action, event) |
117 | 150 | } |
118 | 151 |
|
| 152 | + private fun connectToBusAndGetNotificationRef(): Ref<Notification> { |
| 153 | + val notificationRef = Ref<Notification>() |
| 154 | + project.messageBus.connect(testRootDisposable).subscribe(Notifications.TOPIC, |
| 155 | + object : Notifications { |
| 156 | + override fun notify(notification: Notification) = |
| 157 | + notificationRef.set(notification) |
| 158 | + }) |
| 159 | + return notificationRef |
| 160 | + } |
| 161 | + |
119 | 162 | } |
120 | 163 |
|
121 | 164 |
|
|
0 commit comments