예제 #1
0
        public void Should_evaluate_expression_with_user_defined_functions_and_variables()
        {
            // Given
            var context = new Context()
                          .SetVariable("G", 6.67428D)
                          .SetVariable("earth_mass", Evaluate.As <double>("5.97219 * pow(10,24)"))    // 5.97219E+24 kg
                          .SetVariable("lunar_mass", Evaluate.As <double>("7.34767309 * pow(10,22)")) // 7.34767309E+22 kg
                          .SetVariable("perigee_dist", 356700000D)                                    // moon-earth distance at perigee in m
                          .SetFunction("calc_force", args => (
                                           TypeConverter.ToNumber(args[0]) * TypeConverter.ToNumber(args[1]) / Math.Pow(TypeConverter.ToNumber(args[2]), 2)));

            // When
            var result = context.EvaluateAs <double>("G * calc_force(earth_mass, lunar_mass, perigee_dist)");

            // Than
            result.Should().Be(2.3018745174107073E+31D);
        }