Skip to content

Instantly share code, notes, and snippets.

View ininmm's full-sized avatar
🎯
Focusing

ininmm

🎯
Focusing
View GitHub Profile
@ininmm
ininmm / HeaderItemDecoration.kt
Created July 3, 2020 06:59 — forked from filipkowicz/HeaderItemDecoration.kt
Item Decorator for sticky headers in Kotlin
package com.filipkowicz.headeritemdecorator
/*
solution based on - based on Sevastyan answer on StackOverflow
changes:
- take to account views offsets
- transformed to Kotlin
- now works on viewHolders
@ininmm
ininmm / SampleTest.kt
Created October 14, 2019 09:59
Refactoring Test
@Test
fun RefactoringTest() {
val ACCESS_TOKEN = "access token"
val API_RESULT_OK = 200
val service = APIService()
val result = service.getSomeData(ACCESS_TOKEN)
assertThat(result, `is`(API_RESULT_OK))
}
@ininmm
ininmm / SampleTest.kt
Created October 14, 2019 09:56
Bad naming test
@Test
fun BadNamingTest() {
val service = APIService()
val result = service.getSomeData("access token")
assertThat(result, `is`(200))
}
@ininmm
ininmm / SampleTest.kt
Created October 14, 2019 09:55
Refactoring test
fun selectMaxValue(first: Int, second: Int, third: Int): Int {
......
return 正確的最大數
}
......
@Test
fun selectMaxValue_whenInputMaxValueIntoFirstParam_thenReturnMaxValue() {
val result = chooseTheMaxValue(6, 2, 1)
assertThat(result, `is`(6))
}
@ininmm
ininmm / SampleTest.kt
Created October 14, 2019 09:54
a test for choosing the max value
fun chooseTheMaxValue(first: Int, second: Int, third: Int): Int {
// 從三個數中找出最大數
// 假設了一個故意錯誤的邏輯
if (second > third) {
return third
}
if (first > third) {
return third
@ininmm
ininmm / SearchServiceTest.kt
Created October 12, 2019 12:52
Write a api test
@ExperimentalCoroutinesApi
class SearchServiceTest {
......
@Test
fun searchRepoThenResponse() {
val query = "coil+org:coil-kt"
enqueueResponse("search-repo.json")
val response = runBlocking {
@ininmm
ininmm / SearchServiceTest.kt
Last active October 12, 2019 12:51
Create a API Test
@ExperimentalCoroutinesApi
class SearchServiceTest {
private lateinit var service: SearchService
private lateinit var mockWebServer: MockWebServer
@Before
fun setup() {
mockWebServer = MockWebServer()
@ininmm
ininmm / SampleService.kt
Created October 12, 2019 12:50
Write a sample api
interface SearchService {
@GET("search/repositories")
suspend fun searchRepo(
@Query("q", encoded = true) repo: String = "coil+org:coil-kt"
): SearchRepo
}
@Module
class NetworkModule {
@Singleton
@Provides
@ininmm
ininmm / build.gradle
Created October 12, 2019 12:49
Add Retrofit and MockWebServer libraries
dependency {
def retrofitVersion = '2.6.1'
def mockwebserverVersion = '3.8.1'
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
implementation "com.squareup.okhttp3:mockwebserver:$mockwebserverVersion"
}
@ininmm
ininmm / TitleScreenTest.kt
Created October 11, 2019 12:46
Allowing the fragment to use NavigationUI methods without crashing.
val scenario = launchFragmentInContainer {
TitleScreen().also { fragment ->
fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
Navigation.setViewNavController(
fragment.requireView(),
mockNavController
)
}
}