|
| 1 | +/* |
| 2 | + * Copyright 2023 DiffPlug |
| 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 | + * http://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 com.diffplug.spotless.glue.ktlint.compat; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
| 31 | + |
| 32 | +public class KtLintCompat0Dot48Dot0AdapterTest { |
| 33 | + @Test |
| 34 | + public void testDefaults(@TempDir Path path) throws IOException { |
| 35 | + KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter(); |
| 36 | + String text = loadAndWriteText(path, "empty_class_body.kt"); |
| 37 | + final Path filePath = Paths.get(path.toString(), "empty_class_body.kt"); |
| 38 | + |
| 39 | + Map<String, String> userData = new HashMap<>(); |
| 40 | + |
| 41 | + Map<String, Object> editorConfigOverrideMap = new HashMap<>(); |
| 42 | + |
| 43 | + String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, false, null, userData, editorConfigOverrideMap); |
| 44 | + assertEquals("class empty_class_body\n", formatted); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { |
| 49 | + KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter(); |
| 50 | + String text = loadAndWriteText(path, "fails_no_semicolons.kt"); |
| 51 | + final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt"); |
| 52 | + |
| 53 | + Map<String, String> userData = new HashMap<>(); |
| 54 | + |
| 55 | + Map<String, Object> editorConfigOverrideMap = new HashMap<>(); |
| 56 | + editorConfigOverrideMap.put("indent_style", "tab"); |
| 57 | + editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled"); |
| 58 | + // ktlint_filename is an invalid rule in ktlint 0.48.0 |
| 59 | + editorConfigOverrideMap.put("ktlint_filename", "disabled"); |
| 60 | + |
| 61 | + String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, false, null, userData, editorConfigOverrideMap); |
| 62 | + assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted); |
| 63 | + } |
| 64 | + |
| 65 | + private static String loadAndWriteText(Path path, String name) throws IOException { |
| 66 | + try (InputStream is = KtLintCompat0Dot48Dot0AdapterTest.class.getResourceAsStream("/" + name)) { |
| 67 | + Files.copy(is, path.resolve(name)); |
| 68 | + } |
| 69 | + return new String(Files.readAllBytes(path.resolve(name)), StandardCharsets.UTF_8); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments