Beispiel #1
0
        protected override void Context()
        {
            _formulaMapper           = A.Fake <IFormulaMapper>();
            _dimensionMapper         = A.Fake <IDimensionMapper>();
            _usingDimensionConverter = A.Fake <IUsingDimensionConverter>();
            var dimension = A.Fake <IDimension>();

            _explicitFormula = new ExplicitFormula("A+B").WithDimension(dimension);
            A.CallTo(() => _formulaMapper.NewFormulaFor(_explicitFormula.FormulaString)).Returns(string.Empty);
            A.CallTo(() => _dimensionMapper.ConversionFactor(_formula)).Returns(1);
            _formula = _explicitFormula;
            sut      = new UsingFormulaConverter(_dimensionMapper, _formulaMapper, _usingDimensionConverter);
        }
        public void Convert(IFormula formula)
        {
            if (formula == null)
            {
                return;
            }

            //this has to be done before converting the formula
            var conversionFactor = _dimensionMapper.ConversionFactor(formula);

            _usingDimensionConverter.Convert(formula);

            if (formula.IsAnImplementationOf <DynamicFormula>())
            {
                return;
            }

            if (formula.IsAnImplementationOf <TableFormulaWithOffset>())
            {
                return;
            }

            if (formula.IsAnImplementationOf <TableFormulaWithXArgument>())
            {
                return;
            }

            foreach (var objectPath in formula.ObjectPaths)
            {
                _usingDimensionConverter.Convert(objectPath);
            }

            var constantFormula = formula as ConstantFormula;

            if (constantFormula != null)
            {
                constantFormula.Value *= conversionFactor;
                return;
            }

            var tableFormula = formula as TableFormula;

            if (tableFormula != null)
            {
                foreach (var point in tableFormula.AllPoints())
                {
                    point.Y *= conversionFactor;
                }
            }

            var explicitFormula = formula as ExplicitFormula;

            if (explicitFormula == null)
            {
                return;
            }

            var newFormula = _formulaMapper.NewFormulaFor(explicitFormula.FormulaString);

            if (string.IsNullOrEmpty(newFormula))
            {
                return;
            }

            explicitFormula.FormulaString = newFormula;
        }