blob: 69f49c8b97c3e5809f77799d346bb3cb12e9257b [file] [log] [blame]
Adam Lesinskib274e352015-11-06 15:14:35 -08001/*
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 Lesinskib274e352015-11-06 15:14:35 -080017#include "java/AnnotationProcessor.h"
Adam Lesinskice5e56e22016-10-21 17:56:45 -070018
Adam Lesinskia693c4a2017-11-09 11:29:39 -080019#include "io/StringStream.h"
Adam Lesinski626b3db2016-04-07 13:24:59 -070020#include "test/Test.h"
Adam Lesinskia693c4a2017-11-09 11:29:39 -080021#include "text/Printer.h"
Adam Lesinskib274e352015-11-06 15:14:35 -080022
Adam Lesinskia693c4a2017-11-09 11:29:39 -080023using ::aapt::io::StringOutputStream;
24using ::aapt::text::Printer;
Adam Lesinskie967d3f2017-07-24 18:19:36 -070025using ::testing::Eq;
26using ::testing::HasSubstr;
27using ::testing::Not;
28
Adam Lesinskib274e352015-11-06 15:14:35 -080029namespace aapt {
30
Adam Lesinski626b3db2016-04-07 13:24:59 -070031TEST(AnnotationProcessorTest, EmitsDeprecated) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070032 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 Lesinskib274e352015-11-06 15:14:35 -080036
Adam Lesinskice5e56e22016-10-21 17:56:45 -070037 AnnotationProcessor processor;
38 processor.AppendComment(comment);
Adam Lesinskib274e352015-11-06 15:14:35 -080039
Adam Lesinskia693c4a2017-11-09 11:29:39 -080040 std::string annotations;
41 StringOutputStream out(&annotations);
42 Printer printer(&out);
43 processor.Print(&printer);
44 out.Flush();
Adam Lesinskib274e352015-11-06 15:14:35 -080045
Adam Lesinskie967d3f2017-07-24 18:19:36 -070046 EXPECT_THAT(annotations, HasSubstr("@Deprecated"));
Adam Lesinskib274e352015-11-06 15:14:35 -080047}
48
Adam Lesinski626b3db2016-04-07 13:24:59 -070049TEST(AnnotationProcessorTest, EmitsSystemApiAnnotationAndRemovesFromComment) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070050 AnnotationProcessor processor;
51 processor.AppendComment("@SystemApi This is a system API");
Adam Lesinski626b3db2016-04-07 13:24:59 -070052
Adam Lesinskia693c4a2017-11-09 11:29:39 -080053 std::string annotations;
54 StringOutputStream out(&annotations);
55 Printer printer(&out);
56 processor.Print(&printer);
57 out.Flush();
Adam Lesinski626b3db2016-04-07 13:24:59 -070058
Adam Lesinskie967d3f2017-07-24 18:19:36 -070059 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 Lesinski09f4d702017-08-08 10:39:55 -070064TEST(AnnotationProcessorTest, EmitsTestApiAnnotationAndRemovesFromComment) {
65 AnnotationProcessor processor;
66 processor.AppendComment("@TestApi This is a test API");
67
Adam Lesinskia693c4a2017-11-09 11:29:39 -080068 std::string annotations;
69 StringOutputStream out(&annotations);
70 Printer printer(&out);
71 processor.Print(&printer);
72 out.Flush();
Adam Lesinski09f4d702017-08-08 10:39:55 -070073
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 Lesinskie967d3f2017-07-24 18:19:36 -070079TEST(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 Lesinski626b3db2016-04-07 13:24:59 -070088}
89
Adam Lesinskice5e56e22016-10-21 17:56:45 -070090} // namespace aapt