Skip to content

Commit

Permalink
Changed XElementExtensions to use (prefix,name) arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
markrendle committed Feb 16, 2010
1 parent 9079ed3 commit 0936737
Show file tree
Hide file tree
Showing 22 changed files with 502 additions and 20 deletions.
2 changes: 1 addition & 1 deletion NExtLib.Tests/Linq/EnumerableExtensionTests.cs
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using NUnit.Framework;
using NExtLib.Linq;
using NExtLib.UnitTest;
using NExtLib.Unit;

namespace NExtLib.Tests.Linq
{
Expand Down
7 changes: 5 additions & 2 deletions NExtLib.Tests/NExtLib.Tests.csproj
Expand Up @@ -59,9 +59,9 @@
<None Include="Resources\XmlWithDefaultNamespace.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NExtLib.UnitTest\NExtLib.UnitTest.csproj">
<ProjectReference Include="..\NExtLib.Unit\NExtLib.Unit.csproj">
<Project>{072F8BE4-81C1-4276-A9A5-1AEC87A84265}</Project>
<Name>NExtLib.UnitTest</Name>
<Name>NExtLib.Unit</Name>
</ProjectReference>
<ProjectReference Include="..\NExtLib\NExtLib.csproj">
<Project>{4A191FAD-DC1D-46FC-A941-7DEB2C8C4C15}</Project>
Expand All @@ -71,6 +71,9 @@
<ItemGroup>
<None Include="Resources\XmlWithNoNamespace.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\XmlWithPrefixedNamespace.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
17 changes: 17 additions & 0 deletions NExtLib.Tests/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion NExtLib.Tests/Properties/Resources.resx
Expand Up @@ -117,11 +117,15 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="XmlWithDefaultNamespace" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\XmlWithDefaultNamespace.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="XmlWithNoNamespace" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\XmlWithNoNamespace.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="XmlWithPrefixedNamespace" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\XmlWithPrefixedNamespace.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>
9 changes: 9 additions & 0 deletions NExtLib.Tests/Resources/XmlWithPrefixedNamespace.txt
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:c="http://schemas.next.lib/tests">
<c:child>
<c:sub>Foo</c:sub>
</c:child>
<c:child>
<c:sub>Bar</c:sub>
</c:child>
</root>
24 changes: 17 additions & 7 deletions NExtLib.Tests/Xml/Linq/XElementExtensionsTests.cs
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using NUnit.Framework;
using System.Xml.Linq;
using NExtLib.UnitTest;
using NExtLib.Unit;

namespace NExtLib.Tests.Xml.Linq
{
Expand All @@ -15,20 +15,30 @@ public class XElementExtensionsTests
public void TestXElementWithDefaultNamespace()
{
var element = XElement.Parse(Properties.Resources.XmlWithDefaultNamespace);
var list = element.XElements("child").ToList();
var list = element.Elements(null, "child").ToList();
list.Count.ShouldEqual(2);
list[0].XElement("sub").Value.ShouldEqual("Foo");
list[1].XElement("sub").Value.ShouldEqual("Bar");
list[0].Element(null, "sub").Value.ShouldEqual("Foo");
list[1].Element(null, "sub").Value.ShouldEqual("Bar");
}

[Test]
public void TestXElementWithNoNamespace()
{
var element = XElement.Parse(Properties.Resources.XmlWithNoNamespace);
var list = element.XElements("child").ToList();
var list = element.Elements(null, "child").ToList();
list.Count.ShouldEqual(2);
list[0].XElement("sub").Value.ShouldEqual("Foo");
list[1].XElement("sub").Value.ShouldEqual("Bar");
list[0].Element(null, "sub").Value.ShouldEqual("Foo");
list[1].Element(null, "sub").Value.ShouldEqual("Bar");
}

[Test]
public void TestXElementWithPrefixedNamespace()
{
var element = XElement.Parse(Properties.Resources.XmlWithPrefixedNamespace);
var list = element.Elements("c", "child").ToList();
list.Count.ShouldEqual(2);
list[0].Element("c", "sub").Value.ShouldEqual("Foo");
list[1].Element("c", "sub").Value.ShouldEqual("Bar");
}
}
}
Expand Up @@ -8,7 +8,7 @@
<ProjectGuid>{072F8BE4-81C1-4276-A9A5-1AEC87A84265}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NExtLib.UnitTest</RootNamespace>
<RootNamespace>NExtLib.Unit</RootNamespace>
<AssemblyName>NExtLib.UnitTest</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
Expand Down
File renamed without changes.
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using NUnit.Framework;

namespace NExtLib.UnitTest
namespace NExtLib.Unit
{
public static class ShouldExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion NExtLib.sln
Expand Up @@ -5,7 +5,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NExtLib", "NExtLib\NExtLib.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NExtLib.Tests", "NExtLib.Tests\NExtLib.Tests.csproj", "{8DF17038-537D-45BE-B229-C99EECBB4EDC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NExtLib.UnitTest", "NExtLib.UnitTest\NExtLib.UnitTest.csproj", "{072F8BE4-81C1-4276-A9A5-1AEC87A84265}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NExtLib.Unit", "NExtLib.Unit\NExtLib.Unit.csproj", "{072F8BE4-81C1-4276-A9A5-1AEC87A84265}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
19 changes: 19 additions & 0 deletions NExtLib/Async/AsyncException.cs
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib.Async
{
[Serializable]
public class AsyncException : Exception
{
public AsyncException() { }
public AsyncException(string message) : base(message) { }
public AsyncException(string message, Exception inner) : base(message, inner) { }
protected AsyncException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
}
22 changes: 22 additions & 0 deletions NExtLib/Async/Future.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib.Async
{
public static class Future
{
/// <summary>
/// Creates an instance of <see cref="Future`1"/>.
/// </summary>
/// <typeparam name="T">The type of value represented by the Future.</typeparam>
/// <param name="func">The <see cref="Func`1"/> delegate that will return the value.</param>
/// <returns>A new instance of <see cref="Future`1"/>.</returns>
/// <remarks>This method exists only to provide type-inference convenience.</remarks>
public static Future<T> Create<T>(Func<T> func)
{
return new Future<T>(func);
}
}
}
66 changes: 66 additions & 0 deletions NExtLib/Async/Future`1.cs
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib.Async
{
public class Future<T>
{
private readonly Action _action;
private readonly IAsyncResult _asyncResult;
private readonly object _lock = new object();
private bool _ended;
private T _value;
private Exception _error;

public Future(Func<T> func)
{
if (func == null) throw new ArgumentNullException("func");

_action = () => Run(func);
_asyncResult = _action.BeginInvoke(null, null);
}

public T Value
{
get
{
JoinAction();

if (_error != null) throw new AsyncException(Properties.Resources.Async_AsyncExceptionMessage, _error);
return _value;
}
}

private void Run(Func<T> func)
{
try
{
_value = func();
}
catch (Exception ex)
{
if (ex is SystemException) throw;

_error = ex;
_value = default(T);
}
}

private void JoinAction()
{
if (!_ended)
{
lock (_lock)
{
if (!_ended)
{
_action.EndInvoke(_asyncResult);
_ended = true;
}
}
}
}
}
}
16 changes: 16 additions & 0 deletions NExtLib/Func.cs
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib
{
public static class Func
{
public static Func<TIn, TOut> NoOp<TIn, TOut>()
where TIn : TOut
{
return (x) => x;
}
}
}
22 changes: 22 additions & 0 deletions NExtLib/Linq/EnumerableOfKeyValuePairExtensions.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Linq
{
public static class EnumerableOfKeyValuePairExtensions
{
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
Dictionary<TKey, TValue> dictionary;

if ((dictionary = source as Dictionary<TKey, TValue>) == null)
{
dictionary = source.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}

return dictionary;
}
}
}
23 changes: 23 additions & 0 deletions NExtLib/NExtLib.csproj
Expand Up @@ -33,20 +33,43 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Async\AsyncException.cs" />
<Compile Include="Async\Future.cs" />
<Compile Include="Async\Future`1.cs" />
<Compile Include="Func.cs" />
<Compile Include="Linq\EnumerableOfKeyValuePairExtensions.cs" />
<Compile Include="Linq\EnumerableExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="StringExtensions.cs" />
<Compile Include="Tuple.cs" />
<Compile Include="Linq\TupleEnumerableExtensions.cs" />
<Compile Include="TupleExtensions.cs" />
<Compile Include="Tuple`2.cs" />
<Compile Include="Xml\Linq\XAttributeExtensions.cs" />
<Compile Include="Xml\Linq\XElementExtensions.cs" />
<Compile Include="Xml\Syndication\SyndicationItemExtensions.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit 0936737

Please sign in to comment.