Skip to content

Instantly share code, notes, and snippets.

View amatkivskiy's full-sized avatar
🇺🇦

Andrii Matkivskyi amatkivskiy

🇺🇦
View GitHub Profile
@amatkivskiy
amatkivskiy / TokenDataProvider.kt
Last active November 25, 2018 20:26
medium_TokenDataProvider
class TokenDataProvider {
fun getToken(): Observable<Result<String>> {
// Just return sample string
return Observable.just("token")
.toResult()
}
}
/**
* Just an handful extension function which wraps any value or error produced by [Observable] into [Result].
@amatkivskiy
amatkivskiy / TokenDataProviderTest.kt
Created November 25, 2018 20:28
medium_TokenDataProviderTest
class TokenDataProviderTest {
@Test
fun `cast issue`() {
val result = TokenDataProvider()
.getToken()
.blockingFirst()
result.fold({
assertEquals(it, "token")
}, {
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE report
PUBLIC '-//JACOCO//DTD Report 1.1//EN'
'report.dtd'>
<report name="app">
<sessioninfo dump="1575989429103" id="fv-az606-3e43c913" start="1575989418154"/>
<package name=" com/java/android/example/domain/model">
<class name=" com/java/android/example/domain/model/TripEvents" sourcefilename="Trip.kt">
<method desc="()Ljava/lang/String;" line="40" name="getDistance">
<counter covered="0" missed="3" type="INSTRUCTION"/>
val child = parentObj.child?.child?.child
if (child != null) {
doSomethingOnBooleanValue(child.isValid)
} else {
// handle null value here
}
fun doSomethingOnBooleanValue(valid: Boolean) {
if (valid) {
// `valid` is really true
data class Parent(val child: Nested?)
class Nested(val child: Nested?, val isValid: Boolean = false)
// Assume you got this object from the dark and deep internals of your app
val parentObj = getParentObjFromInternals()
doSomethingOnBooleanValue(parentObj.child?.child?.child?.isValid)
fun doSomethingOnBooleanValue(valid: Boolean?) {
if (valid == true) {
// Arrange
data class Parent(val child: Nested?)
class Nested(val child: Nested?, val isValid : Boolean = false)
// Assume you got this object from the dark and deep internals of your app
val parentObj = getParentObjFromInternals()
// This is straightforward but it won't work
if (parentObj.child?.child?.child?.isValid) {
// Error.
fun childFunction(parentObj: Parent) {
// Do whatever needed with child obj
println("child = ${parentObj.child.child?.child?.child}")
}
fun parentFunction() {
val parentObj = Parent(Child())
childFunction(parentObj)
}
fun childFunction(parentObj: Parent) {
// Do whatever needed with child obj
println("child = ${parentObj.child}")
}
data class Parent(val child: Child)
class Child
@amatkivskiy
amatkivskiy / vntu_test_enable_copy.user.js
Last active November 2, 2023 08:06
Userscript for Tampermonkey that enbles copy paste on VNTU test
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://test.vntu.edu.ua/web_thesaurus/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=hibbard.eu
// @grant none
// ==/UserScript==