コード例 #1
0
 public void AddPacket()
 {
     Assert.True(ProtocolBuilder
                 .Create <int>()
                 .RegisterPacket((a) => { })
                 .Build(nullBuilder).Packets.Length == 1);
 }
コード例 #2
0
        public void WriteField()
        {
            var def = ProtocolBuilder
                      .Create <int>()
                      .RegisterDefaultAccessors()
                      .RegisterPacket((a) => a
                                      .Id(0x00)
                                      .BindingType <SamplePacket>()
                                      .BindField(nameof(SamplePacket.Field))
                                      ).Build(new ExpressionDelegateBuilder());

            var packetDef = def.Packets.First();

            var vals = new byte[1];

            rnd.NextBytes(vals);
            var val = vals[0];

            using (var stream = new MemoryStream())
            {
                var packet = new SamplePacket()
                {
                    Field = val,
                };
                packetDef.WriteDelegate(stream, packet);
                stream.Position = 0;
                Assert.AreEqual(val, (byte)stream.ReadByte());
            }
        }
コード例 #3
0
        public void ReadProperty()
        {
            var def = ProtocolBuilder
                      .Create <int>()
                      .RegisterDefaultAccessors()
                      .RegisterPacket((a) => a
                                      .Id(0x00)
                                      .BindingType <SamplePacket>()
                                      .BindProperty(nameof(SamplePacket.Property))
                                      ).Build(new ExpressionDelegateBuilder());

            var packetDef = def.Packets.First();

            var vals = new byte[1];

            rnd.NextBytes(vals);
            var val = vals[0];

            using (var stream = new MemoryStream(new byte[] { val }))
            {
                var readPacket = (SamplePacket)packetDef.ReadDelegate(stream);
                Assert.NotNull(readPacket);
                Assert.AreEqual(val, readPacket.Property);
            }
        }
コード例 #4
0
 public void RegisterAccessorCannotConvertAccessorTypeException()
 {
     Assert.Throws <ArgumentException>(() =>
                                       ProtocolBuilder
                                       .Create <int>() // Only "Implementing" Byte
                                       .RegisterAccessor(typeof(Int32), typeof(NullAccessor)));
 }
コード例 #5
0
 public void RegisterAccessorAccessorNullException()
 {
     Assert.Throws <ArgumentNullException>(() =>
                                           ProtocolBuilder
                                           .Create <int>()
                                           .RegisterAccessor(typeof(Byte), null));
 }
コード例 #6
0
 public void RegisterAccessorInvalidAccessorTypeException()
 {
     Assert.Throws <ArgumentException>(() =>
                                       ProtocolBuilder
                                       .Create <int>() // Clearly not implementing INetworkAccessor
                                       .RegisterAccessor(typeof(Byte), typeof(Byte)));
 }
コード例 #7
0
 public void BindingTypeNullException()
 {
     Assert.Throws <ArgumentNullException>(() =>
                                           ProtocolBuilder
                                           .Create <int>()
                                           .RegisterPacket((a) => a
                                                           .BindingType(null)));
 }
コード例 #8
0
 public void IdWrongTypeException()
 {
     Assert.Throws <ArgumentException>(() =>
                                       ProtocolBuilder
                                       .Create <int>()
                                       .RegisterPacket((a) => a
                                                       .Id("this is the wrong type")));
 }
コード例 #9
0
 public void SetBindingTypeBeforePropertyBinding()
 {
     Assert.Throws <InvalidOperationException>(() =>
                                               ProtocolBuilder
                                               .Create <int>()
                                               .RegisterPacket((a) => a
                                                               .BindProperty(nameof(SamplePacket.Field))));
 }
コード例 #10
0
        public void IdSaved()
        {
            int Id = rnd.Next();

            Assert.AreEqual(Id, ProtocolBuilder
                            .Create <int>()
                            .RegisterPacket((a) => a
                                            .Id(Id)).Build(nullBuilder).Packets.First().Id);
        }
コード例 #11
0
 public void PropertyBindingFieldException()
 {
     Assert.Throws <ArgumentException>(() => ProtocolBuilder
                                       .Create <int>()
                                       .RegisterPacket((a) => a
                                                       .Id(0)
                                                       .BindingType <SamplePacket>()
                                                       .BindProperty(nameof(SamplePacket.Field))
                                                       ));
 }
コード例 #12
0
        public void BindingType()
        {
            var def = ProtocolBuilder
                      .Create <int>()
                      .RegisterPacket((a) => a
                                      .Id(0)
                                      .BindingType <SamplePacket>()
                                      ).Build(nullBuilder);

            Assert.True(def.Packets.First().Binding == typeof(SamplePacket));
        }
コード例 #13
0
 public static ProtocolDefinition GetNetworkDefinition()
 => ProtocolBuilder
 .Create <int>()
 .LengthBehaviour(new DefaultDynamicInt32LengthBehaviour())
 .UseIds(new DefaultIdHeader(), new DelegateIdMapper((id, protocol)
                                                     => protocol.Packets.First(x => ((int)x.Id) == (int)id),
                                                     (def, protocol) => def.Id))
 .RegisterDefaultAccessors()
 .RegisterPacket((a) => a
                 .Id(0x00)
                 .BindingType <SingleByteTransferPacket>()
                 .BindField(nameof(SingleByteTransferPacket.Data))
                 ).Build(new ExpressionDelegateBuilder());
コード例 #14
0
        public void PropertyBinding()
        {
            var def = ProtocolBuilder
                      .Create <int>()
                      .RegisterPacket((a) => a
                                      .Id(0)
                                      .BindingType <SamplePacket>()
                                      .BindProperty(nameof(SamplePacket.Property))
                                      ).Build(nullBuilder);

            Assert.True(def.Packets.First().Bindings.Length == 1, "Wrong Length");
            Assert.True(def.Packets.First().Bindings.First() == typeof(SamplePacket).GetProperty(nameof(SamplePacket.Property)), "Binding Type Wrong");
        }
コード例 #15
0
 public void CreateExplicit()
 {
     Assert.AreEqual(typeof(Int32), ProtocolBuilder.Create(typeof(Int32)).Build(nullBuilder).IdType);
 }
コード例 #16
0
 public void CreateGeneric()
 {
     Assert.AreEqual(typeof(Int32), ProtocolBuilder.Create <Int32>().Build(nullBuilder).IdType);
 }
コード例 #17
0
 public void IdTypeNullException()
 {
     Assert.Throws <ArgumentNullException>(() => ProtocolBuilder.Create(null));
 }