Skip to content

Commit

Permalink
Speccing out spark element /node wrapper interfaces
Browse files Browse the repository at this point in the history
git-svn-id: http://openrastasparkcodec.googlecode.com/svn/trunk@22 1f2a5d12-3dac-11de-b232-599c1b3c59e8
  • Loading branch information
Jennifer.E.M.Smith authored and Jennifer.E.M.Smith committed Sep 11, 2009
1 parent 82d196f commit 7772849
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 112 deletions.
Expand Up @@ -6,6 +6,7 @@
using OpenRasta.Codecs.Spark.Tests.Stubs;
using OpenRasta.Codecs.Spark2.SparkInterface;
using OpenRasta.Codecs.Spark2.Specification;
using OpenRasta.Codecs.Spark2.Specification.Syntax;
using OpenRasta.Codecs.Spark2.Transformers;
using PanelSystem.WorkingDays.Tests;
using Spark;
Expand Down Expand Up @@ -56,7 +57,7 @@ private static IWindsorContainer CreateTestDependencies()
result.AddComponent<ISparkExtensionFactory, CodecSparkExtensionFactory>();
result.AddComponent<ISparkElementTransformerService, SparkElementTransformerService>();
result.AddComponent<IElementTransformerService, ElementTransformerService>();
result.AddComponent<ISpecificationProvider, SpecificationProvider>();
result.AddComponent<ISpecificationProvider, DefaultSpecification>();
return result;
}
public string RenderTemplate(string templateSource, object data)
Expand Down
Expand Up @@ -63,6 +63,7 @@
</Compile>
<Compile Include="ElementMatcherExtensionTests.cs" />
<Compile Include="ElementMatcherTests.cs" />
<Compile Include="SparkInterface\SparkElementWrapperTests.cs" />
<Compile Include="SparkNodeExtensionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SparkTestNodes.cs" />
Expand All @@ -75,6 +76,7 @@
<Compile Include="Specifications\Builders\ElementTransformerActionsByMatchBuilderTests.cs" />
<Compile Include="Specifications\Builders\ElementTransformerSpecificationBuilderTests.cs" />
<Compile Include="Specifications\ElementTransformerSpecificationTests.cs" />
<Compile Include="TestAttributeNode.cs" />
<Compile Include="Transformers\ElementTransformerServiceTests.cs" />
<Compile Include="Transformers\ElementTransformerTests.cs" />
</ItemGroup>
Expand Down
Expand Up @@ -28,7 +28,7 @@ public void ShouldPassTheInnerNodesToTheElementTransformerWhenTransforming()
{
IList<Node> innerNodes = new Node[]
{
SparkTestNodes.BasicAttributeNode(),
SparkTestNodes.BasicAttributeNode("attributeName"),
SparkTestNodes.BasicElementNode()
};
GivenAnElementResultOf(new SparkElementWrapper(SparkTestNodes.BasicElementNode()));
Expand All @@ -47,7 +47,7 @@ public void ShouldReturnUnwrappedElementNode()

private void ThenTheBodyResultShouldBeTheElementTransformerResultUnwrapped()
{
Context.TransformationResult.ShouldEqual(Context.ElementTransformerResult.As<SparkElementWrapper>().WrappedNode);
Context.TransformationResult.ShouldEqual(Context.ElementTransformerResult.As<SparkElementWrapper>().GetWrappedNode());
}

private void WhenNodeIsTransformed()
Expand All @@ -69,7 +69,7 @@ private void GivenInnerNodesOf(IList<Node> nodes)
private void ThenTheBodyNodesShouldBePassedIntoTheElementTransformer(IEnumerable<Node> nodes)
{
IEnumerable<INode> bodyNodes = Context.ElementTransformer.GetFirstArgumentFor<IElementTransformer, IEnumerable<INode>>(x => x.Transform(null));
IEnumerable<Node> unwrappedNodes = bodyNodes.Cast<SparkNodeWrapper>().Select(x => x.WrappedNode);
IEnumerable<Node> unwrappedNodes = bodyNodes.Cast<SparkNodeWrapper>().Select(x => x.GetWrappedNode());
Assert.That(unwrappedNodes, Is.EqualTo(nodes));
}

Expand Down
@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenRasta.Codecs.Spark2.Model;
using OpenRasta.Codecs.Spark2.SparkInterface;
using Spark.Parser.Markup;

namespace OpenRasta.Codecs.Spark.UnitTests.SparkInterface
{
[TestFixture]
public class SparkElementWrapperTests
{
[SetUp]
public void SetUp()
{
Context = new SparkElementWrapperTestContext();
}

[Test]
public void ShouldAddAnAttributeToOriginalNodeOnAddAttribute()
{
GivenAnOriginalElement(SparkTestNodes.BasicElementNode());
WhenAnAttributeIsAdded("theAttribute");
ThenTheWrappedElementShouldHaveABlankAttributeNamed("theAttribute");
}
[Test]
public void ShouldReturnWrappedAttributeNodeWhenAttributeIsAdded()
{
GivenAnOriginalElement(SparkTestNodes.BasicElementNode());
WhenAnAttributeIsAdded("theAttribute");
ThenTheReturnedAttributeShouldBeWrappedSparkAttributeNamed("theAttribute");
}
[Test]
public void ShouldReturnTrueForHasAttributeIfUnderlyingNodeHasAttribute()
{
GivenAnOriginalElement(SparkTestNodes.BasicElementNode());
WhenAnAttributeIsAdded("theAttribute");
ThenHasAttributeShouldBeTrueFor("theAttribute");
}
[Test]
public void ShouldReturnFalseForHasAttributeIfUnderlyingNodeDoesntHaveAttribute()
{
GivenAnOriginalElement(SparkTestNodes.BasicElementNode());
ThenHasAttributeShouldBeFalseFor("theAttribute");
}

[Test]
public void ShouldReturnTrueForHasAttributeIfUnderlyingNodeHasAttributeWithDifferentCase()
{
GivenAnOriginalElement(SparkTestNodes.BasicElementNode());
WhenAnAttributeIsAdded("theATTriBUTE");
ThenHasAttributeShouldBeTrueFor("theAttribute");
}

[Test]
public void GetAttributeShouldReturnEmptyAttributreNodeIfTheAttributeDoesntExist()
{
GivenAnOriginalElement(SparkTestNodes.BasicElementNode());
WhenAttributeIsRetreived("thisAttribute");
ThenGetAttributeResultShouldBeEmpty();
}

[Test]
public void ShouldReturnWrappedAttributeNodeIfItExists()
{
AttributeNode attributeNode = SparkTestNodes.BasicAttributeNode("thisAttribute");
ElementNode node = SparkTestNodes.BasicElementNode().WithAttribute(attributeNode);
GivenAnOriginalElement(node);
WhenAttributeIsRetreived("thisAttribute");
ThenRetrievedAttributeShouldWrap(attributeNode);
}

private void ThenRetrievedAttributeShouldWrap(Node attributeNode)
{
Context.GetAttributeResult.ShouldNotBeNull();
Context.GetAttributeResult.Unwrap().ShouldEqual(attributeNode);
}

private void ThenGetAttributeResultShouldBeEmpty()
{
Context.GetAttributeResult.Unwrap().ShouldBeNull();
}

private void WhenAttributeIsRetreived(string attributeName)
{
Context.GetAttributeResult = Context.Target.GetAttribute(attributeName);
}

private void ThenHasAttributeShouldBeFalseFor(string attributeName)
{
Context.Target.HasAttribute(attributeName).ShouldBeFalse();
}

private void ThenHasAttributeShouldBeTrueFor(string attributeName)
{
Context.Target.HasAttribute(attributeName).ShouldBeTrue();
}


private void ThenTheReturnedAttributeShouldBeWrappedSparkAttributeNamed(string attributeName)
{
Context.AddAttributeResult.Unwrap().As<AttributeNode>().Name.ShouldEqual(attributeName);
}

private void ThenTheWrappedElementShouldHaveABlankAttributeNamed(string attribute)
{
Context.Target.CurrentNode.Attributes.ShouldContain(x => x.Name == attribute);
AttributeNode attributeNode = Context.Target.CurrentNode.Attributes.Where(x => x.Name == attribute).First();
attributeNode.Value.ShouldBeEmpty();
}

private void WhenAnAttributeIsAdded(string attributeName)
{
Context.AddAttributeResult = Context.Target.AddAttribute(attributeName);
}

private void GivenAnOriginalElement(ElementNode elementNode)
{
Context.Target = new SparkElementWrapper(elementNode);
}

public SparkElementWrapperTestContext Context { get; set; }

public class SparkElementWrapperTestContext
{
public SparkElementWrapper Target { get; set; }

public IAttribute AddAttributeResult { get; set; }

public IAttribute GetAttributeResult { get; set; }
}
}
}
Expand Up @@ -22,8 +22,8 @@ public void SetUp()
[Test]
public void WrapAllShouldBeAbleToMapAWholeBunchOfNodes()
{
GivenNodesToWrap(SparkTestNodes.BasicAttributeNode(), SparkTestNodes.BasicElementNode(),
SparkTestNodes.BasicAttributeNode());
GivenNodesToWrap(SparkTestNodes.BasicAttributeNode("attributeName"), SparkTestNodes.BasicElementNode(),
SparkTestNodes.BasicAttributeNode("attributeName"));

WhenAllNodesAreMapped();
TheResultShouldWrapTheOriginalSetOfNodes();
Expand Down Expand Up @@ -53,7 +53,7 @@ private void GivenNodeToUnwrap(IElement element)

private void TheResultShouldWrapTheOriginalSetOfNodes()
{
var wrappedNodes = Context.WrappedNodes.Select(x => x.As<SparkNodeWrapper>().WrappedNode);
var wrappedNodes = Context.WrappedNodes.Select(x => x.As<SparkNodeWrapper>().GetWrappedNode());
Assert.That(wrappedNodes, Is.EqualTo(Context.NodesToWrap));
}

Expand Down
46 changes: 14 additions & 32 deletions src/OpenRasta.Codecs.Spark.UnitTests/SparkTestNodes.cs
Expand Up @@ -13,10 +13,14 @@ public static ElementNode BasicElementNode()
{
return new ElementNode("elementNode", new List<AttributeNode>(), false);
}

public static AttributeNode BasicAttributeNode()
public static ElementNode WithAttribute(this ElementNode elementNode, string name, string value)
{
elementNode.Attributes.Add(new AttributeNode(name,value));
return elementNode;
}
public static AttributeNode BasicAttributeNode(string attributeName)
{
return new AttributeNode("attributeName", "attributeValue");
return new AttributeNode(attributeName, "attributeValue");
}
public static Node UnknownNode()
{
Expand All @@ -26,6 +30,12 @@ private class UnknownNodeImpl : Node
{

}

public static ElementNode WithAttribute(this ElementNode elementnode, AttributeNode attributeNode)
{
elementnode.Attributes.Add(attributeNode);
return elementnode;
}
}

public static class InternalTestNodes
Expand All @@ -37,7 +47,7 @@ public static IElement BasicElementNode()

public static INode BasicAttributeNode()
{
return new SparkAttributeWrapper(SparkTestNodes.BasicAttributeNode());
return new SparkAttributeWrapper(SparkTestNodes.BasicAttributeNode("attributeName"));
}
public static TestElement TestElement(string elementName)
{
Expand Down Expand Up @@ -127,32 +137,4 @@ public void SetExpressionBody(string expression)
_body = expression;
}
}
public class TestAttributeNode :ContentContainingNode, IAttribute
{
private readonly string _value;

public TestAttributeNode(string name, string value) : base(name)
{
_value = value;
}
public IEnumerable<ICodeExpressionNode> CodeNodes
{
get
{
return Nodes.Where(x => (x is ICodeExpressionNode)).Cast<ICodeExpressionNode>();
}
}

public ICodeExpressionNode AddExpression()
{
var codeExpressionNode = new TestCodeExpressionNode();
Nodes.Add(codeExpressionNode);
return codeExpressionNode;
}

public string GetTextValue()
{
return _value;
}
}
}
Expand Up @@ -29,7 +29,7 @@ public void ShouldRecogniseAndWrapElementNodes()
[Test]
public void ShouldRecogniseAndWrapAttributeNodes()
{
GivenASparkNode(SparkTestNodes.BasicAttributeNode());
GivenASparkNode(SparkTestNodes.BasicAttributeNode("attributeName"));
WhenTheNodeIsWrapped();
ThenTheWrappedNodeShouldBe<SparkAttributeWrapper>();
AndTheWrappedNodeShouldWrapTheOriginalNode();
Expand All @@ -45,7 +45,7 @@ public void ShouldMapUnknownNodesToUnknownNodeType()

private void AndTheWrappedNodeShouldWrapTheOriginalNode()
{
Assert.That(Context.WrappedNode.As<SparkNodeWrapper>().WrappedNode, Is.EqualTo(Context.NodeToWrap));
Assert.That(Context.WrappedNode.As<SparkNodeWrapper>().GetWrappedNode(), Is.EqualTo(Context.NodeToWrap));
}

private void ThenTheWrappedNodeShouldBe<TWrapper>()
Expand Down
42 changes: 42 additions & 0 deletions src/OpenRasta.Codecs.Spark.UnitTests/TestAttributeNode.cs
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRasta.Codecs.Spark2.Model;

namespace OpenRasta.Codecs.Spark.UnitTests
{
public class TestAttributeNode :ContentContainingNode, IAttribute
{
private readonly string _value;

public TestAttributeNode(string name, string value) : base(name)
{
_value = value;
}
public IEnumerable<ICodeExpressionNode> CodeNodes
{
get
{
return Nodes.Where(x => (x is ICodeExpressionNode)).Cast<ICodeExpressionNode>();
}
}

public ICodeExpressionNode AddExpression()
{
var codeExpressionNode = new TestCodeExpressionNode();
Nodes.Add(codeExpressionNode);
return codeExpressionNode;
}

public string GetTextValue()
{
return _value;
}

public bool Exists()
{
return true;
}
}

}
Expand Up @@ -2,7 +2,7 @@

namespace OpenRasta.Codecs.Spark2.Specification.Syntax
{
public class Scratch : ISpecificationProvider
public class DefaultSpecification : ISpecificationProvider
{
public IElementTransformerSpecification CreateSpecification()
{
Expand Down
1 change: 1 addition & 0 deletions src/OpenRasta.Codecs.Spark2/Model/IElement.cs
Expand Up @@ -21,5 +21,6 @@ public interface IAttribute : INode
string Name { get; }
ICodeExpressionNode AddExpression();
string GetTextValue();
bool Exists();
}
}
5 changes: 3 additions & 2 deletions src/OpenRasta.Codecs.Spark2/OpenRasta.Codecs.Spark2.csproj
Expand Up @@ -50,6 +50,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Matchers\NodeMatcher.cs" />
<Compile Include="SparkInterface\SparkElementWrapper.cs" />
<Compile Include="SparkInterface\SparkNodeWrapper.cs" />
<Compile Include="Specification\Builders\IElementTransformerActionsByMatchBuilder.cs" />
<Compile Include="Specification\ElementTransformerActionsByMatch.cs" />
<Compile Include="Specification\Builders\ElementTransformerActionsByMatchBuilder.cs" />
Expand All @@ -62,7 +64,7 @@
<Compile Include="Specification\Actions\ConvertAttributeToUriAction.cs" />
<Compile Include="Specification\Helpers\ElementMatchSpecificationExtensions.cs" />
<Compile Include="Specification\Helpers\ElementTransformerExtensions.cs" />
<Compile Include="Specification\Helpers\Scratch.cs" />
<Compile Include="DefaultSpecification.cs" />
<Compile Include="Model\IElement.cs" />
<Compile Include="Model\INode.cs" />
<Compile Include="SparkInterface\ISparkElementTransformer.cs" />
Expand All @@ -74,7 +76,6 @@
<Compile Include="SparkInterface\SparkElementTransformer.cs" />
<Compile Include="SparkInterface\SparkOverrideExtension.cs" />
<Compile Include="SparkInterface\SparkWrapperFactory.cs" />
<Compile Include="Specification\SpecificationProvider.cs" />
<Compile Include="Specification\Helpers\Tag.cs" />
<Compile Include="Syntax\ISyntaxProvider.cs" />
<Compile Include="Transformers\ElementTransformerService.cs" />
Expand Down

0 comments on commit 7772849

Please sign in to comment.