public void ShouldSetCorrectDeclaredType()
        {
            var target1 = new ListTarget(typeof(string), new ITarget[0], asArray: false);
            var target2 = new ListTarget(typeof(string), new ITarget[0], asArray: true);

            Assert.Equal(typeof(List <string>), target1.DeclaredType);
            //cheeky (getting bored now... sorry) - make sure the constructor property is set.
            Assert.NotNull(target1.ListConstructor);

            Assert.Equal(typeof(string[]), target2.DeclaredType);
        }
        public void ListTarget_ArrayOfStrings()
        {
            var targets         = CreateTargetContainer();
            var expectedStrings = Enumerable.Range(0, 3).Select(i => $"Item{i}");
            var target          = new ListTarget(typeof(string), expectedStrings.Select(s => Target.ForObject(s)), asArray: true);

            targets.Register(target);
            var container = CreateContainer(targets);

            var result = container.Resolve <string[]>();

            Assert.NotNull(result);
            Assert.Equal(expectedStrings, result);
        }
        public void ShouldSetProperties()
        {
            var targets = new[] { new TestTarget(typeof(string), supportsType: true) };
            var target1 = new ListTarget(typeof(string), targets, asArray: false);
            var target2 = new ListTarget(typeof(string), targets, asArray: true);

            Assert.Equal(typeof(string), target1.ElementType);
            Assert.NotNull(target1.Items);
            Assert.Collection(target1.Items, targets.Select(t => new Action <ITarget>(tt => Assert.Same(t, tt))).ToArray());
            Assert.False(target1.AsArray);

            //don't need to test the other properties of target1 again: just the AsArray property
            Assert.True(target2.AsArray);
        }
        public void ListTarget_ListOfStrings_AsEnumerable()
        {
            var targets = CreateTargetContainer();
            //enumerable of object targets
            var expectedStrings = Enumerable.Range(0, 3).Select(i => $"Item{i}");
            var target          = new ListTarget(typeof(string), expectedStrings.Select(s => Target.ForObject(s)));

            targets.Register(target, typeof(IEnumerable <string>));
            var container = CreateContainer(targets);

            var result = container.Resolve <IEnumerable <string> >();

            Assert.NotNull(result);
            Assert.Equal(expectedStrings, result);
        }
示例#5
0
        private ListTarget GetListTarget(ListResult result, IBindingMetadata metadata, KeyReader joinKey)
        {
            int bufferIndex = this.Buffer.GetListIndex(metadata, joinKey?.Reference);

            metadata = joinKey?.Target ?? metadata;

            ListTarget target = result.Targets.FirstOrDefault(t => t.Index == bufferIndex);

            if (target != null)
            {
                return(target);
            }

            target = new ListTarget()
            {
                Index    = bufferIndex,
                Variable = this.GetListVariable(metadata, joinKey),
            };

            if (joinKey == null && metadata.HasFlag(BindingMetadataFlags.Item))
            {
                target.NewList   = target.NewTarget = metadata.Parent.Composition?.Construct ?? throw BindingException.InvalidConstructor(metadata.Parent);
                target.AddMethod = metadata.Parent.Composition.Add;
            }

            if (joinKey != null)
            {
                target.NewTarget = Expression.New(target.Variable.Type);
            }

            if (target.NewTarget != null)
            {
                result.Targets.Add(target);
            }

            return(target);
        }
示例#6
0
        private JoinTarget GetJoinTarget(ListResult result, KeyReader joinKey)
        {
            if (joinKey == null)
            {
                return(null);
            }

            ListTarget list   = this.GetListTarget(result, joinKey.Target, joinKey);
            JoinTarget target = new JoinTarget()
            {
                Key    = joinKey,
                Buffer = Expression.Variable(typeof(ElasticArray), "_joinbuf"),
                Index  = this.Buffer.GetJoinIndex(joinKey.Reference),
                List   = list,
            };

            if (joinKey.Target.HasFlag(BindingMetadataFlags.Item))
            {
                target.NewList   = joinKey.Target.Parent.Composition.Construct;
                target.AddMethod = joinKey.Target.Parent.Composition.Add;
            }

            return(target);
        }