This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Represents the UI state of a long running operation. | |
*/ | |
sealed class UIState<out Int> { | |
/** | |
* Indicates the operation succeeded. | |
*/ | |
object Success : UIState<Nothing>() | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val viewModel: AuthViewMode by viewModels() | |
private fun observeState() { | |
viewModel.state.observe( | |
this, | |
{ uiState -> | |
handleUiState(uiState) | |
} | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unit under test | |
fun fetchJobs(user: User) : List<Job> { | |
if (user.isRegistered()) { | |
return emptyList() | |
} else { | |
val jobs = jobsApi.getJobs(user) | |
return jobs | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unit under test | |
fun toggleFavoriteForJob(job: Job) { | |
if (job.isFavorite) { | |
cache.unfavorite(job) | |
} else { | |
cache.favorite(job) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun referFriend(userId: String, referral: Referral) { | |
if (isReferralValid(referral)) { | |
processReferral(userId) | |
} | |
} | |
private fun isReferralValid(referral: Referral): Boolean { | |
// Checks the validity of the referral | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun playArtist(artist: Artist) { | |
if (artist != Artist.TAYLOR_SWIFT) { | |
return IllegalArgException() | |
} | |
addToRecentlyPlayed(artist) | |
player.play(artist.getRandomSong()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
OlderNewer