forked from HNIdesu/RubyMarshal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental support for dynamic/object
- Loading branch information
1 parent
3d3d300
commit 1c60591
Showing
10 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using RmSharp.Converters; | ||
using RmSharp.Tokens; | ||
|
||
namespace RmSharp.Tests.Converters | ||
{ | ||
[TestClass] | ||
public class Dynamic | ||
{ | ||
private readonly byte[] _rubyMarshalData = new byte[] { ( byte ) RubyMarshalToken.Fixnum, 0x02, 0xD2, 0x04 }; | ||
private readonly dynamic _expectedValue = 1234; | ||
|
||
[TestMethod] | ||
public void Read( ) | ||
{ | ||
var converter = RmConverterFactory.GetConverter( _expectedValue.GetType( ) ); | ||
using ( var memoryStream = new MemoryStream( _rubyMarshalData ) ) | ||
using ( var reader = new BinaryReader( memoryStream ) ) | ||
{ | ||
var result = converter.Read( reader ); | ||
|
||
Assert.IsNotNull( result ); | ||
Assert.IsInstanceOfType( result, _expectedValue.GetType( ) ); | ||
Assert.AreEqual( _expectedValue, result ); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void Write( ) | ||
{ | ||
var converter = RmConverterFactory.GetConverter( _expectedValue.GetType( ) ); | ||
using ( var memoryStream = new MemoryStream( ) ) | ||
using ( var writer = new BinaryWriter( memoryStream ) ) | ||
{ | ||
converter.Write( writer, _expectedValue ); | ||
|
||
byte[] writtenData = memoryStream.ToArray( ); | ||
CollectionAssert.AreEqual( _rubyMarshalData, writtenData ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using RmSharp.Converters; | ||
using RmSharp.Tokens; | ||
|
||
namespace RmSharp.Tests.Converters | ||
{ | ||
[TestClass] | ||
public class DyamicArray | ||
{ | ||
private readonly byte[] _rubyMarshalData = new byte[] { | ||
|
||
( byte ) RubyMarshalToken.Array, 0x0A, | ||
( byte ) RubyMarshalToken.Fixnum, 0x02, 0xD2, 0x04, | ||
( byte ) RubyMarshalToken.String, 0x10, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, | ||
( byte ) RubyMarshalToken.Fixnum, 0x02, 0xD2, 0x04, | ||
( byte ) RubyMarshalToken.Fixnum, 0x02, 0xD2, 0x04, | ||
( byte ) RubyMarshalToken.Double, 0x08, 0x31, 0x2E, 0x32 | ||
}; | ||
|
||
private readonly dynamic[] _expectedValue = [1234, "Hello World", 1234, 1234, 1.2]; | ||
|
||
[TestMethod] | ||
public void Read( ) | ||
{ | ||
var converter = RmConverterFactory.GetConverter( _expectedValue.GetType() ); | ||
using ( var memoryStream = new MemoryStream( _rubyMarshalData ) ) | ||
using ( var reader = new BinaryReader( memoryStream ) ) | ||
{ | ||
var result = ( dynamic[] ) converter.Read( reader ); | ||
|
||
Assert.IsNotNull( result ); | ||
Assert.IsInstanceOfType( result, _expectedValue.GetType( ) ); | ||
|
||
for ( var i = 0; i < result.Length; i++ ) | ||
{ | ||
if ( result[i] != _expectedValue[i] ) | ||
Assert.Fail( $"Item at index '{i}' is not equal to '{_expectedValue[i]}' got '{result[i]}'." ); | ||
} | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void Write( ) | ||
{ | ||
var converter = RmConverterFactory.GetConverter( _expectedValue.GetType( ) ); | ||
using ( var memoryStream = new MemoryStream( ) ) | ||
using ( var writer = new BinaryWriter( memoryStream ) ) | ||
{ | ||
converter.Write( writer, _expectedValue ); | ||
|
||
byte[] writtenData = memoryStream.ToArray( ); | ||
CollectionAssert.AreEqual( _rubyMarshalData, writtenData ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using RmSharp.Extensions; | ||
using RmSharp.Tokens; | ||
using System; | ||
using System.IO; | ||
|
||
namespace RmSharp.Converters | ||
{ | ||
public class DynamicConverter : RmConverter | ||
{ | ||
public override object Read( BinaryReader reader ) | ||
{ | ||
// Peek the byte to find out a token to process | ||
if ( reader.PeekByte( out var byteValue ) ) | ||
{ | ||
if ( Enum.IsDefined( typeof( RubyMarshalToken ), byteValue ) ) | ||
{ | ||
var tokenConverter = RmConverterFactory.GetConverter( ( RubyMarshalToken ) byteValue ); | ||
|
||
if ( tokenConverter != null ) | ||
{ | ||
return tokenConverter.Read( reader ); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override void Write( BinaryWriter writer, object instance ) | ||
{ | ||
var converter = RmConverterFactory.GetConverter( instance.GetType() ); | ||
|
||
if ( converter != null ) | ||
{ | ||
converter.Write( writer, instance ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters