Ejemplo n.º 1
0
        public async Task EvaluateAsyncShouldBeAbleToRoundtripAJsonStringOfAnArrayViaJsonStringify(
            [Frozen] IMediator mediator,
            [JintEvaluatorWithConverter] JintJavaScriptEvaluator sut,
            [StubActivityExecutionContext] ActivityExecutionContext context1,
            [StubActivityExecutionContext] ActivityExecutionContext context2)
        {
            object returnedValue = null;

            Mock.Get(mediator)
            .Setup(x => x.Publish(It.Is <EvaluatingJavaScriptExpression>(e => e.ActivityExecutionContext == context2), It.IsAny <CancellationToken>()))
            .Callback((EvaluatingJavaScriptExpression expression, CancellationToken t) => { expression.Engine.SetValue("MyVariable", returnedValue); });

            returnedValue = await sut.EvaluateAsync(@"JSON.parse(""[\""foo\"",\""bar\""]"")", typeof(object), context1);

            var result = await sut.EvaluateAsync("JSON.stringify(MyVariable)", typeof(object), context2);

            Assert.Equal(@"[""foo"",""bar""]", result);
        }
Ejemplo n.º 2
0
        public async Task EvaluateAsyncShouldBeAbleToAccessAPropertyWhichWasParsed(
            [Frozen] IMediator mediator,
            [JintEvaluatorWithConverter] JintJavaScriptEvaluator sut,
            [StubActivityExecutionContext] ActivityExecutionContext context1,
            [StubActivityExecutionContext] ActivityExecutionContext context2)
        {
            object returnedValue = null;

            Mock.Get(mediator)
            .Setup(x => x.Publish(It.Is <EvaluatingJavaScriptExpression>(e => e.ActivityExecutionContext == context2), It.IsAny <CancellationToken>()))
            .Callback((EvaluatingJavaScriptExpression expression, CancellationToken t) => { expression.Engine.SetValue("MyVariable", returnedValue); });

            returnedValue = await sut.EvaluateAsync(@"JSON.parse(""{\""foo\"":\""bar\""}"")", typeof(object), context1);

            var result = await sut.EvaluateAsync("MyVariable.foo", typeof(object), context2);

            Assert.Equal("bar", result);
        }
Ejemplo n.º 3
0
        public async Task EvaluateAsyncShouldBeAbleToEvaluateAndReturnANodaTimeObject(
            [Frozen] IMediator mediator,
            [JintEvaluatorWithConverter] JintJavaScriptEvaluator sut,
            [StubActivityExecutionContext] ActivityExecutionContext context1)
        {
            var duration1 = NodaTime.Duration.FromDays(1);
            var duration2 = NodaTime.Duration.FromHours(12);

            Mock.Get(mediator)
            .Setup(x => x.Publish(It.Is <EvaluatingJavaScriptExpression>(e => e.ActivityExecutionContext == context1), It.IsAny <CancellationToken>()))
            .Callback((EvaluatingJavaScriptExpression expression, CancellationToken t) =>
            {
                expression.Engine.SetValue("duration1", duration1);
                expression.Engine.SetValue("duration2", duration2);
            });

            var result = await sut.EvaluateAsync("duration1.Plus(duration2)", typeof(NodaTime.Duration), context1);

            Assert.Equal(NodaTime.Duration.FromHours(36), result);
        }