Created
September 21, 2009 15:16
-
-
Save davetheninja/190295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using Core.Web; | |
using Machine.Specifications; | |
namespace Core.Specs.Web | |
{ | |
[Subject(typeof(TempDataExtensions))] | |
public class when_setting_success_flash | |
{ | |
Establish context = () => | |
{ | |
_tempData = new TempDataDictionary(); | |
}; | |
Because of = () => _tempData.Flash(FlashResult.Success); | |
It should_contain_a_success_flash = () => | |
{ | |
var flash = (Flash) _tempData["FLASH"]; | |
flash.Result.ShouldEqual(FlashResult.Success); | |
}; | |
static TempDataDictionary _tempData; | |
} | |
[Subject(typeof(TempDataExtensions))] | |
public class when_setting_warning_flash | |
{ | |
Establish context = () => | |
{ | |
_tempData = new TempDataDictionary(); | |
}; | |
Because of = () => _tempData.Flash(FlashResult.Warning); | |
It should_contain_a_warning_flash = () => | |
{ | |
var flash = (Flash)_tempData["FLASH"]; | |
flash.Result.ShouldEqual(FlashResult.Warning); | |
}; | |
static TempDataDictionary _tempData; | |
} | |
[Subject(typeof(TempDataExtensions))] | |
public class when_setting_error_flash | |
{ | |
Establish context = () => | |
{ | |
_tempData = new TempDataDictionary(); | |
}; | |
Because of = () => _tempData.Flash(FlashResult.Error); | |
It should_contain_a_error_flash = () => | |
{ | |
var flash = (Flash)_tempData["FLASH"]; | |
flash.Result.ShouldEqual(FlashResult.Error); | |
}; | |
static TempDataDictionary _tempData; | |
} | |
[Subject(typeof(TempDataExtensions))] | |
public class when_setting_flash_with_custom_messages | |
{ | |
Establish context = () => | |
{ | |
_tempData = new TempDataDictionary(); | |
_messages = new [] { new FlashMessage("Hello"), new FlashMessage("World") }; | |
}; | |
Because of = () => _tempData.Flash(FlashResult.Error, _messages); | |
It should_contain_the_flash_messages = () => | |
{ | |
var flash = (Flash)_tempData["FLASH"]; | |
flash.Messages.ShouldContainOnly(_messages); | |
}; | |
static TempDataDictionary _tempData; | |
static FlashMessage[] _messages; | |
} | |
} | |
=========================================================== | |
using System.Collections.Generic; | |
namespace Core.Web | |
{ | |
public class Flash | |
{ | |
public Flash(FlashResult result, IEnumerable<FlashMessage> messages) | |
{ | |
Result = result; | |
Messages = messages; | |
} | |
public FlashResult Result { get; private set; } | |
public IEnumerable<FlashMessage> Messages { get; private set; } | |
} | |
} | |
=========================================================== | |
namespace Core.Web | |
{ | |
public struct FlashMessage | |
{ | |
readonly string _message; | |
public FlashMessage(string message) | |
{ | |
_message = message; | |
} | |
public static explicit operator string(FlashMessage instance) | |
{ | |
return instance._message; | |
} | |
} | |
} | |
=========================================================== | |
namespace Core.Web | |
{ | |
public enum FlashResult | |
{ | |
Success = 0, | |
Warning = 1, | |
Error = 2 | |
} | |
} | |
=========================================================== | |
using System.Collections.Generic; | |
using System.Web.Mvc; | |
namespace Core.Web | |
{ | |
public static class TempDataExtensions | |
{ | |
public static void Flash(this TempDataDictionary tempDataDictionary, FlashResult result) | |
{ | |
tempDataDictionary.Flash(result, new List<FlashMessage>()); | |
} | |
public static void Flash(this TempDataDictionary tempDataDictionary, FlashResult result, IEnumerable<FlashMessage> messages) | |
{ | |
if (tempDataDictionary.ContainsKey("FLASH")) tempDataDictionary.Remove("FLASH"); | |
tempDataDictionary.Add("FLASH", new Flash(result, messages)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment