Skip to content

Commit

Permalink
Merge pull request ironfede#183 from Visionaid-International-Ltd/refa…
Browse files Browse the repository at this point in the history
…ctor-extension-tests

Refactor extension tests
  • Loading branch information
ironfede authored Oct 7, 2024
2 parents 634ba16 + 74489af commit b488e15
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ internal OLEPropertiesContainer(CFStream cfStream)
PropertySetStream pStream = new PropertySetStream();

this.cfStream = cfStream;
pStream.Read(new BinaryReader(new StreamDecorator(cfStream)));

using StreamDecorator stream = new(cfStream);
using BinaryReader reader = new(stream);
pStream.Read(reader);

ContainerType = pStream.FMTID0.ToString("B").ToUpperInvariant() switch
{
Expand Down Expand Up @@ -222,8 +225,8 @@ public void Save(CFStream cfStream)
//throw new NotImplementedException("API Unstable - Work in progress - Milestone 2.3.0.0");
//properties.Sort((a, b) => a.PropertyIdentifier.CompareTo(b.PropertyIdentifier));

Stream s = new StreamDecorator(cfStream);
BinaryWriter bw = new BinaryWriter(s);
using StreamDecorator s = new(cfStream);
using BinaryWriter bw = new BinaryWriter(s);

Guid fmtId0 = FmtID0 ?? (ContainerType == ContainerType.SummaryInfo ? new Guid(WellKnownFMTID.FMTID_SummaryInformation) : new Guid(WellKnownFMTID.FMTID_DocSummaryInformation));

Expand Down
4 changes: 3 additions & 1 deletion sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public object Value

public override bool Equals(object obj)
{
if (obj is not OLEProperty other)
if (obj is not OLEProperty other)
return false;

return other.PropertyIdentifier == PropertyIdentifier;
Expand All @@ -65,5 +65,7 @@ public override int GetHashCode()
{
return (int)PropertyIdentifier;
}

public override string ToString() => $"{PropertyName} - {VTType} - {Value}";
}
}
81 changes: 34 additions & 47 deletions sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public CFSStreamExtensionsTest()
[TestMethod]
public void Test_AS_IOSTREAM_READ()
{
CompoundFile cf = new CompoundFile("MultipleStorage.cfs");

Stream s = cf.RootStorage.GetStorage("MyStorage").GetStream("MyStream").AsIOStream();
BinaryReader br = new BinaryReader(s);
using CompoundFile cf = new("MultipleStorage.cfs");
using Stream s = cf.RootStorage.GetStorage("MyStorage").GetStream("MyStream").AsIOStream();
using BinaryReader br = new(s);
byte[] result = br.ReadBytes(32);
CollectionAssert.AreEqual(Helpers.GetBuffer(32, 1), result);
}
Expand All @@ -59,18 +58,21 @@ public void Test_AS_IOSTREAM_WRITE()
{
const string cmp = "Hello World of BinaryWriter !";

CompoundFile cf = new CompoundFile();
Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream();
BinaryWriter bw = new BinaryWriter(s);
bw.Write(cmp);
cf.SaveAs("$ACFFile.cfs");
cf.Close();
using (CompoundFile cf = new())
{
using Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream();
using BinaryWriter bw = new(s);
bw.Write(cmp);
cf.SaveAs("$ACFFile.cfs");
}

cf = new CompoundFile("$ACFFile.cfs");
BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream());
string st = br.ReadString();
Assert.AreEqual(cmp, st);
cf.Close();
using (CompoundFile cf = new("$ACFFile.cfs"))
{
using Stream s = cf.RootStorage.GetStream("ANewStream").AsIOStream();
using BinaryReader br = new(s);
string st = br.ReadString();
Assert.AreEqual(cmp, st);
}
}

[TestMethod]
Expand All @@ -82,47 +84,32 @@ public void Test_AS_IOSTREAM_MULTISECTOR_WRITE()
data[i] = (byte)(i % 255);
}

using (CompoundFile cf = new CompoundFile())
using (CompoundFile cf = new())
{
using (Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream())
{
using (BinaryWriter bw = new BinaryWriter(s))
{
bw.Write(data);
cf.SaveAs("$ACFFile2.cfs");
cf.Close();
}
}
using Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream();
using BinaryWriter bw = new(s);
bw.Write(data);
cf.SaveAs("$ACFFile2.cfs");
}

// Works
using (CompoundFile cf = new CompoundFile("$ACFFile2.cfs"))
using (CompoundFile cf = new("$ACFFile2.cfs"))
{
using (BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream()))
{
byte[] readData = new byte[data.Length];
int readCount = br.Read(readData, 0, readData.Length);
Assert.AreEqual(readData.Length, readCount);
CollectionAssert.AreEqual(data, readData);
cf.Close();
}
using Stream s = cf.RootStorage.GetStream("ANewStream").AsIOStream();
using BinaryReader br = new(s);
byte[] readData = new byte[data.Length];
int readCount = br.Read(readData, 0, readData.Length);
Assert.AreEqual(readData.Length, readCount);
CollectionAssert.AreEqual(data, readData);
}

// Won't work until #88 is fixed.
using (CompoundFile cf = new CompoundFile("$ACFFile2.cfs"))
using (CompoundFile cf = new("$ACFFile2.cfs"))
{
using (Stream readStream = cf.RootStorage.GetStream("ANewStream").AsIOStream())
{
byte[] readData;
using (MemoryStream ms = new MemoryStream())
{
readStream.CopyTo(ms);
readData = ms.ToArray();
}

CollectionAssert.AreEqual(data, readData);
cf.Close();
}
using Stream readStream = cf.RootStorage.GetStream("ANewStream").AsIOStream();
using MemoryStream ms = new();
readStream.CopyTo(ms);
CollectionAssert.AreEqual(data, ms.ToArray());
}
}
}
Expand Down
23 changes: 0 additions & 23 deletions sources/Test/OpenMcdf.Extensions.Test/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,5 @@ public static byte[] GetBuffer(int count, byte c)

return b;
}

public static bool CompareBuffer(byte[] b, byte[] p)
{
if (b == null && p == null)
throw new Exception("Null buffers");

if (b == null && p != null)
return false;

if (b != null && p == null)
return false;

if (b.Length != p.Length)
return false;

for (int i = 0; i < b.Length; i++)
{
if (b[i] != p[i])
return false;
}

return true;
}
}
}
Loading

0 comments on commit b488e15

Please sign in to comment.