public static CurrenciesFactorsPer1UnitInUsd ParseFileData(TextReader fileReader)
        {
            var factors = new CurrenciesFactorsPer1UnitInUsd();
            var reader = new CsvReader(fileReader, CreateCsvReaderConfiguration());

            while (reader.Read())
            {
                var symbol = reader.GetField(0);

                Currency currency;

                try { currency = (Currency) Enum.Parse(typeof (Currency), symbol.ToUpper()); }
                catch { continue; }

                if (currency == Currency.UNKNOWN || currency == Currency.USD)
                    continue;

                decimal factor;

                if (!Decimal.TryParse(reader.GetField(1), NumberStyles.Number, CultureInfo.InvariantCulture, out factor)
                    || factor <= 0m)
                    continue;

                factors[currency] = factor;
            }

            return factors;
        }
        public void ItemTest()
        {
            var target = new CurrenciesFactorsPer1UnitInUsd();

            target[Currency.EUR] = 1.4m;

            Assert.AreEqual(target[Currency.EUR], 1.4m);
        }
        public void CurrenciesFactorsPer1UsdConstructorTest()
        {
            var target = new CurrenciesFactorsPer1UnitInUsd();

            Assert.AreEqual(target[Currency.UNKNOWN], 1m);
            Assert.AreEqual(target[Currency.USD], 1m);
            Assert.AreEqual(target[Currency.EUR], 1m);
            Assert.AreEqual(target[Currency.JPY], 1m);
        }
        public void CloneTest()
        {
            var target = new CurrenciesFactorsPer1UnitInUsd();

            target[Currency.EUR] = 1.4m;
            target[Currency.JPY] = 2m;

            var actual = (CurrenciesFactorsPer1UnitInUsd) target.Clone();

            Assert.AreEqual(target[Currency.EUR], actual[Currency.EUR]);
            Assert.AreEqual(target[Currency.JPY], actual[Currency.JPY]);
        }
        public static string CreateFileData(CurrenciesFactorsPer1UnitInUsd factors)
        {
            var builder = new StringBuilder();

            foreach (var factor in factors)
            {
                if (builder.Length > 0)
                    builder.AppendLine();
                builder.AppendFormat("{0},{1}", factor.Key, factor.Value.ToString(CultureInfo.InvariantCulture));
            }

            return builder.ToString();
        }
        public void GetFactorTest()
        {
            var factorsPer1Usd = new CurrenciesFactorsPer1UnitInUsd();

            factorsPer1Usd[Currency.EUR] = 1.4m;
            factorsPer1Usd[Currency.JPY] = 2.8m;

            var target = new DefaultCurrenciesFactorProvider(factorsPer1Usd);

            Assert.AreEqual(0.5m, target.GetFactor(Currency.EUR, Currency.JPY));
            Assert.AreEqual(2m, target.GetFactor(Currency.JPY, Currency.EUR));
            Assert.AreEqual(1m/2.8m, target.GetFactor(Currency.USD, Currency.JPY));
            Assert.AreEqual(1m, target.GetFactor(Currency.USD, Currency.USD));
            Assert.AreEqual(1m, target.GetFactor(Currency.JPY, Currency.JPY));
            Assert.AreEqual(1m, target.GetFactor(Currency.USD, Currency.UNKNOWN));
        }
        public static void MyClassInitialize(TestContext testContext)
        {
            var strategy =
                new FakeLoadCurrenciesFactorsUpdateStrategy(new List<KeyValuePair<Currency, decimal>>
                                                                {
                                                                    new KeyValuePair<Currency, decimal>(Currency.EUR, 1.4m),
                                                                    new KeyValuePair<Currency, decimal>(Currency.JPY, 1.5m),
                                                                    new KeyValuePair<Currency, decimal>(Currency.UAH, 0.2m),
                                                                    new KeyValuePair<Currency, decimal>(Currency.RUB, 0.11m),
                                                                    new KeyValuePair<Currency, decimal>(Currency.CHF, 2m)
                                                                });

            Factors = ((DefaultCurrenciesFactorProvider)strategy.CreateAndInitProvider()).GetFactorsCopy();

            MoneyHandlerSettings.Init(new MoneyHandlerSettings(strategy, Currency.USD));
        }
        public void CountTest()
        {
            var target = new CurrenciesFactorsPer1UnitInUsd();

            Assert.AreEqual(target.Count, Enum.GetValues(typeof (Currency)).Length);
        }
 public FakeCurrenciesFactorsLoader(CurrenciesFactorsPer1UnitInUsd factors)
 {
     _factors = factors;
 }
        private static CurrenciesFactorsPer1UnitInUsd ParseLoadedData(string yahooData)
        {
            var factors = new CurrenciesFactorsPer1UnitInUsd();
            using (var stringReader = new StringReader(yahooData))
            {
                var reader = new CsvReader(stringReader, CreateCsvReaderConfiguration());

                while (reader.Read())
                {
                    var symbol = reader.GetField(0);

                    if (symbol.Length < 6)
                        continue;

                    symbol = symbol.Substring(0, symbol.Length - 5).ToLower();

                    Currency currency;

                    try { currency = (Currency) Enum.Parse(typeof (Currency), symbol.ToUpper()); }
                    catch { continue; }

                    if (currency == Currency.UNKNOWN || currency == Currency.USD)
                        continue;

                    decimal factor;

                    if (!Decimal.TryParse(reader.GetField(1), NumberStyles.Number, CultureInfo.InvariantCulture,
                                          out factor) || factor <= 0m)
                        continue;

                    factors[currency] = factor;
                }
            }
            return factors;
        }
        private void SetCurrencyFactors2Cache(CurrenciesFactorsPer1UnitInUsd factors)
        {
            if (factors == null)
                return;

            var setCacheData = SetFactorsData;

            if (setCacheData == null)
                return;

            setCacheData(Encoding.UTF8.GetBytes(CsvFileCurrenciesFactorsLoader.CreateFileData(factors)));
        }
 public void UpdateFactors(CurrenciesFactorsPer1UnitInUsd factors)
 {
     _factors = factors;
 }
 public DefaultCurrenciesFactorProvider(CurrenciesFactorsPer1UnitInUsd factors)
 {
     UpdateFactors(factors);
 }
 public CurrenciesFactorsLoaderCalbackState(CurrenciesFactorsPer1UnitInUsd factors, Exception error, bool isCancelled)
 {
     _factors = factors;
     _error = error;
     _isCancelled = isCancelled;
 }
 public FakeLoadCurrenciesFactorsUpdateStrategy()
 {
     _factors = new CurrenciesFactorsPer1UnitInUsd();
 }