Beispiel #1
0
 public void Round__Scale_Specified__Produces_Correct_Result(string a, string b)
 {
     Money18
     .Round(Money18.Parse(a), 2)
     .Should()
     .Be(b);
 }
        public async Task WhenConversionIsMade_ConversionShoudBeValid()
        {
            var currencyRateModels = new List <CurrencyRateModel>
            {
                new CurrencyRateModel {
                    BaseAsset = _token, QuoteAsset = "USD", Rate = 2M
                },
                new CurrencyRateModel {
                    BaseAsset = "USD", QuoteAsset = "AED", Rate = 3.67M
                },
                new CurrencyRateModel {
                    BaseAsset = "USD", QuoteAsset = "NZD", Rate = 1.58M
                },
                new CurrencyRateModel {
                    BaseAsset = "AED", QuoteAsset = "USD", Rate = 0.25M
                },
                new CurrencyRateModel {
                    BaseAsset = "AED", QuoteAsset = "EUR", Rate = 0.25M
                },
                new CurrencyRateModel {
                    BaseAsset = "EUR", QuoteAsset = "GBP", Rate = 0.89M
                },
                new CurrencyRateModel {
                    BaseAsset = "EUR", QuoteAsset = "BGN", Rate = 1.96M
                },
            };

            // Arrange
            _currencyConvertorClientMock.Setup(c => c.CurrencyRates.GetAllAsync())
            .ReturnsAsync(() =>
            {
                return(currencyRateModels);
            });

            // Act
            var initialAmount     = Money18.Create(200);
            var forwardConversion = await _service.CalculateConversionRate(10, 2, _token, "BGN");

            var convertedAmount = forwardConversion * initialAmount;

            var backwardConversion = await _service.CalculateConversionRate(10, 2, "BGN", _token);

            var revertedAmount = backwardConversion * convertedAmount;

            Money18 expected = (Money18)currencyRateModels[1].Rate;

            expected *= (Money18)currencyRateModels[4].Rate;
            expected *= (Money18)currencyRateModels[6].Rate;
            expected *= (Money18)(10M / 2M);

            // Assert
            Assert.Equal(expected, forwardConversion);
            // Due to the amount of operations, 18 digits after the decimal point are exceeded
            // and thus rounded, which leads to 4 to 6 digits difference between the initial and
            // the end value
            Assert.Equal(Money18.Round(initialAmount, 10), Money18.Round(revertedAmount, 10));
        }
Beispiel #3
0
        public void Round__All_MidpointRounding_Modes_Are_Supported()
        {
            foreach (var mode in Enum.GetValues(typeof(MidpointRounding)).Cast <MidpointRounding>())
            {
                // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                Action round = () => Money18.Round(0.5m, mode);

                round
                .Should()
                .NotThrow();
            }
        }