|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2024 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.creator |
| 22 | + |
| 23 | +import com.demonwav.mcdev.creator.custom.BuiltinValidations |
| 24 | +import com.demonwav.mcdev.creator.custom.TemplateDescriptor |
| 25 | +import com.demonwav.mcdev.creator.custom.model.BuildSystemCoordinates |
| 26 | +import com.demonwav.mcdev.creator.custom.model.ClassFqn |
| 27 | +import com.intellij.openapi.ui.validation.invoke |
| 28 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 29 | +import org.junit.jupiter.api.Assertions.assertNotNull |
| 30 | +import org.junit.jupiter.api.Assertions.assertNull |
| 31 | +import org.junit.jupiter.api.DisplayName |
| 32 | +import org.junit.jupiter.api.Test |
| 33 | + |
| 34 | +@DisplayName("Class FQN Creator Property Tests") |
| 35 | +class ClassFqnCreatorPropertyTest : CreatorTemplateProcessorTestBase() { |
| 36 | + |
| 37 | + private fun checkValidation(input: String, expectedMessage: String?) { |
| 38 | + val validation = BuiltinValidations.validClassFqn { input } |
| 39 | + val validationInfo = validation.validate() |
| 40 | + if (expectedMessage == null) { |
| 41 | + assertNull(validationInfo?.message) { "Expected input to be valid: '$input'" } |
| 42 | + } else { |
| 43 | + assertNotNull(validationInfo, "Expected input to be invalid: '$input'") |
| 44 | + assertEquals(expectedMessage, validationInfo!!.message) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @DisplayName("Validation") |
| 50 | + fun validation() { |
| 51 | + checkValidation("test.TestName", null) |
| 52 | + val invalidFqns = listOf( |
| 53 | + "test.0InvalidName", |
| 54 | + "test.Invalid-Name", |
| 55 | + "test.package.InvalidPackage", |
| 56 | + "test..InvalidPackage", |
| 57 | + "test." |
| 58 | + ) |
| 59 | + for (fqn in invalidFqns) { |
| 60 | + checkValidation(fqn, "Must be a valid class fully qualified name") |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("Class Name Suggestion") |
| 66 | + fun classNameSuggestion() { |
| 67 | + makeTemplate( |
| 68 | + """ |
| 69 | + { |
| 70 | + "version": ${TemplateDescriptor.FORMAT_VERSION}, |
| 71 | + "properties": [ |
| 72 | + { |
| 73 | + "name": "BUILD_COORDS", |
| 74 | + "type": "build_system_coordinates" |
| 75 | + }, |
| 76 | + { |
| 77 | + "name": "NAME", |
| 78 | + "type": "string" |
| 79 | + }, |
| 80 | + { |
| 81 | + "name": "FQN", |
| 82 | + "type": "class_fqn", |
| 83 | + "derives": { |
| 84 | + "method": "suggestClassName", |
| 85 | + "parents": ["BUILD_COORDS", "NAME"] |
| 86 | + } |
| 87 | + } |
| 88 | + ] |
| 89 | + } |
| 90 | + """.trimIndent() |
| 91 | + ) |
| 92 | + |
| 93 | + val buildCoordsProperty = processor.context.property<BuildSystemCoordinates>("BUILD_COORDS") |
| 94 | + val nameProperty = processor.context.property<String>("NAME") |
| 95 | + val fqnProperty = processor.context.property<ClassFqn>("FQN") |
| 96 | + |
| 97 | + buildCoordsProperty.graphProperty.set(BuildSystemCoordinates("com.example.project", "example-project", "1.0")) |
| 98 | + nameProperty.graphProperty.set("My Project") |
| 99 | + assertEquals(ClassFqn("com.example.project.myProject.MyProject"), fqnProperty.get()) |
| 100 | + } |
| 101 | +} |
0 commit comments