IDEA-119192 LiveTemplate function snakeCase() should convert the hyphens to underscores

This commit is contained in:
peter
2014-01-13 19:58:46 +01:00
parent db8902f75d
commit a0be495846
4 changed files with 98 additions and 27 deletions
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.template
import com.intellij.codeInsight.template.macro.CapitalizeAndUnderscoreMacro
import com.intellij.codeInsight.template.macro.SnakeCaseMacro
import junit.framework.TestCase
/**
* @author peter
*/
class CapitalizeAndUnderscoreTest extends TestCase {
public void "test capitalize and underscore"() {
assert "FOO_BAR" == cau("fooBar")
assert "FOO_BAR" == cau("fooBar")
assert "FOO_BAR" == cau("foo-Bar")
assert "FOO_BAR" == cau("foo-bar")
}
public void "test snake case"() {
assert "foo" == snakeCase("foo")
assert "foo_bar" == snakeCase("foo-bar")
assert "foo_bar" == snakeCase("fooBar")
assert "foo_bar" == snakeCase("FOO_BAR")
assert "_foo_bar_" == snakeCase("-FOO-BAR-")
}
private static def snakeCase(String s) {
return new SnakeCaseMacro().convertString(s)
}
private static def cau(String s) {
return new CapitalizeAndUnderscoreMacro().convertString(s)
}
}
@@ -23,6 +23,7 @@ import com.intellij.codeInsight.template.impl.*
import com.intellij.codeInsight.template.macro.ClassNameCompleteMacro
import com.intellij.codeInsight.template.macro.CompleteMacro
import com.intellij.codeInsight.template.macro.MethodReturnTypeMacro
import com.intellij.codeInsight.template.macro.SnakeCaseMacro
import com.intellij.openapi.Disposable
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
@@ -704,4 +705,21 @@ class Foo {
}
public void "test snakeCase should convert hyphens to underscores"() {
final TemplateManager manager = TemplateManager.getInstance(getProject());
final Template template = manager.createTemplate("result", "user", '$A$ $B$ c');
template.addVariable('A', new EmptyNode(), true)
def macroCallNode = new MacroCallNode(new SnakeCaseMacro())
macroCallNode.addParameter(new VariableNode('A', null))
template.addVariable('B', macroCallNode, false)
myFixture.configureByText "a.txt", "<caret>"
manager.startTemplate(editor, template);
myFixture.type('-foo-bar_goo-')
state.nextTab()
assert !state
myFixture.checkResult('-foo-bar_goo- _foo_bar_goo_ c<caret>')
}
}