|
| 1 | +/* |
| 2 | + * Copyright Doma Tools Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.domaframework.doma.intellij.formatter |
| 17 | + |
| 18 | +import com.intellij.openapi.command.WriteCommandAction |
| 19 | +import com.intellij.psi.codeStyle.CodeStyleManager |
| 20 | +import com.intellij.testFramework.fixtures.BasePlatformTestCase |
| 21 | +import com.intellij.util.ThrowableRunnable |
| 22 | +import org.domaframework.doma.intellij.state.DomaToolsFunctionEnableSettings |
| 23 | + |
| 24 | +class SqlFormatterTest : BasePlatformTestCase() { |
| 25 | + override fun getBasePath(): String? = "src/test/testData/sql/formatter" |
| 26 | + |
| 27 | + override fun getTestDataPath(): String? = "src/test/testData/sql/formatter" |
| 28 | + |
| 29 | + override fun setUp() { |
| 30 | + super.setUp() |
| 31 | + val settings = DomaToolsFunctionEnableSettings.getInstance() |
| 32 | + settings.state.isEnableSqlFormat = true |
| 33 | + } |
| 34 | + |
| 35 | + override fun tearDown() { |
| 36 | + try { |
| 37 | + val settings = DomaToolsFunctionEnableSettings.getInstance() |
| 38 | + settings.state.isEnableSqlFormat = false |
| 39 | + } finally { |
| 40 | + super.tearDown() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + fun testSelectFormatter() { |
| 45 | + formatSqlFime("Select.sql", "FormattedSelect.sql") |
| 46 | + } |
| 47 | + |
| 48 | + fun testCreateTableFormatter() { |
| 49 | + formatSqlFime("CreateTable.sql", "FormattedCreateTable.sql") |
| 50 | + } |
| 51 | + |
| 52 | + fun testCreateViewFormatter() { |
| 53 | + formatSqlFime("CreateView.sql", "FormattedCreateView.sql") |
| 54 | + } |
| 55 | + |
| 56 | + fun testInsertFormatter() { |
| 57 | + formatSqlFime("Insert.sql", "FormattedInsert.sql") |
| 58 | + } |
| 59 | + |
| 60 | + fun testDeleteFormatter() { |
| 61 | + formatSqlFime("Delete.sql", "FormattedDelete.sql") |
| 62 | + } |
| 63 | + |
| 64 | + private fun formatSqlFime( |
| 65 | + beforeFile: String, |
| 66 | + afterFile: String, |
| 67 | + ) { |
| 68 | + myFixture.configureByFiles(beforeFile) |
| 69 | + val currentFile = myFixture.file |
| 70 | + WriteCommandAction |
| 71 | + .writeCommandAction(project) |
| 72 | + .run<RuntimeException?>( |
| 73 | + ThrowableRunnable { |
| 74 | + CodeStyleManager.getInstance(project).reformatText( |
| 75 | + currentFile, |
| 76 | + arrayListOf(currentFile.textRange), |
| 77 | + ) |
| 78 | + }, |
| 79 | + ) |
| 80 | + myFixture.checkResultByFile(afterFile) |
| 81 | + } |
| 82 | +} |
0 commit comments