Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.4 kB
2
Indexable
Never
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('calculateMaxTransferByPercentage', () {
    test('returns correct result when feeInPercentage is 0', () {
      final maxTransfer = calculateMaxTransferByPercentage(
        maxBalance: 100,
        feeInPercentage: 0,
      );
      expect(maxTransfer, equals(100.0));
    });

    test('returns correct result when feeInPercentage is 50', () {
      final maxTransfer = calculateMaxTransferByPercentage(
        maxBalance: 100,
        feeInPercentage: 50,
      );
      expect(maxTransfer, equals(66.66));
    });

    test('returns correct result when feeInPercentage is 100', () {
      final maxTransfer = calculateMaxTransferByPercentage(
        maxBalance: 100,
        feeInPercentage: 100,
      );
      expect(maxTransfer, equals(50.0));
    });

    test('returns correct result when maxBalance is 0', () {
      final maxTransfer = calculateMaxTransferByPercentage(
        maxBalance: 0,
        feeInPercentage: 50,
      );
      expect(maxTransfer, equals(0.0));
    });
  });

  group('calculateMaxTransferByFlat', () {
    test('returns correct result when feeInFlat is 0', () {
      final maxTransfer = calculateMaxTransferByFlat(
        maxBalance: 100,
        feeInFlat: 0,
      );
      expect(maxTransfer, equals(100.0));
    });

    test('returns correct result when feeInFlat is 50', () {
      final maxTransfer = calculateMaxTransferByFlat(
        maxBalance: 100,
        feeInFlat: 50,
      );
      expect(maxTransfer, equals(50.0));
    });

    test('returns correct result when maxBalance is 0', () {
      final maxTransfer = calculateMaxTransferByFlat(
        maxBalance: 0,
        feeInFlat: 50,
      );
      expect(maxTransfer, equals(-50.0));
    });
  });
}


/// Helper function which use to calculate max transfer available from  total balance
/// by percentage.
/// [feeInPercentage] should be 1-100
///
double calculateMaxTransferByPercentage(
    {required double maxBalance, required double feeInPercentage}) {
  return (((maxBalance * 100) / (100 + feeInPercentage)) * 100).truncate() /
      100;
}

/// Helper function which use to calculate max transfer available from total balance
///
double calculateMaxTransferByFlat(
    {required double maxBalance, required double feeInFlat}) {
  return maxBalance - feeInFlat;
}