Skip to content

Commit

Permalink
Create extensions library (#24)
Browse files Browse the repository at this point in the history
* Create a VarDump.Extensions library #20
  • Loading branch information
ycherkes authored Dec 29, 2023
1 parent c74de34 commit 4d92761
Show file tree
Hide file tree
Showing 139 changed files with 224 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VarDump is a utility for serialization runtime objects to C# or Visual Basic str

Developed as a free alternative of [ObjectDumper.NET](https://github.com/thomasgalliker/ObjectDumper), which is not free for commercial use.

[![nuget version](https://img.shields.io/badge/Nuget-v0.2.8-blue)](https://www.nuget.org/packages/VarDump)
[![nuget version](https://img.shields.io/badge/Nuget-v0.2.9-blue)](https://www.nuget.org/packages/VarDump)
[![nuget downloads](https://img.shields.io/nuget/dt/VarDump?label=Downloads)](https://www.nuget.org/packages/VarDump)

# Powered By
Expand Down
16 changes: 14 additions & 2 deletions VarDump.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VarDump", "src\VarDump.csproj", "{ED778772-EC95-4362-8C3C-C2DFF8F0ABD6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VarDump", "src\VarDump\VarDump.csproj", "{ED778772-EC95-4362-8C3C-C2DFF8F0ABD6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{6CDE3AAC-4726-438B-8D09-51ED6434BF68}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VarDump.UnitTests", "test\VarDump.UnitTests\VarDump.UnitTests.csproj", "{6CDE3AAC-4726-438B-8D09-51ED6434BF68}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VarDump.Extensions", "src\VarDump.Extensions\VarDump.Extensions.csproj", "{8A369FCC-4E46-4EE2-AFEB-0904CD83A9FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VarDump.Extensions.UnitTests", "test\VarDump.Extensions.UnitTests\VarDump.Extensions.UnitTests.csproj", "{15B52F38-0351-4306-8A14-349B46655C54}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +25,14 @@ Global
{6CDE3AAC-4726-438B-8D09-51ED6434BF68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CDE3AAC-4726-438B-8D09-51ED6434BF68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CDE3AAC-4726-438B-8D09-51ED6434BF68}.Release|Any CPU.Build.0 = Release|Any CPU
{8A369FCC-4E46-4EE2-AFEB-0904CD83A9FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A369FCC-4E46-4EE2-AFEB-0904CD83A9FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A369FCC-4E46-4EE2-AFEB-0904CD83A9FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A369FCC-4E46-4EE2-AFEB-0904CD83A9FE}.Release|Any CPU.Build.0 = Release|Any CPU
{15B52F38-0351-4306-8A14-349B46655C54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15B52F38-0351-4306-8A14-349B46655C54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15B52F38-0351-4306-8A14-349B46655C54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15B52F38-0351-4306-8A14-349B46655C54}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
29 changes: 29 additions & 0 deletions src/VarDump.Extensions/DumpExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using VarDump.Visitor;

namespace VarDump.Extensions;

public static class VarDumpExtensions
{
public static Func<DumpOptions, IDumper> VarDumpFactory { get; set; } = VarDumpFactories.CSharp;

public static string Dump(this object obj)
{
return VarDumpFactory(DumpOptions.Default).Dump(obj);
}

public static string Dump(this object obj, DumpOptions options)
{
return VarDumpFactory(options).Dump(obj);
}

public static void Dump(this object obj, DumpOptions options, TextWriter textWriter)
{
VarDumpFactory(options).Dump(obj, textWriter);
}
}

public class VarDumpFactories
{
public static readonly Func<DumpOptions, IDumper> CSharp = options => new CSharpDumper(options);
public static readonly Func<DumpOptions, IDumper> VisualBasic = options => new VisualBasicDumper(options);
}
36 changes: 36 additions & 0 deletions src/VarDump.Extensions/VarDump.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<AssemblyName>VarDump.Extensions</AssemblyName>
<AssemblyVersion>0.2.9</AssemblyVersion>
<FileVersion>0.2.9</FileVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Copyright>Copyright $([System.DateTime]::Now.Year) Yevhen Cherkes</Copyright>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>VarDump.Extensions</Title>
<Version>0.2.9</Version>
<Description>VarDump is a utility for serialization runtime objects to C# and Visual Basic string.</Description>
<PackageProjectUrl>https://github.com/ycherkes/VarDump</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/ycherkes/VarDump.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>dump;dumpobject;c#;visulabasic;debug</PackageTags>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="VarDump" Version="0.2.9" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 6 additions & 5 deletions src/VarDump.csproj → src/VarDump/VarDump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AssemblyName>VarDump</AssemblyName>
<AssemblyVersion>0.2.8</AssemblyVersion>
<FileVersion>0.2.8</FileVersion>
<AssemblyVersion>0.2.9</AssemblyVersion>
<FileVersion>0.2.9</FileVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Copyright>Copyright $([System.DateTime]::Now.Year) Yevhen Cherkes</Copyright>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>VarDump</Title>
<Version>0.2.8</Version>
<Version>0.2.9</Version>
<Description>VarDump is a utility for serialization runtime objects to C# and Visual Basic string.</Description>
<PackageProjectUrl>https://github.com/ycherkes/VarDump</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -21,11 +21,12 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md">
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<InternalsVisibleTo Include="UnitTests" />
<InternalsVisibleTo Include="$(AssemblyName).UnitTests" />
<InternalsVisibleTo Include="YellowFlavor.Serialization" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net461</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Portable.System.DateTimeOnly" Version="7.0.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\VarDump.Extensions\VarDump.Extensions.csproj" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions test/VarDump.Extensions.UnitTests/VarDumpExtensionsSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Xunit;

namespace VarDump.Extensions.UnitTests;

public class VarDumpExtensionsSpec
{
[Fact]
public void DumpAnonymousTypeCsharp()
{
var anonymous = new[]
{
new { Name = "Steeve", Age = (int?)int.MaxValue, Reference = "Test reference" },
new { Name = "Peter", Age = (int?)null, Reference = (string)null }
};

VarDumpExtensions.VarDumpFactory = VarDumpFactories.CSharp;

var result = anonymous.Dump();

Assert.Equal(
@"var arrayOfAnonymousType = new []
{
new
{
Name = ""Steeve"",
Age = (int?)int.MaxValue,
Reference = ""Test reference""
},
new
{
Name = ""Peter"",
Age = (int?)null,
Reference = (string)null
}
};
", result);
}

[Fact]
public void DumpAnonymousTypeVb()
{
VarDumpExtensions.VarDumpFactory = VarDumpFactories.VisualBasic;

var anonymous = new[]
{
new { Name = "Steeve", Age = (int?)int.MaxValue, Reference = "Test reference" },
new { Name = "Peter", Age = (int?)null, Reference = (string)null }
};

var result = anonymous.Dump();

Assert.Equal(
@"Dim arrayOfAnonymousType = {
New With {
.Name = ""Steeve"",
.Age = CType(Integer.MaxValue, Integer?),
.Reference = ""Test reference""
},
New With {
.Name = ""Peter"",
.Age = CType(Nothing, Integer?),
.Reference = CType(Nothing, String)
}
}
", result);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Linq;
using VarDump;
using VarDump.Utils;
using VarDump.Visitor;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class AnonymousTypeSpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Collections.Immutable;
using VarDump;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class ArraySpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using UnitTests.TestModel;
using VarDump;
using VarDump.UnitTests.TestModel;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class CustomCollectionSpec
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnitTests.TestModel;
using VarDump;
using VarDump.UnitTests.TestModel;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

#if !NET8_0_OR_GREATER

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//using Microsoft.CodeAnalysis.CSharp.Scripting;

using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using VarDump;
using VarDump.Visitor;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class DateTimeSpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using VarDump;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
using VarDump.Visitor;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class DiagnosticsSpec
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using UnitTests.TestModel;
using VarDump;
using VarDump.UnitTests.TestModel;
using VarDump.Visitor;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class DictionarySpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Net;
using VarDump;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class DnsEndPointSpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Linq;
using VarDump;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class EnumerableRangeSpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using VarDump;
using VarDump.Visitor;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class ExceptionSpec
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using VarDump;
using Xunit;

namespace UnitTests;
namespace VarDump.UnitTests;

public class FlagsSpec
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

using System.Collections.Frozen;
using System.Collections.Generic;
using VarDump;
using Xunit;

namespace UnitTests
namespace VarDump.UnitTests
{
public class FrozenCollectionsNet8Spec
{
Expand Down
Loading

0 comments on commit 4d92761

Please sign in to comment.