Skip to content

Instantly share code, notes, and snippets.

View ArunYogeshwaran's full-sized avatar
🌎
Office

Arun ArunYogeshwaran

🌎
Office
View GitHub Profile
@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>()
/**
@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 / 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 / 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 / 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 / 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 / FriendReferral.kt
Created February 5, 2023 21:49
An example of using the public APIs to test
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 / AudioPlayer.kt
Created February 5, 2023 22:27
An example of testing methods vs behaviours
fun playArtist(artist: Artist) {
if (artist != Artist.TAYLOR_SWIFT) {
return IllegalArgException()
}
addToRecentlyPlayed(artist)
player.play(artist.getRandomSong())
}
@ArunYogeshwaran
ArunYogeshwaran / DescriptiveTestNameExample.kt
Created July 6, 2023 13:55
The example below shows a simple system that fetches jobs for a user who can be either registered or unregistered.
// Unit under test.
fun fetchJobs(user: User) : List<Job> {
 if (user.isRegistered()) {
 return emptyList()
 } else {
 val jobs = jobsApi.getJobs(user)
 return jobs
 }
}
// A test with a non-descriptive name.
// A test with relevant details 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) 
}