|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rust.codegen.server.smithy |
| 7 | + |
| 8 | +import io.kotest.assertions.throwables.shouldThrow |
| 9 | +import io.kotest.matchers.collections.shouldHaveSize |
| 10 | +import io.kotest.matchers.shouldBe |
| 11 | +import org.junit.jupiter.api.Test |
| 12 | +import software.amazon.smithy.model.shapes.ShapeId |
| 13 | +import software.amazon.smithy.model.validation.Severity |
| 14 | +import software.amazon.smithy.model.validation.ValidatedResultException |
| 15 | +import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel |
| 16 | + |
| 17 | +class PatternTraitEscapedSpecialCharsValidatorTest { |
| 18 | + @Test |
| 19 | + fun `should error out with a suggestion if non-escaped special chars used inside @pattern`() { |
| 20 | + val exception = shouldThrow<ValidatedResultException> { |
| 21 | + """ |
| 22 | + namespace test |
| 23 | + |
| 24 | + @pattern("\t") |
| 25 | + string MyString |
| 26 | + """.asSmithyModel(smithyVersion = "2") |
| 27 | + } |
| 28 | + val events = exception.validationEvents.filter { it.severity == Severity.ERROR } |
| 29 | + |
| 30 | + events shouldHaveSize 1 |
| 31 | + events[0].shapeId.get() shouldBe ShapeId.from("test#MyString") |
| 32 | + events[0].message shouldBe """ |
| 33 | + Non-escaped special characters used inside `@pattern`. |
| 34 | + You must escape them: `@pattern("\\t")`. |
| 35 | + See https://github.com/awslabs/smithy-rs/issues/2508 for more details. |
| 36 | + """.trimIndent() |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `should suggest escaping spacial characters properly`() { |
| 41 | + val exception = shouldThrow<ValidatedResultException> { |
| 42 | + """ |
| 43 | + namespace test |
| 44 | + |
| 45 | + @pattern("[.\n\\r]+") |
| 46 | + string MyString |
| 47 | + """.asSmithyModel(smithyVersion = "2") |
| 48 | + } |
| 49 | + val events = exception.validationEvents.filter { it.severity == Severity.ERROR } |
| 50 | + |
| 51 | + events shouldHaveSize 1 |
| 52 | + events[0].shapeId.get() shouldBe ShapeId.from("test#MyString") |
| 53 | + events[0].message shouldBe """ |
| 54 | + Non-escaped special characters used inside `@pattern`. |
| 55 | + You must escape them: `@pattern("[.\\n\\r]+")`. |
| 56 | + See https://github.com/awslabs/smithy-rs/issues/2508 for more details. |
| 57 | + """.trimIndent() |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `should report all non-escaped special characters`() { |
| 62 | + val exception = shouldThrow<ValidatedResultException> { |
| 63 | + """ |
| 64 | + namespace test |
| 65 | + |
| 66 | + @pattern("\b") |
| 67 | + string MyString |
| 68 | + |
| 69 | + @pattern("^\n$") |
| 70 | + string MyString2 |
| 71 | + |
| 72 | + @pattern("^[\n]+$") |
| 73 | + string MyString3 |
| 74 | + |
| 75 | + @pattern("^[\r\t]$") |
| 76 | + string MyString4 |
| 77 | + """.asSmithyModel(smithyVersion = "2") |
| 78 | + } |
| 79 | + val events = exception.validationEvents.filter { it.severity == Severity.ERROR } |
| 80 | + events shouldHaveSize 4 |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun `should report errors on string members`() { |
| 85 | + val exception = shouldThrow<ValidatedResultException> { |
| 86 | + """ |
| 87 | + namespace test |
| 88 | + |
| 89 | + @pattern("\t") |
| 90 | + string MyString |
| 91 | + |
| 92 | + structure MyStructure { |
| 93 | + @pattern("\b") |
| 94 | + field: String |
| 95 | + } |
| 96 | + """.asSmithyModel(smithyVersion = "2") |
| 97 | + } |
| 98 | + val events = exception.validationEvents.filter { it.severity == Severity.ERROR } |
| 99 | + |
| 100 | + events shouldHaveSize 2 |
| 101 | + events[0].shapeId.get() shouldBe ShapeId.from("test#MyString") |
| 102 | + events[1].shapeId.get() shouldBe ShapeId.from("test#MyStructure\$field") |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun `shouldn't error out if special chars are properly escaped`() { |
| 107 | + """ |
| 108 | + namespace test |
| 109 | +
|
| 110 | + @pattern("\\t") |
| 111 | + string MyString |
| 112 | + |
| 113 | + @pattern("[.\\n\\r]+") |
| 114 | + string MyString2 |
| 115 | + |
| 116 | + @pattern("\\b\\f\\n\\r\\t") |
| 117 | + string MyString3 |
| 118 | +
|
| 119 | + @pattern("\\w+") |
| 120 | + string MyString4 |
| 121 | + """.asSmithyModel(smithyVersion = "2") |
| 122 | + } |
| 123 | +} |
0 commit comments