| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 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 | |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 17 | #include "java/AnnotationProcessor.h" |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 19 | #include "io/StringStream.h" |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 20 | #include "test/Test.h" |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 21 | #include "text/Printer.h" |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 22 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 23 | using ::aapt::io::StringOutputStream; |
| 24 | using ::aapt::text::Printer; |
| Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 25 | using ::testing::Eq; |
| 26 | using ::testing::HasSubstr; |
| 27 | using ::testing::Not; |
| 28 | |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 31 | TEST(AnnotationProcessorTest, EmitsDeprecated) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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! }"; |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 36 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | AnnotationProcessor processor; |
| 38 | processor.AppendComment(comment); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 39 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 40 | std::string annotations; |
| 41 | StringOutputStream out(&annotations); |
| 42 | Printer printer(&out); |
| 43 | processor.Print(&printer); |
| 44 | out.Flush(); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 45 | |
| Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 46 | EXPECT_THAT(annotations, HasSubstr("@Deprecated")); |
| Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 49 | TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) { |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | AnnotationProcessor processor; |
| 51 | processor.AppendComment("@SystemApi This is a system API"); |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 52 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 53 | std::string annotations; |
| 54 | StringOutputStream out(&annotations); |
| 55 | Printer printer(&out); |
| 56 | processor.Print(&printer); |
| 57 | out.Flush(); |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 58 | |
| Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 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 | |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 64 | TEST(AnnotationProcessorTest, EmitsTestApiAnnotationAndRemovesFromComment) { |
| 65 | AnnotationProcessor processor; |
| 66 | processor.AppendComment("@TestApi This is a test API"); |
| 67 | |
| Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 68 | std::string annotations; |
| 69 | StringOutputStream out(&annotations); |
| 70 | Printer printer(&out); |
| 71 | processor.Print(&printer); |
| 72 | out.Flush(); |
| Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 73 | |
| 74 | EXPECT_THAT(annotations, HasSubstr("@android.annotation.TestApi")); |
| 75 | EXPECT_THAT(annotations, Not(HasSubstr("@TestApi"))); |
| 76 | EXPECT_THAT(annotations, HasSubstr("This is a test API")); |
| 77 | } |
| 78 | |
| Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 79 | TEST(AnnotationProcessor, ExtractsFirstSentence) { |
| 80 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence("This is the only sentence"), |
| 81 | Eq("This is the only sentence")); |
| 82 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence( |
| 83 | "This is the\n first sentence. This is the rest of the paragraph."), |
| 84 | Eq("This is the\n first sentence.")); |
| 85 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence( |
| 86 | "This is the first sentence with a {@link android.R.styleable.Theme}."), |
| 87 | Eq("This is the first sentence with a {@link android.R.styleable.Theme}.")); |
| Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | } // namespace aapt |