Skip to content

Instantly share code, notes, and snippets.

import java.util.*
import kotlin.math.pow
/**
* @author Preyea R. Regmi
* Entry point to the division operation
* 1) Takes input from CLI as string and parse into decimal format.
* 2) Converts the input and divisor into binary string
* 3) Performs division on binary representation
* 4) Prints Quotient and Remainder in both binary and decimal format
test(
"Fetch loan should not fail with any exception when mocked with success response",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedSucessRateFetchRepository(LoanDetailDTO(3)));
fetchLoanUseCase.execute(LoanParamRequest(100, 2), (data) {},
expectAsync1((error) {
expect(error, isNot(isInstanceOf<UseCaseNotImplementedException>()),
class LoanDetailResponse {
final String _emi;
LoanDetailResponse(this._emi);
static from(EMICalculator emiCalculator) {
return LoanDetailResponse(emiCalculator.calculateEMI().toString());
}
}
test(
"EMI must be 150391.64490861617 for amount = 10,000, rate = 15%, time =24 months",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedSucessRateFetchRepository(LoanDetailDTO("15")));
fetchLoanUseCase.execute(
LoanParamRequest(10000, 24), expectAsync1((data) {
expect("150391.64490861617",equals( data._emi),reason: "Emi calculation invalid");
//Domain Entity responsible for calculating EMI for given params.
class EMICalculator {
final double amount, rate, time;
EMICalculator(this.amount, this.rate, this.time){
if(amount<0)
throw ArgumentError("Amount cannot be less than zero");
if(rate<0)
throw ArgumentError("Rate cannot be less than zero");
if(time<0)
test(
"Use case must also fail when fetch loan fails from network",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedFailedRateFetchRepository(RateFetchFailType.NETWORK));
fetchLoanUseCase.execute(LoanParamRequest(100, 2), (data) {
}, expectAsync1((error) {
expect(error, isNot(isInstanceOf<RateFetchFailException>()),
class FetchLoanDetail extends UseCase<LoanParamRequest, LoanDetailResponse> {
final ILoanRepository _loanRepository;
FetchLoanDetail(this._loanRepository);
@override
Stream<LoanDetailResponse> buildUseCase(LoanParamRequest param) {
return _loanRepository
.getLoanFromServer(param.amount, param.time)
.asStream()
test(
"Fetch loan should not fail with any exception when mocked with success response",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedSucessRateFetchRepository(LoanDetailDTO(3)));
fetchLoanUseCase.execute(LoanParamRequest(100, 2), (data) {
}, expectAsync1((error) {
expect(error, isNot(isInstanceOf<UseCaseNotImplementedException>()),
class MockedSucessRateFetchRepository extends ILoanRepository {
final LoanDetailDTO _mockedResponse;
MockedSucessRateFetchRepository(this._mockedResponse);
Future<LoanDetailDTO> getLoanFromServer(double amount, double time) {
return Future.value(this._mockedResponse);
}
}
class LoanParamRequest {
final double amount, time;
LoanParamRequest(this.amount, this.time);
}
class LoanDetailResponse {
String? _emi;
}