| 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 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 | |
| 17 | #include "java/AnnotationProcessor.h" |
| 18 | |
| 19 | #include "io/StringStream.h" |
| 20 | #include "test/Test.h" |
| 21 | #include "text/Printer.h" |
| 22 | |
| 23 | using ::aapt::io::StringOutputStream; |
| 24 | using ::aapt::text::Printer; |
| 25 | using ::testing::Eq; |
| 26 | using ::testing::HasSubstr; |
| 27 | using ::testing::Not; |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
| 31 | TEST(AnnotationProcessorTest, EmitsDeprecated) { |
| 32 | const char* comment = |
| 33 | "Some comment, and it should contain a marker word, " |
| 34 | "something that marks this resource as nor needed. " |
| 35 | "{@deprecated That's the marker! }"; |
| 36 | |
| 37 | AnnotationProcessor processor; |
| 38 | processor.AppendComment(comment); |
| 39 | |
| 40 | std::string annotations; |
| 41 | StringOutputStream out(&annotations); |
| 42 | Printer printer(&out); |
| 43 | processor.Print(&printer); |
| 44 | out.Flush(); |
| 45 | |
| 46 | EXPECT_THAT(annotations, HasSubstr("@Deprecated")); |
| 47 | } |
| 48 | |
| 49 | TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) { |
| 50 | AnnotationProcessor processor; |
| 51 | processor.AppendComment("@SystemApi This is a system API"); |
| 52 | |
| 53 | std::string annotations; |
| 54 | StringOutputStream out(&annotations); |
| 55 | Printer printer(&out); |
| 56 | processor.Print(&printer); |
| 57 | out.Flush(); |
| 58 | |
| 59 | EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi")); |
| 60 | EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi"))); |
| 61 | EXPECT_THAT(annotations, HasSubstr("This is a system API")); |
| 62 | } |
| 63 | |
| 64 | TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationParamsAndRemovesFromComment) { |
| 65 | AnnotationProcessor processor; |
| 66 | processor.AppendComment("@SystemApi (p1=k1,p2=k2) This is a system API"); |
| 67 | |
| 68 | std::string annotations; |
| 69 | StringOutputStream out(&annotations); |
| 70 | Printer printer(&out); |
| 71 | processor.Print(&printer); |
| 72 | out.Flush(); |
| 73 | |
| 74 | EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi(p1=k1,p2=k2)")); |
| 75 | EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi"))); |
| 76 | EXPECT_THAT(annotations, HasSubstr("This is a system API")); |
| 77 | } |
| 78 | |
| 79 | TEST(AnnotationProcessorTest, EmitsTestApiAnnotationAndRemovesFromComment) { |
| 80 | AnnotationProcessor processor; |
| 81 | processor.AppendComment("@TestApi This is a test API"); |
| 82 | |
| 83 | std::string annotations; |
| 84 | StringOutputStream out(&annotations); |
| 85 | Printer printer(&out); |
| 86 | processor.Print(&printer); |
| 87 | out.Flush(); |
| 88 | |
| 89 | EXPECT_THAT(annotations, HasSubstr("@android.annotation.TestApi")); |
| 90 | EXPECT_THAT(annotations, Not(HasSubstr("@TestApi"))); |
| 91 | EXPECT_THAT(annotations, HasSubstr("This is a test API")); |
| 92 | } |
| 93 | |
| 94 | TEST(AnnotationProcessorTest, NotEmitSystemApiAnnotation) { |
| 95 | AnnotationProcessor processor; |
| 96 | processor.AppendComment("@SystemApi This is a system API"); |
| 97 | |
| 98 | std::string annotations; |
| 99 | StringOutputStream out(&annotations); |
| 100 | Printer printer(&out); |
| 101 | processor.Print(&printer, true /* strip_api_annotations */); |
| 102 | out.Flush(); |
| 103 | |
| 104 | EXPECT_THAT(annotations, Not(HasSubstr("@android.annotation.SystemApi"))); |
| 105 | EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi"))); |
| 106 | EXPECT_THAT(annotations, HasSubstr("This is a system API")); |
| 107 | } |
| 108 | |
| 109 | TEST(AnnotationProcessor, ExtractsFirstSentence) { |
| 110 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence("This is the only sentence"), |
| 111 | Eq("This is the only sentence")); |
| 112 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence( |
| 113 | "This is the\n first sentence. This is the rest of the paragraph."), |
| 114 | Eq("This is the\n first sentence.")); |
| 115 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence( |
| 116 | "This is the first sentence with a {@link android.R.styleable.Theme}."), |
| 117 | Eq("This is the first sentence with a {@link android.R.styleable.Theme}.")); |
| 118 | } |
| 119 | |
| 120 | } // namespace aapt |