Пример #1
0
        public async Task AsyncMethodExceptionPassedAlong()
        {
            var methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>()
            {
                { 1, ObjectDescriptor.Create().WithMethods(new List <MethodDescriptor>()
                    {
                        MethodDescriptor.Create()
                        .WithId(2)
                        .WithParameterCount(1)
                        .WithParameters(new List <MethodParameterDescriptor>
                        {
                            new MethodParameterDescriptor(typeof(string), false)
                        })
                        .WithExecute((o, a) => Task.FromException(new NotSupportedException("Error")))
                        .Get()
                    }).WithId(1).Get() }
            }), context => { });

            const string Value  = "expected";
            var          result = await methodExecutor.Execute(new MethodExecution <object>()
            {
                ObjectId   = 1,
                MethodId   = 2,
                Parameters = new object[] { Value }
            });

            Assert.Equal("Error", result.Error);
            Assert.False(result.Success);
        }
Пример #2
0
        private static void PopulateHash(HashParameterDictionary hash, object from)
        {
            var descriptor = ObjectDescriptor.Create(from);

            if (descriptor == ObjectDescriptor.Empty)
            {
                return;
            }

            var accessor   = descriptor.MemberAccessor;
            var properties = descriptor.GetProperties(descriptor, from);
            var enumerator = properties.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var segment = ChainSegment.Create(enumerator.Current);
                if (hash.ContainsKey(segment))
                {
                    continue;
                }
                if (!accessor.TryGetValue(@from, segment, out var value))
                {
                    continue;
                }
                hash[segment] = value;
            }
        }
Пример #3
0
        public async Task MethodParameterTypeMismatchThrows()
        {
            var methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>()
            {
                { 1, ObjectDescriptor.Create().WithMethods(new List <MethodDescriptor>()
                    {
                        MethodDescriptor.Create().WithId(2).WithParameterCount(1).WithParameters(new List <MethodParameterDescriptor>
                        {
                            new MethodParameterDescriptor(typeof(string), false)
                        }).Get()
                    }).WithId(1).Get() }
            }), context => { context.ObjectValue = context.NativeValue; });

            var result = await methodExecutor.Execute(new MethodExecution <object>()
            {
                ObjectId   = 1,
                MethodId   = 2,
                Parameters = new object[] { 1 }
            });

            Assert.Equal("Parameter mismatch.", result.Error);
            Assert.False(result.Success);
        }
Пример #4
0
        public async Task MethodCallExceptionPassedAlong()
        {
            var method = typeof(SimpleClassWithExceptions).GetMethod("ThrowException");

            var methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>
            {
                { 1, ObjectDescriptor.Create()
                  .WithObject(new SimpleClassWithExceptions())
                  .WithMethods(new List <MethodDescriptor>
                    {
                        MethodDescriptor.Create()
                        .WithId(2)
                        .WithParameterCount(0)
                        .WithParameters(new List <MethodParameterDescriptor>
                        {
                        })
                        .WithExecute((o, a) => method.Invoke(o, a))
                        .Get()
                    }).WithId(1).Get() }
            }), context => { });

            var result = await methodExecutor.Execute(new MethodExecution <object>
            {
                ObjectId   = 1,
                MethodId   = 2,
                Parameters = new object[] { }
            });

            Assert.Equal("Error", result.Error);
            Assert.False(result.Success);
        }
Пример #5
0
        public void GetExecptionPassedAlong()
        {
            const string message = "message";

            var propertyExecutor = new PropertyExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>()
            {
                { 1, ObjectDescriptor.Create().WithProperties(new List <PropertyDescriptor>()
                    {
                        PropertyDescriptor.Create().WithId(2).WithGetter(o => throw new Exception(message)).Get()
                    }).WithId(1).Get() }
Пример #6
0
        private BindingContext()
        {
            InlinePartialTemplates = new CascadeIndex <string, Action <EncodedTextWriter, BindingContext>, StringEqualityComparer>(new StringEqualityComparer(StringComparison.OrdinalIgnoreCase));

            Bag = new CascadeIndex <string, object, StringEqualityComparer>(new StringEqualityComparer(StringComparison.OrdinalIgnoreCase));
            ContextDataObject = new FixedSizeDictionary <ChainSegment, object, ChainSegment.ChainSegmentEqualityComparer>(16, 7, ChainSegment.EqualityComparer);
            BlockParamsObject = new FixedSizeDictionary <ChainSegment, object, ChainSegment.ChainSegmentEqualityComparer>(16, 7, ChainSegment.EqualityComparer);

            Descriptor = new DeferredValue <BindingContext, ObjectDescriptor>(this, context =>
            {
                return(ObjectDescriptor.Create(context.Value));
            });
        }
Пример #7
0
        public ObjectDescriptor AnalyzeObject(object o, AnalyzeOptions options)
        {
            var type    = o.GetType();
            var builder = ObjectDescriptor.Create()
                          .WithId(idGenerator.GetNextId())
                          .WithMethods(methodAnalyzer.AnalyzeMethods(type))
                          .WithName(options.Name)
                          .WithObject(o);

            if (options.AnalyzeProperties)
            {
                builder.WithProperties(propertyAnalyzer.AnalyzeProperties(type, o, options.ExtractPropertyValues));
            }

            return(builder.Get());
        }
Пример #8
0
        public async Task MethodCalled()
        {
            var methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>()
            {
                {
                    1, ObjectDescriptor.Create().WithMethods(new List <MethodDescriptor>()
                    {
                        MethodDescriptor.Create()
                        .WithId(2)
                        .WithParameterCount(1)
                        .WithParameters(new List <MethodParameterDescriptor>
                        {
                            new MethodParameterDescriptor(typeof(string), false)
                        })
                        .WithExecute((o, a) => a[0] as string)
                        .Get()
                    }).WithId(1).Get()
                }
            }), context =>
            {
                if (context.Direction == ObjectBindingDirection.In)
                {
                    context.ObjectValue = context.NativeValue;
                }
                else
                {
                    context.NativeValue = context.ObjectValue;
                }
            });

            const string Value       = "expected";
            const int    ExecutionId = 3;
            var          result      = await methodExecutor.Execute(new MethodExecution <object>()
            {
                ExecutionId = ExecutionId,
                ObjectId    = 1,
                MethodId    = 2,
                Parameters  = new object[] { Value }
            });

            Assert.Equal(Value, result.Result as string);
            Assert.Equal(ExecutionId, result.ExecutionId);
            Assert.True(result.Success);
        }
