Esempio n. 1
0
        public void TestFromSameNamespace()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  public enum A : byte {
  }

  [BinarySchema]
  public partial class Wrapper : IBiSerializable {
    public A Field { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class Wrapper {
    public void Read(EndianBinaryReader er) {
      this.Field = (A) er.ReadByte();
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class Wrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteByte((byte) this.Field);
    }
  }
}
");
        }
        [Test] public void TestAlign()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.ignore;

namespace foo.bar {
  [BinarySchema]
  public partial class IgnoreWrapper : IBiSerializable {
    [Ignore]
    public byte Field { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class IgnoreWrapper {
    public void Read(EndianBinaryReader er) {
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class IgnoreWrapper {
    public void Write(EndianBinaryWriter ew) {
    }
  }
}
");
        }
Esempio n. 3
0
        public void TestConstLengthString()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class StringWrapper {
    [StringLengthSource(3)]
    public string Field;
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class StringWrapper {
    public void Read(EndianBinaryReader er) {
      this.Field = er.ReadString(3);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class StringWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteStringWithExactLength(this.Field, 3);
    }
  }
}
");
        }
Esempio n. 4
0
        public void TestConstString()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class StringWrapper {
    public readonly string Field = ""foo"";
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class StringWrapper {
    public void Read(EndianBinaryReader er) {
      er.AssertString(this.Field);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class StringWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteString(this.Field);
    }
  }
}
");
        }
        public void TestGenericStructure()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class GenericWrapper<T> : IBiSerializable where T : IBiSerializable, new() {
    public T Data { get; } = new();
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class GenericWrapper<T> {
    public void Read(EndianBinaryReader er) {
      this.Data.Read(er);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class GenericWrapper<T> {
    public void Write(EndianBinaryWriter ew) {
      this.Data.Write(ew);
    }
  }
}
");
        }
Esempio n. 6
0
        private void AssertGenerated_(string src, string expectedGenerated)
        {
            var structure = SchemaTestUtil.Parse(src);

            Assert.IsEmpty(structure.Diagnostics);

            var actualGenerated = new SchemaWriterGenerator().Generate(structure);

            Assert.AreEqual(expectedGenerated, actualGenerated.ReplaceLineEndings());
        }
Esempio n. 7
0
        public void TestOtherFieldBlock()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.memory;
using schema.memory;

namespace foo.bar {
  [BinarySchema]
  public partial class BlockWrapper {
    public long Size;

    [Block(nameof(Size))]
    public IMemoryBlock Block;

    public long Offset;

    [Pointer(nameof(Block), nameof(Offset))]
    public float Field;
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class BlockWrapper {
    public void Read(EndianBinaryReader er) {
      this.Size = er.ReadInt64();
      this.Block = new MemoryBlock(MemoryBlockType.DATA, this.Size);
      this.Offset = er.ReadInt64();
      this.Pointer = this.Block.ClaimPointerWithin(
        this.Offset,
        er => {
          this.Field = ew.ReadSingle();
        },
        ew => {
          ew.WriteSingle(this.Field);
        });
      this.Pointer.Read(er);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class BlockWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteInt64(this.Size);
      er.WriteInt64(this.Offset);
      this.Pointer.Write(ew);
    }
  }
}
");
        }
Esempio n. 8
0
        public void TestParentOfArray()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.child_of;

namespace foo.bar {
  [BinarySchema]
  public partial class Parent {
    public uint Length { get; set; }

    [ArrayLengthSource(nameof(Length))]
    public ChildOfWrapper[] Child { get; set; }
  }

  public partial class ChildOfWrapper : IBiSerializable, IChildOf<Parent> {
    public Parent Parent { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class Parent {
    public void Read(EndianBinaryReader er) {
      this.Length = er.ReadUInt32();
      if (this.Length < 0) {
        throw new Exception(""Expected length to be nonnegative!"");
      }
      this.Child = new ChildOfWrapper[this.Length];
      for (var i = 0; i < this.Length; ++i) {
        this.Child[i] = new ChildOfWrapper();
      }
      foreach (var e in this.Child) {
        e.Parent = this;
        e.Read(er);
      }
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class Parent {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteUInt32(this.Length);
      foreach (var e in this.Child) {
        e.Write(ew);
      }
    }
  }
}
");
        }
Esempio n. 9
0
        public void TestOffsetFromParent()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.child_of;
using schema.attributes.offset;

namespace foo.bar {
  [BinarySchema]
  public partial class OffsetWrapper : IBiSerializable, IChildOf<Parent> {
    public Parent Parent { get; set; }

    public uint Offset { get; set; }

    [Offset($""{nameof(Parent)}.{nameof(Parent.BaseLocation)}"", nameof(Offset))]
    public byte Field { get; set; }
  }

  public partial class Parent : IBiSerializable {
    public OffsetWrapper Child { get; set; }

    public uint BaseLocation { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class OffsetWrapper {
    public void Read(EndianBinaryReader er) {
      this.Offset = er.ReadUInt32();
      {
        var tempLocation = er.Position;
        er.Position = this.Parent.BaseLocation + this.Offset;
        this.Field = er.ReadByte();
        er.Position = tempLocation;
      }
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class OffsetWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteUInt32(this.Offset);
      throw new NotImplementedException();
    }
  }
}
");
        }
        public void TestIfBooleanNonReference()
        {
            var structure = SchemaTestUtil.Parse(@"
using schema;
namespace foo.bar {
  [BinarySchema]
  public partial class BooleanWrapper : IBiSerializable {
    [IfBoolean(SchemaIntegerType.BYTE)]
    public int field;
  }
}");

            SchemaTestUtil.AssertDiagnostics(structure.Diagnostics,
                                             Rules.IfBooleanNeedsNullable);
        }
        public void TestHalf()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class HalfWrapper {
    [NumberFormat(SchemaNumberType.HALF)]
    public float field1;

    [NumberFormat(SchemaNumberType.HALF)]
    public readonly float field2;

    [NumberFormat(SchemaNumberType.HALF)]
    public readonly float[] field3 = new float[5];
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class HalfWrapper {
    public void Read(EndianBinaryReader er) {
      this.field1 = (Single) er.ReadHalf();
      er.AssertHalf((float) this.field2);
      for (var i = 0; i < this.field3.Length; ++i) {
        this.field3[i] = (Single) er.ReadHalf();
      }
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class HalfWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteHalf((float) this.field1);
      ew.WriteHalf((float) this.field2);
      for (var i = 0; i < this.field3.Length; ++i) {
        ew.WriteHalf((Single) this.field3[i]);
      }
    }
  }
}
");
        }
Esempio n. 12
0
        public void TestUsingBoolFromChild()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class ByteWrapper : IBiSerializable {
    public ClassWith1Bool Field { get; set; }

    [IfBoolean($""{nameof(Field)}.{nameof(Field.Bool)}"")]
    public int? OtherValue { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ByteWrapper {
    public void Read(EndianBinaryReader er) {
      this.Field.Read(er);
      if (this.Field.Bool) {
        this.OtherValue = er.ReadInt32();
      }
      else {
        this.OtherValue = null;
      }
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ByteWrapper {
    public void Write(EndianBinaryWriter ew) {
      this.Field.Write(ew);
      if (this.Field.Bool) {
        ew.WriteInt32(this.OtherValue);
      }
    }
  }
}
");
        }
Esempio n. 13
0
        public void TestEnum()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  enum A {}

  enum B : int {
  }
 
  [BinarySchema]
  public partial class EnumWrapper {
    [IntegerFormat(SchemaIntegerType.BYTE)]
    public A fieldA;

    public B fieldB;
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class EnumWrapper {
    public void Read(EndianBinaryReader er) {
      this.fieldA = (A) er.ReadByte();
      this.fieldB = (B) er.ReadInt32();
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class EnumWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteByte((byte) this.fieldA);
      ew.WriteInt32((int) this.fieldB);
    }
  }
}
");
        }
        public void TestPosition()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.position;

namespace foo.bar {
  [BinarySchema]
  public partial class PositionWrapper : IBiSerializable {
    [Position]
    public long Position { get; set; }

    public byte Value { get; set; }

    [Position]
    public long ExpectedPosition { get; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class PositionWrapper {
    public void Read(EndianBinaryReader er) {
      this.Position = er.Position;
      this.Value = er.ReadByte();
      er.AssertPosition(this.ExpectedPosition);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class PositionWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteByte(this.Value);
    }
  }
}
");
        }
Esempio n. 15
0
        public void TestParent()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.child_of;

namespace foo.bar {
  [BinarySchema]
  public partial class Parent {
    public ChildOfWrapper Child { get; set; }
  }

  public partial class ChildOfWrapper : IBiSerializable, IChildOf<Parent> {
    public Parent Parent { get; set; }

    public byte Field { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class Parent {
    public void Read(EndianBinaryReader er) {
      this.Child.Parent = this;
      this.Child.Read(er);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class Parent {
    public void Write(EndianBinaryWriter ew) {
      this.Child.Write(ew);
    }
  }
}
");
        }
Esempio n. 16
0
        public void TestChildInArray()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.child_of;

namespace foo.bar {
  [BinarySchema]
  public partial class ChildOfWrapper : IBiSerializable, IChildOf<Parent> {
    public Parent Parent { get; set; }
  }

  public partial class Parent {
    public uint Length { get; set; }

    [ArrayLengthSource(nameof(Length))]
    public ChildOfWrapper[] Child { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ChildOfWrapper {
    public void Read(EndianBinaryReader er) {
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ChildOfWrapper {
    public void Write(EndianBinaryWriter ew) {
    }
  }
}
");
        }
        [Test] public void TestByte()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class ByteWrapper {
    [IntegerFormat(SchemaIntegerType.BYTE)]
    public bool Field { get; set; }

    [IntegerFormat(SchemaIntegerType.BYTE)]
    public bool ReadonlyField { get; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ByteWrapper {
    public void Read(EndianBinaryReader er) {
      this.Field = er.ReadByte() != 0;
      er.AssertByte(this.ReadonlyField ? 1 : 0);
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ByteWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteByte((byte) (this.Field ? 1 : 0));
      ew.WriteByte((byte) (this.ReadonlyField ? 1 : 0));
    }
  }
}
");
        }
        [Test] public void TestAlign()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;
using schema.attributes.align;

namespace foo.bar {
  [BinarySchema]
  public partial class AlignWrapper : IBiSerializable {
    [Align(0x2)]
    public byte Field { get; set; }
  }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class AlignWrapper {
    public void Read(EndianBinaryReader er) {
      er.Align(2);
      this.Field = er.ReadByte();
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class AlignWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.Align(2);
      ew.WriteByte(this.Field);
    }
  }
}
");
        }
Esempio n. 19
0
        public void TestIfBoolean()
        {
            SchemaTestUtil.AssertGenerated(@"
using schema;

namespace foo.bar {
  [BinarySchema]
  public partial class ByteWrapper : IBiSerializable {
    [IfBoolean(SchemaIntegerType.BYTE)]
    public A? ImmediateValue { get; set; }

    [IntegerFormat(SchemaIntegerType.BYTE)]
    public bool Field { get; set; }

    [IfBoolean(nameof(Field))]
    public int? OtherValue { get; set; }
  }

  public class A : IBiSerializable { }
}",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ByteWrapper {
    public void Read(EndianBinaryReader er) {
      {
        var b = er.ReadByte() != 0;
        if (b) {
          this.ImmediateValue = new A();
          this.ImmediateValue.Read(er);
        }
        else {
          this.ImmediateValue = null;
        }
      }
      this.Field = er.ReadByte() != 0;
      if (this.Field) {
        this.OtherValue = er.ReadInt32();
      }
      else {
        this.OtherValue = null;
      }
    }
  }
}
",
                                           @"using System;
using System.IO;
namespace foo.bar {
  public partial class ByteWrapper {
    public void Write(EndianBinaryWriter ew) {
      ew.WriteByte((byte) (this.ImmediateValue != null ? 1 : 0));
      if (this.ImmediateValue != null) {
        this.ImmediateValue.Write(ew);
      }
      ew.WriteByte((byte) (this.Field ? 1 : 0));
      if (this.Field) {
        ew.WriteInt32(this.OtherValue);
      }
    }
  }
}
");
        }