|
| 1 | +package io.github.composegears.valkyrie.cli.command |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.isEqualTo |
| 5 | +import com.github.ajalt.clikt.testing.test |
| 6 | +import io.github.composegears.valkyrie.cli.ext.outputError |
| 7 | +import io.github.composegears.valkyrie.cli.ext.outputInfo |
| 8 | +import io.github.composegears.valkyrie.extensions.ResourceUtils.getResourcePath |
| 9 | +import io.mockk.Runs |
| 10 | +import io.mockk.every |
| 11 | +import io.mockk.just |
| 12 | +import io.mockk.mockkStatic |
| 13 | +import io.mockk.verify |
| 14 | +import kotlin.io.path.absolutePathString |
| 15 | +import kotlin.io.path.createTempDirectory |
| 16 | +import kotlin.io.path.createTempFile |
| 17 | +import kotlin.io.path.listDirectoryEntries |
| 18 | +import kotlin.test.Test |
| 19 | +import kotlin.test.assertFailsWith |
| 20 | + |
| 21 | +class SvgXmlToImageVectorCommandTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + fun `outputFormatOption should throw error for invalid value`() { |
| 25 | + mockkStatic(::outputError) |
| 26 | + every { outputError(any()) } answers { error("") } |
| 27 | + |
| 28 | + SvgXmlToImageVectorCommand().test( |
| 29 | + "--input-path", |
| 30 | + "/some/input", |
| 31 | + "--output-path", |
| 32 | + "/some/output", |
| 33 | + "--package-name", |
| 34 | + "com.example", |
| 35 | + "--output-format", |
| 36 | + "invalid-value", |
| 37 | + ) |
| 38 | + |
| 39 | + verify { outputError("Invalid output format, must be backing-property or lazy-property") } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `previewAnnotationType should throw error for invalid value`() { |
| 44 | + mockkStatic(::outputError) |
| 45 | + every { outputError(any()) } answers { error("") } |
| 46 | + |
| 47 | + SvgXmlToImageVectorCommand().test( |
| 48 | + "--input-path", |
| 49 | + "/some/input", |
| 50 | + "--output-path", |
| 51 | + "/some/output", |
| 52 | + "--package-name", |
| 53 | + "com.example", |
| 54 | + "--preview-annotation-type", |
| 55 | + "invalid-value", |
| 56 | + ) |
| 57 | + |
| 58 | + verify { outputError("Invalid preview annotation type, must be 'androidx' or 'jetbrains'") } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `should throw error if input file is not SVG or XML`() { |
| 63 | + val mockMessage = "The input file must be an SVG or XML file." |
| 64 | + mockkStatic(::outputError) |
| 65 | + every { outputError(mockMessage) } answers { error(mockMessage) } |
| 66 | + |
| 67 | + val exception = assertFailsWith<IllegalStateException> { |
| 68 | + SvgXmlToImageVectorCommand().test( |
| 69 | + "--input-path", |
| 70 | + createTempFile().absolutePathString(), |
| 71 | + "--output-path", |
| 72 | + "/some/output", |
| 73 | + "--package-name", |
| 74 | + "com.example", |
| 75 | + ) |
| 76 | + } |
| 77 | + assertThat(exception.message).isEqualTo(mockMessage) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `should throw error if input path is not valid`() { |
| 82 | + val mockMessage = "The input path is not valid." |
| 83 | + mockkStatic(::outputError) |
| 84 | + every { outputError(mockMessage) } answers { error(mockMessage) } |
| 85 | + |
| 86 | + val exception = assertFailsWith<IllegalStateException> { |
| 87 | + SvgXmlToImageVectorCommand().test( |
| 88 | + "--input-path", |
| 89 | + "/invalid/path", |
| 90 | + "--output-path", |
| 91 | + "/some/output", |
| 92 | + "--package-name", |
| 93 | + "com.example", |
| 94 | + ) |
| 95 | + } |
| 96 | + assertThat(exception.message).isEqualTo(mockMessage) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun `should throw error if no icons to process`() { |
| 101 | + val mockMessage = "No any icons to process" |
| 102 | + mockkStatic(::outputError) |
| 103 | + every { outputError(mockMessage) } answers { error(mockMessage) } |
| 104 | + |
| 105 | + val exception = assertFailsWith<IllegalStateException> { |
| 106 | + SvgXmlToImageVectorCommand().test( |
| 107 | + "--input-path", |
| 108 | + createTempDirectory().absolutePathString(), |
| 109 | + "--output-path", |
| 110 | + "/some/output", |
| 111 | + "--package-name", |
| 112 | + "com.example", |
| 113 | + ) |
| 114 | + } |
| 115 | + assertThat(exception.message).isEqualTo(mockMessage) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun `should throw error if output path is not a directory`() { |
| 120 | + val mockMessage = "The output path must be a directory." |
| 121 | + mockkStatic(::outputError) |
| 122 | + every { outputError(mockMessage) } answers { error(mockMessage) } |
| 123 | + |
| 124 | + val exception = assertFailsWith<IllegalStateException> { |
| 125 | + SvgXmlToImageVectorCommand().test( |
| 126 | + "--input-path", |
| 127 | + getResourcePath("imagevector/svg/ic_linear_gradient.svg").absolutePathString(), |
| 128 | + "--output-path", |
| 129 | + createTempFile().absolutePathString(), |
| 130 | + "--package-name", |
| 131 | + "com.example", |
| 132 | + ) |
| 133 | + } |
| 134 | + assertThat(exception.message).isEqualTo(mockMessage) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun `should log processing path for each file in verbose mode`() { |
| 139 | + mockkStatic(::outputInfo) |
| 140 | + every { outputInfo(any()) } just Runs |
| 141 | + |
| 142 | + val inputPath = getResourcePath("imagevector/svg") |
| 143 | + SvgXmlToImageVectorCommand().test( |
| 144 | + "--input-path", |
| 145 | + inputPath.absolutePathString(), |
| 146 | + "--output-path", |
| 147 | + createTempDirectory().absolutePathString(), |
| 148 | + "--package-name", |
| 149 | + "com.example", |
| 150 | + "--verbose", |
| 151 | + ) |
| 152 | + |
| 153 | + inputPath.listDirectoryEntries("*.svg").forEach { path -> |
| 154 | + verify { outputInfo("process = $path") } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments