Skip to content

Instantly share code, notes, and snippets.

View mirokolodii's full-sized avatar

Myroslav Kolodii mirokolodii

View GitHub Profile
@mirokolodii
mirokolodii / gist:fc816d262ac91c344659bde90c0c284e
Created February 11, 2019 15:45
Espresso RecyclerView count test
@Test
public void FilterClickShouldChangeRecyclerViewCount() {
// Get items count before action
RecyclerView recyclerView = mActivityTestRule.getActivity().findViewById(R.id.recycler_view);
int countBefore = Objects.requireNonNull(recyclerView.getAdapter()).getItemCount();
Log.e("count", "Items count before: " + countBefore);
// Perform action
ViewInteraction actionMenuItemView = onView(
allOf(
@mirokolodii
mirokolodii / Test.java
Created October 18, 2019 06:21
Switch to Kotlin #2.1
String someStr1 = ""; // initialized with empty string
String someStr2; // by default value of someStr2 is null
@mirokolodii
mirokolodii / Test.kt
Last active October 18, 2019 07:13
Switch to Kotlin #2.2
val someStr1: String = "" // same as in Java, i.e initialized with empty string
val someStr2: String // warning: no default value assigned. Should be initialized
// with a non-null value before its first use
val someStr3: String = null // error: can't be null
@mirokolodii
mirokolodii / Test.kt
Last active October 24, 2019 06:09
Switch to Kotlin #2.3
val someStr1: String? = null // valid statement, notice a question mark after 'String'
val someStr2: String? // warning: no default value assigned. Should be initialized
// with some (nullable or non-nullable) value before its first use
@mirokolodii
mirokolodii / Test.kt
Last active October 18, 2019 07:16
Switch to Kotlin #2.4
Class Test(val someInt: Int)
val test1: Test // warning: no default value, should be initialized
// with non-null value before first use
val test2: Test = null // error: non-nullable type can't hold null value
val test3: Test? = null // valid
@mirokolodii
mirokolodii / Test.kt
Created October 18, 2019 06:35
Switch to Kotlin #2.5
var nullableStr: String? = null
var nonNullStr: String = "a string"
nullableStr = nonNullStr // valid
nonNullStr = nullableStr // error, should be checked for nullability first
If (nullableStr != null) {
nonNullStr = nullableStr // valid
}
@mirokolodii
mirokolodii / Test.java
Created October 18, 2019 07:00
Switch to Kotlin #2.6
Integer intObj = null;
int primitiveInt;
primitiveInt = intObj; // NullPointerException during unboxing
@mirokolodii
mirokolodii / Test.java
Last active October 24, 2019 06:53
Switch to Kotlin #2.7
Integer nullInt = null;
if (someInt.equals(23)) { // NullPointerException because of attempt to call
// a method on a null
// Rest of code
}
@mirokolodii
mirokolodii / Test.kt
Last active October 24, 2019 06:51
Switch to Kotlin #2.8
var nonNullInt = 1
val nullInt: Int? = null
// 1. Error
nonNullInt = nullInt // Error: type mismatch
// 2. Valid
if (nullInt != null) {
/* Here is used a smart cast (one of Kotlin compiler's feature),
which casts Int? to Int automatically */
nonNullInt = nullInt
}
@mirokolodii
mirokolodii / Test.kt
Created October 24, 2019 06:57
Switch to Kotlin 2.9
val nonNullInt = 1
val nullInt: Int? = null
if (nullInt == nonNullInt) {
println("Both are equal")
} else {
println("Not equal") // Result is 'Not equal'
}