Adrian Roos | 5ed42b6 | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import unittest |
| 18 | |
| 19 | import apilint |
| 20 | |
| 21 | def cls(pkg, name): |
| 22 | return apilint.Class(apilint.Package(999, "package %s {" % pkg, None), 999, |
| 23 | "public final class %s {" % name, None) |
| 24 | |
| 25 | _ri = apilint._retry_iterator |
| 26 | |
| 27 | c1 = cls("android.app", "ActivityManager") |
| 28 | c2 = cls("android.app", "Notification") |
| 29 | c3 = cls("android.app", "Notification.Action") |
| 30 | c4 = cls("android.graphics", "Bitmap") |
| 31 | |
| 32 | class UtilTests(unittest.TestCase): |
| 33 | def test_retry_iterator(self): |
| 34 | it = apilint._retry_iterator([1, 2, 3, 4]) |
| 35 | self.assertEqual(it.next(), 1) |
| 36 | self.assertEqual(it.next(), 2) |
| 37 | self.assertEqual(it.next(), 3) |
| 38 | it.send("retry") |
| 39 | self.assertEqual(it.next(), 3) |
| 40 | self.assertEqual(it.next(), 4) |
| 41 | with self.assertRaises(StopIteration): |
| 42 | it.next() |
| 43 | |
| 44 | def test_retry_iterator_one(self): |
| 45 | it = apilint._retry_iterator([1]) |
| 46 | self.assertEqual(it.next(), 1) |
| 47 | it.send("retry") |
| 48 | self.assertEqual(it.next(), 1) |
| 49 | with self.assertRaises(StopIteration): |
| 50 | it.next() |
| 51 | |
| 52 | def test_retry_iterator_one(self): |
| 53 | it = apilint._retry_iterator([1]) |
| 54 | self.assertEqual(it.next(), 1) |
| 55 | it.send("retry") |
| 56 | self.assertEqual(it.next(), 1) |
| 57 | with self.assertRaises(StopIteration): |
| 58 | it.next() |
| 59 | |
| 60 | def test_skip_to_matching_class_found(self): |
| 61 | it = _ri([c1, c2, c3, c4]) |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 62 | self.assertEquals(apilint._skip_to_matching_class(it, c3), |
Adrian Roos | 5ed42b6 | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 63 | c3) |
| 64 | self.assertEqual(it.next(), c4) |
| 65 | |
| 66 | def test_skip_to_matching_class_not_found(self): |
| 67 | it = _ri([c1, c2, c3, c4]) |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 68 | self.assertEquals(apilint._skip_to_matching_class(it, cls("android.content", "ContentProvider")), |
Adrian Roos | 5ed42b6 | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 69 | None) |
| 70 | self.assertEqual(it.next(), c4) |
| 71 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 72 | def test_yield_until_matching_class_found(self): |
| 73 | it = _ri([c1, c2, c3, c4]) |
| 74 | self.assertEquals(list(apilint._yield_until_matching_class(it, c3)), |
| 75 | [c1, c2]) |
| 76 | self.assertEqual(it.next(), c4) |
| 77 | |
| 78 | def test_yield_until_matching_class_not_found(self): |
| 79 | it = _ri([c1, c2, c3, c4]) |
| 80 | self.assertEquals(list(apilint._yield_until_matching_class(it, cls("android.content", "ContentProvider"))), |
| 81 | [c1, c2, c3]) |
| 82 | self.assertEqual(it.next(), c4) |
| 83 | |
| 84 | def test_yield_until_matching_class_None(self): |
| 85 | it = _ri([c1, c2, c3, c4]) |
| 86 | self.assertEquals(list(apilint._yield_until_matching_class(it, None)), |
| 87 | [c1, c2, c3, c4]) |
| 88 | |
| 89 | |
| 90 | faulty_current_txt = """ |
| 91 | package android.app { |
| 92 | public final class Activity { |
| 93 | } |
| 94 | |
| 95 | public final class WallpaperColors implements android.os.Parcelable { |
| 96 | ctor public WallpaperColors(android.os.Parcel); |
| 97 | method public int describeContents(); |
| 98 | method public void writeToParcel(android.os.Parcel, int); |
| 99 | field public static final android.os.Parcelable.Creator<android.app.WallpaperColors> CREATOR; |
| 100 | } |
| 101 | } |
| 102 | """.split('\n') |
| 103 | |
| 104 | ok_current_txt = """ |
| 105 | package android.app { |
| 106 | public final class Activity { |
| 107 | } |
| 108 | |
| 109 | public final class WallpaperColors implements android.os.Parcelable { |
| 110 | ctor public WallpaperColors(); |
| 111 | method public int describeContents(); |
| 112 | method public void writeToParcel(android.os.Parcel, int); |
| 113 | field public static final android.os.Parcelable.Creator<android.app.WallpaperColors> CREATOR; |
| 114 | } |
| 115 | } |
| 116 | """.split('\n') |
| 117 | |
| 118 | system_current_txt = """ |
| 119 | package android.app { |
| 120 | public final class WallpaperColors implements android.os.Parcelable { |
| 121 | method public int getSomething(); |
| 122 | } |
| 123 | } |
| 124 | """.split('\n') |
| 125 | |
| 126 | |
| 127 | |
| 128 | class BaseFileTests(unittest.TestCase): |
| 129 | def test_base_file_avoids_errors(self): |
| 130 | failures, _ = apilint.examine_stream(system_current_txt, ok_current_txt) |
| 131 | self.assertEquals(failures, {}) |
| 132 | |
| 133 | def test_class_with_base_finds_same_errors(self): |
| 134 | failures_with_classes_with_base, _ = apilint.examine_stream("", faulty_current_txt, |
| 135 | in_classes_with_base=[cls("android.app", "WallpaperColors")]) |
| 136 | failures_with_system_txt, _ = apilint.examine_stream(system_current_txt, faulty_current_txt) |
| 137 | |
| 138 | self.assertEquals(failures_with_classes_with_base.keys(), failures_with_system_txt.keys()) |
| 139 | |
| 140 | def test_classes_with_base_is_emited(self): |
| 141 | classes_with_base = [] |
| 142 | _, _ = apilint.examine_stream(system_current_txt, faulty_current_txt, |
| 143 | out_classes_with_base=classes_with_base) |
| 144 | self.assertEquals(map(lambda x: x.fullname, classes_with_base), ["android.app.WallpaperColors"]) |
| 145 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 146 | class V2TokenizerTests(unittest.TestCase): |
| 147 | def _test(self, raw, expected): |
| 148 | self.assertEquals(apilint.V2Tokenizer(raw).tokenize(), expected) |
| 149 | |
| 150 | def test_simple(self): |
| 151 | self._test(" method public some.Type someName(some.Argument arg, int arg);", |
| 152 | ['method', 'public', 'some.Type', 'someName', '(', 'some.Argument', |
| 153 | 'arg', ',', 'int', 'arg', ')', ';']) |
| 154 | self._test("class Some.Class extends SomeOther {", |
| 155 | ['class', 'Some.Class', 'extends', 'SomeOther', '{']) |
| 156 | |
| 157 | def test_annotation(self): |
| 158 | self._test("method @Nullable public void name();", |
| 159 | ['method', '@', 'Nullable', 'public', 'void', 'name', '(', ')', ';']) |
| 160 | |
| 161 | def test_annotation_args(self): |
| 162 | self._test("@Some(val=1, other=2) class Class {", |
| 163 | ['@', 'Some', '(', 'val', '=', '1', ',', 'other', '=', '2', ')', |
| 164 | 'class', 'Class', '{']) |
| 165 | def test_comment(self): |
| 166 | self._test("some //comment", ['some']) |
| 167 | |
| 168 | def test_strings(self): |
| 169 | self._test(r'"" "foo" "\"" "\\"', ['""', '"foo"', r'"\""', r'"\\"']) |
| 170 | |
| 171 | def test_at_interface(self): |
| 172 | self._test("public @interface Annotation {", |
| 173 | ['public', '@interface', 'Annotation', '{']) |
| 174 | |
| 175 | def test_array_type(self): |
| 176 | self._test("int[][]", ['int', '[]', '[]']) |
| 177 | |
| 178 | def test_generics(self): |
| 179 | self._test("<>foobar<A extends Object>", |
| 180 | ['<', '>', 'foobar', '<', 'A', 'extends', 'Object', '>']) |
| 181 | |
| 182 | class V2ParserTests(unittest.TestCase): |
| 183 | def _cls(self, raw): |
| 184 | pkg = apilint.Package(999, "package pkg {", None) |
| 185 | return apilint.Class(pkg, 1, raw, '', sig_format=2) |
| 186 | |
| 187 | def _method(self, raw, cls=None): |
| 188 | if not cls: |
| 189 | cls = self._cls("class Class {") |
| 190 | return apilint.Method(cls, 1, raw, '', sig_format=2) |
| 191 | |
| 192 | def _field(self, raw): |
| 193 | cls = self._cls("class Class {") |
| 194 | return apilint.Field(cls, 1, raw, '', sig_format=2) |
| 195 | |
| 196 | def test_class(self): |
| 197 | cls = self._cls("@Deprecated @IntRange(from=1, to=2) public static abstract class Some.Name extends Super<Class> implements Interface<Class> {") |
| 198 | self.assertTrue('deprecated' in cls.split) |
| 199 | self.assertTrue('static' in cls.split) |
| 200 | self.assertTrue('abstract' in cls.split) |
| 201 | self.assertTrue('class' in cls.split) |
| 202 | self.assertEquals('Super', cls.extends) |
| 203 | self.assertEquals('Interface', cls.implements) |
| 204 | self.assertEquals('pkg.Some.Name', cls.fullname) |
| 205 | |
| 206 | def test_interface(self): |
| 207 | cls = self._cls("@Deprecated @IntRange(from=1, to=2) public interface Some.Name extends Interface<Class> {") |
| 208 | self.assertTrue('deprecated' in cls.split) |
| 209 | self.assertTrue('interface' in cls.split) |
| 210 | self.assertEquals('Interface', cls.extends) |
| 211 | self.assertEquals('Interface', cls.implements) |
| 212 | self.assertEquals('pkg.Some.Name', cls.fullname) |
| 213 | |
| 214 | def test_at_interface(self): |
| 215 | cls = self._cls("@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.FIELD, java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.CONSTRUCTOR, java.lang.annotation.ElementType.LOCAL_VARIABLE}) @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS) public @interface SuppressLint {") |
| 216 | self.assertTrue('@interface' in cls.split) |
| 217 | self.assertEquals('pkg.SuppressLint', cls.fullname) |
| 218 | |
| 219 | def test_parse_method(self): |
| 220 | m = self._method("method @Deprecated public static <T> Class<T>[][] name(" |
| 221 | + "Class<T[]>[][], Class<T[][][]>[][]...) throws Exception, T;") |
| 222 | self.assertTrue('static' in m.split) |
| 223 | self.assertTrue('public' in m.split) |
| 224 | self.assertTrue('method' in m.split) |
| 225 | self.assertTrue('deprecated' in m.split) |
| 226 | self.assertEquals('java.lang.Class[][]', m.typ) |
| 227 | self.assertEquals('name', m.name) |
| 228 | self.assertEquals(['java.lang.Class[][]', 'java.lang.Class[][]...'], m.args) |
| 229 | self.assertEquals(['java.lang.Exception', 'T'], m.throws) |
| 230 | |
| 231 | def test_ctor(self): |
| 232 | m = self._method("ctor @Deprecated <T> ClassName();") |
| 233 | self.assertTrue('ctor' in m.split) |
| 234 | self.assertTrue('deprecated' in m.split) |
| 235 | self.assertEquals('ctor', m.typ) |
| 236 | self.assertEquals('ClassName', m.name) |
| 237 | |
| 238 | def test_parse_annotation_method(self): |
| 239 | cls = self._cls("@interface Annotation {") |
| 240 | self._method('method abstract String category() default "";', cls=cls) |
| 241 | self._method('method abstract boolean deepExport() default false;', cls=cls) |
| 242 | self._method('method abstract ViewDebug.FlagToString[] flagMapping() default {};', cls=cls) |
| 243 | |
| 244 | def test_parse_string_field(self): |
| 245 | f = self._field('field @Deprecated public final String SOME_NAME = "value";') |
| 246 | self.assertTrue('field' in f.split) |
| 247 | self.assertTrue('deprecated' in f.split) |
| 248 | self.assertTrue('final' in f.split) |
| 249 | self.assertEquals('java.lang.String', f.typ) |
| 250 | self.assertEquals('SOME_NAME', f.name) |
| 251 | self.assertEquals('value', f.value) |
| 252 | |
| 253 | def test_parse_field(self): |
| 254 | f = self._field('field public Object SOME_NAME;') |
| 255 | self.assertTrue('field' in f.split) |
| 256 | self.assertEquals('java.lang.Object', f.typ) |
| 257 | self.assertEquals('SOME_NAME', f.name) |
| 258 | self.assertEquals(None, f.value) |
| 259 | |
| 260 | def test_parse_int_field(self): |
| 261 | f = self._field('field public int NAME = 123;') |
| 262 | self.assertTrue('field' in f.split) |
| 263 | self.assertEquals('int', f.typ) |
| 264 | self.assertEquals('NAME', f.name) |
| 265 | self.assertEquals('123', f.value) |
| 266 | |
| 267 | def test_parse_quotient_field(self): |
| 268 | f = self._field('field public int NAME = (0.0/0.0);') |
| 269 | self.assertTrue('field' in f.split) |
| 270 | self.assertEquals('int', f.typ) |
| 271 | self.assertEquals('NAME', f.name) |
| 272 | self.assertEquals('( 0.0 / 0.0 )', f.value) |
| 273 | |
Adrian Roos | 5ed42b6 | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 274 | if __name__ == "__main__": |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 275 | unittest.main() |