Skip to content

Commit

Permalink
Merge branch 'master' of github.com:DarthFubuMVC/fubumvc
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed May 6, 2010
2 parents c0a2a18 + 8273374 commit 8567d36
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 1 deletion.
162 changes: 162 additions & 0 deletions src/FubuMVC.Tests/UI/DefaultHtmlConventionsTester.cs
@@ -1,10 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Routing;
using FubuCore;
using FubuCore.Reflection;
using FubuMVC.Core;
using FubuMVC.Core.Runtime;
using FubuMVC.StructureMap;
using FubuMVC.UI;
using FubuMVC.UI.Configuration;
using FubuMVC.UI.Tags;
using HtmlTags;
using NUnit.Framework;
using StructureMap;

namespace FubuMVC.Tests.UI
{
Expand Down Expand Up @@ -63,4 +71,158 @@ public void do_not_add_element_name_to_span()
span.HasAttr("name").ShouldBeFalse();
}
}


[TestFixture]
public class when_generating_an_input_for_a_string_property : using_the_default_html_conventions
{
protected override HtmlTag createTag()
{
return Generator.InputFor(x => x.Address1);
}

[Test]
public void should_create_a_text_input_tag()
{
Tag.ShouldHaveTagName("input");
Tag.ShouldHaveAttribute("type", "text");
}

[Test]
public void should_name_the_tag_using_the_property_name()
{
Tag.ShouldHaveAttribute("name", "Address1");
}

[Test]
public void should_populate_the_input_with_the_current_value()
{
Tag.ShouldHaveAttribute("value", Model.Address1);
}
}

[TestFixture]
public class when_generating_a_display_for_a_string_property : using_the_default_html_conventions
{
protected override HtmlTag createTag()
{
return Generator.DisplayFor(x => x.Address1);
}

[Test]
public void should_create_a_span()
{
Tag.ShouldHaveTagName("span");
}

[Test]
public void should_display_the_current_value()
{
Tag.Text().ShouldEqual(Model.Address1);
}
}


[TestFixture]
public class when_generating_a_label_for_a_deep_property : using_the_default_html_conventions
{
protected override HtmlTag createTag()
{
return Generator.LabelFor(x => x.DateEntered.Month);
}

[Test]
public void should_create_a_label_tag()
{
Tag.ShouldHaveTagName("label");
}

[Test]
public void should_display_the_name_of_the_last_property_in_the_chain()
{
Tag.Text().ShouldEqual("Month");
}
}

[TestFixture]
public class when_generating_a_label_for_a_camel_case_property : using_the_default_html_conventions
{
protected override HtmlTag createTag()
{
return Generator.LabelFor(x => x.DateEntered);
}

[Test]
public void should_display_the_name_of_the_property_as_words_broken_up_by_casing()
{
Tag.Text().ShouldEqual("Date Entered");
}
}

[TestFixture]
public class BreakUpCamelCaseTester
{
[Test]
public void should_consider_case_changes_as_word_boundaries()
{
DefaultHtmlConventions.BreakUpCamelCase("DateEntered").ShouldEqual("Date Entered");
}

[Test]
public void should_consider_numbers_as_word_boundaries()
{
DefaultHtmlConventions.BreakUpCamelCase("The1Day2").ShouldEqual("The 1 Day 2");
}

[Test]
public void should_not_consider_consecutive_numbers_as_word_boundaries()
{
DefaultHtmlConventions.BreakUpCamelCase("Address22").ShouldEqual("Address 22");
}

[Test]
public void should_not_consider_consecutive_numbers_between_words_as_word_boundaries_()
{
DefaultHtmlConventions.BreakUpCamelCase("Address223City").ShouldEqual("Address 223 City");
}

[Test]
public void should_consider_underscores_as_word_boundaries()
{
DefaultHtmlConventions.BreakUpCamelCase("Date_Entered").ShouldEqual("Date Entered");
}
}

public class using_the_default_html_conventions
{
protected TagGenerator<Address> Generator;
protected HtmlTag Tag;
protected Address Model;

[SetUp]
public void Setup()
{
var registry = new FubuRegistry(x => x.HtmlConvention<DefaultHtmlConventions>());
var container = new Container(x => x.For<IFubuRequest>().Singleton());
var facility = new StructureMapContainerFacility(container);

new FubuBootstrapper(facility, registry).Bootstrap(new List<RouteBase>());
Model = new Address
{
Address1 = "123 Main St.",
DateEntered = new DateTime(2010, 5, 25, 11, 30, 0),
Order = 42
};
container.GetInstance<IFubuRequest>().Set(Model);

Generator = container.GetInstance<TagGenerator<Address>>();
Generator.Model = Model;
Tag = createTag();
}

protected virtual HtmlTag createTag()
{
return null;
}
}
}
17 changes: 16 additions & 1 deletion src/FubuMVC.UI/Configuration/DefaultHtmlConventions.cs
@@ -1,3 +1,5 @@
using System.Linq;
using System.Text.RegularExpressions;
using FubuMVC.UI.Tags;
using HtmlTags;

Expand All @@ -14,14 +16,27 @@ public DefaultHtmlConventions()

Editors.Always.Modify(AddElementName);
Displays.Always.BuildBy(req => new HtmlTag("span").Text(req.StringValue()));
Labels.Always.BuildBy(req => new HtmlTag("span").Text(req.Accessor.Name));
Labels.Always.BuildBy(req => new HtmlTag("label").Text(BreakUpCamelCase(req.Accessor.FieldName)));

BeforePartial.Always.BuildBy(req => new NoTag());
AfterPartial.Always.BuildBy(req => new NoTag());
BeforeEachOfPartial.Always.BuildBy((req, index, count) => new NoTag());
AfterEachOfPartial.Always.BuildBy((req, index, count) => new NoTag());
}

public static string BreakUpCamelCase(string fieldName)
{
var patterns = new[]
{
"([a-z])([A-Z])",
"([0-9])([a-zA-Z])",
"([a-zA-Z])([0-9])"
};
var output = patterns.Aggregate(fieldName,
(current, pattern) => Regex.Replace(current, pattern, "$1 $2", RegexOptions.IgnorePatternWhitespace));
return output.Replace('_', ' ');
}

public static void AddElementName(ElementRequest request, HtmlTag tag)
{
if (tag.IsInputElement())
Expand Down

0 comments on commit 8567d36

Please sign in to comment.