|
338 | 338 |
|
339 | 339 | ## Binary and Compiled Source Files |
340 | 340 |
|
341 | | -If you need to use binaries in your unit tests, there is |
342 | | -a special test file type for that: base64gzip. Here's an |
343 | | -example from a lint check which tries to recognize usage |
344 | | -of Cordova in the bytecode: |
| 341 | +If you need to use binaries in your unit tests, there are two options: |
| 342 | + |
| 343 | + 1. base64gzip |
| 344 | + 2. API stubs |
| 345 | + |
| 346 | +If you want to analyze bytecode of method bodies, you'll need to use |
| 347 | +the first option. |
| 348 | + |
| 349 | +The first type requires you to actually compile your test file into a |
| 350 | +set of .class files, and check those in as a gzip-compressed, base64 |
| 351 | +encoded string. Lint has utilities for this; see the next section. |
| 352 | + |
| 353 | +The second option is using API stubs. For simple stub files (where you |
| 354 | +only need to provide APIs you'll call as binaries, but not code), lint |
| 355 | +can produce the corresponding bytecode on the fly, so you don't need |
| 356 | +to pre-create binary contents of the class. This is particularly |
| 357 | +helpful when you just want to create stubs for a library your lint |
| 358 | +check is targeting and you want to make sure the detector is seeing |
| 359 | +the same types of elements as it will when analyzing real code outside |
| 360 | +of tests (since there is a difference between resolving into APIs from |
| 361 | +source and form binaries; when you're analyzing calls into source, you |
| 362 | +can access for example method bodies, and this isn't available via |
| 363 | +UAST from byte code.) |
| 364 | + |
| 365 | +These test files also let you specify an artifact name instead of a |
| 366 | +jar path, and lint will use this to place the jar in a special place |
| 367 | +such that it recognizes it (via `JavaEvaluator.findOwnerLibrary`) as |
| 368 | +belonging to this library. |
| 369 | + |
| 370 | +Here's an example of how you can create one of these binary stub |
| 371 | +files: |
| 372 | + |
| 373 | +``` |
| 374 | +fun testIdentityEqualsOkay() { |
| 375 | + lint().files( |
| 376 | + kotlin( |
| 377 | + "/*test contents here *using* some recycler view APIs*/" |
| 378 | + ).indented(), |
| 379 | + mavenLibrary( |
| 380 | + "androidx.recyclerview:recyclerview:1.0.0", |
| 381 | + java( |
| 382 | + """ |
| 383 | + package androidx.recyclerview.widget; |
| 384 | + public class DiffUtil { |
| 385 | + public abstract static class ItemCallback<T> { |
| 386 | + public abstract boolean areItemsTheSame(T oldItem, T newItem); |
| 387 | + public abstract boolean areContentsTheSame(T oldItem, T newItem); |
| 388 | + } |
| 389 | + } |
| 390 | + """ |
| 391 | + ).indented() |
| 392 | + ) |
| 393 | + ).run().expect( |
| 394 | +``` |
| 395 | + |
| 396 | +## Base64-encoded gzipped byte code |
| 397 | + |
| 398 | +Here's an example from a lint check which tries to recognize usage of |
| 399 | +Cordova in the bytecode: |
345 | 400 |
|
346 | 401 | ``` |
347 | 402 | fun testVulnerableCordovaVersionInClasses() { |
|
411 | 466 | Dependencies and Stubs“ section above, as well as the [frequently asked |
412 | 467 | questions](faq.md.html). |
413 | 468 |
|
| 469 | +## Language Level |
| 470 | + |
| 471 | +Lint will analyze Java and Kotlin test files using its own default |
| 472 | +language levels. If you need a higher (or lower) language level in order |
| 473 | +to test a particular scenario, you can use the `kotlinLanguageLevel` |
| 474 | +and `javaLanguageLevel` setter methods on the lint test configuration. |
| 475 | +Here's an example of a unit test setup for Java records: |
| 476 | + |
| 477 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin |
| 478 | + lint() |
| 479 | + .files( |
| 480 | + java(""" |
| 481 | + record Person(String name, int age) { |
| 482 | + } |
| 483 | + """) |
| 484 | + .indented() |
| 485 | + ) |
| 486 | + .javaLanguageLevel("17") |
| 487 | + .run() |
| 488 | + .expect(...) |
| 489 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 490 | + |
414 | 491 | <!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script> |
0 commit comments