Home | History | Annotate | Download | only in java
      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, EmitsTestApiAnnotationAndRemovesFromComment) {
     65   AnnotationProcessor processor;
     66   processor.AppendComment("@TestApi This is a test 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.TestApi"));
     75   EXPECT_THAT(annotations, Not(HasSubstr("@TestApi")));
     76   EXPECT_THAT(annotations, HasSubstr("This is a test API"));
     77 }
     78 
     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}."));
     88 }
     89 
     90 }  // namespace aapt
     91