예제 #1
0
        public void UomFactorWorks()
        {
            ITermVisitor myTermVisitor = new MyTermVisitor(new UnitConverter(), new FxRateRepository(new TestFxPriceSourceStub()));
            const string inputFunction = "2*UomConvert(MT,Kg)";

            var(function, rawTerms) =
                MyGrammarExpressionEvaluator.EvaluateExpression(inputFunction);

            rawTerms.ToList().ForEach(t => t.Accept(myTermVisitor)); // hydrate
            var hydratedTerms = new List <IGrammarTerm>();

            var signal = new AutoResetEvent(false);

            var disposable = myTermVisitor.GetAllTerms().Take(1).Subscribe(terms =>
            {
                hydratedTerms.AddRange(terms);
                signal.Set();
            });

            signal.WaitOne();

            disposable.Dispose();

            var result = function.Invoke(hydratedTerms);

            Assert.That(result, Is.EqualTo(2000m));
        }
예제 #2
0
        public void BasicMathsWork(string numberExpression, decimal expectedResult)
        {
            var(function, _) = MyGrammarExpressionEvaluator.EvaluateExpression(numberExpression);
            var result = function.Invoke(new List <IGrammarTerm>());

            Assert.That(result, Is.EqualTo(expectedResult));
        }
예제 #3
0
        public void VisitNumberExpressionWorks()
        {
            const string numberExpression = "2";
            var          evaluator        = MyGrammarExpressionEvaluator.EvaluateExpression(numberExpression);
            var          result           = evaluator.Invoke(new List <ExpressionTerm>());

            Assert.That(result, Is.EqualTo(2m));
        }
예제 #4
0
        public void UomFactorWorks()
        {
            ITermVisitor myTermVisitor = new MyTermVisitor();
            const string inputFunction = "2*UomConvert(MT,Kg)";

            var(function, rawTerms) =
                MyGrammarExpressionEvaluator.EvaluateExpression(inputFunction);

            rawTerms.ToList().ForEach(t => t.Accept(myTermVisitor)); // hydrate
            var hydratedTerms = myTermVisitor.GetAllTerms();

            var result = function.Invoke(hydratedTerms);

            Assert.That(result, Is.EqualTo(2000m));
        }
예제 #5
0
        public async Task FxRateTicksWork()
        {
            // Add test price source.
            ITermVisitor myTermVisitor = new MyTermVisitor(new UnitConverter(), new FxRateRepository(new TestFxPriceSourceStub()));
            const string inputFunction = "FXRate(EURUSD)";

            var(function, rawTerms) =
                MyGrammarExpressionEvaluator.EvaluateExpression(inputFunction);

            rawTerms.ToList().ForEach(t => t.Accept(myTermVisitor)); // hydrate

            var hydratedTerms = await myTermVisitor.GetAllTerms().Take(1);

            var result = function.Invoke(hydratedTerms as IReadOnlyList <IGrammarTerm>);

            // the actual rate is not testable as is.
            // we'll come back to this hence the (within)
            Assert.That(result, Is.EqualTo(1m));
        }
        public IObservable <decimal> Price(string product)
        {
            return(Observable.Create <decimal>(obs =>
            {
                try
                {
                    // todo no need to have nested subscriptions here.
                    var disposable = new CompositeDisposable();
                    var productStream =
                        _productRepository.GetProduct(product)
                        .Subscribe(p =>
                    {
                        var(priceExpression, rawTerms) =
                            MyGrammarExpressionEvaluator.EvaluateExpression(p.PriceExpression);

                        var termVisitor = new MyTermVisitor(_unitConverter, _fxRateRepository);

                        rawTerms.ForEach(t => t.Accept(termVisitor));

                        var subscription = termVisitor.GetAllTerms().Subscribe(terms =>
                        {
                            var price = priceExpression.Invoke(terms as IReadOnlyList <IGrammarTerm>);

                            obs.OnNext(price);
                        });

                        disposable.Add(subscription);
                    });

                    disposable.Add(productStream);

                    return disposable;
                }
                catch (Exception e)
                {
                    obs.OnError(e);
                    throw;
                }
            }));
        }