Skip to content

Commit

Permalink
Add a bunch of methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Aug 3, 2024
1 parent ad38696 commit 6cfa7f8
Show file tree
Hide file tree
Showing 19 changed files with 979 additions and 66 deletions.
156 changes: 156 additions & 0 deletions src/IKVM.ByteCode.Tests/Writing/ElementValueEncoderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System;

using FluentAssertions;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Parsing;
using IKVM.ByteCode.Writing;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.ByteCode.Tests.Writing
{

[TestClass]
public class ElementValueEncoderTests
{

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ShouldThrowOnEncodeTwo()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Byte(new IntegerConstantHandle(1));
encoder.Byte(new IntegerConstantHandle(1));
}

[TestMethod]
public void CanEncodeByte()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Byte(new IntegerConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Byte);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeChar()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Char(new IntegerConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Char);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeDouble()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Double(new DoubleConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Double);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeFloat()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Float(new FloatConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Float);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeInteger()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Integer(new IntegerConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Integer);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeLong()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Long(new LongConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Long);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeShort()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Short(new IntegerConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Short);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeBoolean()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.Boolean(new IntegerConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.Boolean);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

[TestMethod]
public void CanEncodeString()
{
var builder = new BlobBuilder();
var encoder = new ElementValueEncoder(builder);
encoder.String(new Utf8ConstantHandle(1));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var tag).Should().BeTrue();
tag.Should().Be((byte)ElementValueTag.String);
r.TryReadU2(out var constValueIndex).Should().BeTrue();
constValueIndex.Should().Be(1);
}

}

}
55 changes: 55 additions & 0 deletions src/IKVM.ByteCode.Tests/Writing/TypeAnnotationEncoderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using FluentAssertions;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Parsing;
using IKVM.ByteCode.Writing;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.ByteCode.Tests.Writing
{

[TestClass]
public class TypeAnnotationEncoderTests
{

[TestMethod]
public void CanEncodeClassTypeParameter()
{
var builder = new BlobBuilder();
new TypeAnnotationEncoder(builder)
.ClassTypeParameter(
0,
path => path.Array(),
new Utf8ConstantHandle(1),
elements => elements
.Element(
new Utf8ConstantHandle(2),
value => value.Integer(new IntegerConstantHandle(3))));

var r = new ClassFormatReader(builder.ToArray());
r.TryReadU1(out var targetType).Should().BeTrue();
targetType.Should().Be((byte)TypeAnnotationTargetType.ClassTypeParameter);
r.TryReadU1(out var typeParameterIndex).Should().BeTrue();
typeParameterIndex.Should().Be(0);
r.TryReadU1(out var typePathCount).Should().BeTrue();
typePathCount.Should().Be(1);
r.TryReadU1(out var typePathKind).Should().BeTrue();
typePathKind.Should().Be((byte)TypePathKind.Array);
r.TryReadU1(out var typePathArgumentIndex).Should().BeTrue();
typePathArgumentIndex.Should().Be(0);
r.TryReadU2(out var typeIndex).Should().BeTrue();
typeIndex.Should().Be(1);
r.TryReadU2(out var elementValueCount).Should().BeTrue();
elementValueCount.Should().Be(1);
r.TryReadU2(out var elementNameIndex).Should().BeTrue();
elementNameIndex.Should().Be(2);
r.TryReadU1(out var elementTag).Should().BeTrue();
elementTag.Should().Be((byte)'I');
r.TryReadU2(out var elementValue).Should().BeTrue();
elementValue.Should().Be(3);
}

}

}
135 changes: 135 additions & 0 deletions src/IKVM.ByteCode.Tests/Writing/TypePathEncoderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using FluentAssertions;

using IKVM.ByteCode.Buffers;
using IKVM.ByteCode.Parsing;
using IKVM.ByteCode.Writing;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.ByteCode.Tests.Writing
{

[TestClass]
public class TypePathEncoderTests
{

[TestMethod]
public void CanEncodeArray()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).Array();

var w = new ClassFormatReader(builder.ToArray());
w.TryReadU1(out var l).Should().BeTrue();
l.Should().Be(1);
w.TryReadU1(out var k).Should().BeTrue();
k.Should().Be(0);
w.TryReadU1(out var a).Should().BeTrue();
a.Should().Be(0);
}

[TestMethod]
public void CanEncodeInnerType()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).InnerType();

var w = new ClassFormatReader(builder.ToArray());
w.TryReadU1(out var l).Should().BeTrue();
l.Should().Be(1);
w.TryReadU1(out var k).Should().BeTrue();
k.Should().Be(1);
w.TryReadU1(out var a).Should().BeTrue();
a.Should().Be(0);
}

[TestMethod]
public void CanEncodeWildcard()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).Wildcard();

var w = new ClassFormatReader(builder.ToArray());
w.TryReadU1(out var l).Should().BeTrue();
l.Should().Be(1);
w.TryReadU1(out var k).Should().BeTrue();
k.Should().Be(2);
w.TryReadU1(out var a).Should().BeTrue();
a.Should().Be(0);
}

[TestMethod]
public void CanEncodeTypeArgument()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).TypeArgument(1);

var w = new ClassFormatReader(builder.ToArray());
w.TryReadU1(out var l).Should().BeTrue();
l.Should().Be(1);
w.TryReadU1(out var k).Should().BeTrue();
k.Should().Be(3);
w.TryReadU1(out var a).Should().BeTrue();
a.Should().Be(1);
}

[TestMethod]
public void CanEncodeMultiple()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).Array().Array();

var w = new ClassFormatReader(builder.ToArray());
w.TryReadU1(out var l).Should().BeTrue();
l.Should().Be(2);
w.TryReadU1(out var k1).Should().BeTrue();
k1.Should().Be(0);
w.TryReadU1(out var a1).Should().BeTrue();
a1.Should().Be(0);
w.TryReadU1(out var k2).Should().BeTrue();
k2.Should().Be(0);
w.TryReadU1(out var a2).Should().BeTrue();
a2.Should().Be(0);
}

[TestMethod]
public void CanEncodeRecord()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).Encode(new TypePathRecord([
new TypePathItemRecord(TypePathKind.Array, 0),
new TypePathItemRecord(TypePathKind.Array, 0),
]));

var w = new ClassFormatReader(builder.ToArray());
w.TryReadU1(out var l).Should().BeTrue();
l.Should().Be(2);
w.TryReadU1(out var k1).Should().BeTrue();
k1.Should().Be(0);
w.TryReadU1(out var a1).Should().BeTrue();
a1.Should().Be(0);
w.TryReadU1(out var k2).Should().BeTrue();
k2.Should().Be(0);
w.TryReadU1(out var a2).Should().BeTrue();
a2.Should().Be(0);
}

[TestMethod]
public void CanEncodeRecordAndParse()
{
var builder = new BlobBuilder();
new TypePathEncoder(builder).Encode(new TypePathRecord([
new TypePathItemRecord(TypePathKind.Array, 0),
new TypePathItemRecord(TypePathKind.Array, 0),
]));

var r = new ClassFormatReader(builder.ToArray());
TypePathRecord.TryRead(ref r, out var record).Should().BeTrue();
record.Path.Should().HaveCount(2);
record.Path[0].Kind.Should().Be(TypePathKind.Array);
record.Path[1].Kind.Should().Be(TypePathKind.Array);
}

}

}
Loading

0 comments on commit 6cfa7f8

Please sign in to comment.