Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Arun ArunYogeshwaran

🏠
Working from home
  • Zenjob. Ex-VMware
  • Bangalore
View GitHub Profile
@ArunYogeshwaran
ArunYogeshwaran / StateVsInteractionTesting.kt
Last active February 6, 2023 13:12
An example of testing the interaction vs testing the state
View StateVsInteractionTesting.kt
// Unit under test
fun toggleFavoriteForJob(job: Job) {
if (job.isFavorite) {
cache.unfavorite(job)
} else {
cache.favorite(job)
}
}
@ArunYogeshwaran
ArunYogeshwaran / AudioPlayer.kt
Created February 5, 2023 22:27
An example of testing methods vs behaviours
View AudioPlayer.kt
fun playArtist(artist: Artist) {
if (artist != Artist.TAYLOR_SWIFT) {
return IllegalArgException()
}
addToRecentlyPlayed(artist)
player.play(artist.getRandomSong())
}
@ArunYogeshwaran
ArunYogeshwaran / DescriptiveTestNameExample.kt
Last active February 5, 2023 22:03
Example of a unit test with a descriptive name
View DescriptiveTestNameExample.kt
// Unit under test
fun fetchJobs(user: User) : List<Job> {
if (user.isRegistered()) {
return emptyList()
} else {
val jobs = jobsApi.getJobs(user)
return jobs
}
}
@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
View ClearUnitTest
// 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 / FriendReferral.kt
Created February 5, 2023 21:49
An example of using the public APIs to test
View FriendReferral.kt
fun referFriend(userId: String, referral: Referral) {
if (isReferralValid(referral)) {
processReferral(userId)
}
}
private fun isReferralValid(referral: Referral): Boolean {
// Checks the validity of the referral
}
@ArunYogeshwaran
ArunYogeshwaran / SealedClassObserver.kt
Last active October 12, 2021 14:59
Example of a View Class observing Sealed Class LiveData from ViewModel
View SealedClassObserver.kt
val viewModel: AuthViewMode by viewModels()
private fun observeState() {
viewModel.state.observe(
this,
{ uiState ->
handleUiState(uiState)
}
)
}
@ArunYogeshwaran
ArunYogeshwaran / UIState.kt
Last active October 12, 2021 14:53
Sealed class representing all the possible UI states
View UIState.kt
/**
* Represents the UI state of a long running operation.
*/
sealed class UIState<out Int> {
/**
* Indicates the operation succeeded.
*/
object Success : UIState<Nothing>()
/**
@ArunYogeshwaran
ArunYogeshwaran / SealedClassViewModel.kt
Last active October 12, 2021 14:52
Sealed class usage within LiveData in the ViewModel class
View SealedClassViewModel.kt
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)