Пример #9
0
 public void ThrowsForNonExistentPropertyWhenSet()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         var propertyExecutor = new PropertyExecutor <object>(
             new ReadOnlyDictionary <long, ObjectDescriptor>(
                 new Dictionary <long, ObjectDescriptor>()
         {
             { 1, ObjectDescriptor.Create().WithProperties(new List <PropertyDescriptor>()).WithId(1).Get() }
         }), context => { });
         propertyExecutor.Execute(new PropertySetExecution <object>()
         {
             ObjectId   = 1,
             PropertyId = 2
         });
     });
 }
Пример #10
0
        internal void SetDataObject(object data)
        {
            if (data == null)
            {
                return;
            }

            var objectDescriptor = ObjectDescriptor.Create(data, Configuration);
            var objectAccessor   = new ObjectAccessor(data, objectDescriptor);

            foreach (var property in objectAccessor.Properties)
            {
                var value = objectAccessor[property];
                RootDataObject.AddOrReplace(property, value, out _);
                ContextDataObject.AddOrReplace(property, value, out _);
            }
        }
Пример #11
0
        public async Task NonExistentMethodError()
        {
            var methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>()
            {
                { 1, ObjectDescriptor.Create().WithMethods(new List <MethodDescriptor>()).WithId(1).Get() }
            }), context => { });
            var result = await methodExecutor.Execute(new MethodExecution <object>()
            {
                ObjectId = 1,
                MethodId = 2
            });


            Assert.Equal("Invalid function.", result.Error);
            Assert.False(result.Success);
        }
Пример #12
0
        public async Task ReturnValueBindingSetIfNecessary()
        {
            const string Value          = "expected";
            var          methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>
            {
                {
                    1, ObjectDescriptor.Create().WithMethods(new List <MethodDescriptor>
                    {
                        MethodDescriptor.Create()
                        .WithId(2)
                        .WithParameterCount(1)
                        .WithBindValue(new BindValueAttribute())
                        .WithResultType(typeof(string))
                        .WithParameters(new List <MethodParameterDescriptor>
                        {
                        })
                        .WithExecute((o, a) => Value)
                        .Get()
                    }).WithId(1).Get()
                }
            }), context =>
            {
                if (context.Direction == ObjectBindingDirection.In)
                {
                    context.ObjectValue = context.NativeValue;
                }
                else if (context.BindValue != null && context.ObjectValue == Value)
                {
                    context.NativeValue = context.ObjectValue;
                }
            });

            var result = await methodExecutor.Execute(new MethodExecution <object>()
            {
                ExecutionId = 3,
                ObjectId    = 1,
                MethodId    = 2,
                Parameters  = new object[] { Value }
            });

            Assert.Equal(Value, result.Result as string);
        }
Пример #13
0
        public async Task MethodParameterCountMismatchThrows()
        {
            var methodExecutor = new MethodExecutor <object>(
                new ReadOnlyDictionary <long, ObjectDescriptor>(
                    new Dictionary <long, ObjectDescriptor>()
            {
                { 1, ObjectDescriptor.Create().WithMethods(new List <MethodDescriptor>()
                    {
                        MethodDescriptor.Create().WithId(2).WithParameterCount(2).Get()
                    }).WithId(1).Get() }
            }), context => { });

            var result = await methodExecutor.Execute(new MethodExecution <object>()
            {
                ObjectId   = 1,
                MethodId   = 2,
                Parameters = new object[] { }
            });

            Assert.Equal("Parameter count mismatch.", result.Error);
            Assert.False(result.Success);
        }
 public LayoutViewModel(string body, object value)
 {
     _body            = body;
     _value           = value;
     _valueDescriptor = ObjectDescriptor.Create(value);
 }
Пример #15
0
        private BindingContext()
        {
            InlinePartialTemplates = new CascadeDictionary <string, Action <TextWriter, object> >();

            ContextDataObject = new FixedSizeDictionary <ChainSegment, object, ChainSegment.ChainSegmentEqualityComparer>(16, 7, ChainSegment.EqualityComparer);
            BlockParamsObject = new FixedSizeDictionary <ChainSegment, object, ChainSegment.ChainSegmentEqualityComparer>(16, 7, ChainSegment.EqualityComparer);

            _objectDescriptor = new DeferredValue <BindingContext, ObjectDescriptor>(this, context => ObjectDescriptor.Create(context.Value, context.Configuration));

            Data = new DataValues(this);
        }