<NME> ElmRenameTest.kt
<BEF> /*
The MIT License (MIT)

Derived from intellij-rust
Copyright (c) 2015 Aleksey Kladov, Evgeny Kurbatsky, Alexey Kudinkin and contributors
Copyright (c) 2016 JetBrains

Permission is hereby granted, free of charge, to any person obtaining a copy

class ElmRenameTest: ElmTestBase() {

    fun testValueDeclRename() {
        doTest("quux",
"""
foo : Int
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bar = quux + 2
""")}

    fun testFunctionParameterRename() {
        doTest("z",
"""
f x{-caret-} y = x + y

f z y = z + y
""")}

    fun testUnionTypeRename() {
        doTest("Quux",
"""
type Foo{-caret-} = A | B


class ElmRenameTest : ElmTestBase() {


f : Quux -> String
""")}

    fun testTypeAliasRename() {
        doTest("Quux",
"""
type alias Foo{-caret-} = Int
bar = quux + 2
""")

    fun `test value decl invalid rename`() = doInvalidNameTest("Bar", """
foo{-caret-} = 42
""")



    // TODO [kl] improve this test by also verifying that the FILE is renamed (currently not implemented in the plugin)
    fun testModuleRenameFromDataUserToQuux() {
        checkByDirectory(
"""
--@ Data/User.elm
f z y = z + y
""")


    fun `test let destructuring rename`() = doTest("z", """
f p =
    let (x{-caret-}, y) = p
    in x
""", """
f p =
    let (z, y) = p
    in z
""")


    fun `test case destructuring rename`() = doTest("z", """
f p =
    case p of
        (x{-caret-}, y) -> x
""", """
f p =
    case p of
        (z, y) -> z
""")


        }
    }

    fun testImportAliasRename() {
        checkByDirectory(
"""
--@ Data/User.elm
f : Quux -> String
""")


    fun `test type alias rename`() = doTest("Quux", """
type alias Foo{-caret-} = Int
type Bar = C Foo
f : Foo -> String
""", """
type alias Quux = Int
type Bar = C Quux
f : Quux -> String
""")


    fun `test field access rename`() = doTest("quux", """
foo : { b : String }
foo a{-caret-} = a.b
""", """
foo : { b : String }
foo quux = quux.b
""")


    // disabled until https://github.com/klazuka/intellij-elm/issues/240 is fixed
/*
    fun `test file rename from Data_User to Data_Quux`() = checkByDirectory("""
--@ Data/User.elm
module Data.User exposing (..)
type alias User = { x : String }

--@ main.elm
import Data.User
g = Data.User.User "joe"
""", """
--@ Data/Quux.elm
module Data.Quux exposing (..)
type alias User = { x : String }

--@ main.elm
import Data.Quux
g = Data.Quux.User "joe"
""") {
        val file = myFixture.configureFromTempProjectFile("Data/User.elm")
        myFixture.renameElement(file, "Quux")
    }
*/


    fun `test module decl rename from Data_User to Data_Quux`() = checkByDirectory("""
--@ Data/User.elm
module Data.User exposing (..)
type alias User = { x : String }

--@ main.elm
import Data.User
g = Data.User.User "joe"
""", """
--@ Data/Quux.elm
module Data.Quux exposing (..)
type alias User = { x : String }

--@ main.elm
import Data.Quux
g = Data.Quux.User "joe"
""") {
        val mod = myFixture.configureFromTempProjectFile("Data/User.elm")
                .descendantsOfType<ElmModuleDeclaration>().single()
        check(mod.name == "Data.User")
        myFixture.renameElement(mod, "Quux")
    }


    fun `test import alias rename`() = checkByDirectory("""
--@ Data/User.elm
module Data.User exposing (..)
type alias User = { x : String }
name = identity

--@ main.elm
import Data.User as DU
f : DU.User -> String
f user = DU.name user
g = f (DU.User "joe")
""", """
--@ Data/User.elm
module Data.User exposing (..)
type alias User = { x : String }
name = identity

--@ main.elm
import Data.User as U
f : U.User -> String
f user = U.name user
g = f (U.User "joe")
""") {
        val aliasClause = myFixture.configureFromTempProjectFile("main.elm")
                .descendantsOfType<ElmImportClause>().single()
                .asClause!!
        check(aliasClause.name == "DU")
        myFixture.renameElement(aliasClause, "U")
    }

    fun doTest(newName: String, @Language("Elm") before: String, @Language("Elm") after: String) {
        InlineFile(before).withCaret()
        myFixture.renameElementAtCaret(newName)
        myFixture.checkResult(after)
    }

    fun doInvalidNameTest(newName: String, @Language("Elm") before: String) {
        InlineFile(before).withCaret()
        try {
            myFixture.renameElementAtCaret(newName)
        } catch (e: RuntimeException) {
            UsefulTestCase.assertInstanceOf(e.cause, IncorrectOperationException::class.java)
            myFixture.checkResult(before.replace("{-caret-}", ""))
            return
        }
        throw AssertionError("exception not thrown")
    }
}

<MSG> Introduce an ElmReference interface and cut-down resolve boilerplate

<DFF> @@ -9,7 +9,7 @@ import org.intellij.lang.annotations.Language
 
 class ElmRenameTest: ElmTestBase() {
 
-    fun testValueDeclRename() {
+    fun `test value decl rename`() {
         doTest("quux",
 """
 foo : Int
@@ -21,7 +21,7 @@ quux = 42
 bar = quux + 2
 """)}
 
-    fun testFunctionParameterRename() {
+    fun `test function parameter rename`() {
         doTest("z",
 """
 f x{-caret-} y = x + y
@@ -29,7 +29,7 @@ f x{-caret-} y = x + y
 f z y = z + y
 """)}
 
-    fun testUnionTypeRename() {
+    fun `test union type rename`() {
         doTest("Quux",
 """
 type Foo{-caret-} = A | B
@@ -41,7 +41,7 @@ type Bar = C Quux
 f : Quux -> String
 """)}
 
-    fun testTypeAliasRename() {
+    fun `test type alias rename`() {
         doTest("Quux",
 """
 type alias Foo{-caret-} = Int
@@ -55,7 +55,7 @@ f : Quux -> String
 
 
     // TODO [kl] improve this test by also verifying that the FILE is renamed (currently not implemented in the plugin)
-    fun testModuleRenameFromDataUserToQuux() {
+    fun `test module rename from Data_User to Quux`() {
         checkByDirectory(
 """
 --@ Data/User.elm
@@ -88,7 +88,7 @@ g = f (Quux.User "joe")
         }
     }
 
-    fun testImportAliasRename() {
+    fun `test import alias rename`() {
         checkByDirectory(
 """
 --@ Data/User.elm

Introduce an ElmReference interface and cut-down resolve boilerplate