Skip to content

Instantly share code, notes, and snippets.

View ArunYogeshwaran's full-sized avatar
🌎
Office

Arun ArunYogeshwaran

🌎
Office
  • Zenjob. Ex-VMware
  • Berlin
View GitHub Profile
@ArunYogeshwaran
ArunYogeshwaran / StateVsInteractionTesting.kt
Last active February 6, 2023 13:12
An example of testing the interaction vs testing the state
// Unit under test
fun toggleFavoriteForJob(job: Job) {
if (job.isFavorite) {
cache.unfavorite(job)
} else {
cache.favorite(job)
}
}
@ArunYogeshwaran
ArunYogeshwaran / ClearUnitTest
Last active February 5, 2023 21:56
An example of a clear test for a system which recommends the most appropriate jobs based on machine learning models
// A test with relevant details right in the test itself
@Test
fun `get ML recommended jobs with new user expect predefined list`() {
// Test arrangements and setup
val engine = getRecommendationEngine()
val jobs = engine.getJobs(new User(UserStatus.FAIRLY_NEW, "testId", "Test User Name"))
assertThat(jobs).isEqualTo(expectedJobs)
}
@ArunYogeshwaran
ArunYogeshwaran / DescriptiveTestNameExample.kt
Last active February 5, 2023 22:03
Example of a unit test with a descriptive name
// Unit under test
fun fetchJobs(user: User) : List<Job> {
if (user.isRegistered()) {
return emptyList()
} else {
val jobs = jobsApi.getJobs(user)
return jobs
}
}
@ArunYogeshwaran
ArunYogeshwaran / SealedClassObserver.kt
Last active October 12, 2021 14:59
Example of a View Class observing Sealed Class LiveData from ViewModel
val viewModel: AuthViewMode by viewModels()
private fun observeState() {
viewModel.state.observe(
this,
{ uiState ->
handleUiState(uiState)
}
)
}
@ArunYogeshwaran
ArunYogeshwaran / SealedClassViewModel.kt
Last active October 12, 2021 14:52
Sealed class usage within LiveData in the ViewModel class
private val repository = UserRepository()
private val _state = MutableLiveData<UIState<Int>?>()
val state: LiveData<UIState<Int>?> = _state
fun evaluatePassword(password: Long) {
_state.value = UIState.Loading(R.string.evaluating_creds)
viewmodelScope.launch(Dispatchers.IO) {
// This is a long-running operation making network call
val isSuccess = repository.authenticatUser(password)
@ArunYogeshwaran
ArunYogeshwaran / UIState.kt
Last active October 12, 2021 14:53
Sealed class representing all the possible UI states
/**
* Represents the UI state of a long running operation.
*/
sealed class UIState<out Int> {
/**
* Indicates the operation succeeded.
*/
object Success : UIState<Nothing>()
/**