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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 19 | #include "test/Test.h" |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 20 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 21 | using ::testing::Eq; |
| 22 | using ::testing::HasSubstr; |
| 23 | using ::testing::Not; |
| 24 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 27 | TEST(AnnotationProcessorTest, EmitsDeprecated) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | const char* comment = |
| 29 | "Some comment, and it should contain a marker word, " |
| 30 | "something that marks this resource as nor needed. " |
| 31 | "{@deprecated That's the marker! }"; |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 32 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | AnnotationProcessor processor; |
| 34 | processor.AppendComment(comment); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | std::stringstream result; |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 37 | processor.WriteToStream("", &result); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | std::string annotations = result.str(); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 40 | EXPECT_THAT(annotations, HasSubstr("@Deprecated")); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 43 | TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | AnnotationProcessor processor; |
| 45 | processor.AppendComment("@SystemApi This is a system API"); |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | std::stringstream result; |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 48 | processor.WriteToStream("", &result); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | std::string annotations = result.str(); |
Adam Lesinski | 626b3db | 2016-04-07 13:24:59 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 51 | EXPECT_THAT(annotations, HasSubstr("@android.annotation.SystemApi")); |
| 52 | EXPECT_THAT(annotations, Not(HasSubstr("@SystemApi"))); |
| 53 | EXPECT_THAT(annotations, HasSubstr("This is a system API")); |
| 54 | } |
| 55 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 56 | TEST(AnnotationProcessorTest, EmitsTestApiAnnotationAndRemovesFromComment) { |
| 57 | AnnotationProcessor processor; |
| 58 | processor.AppendComment("@TestApi This is a test API"); |
| 59 | |
| 60 | std::stringstream result; |
| 61 | processor.WriteToStream("", &result); |
| 62 | std::string annotations = result.str(); |
| 63 | |
| 64 | EXPECT_THAT(annotations, HasSubstr("@android.annotation.TestApi")); |
| 65 | EXPECT_THAT(annotations, Not(HasSubstr("@TestApi"))); |
| 66 | EXPECT_THAT(annotations, HasSubstr("This is a test API")); |
| 67 | } |
| 68 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 69 | TEST(AnnotationProcessor, ExtractsFirstSentence) { |
| 70 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence("This is the only sentence"), |
| 71 | Eq("This is the only sentence")); |
| 72 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence( |
| 73 | "This is the\n first sentence. This is the rest of the paragraph."), |
| 74 | Eq("This is the\n first sentence.")); |
| 75 | EXPECT_THAT(AnnotationProcessor::ExtractFirstSentence( |
| 76 | "This is the first sentence with a {@link android.R.styleable.Theme}."), |
| 77 | 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] | 78 | } |
| 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | } // namespace aapt |