Created
July 31, 2009 20:44
-
-
Save joeybeninghove/159442 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
namespace TrackAbout.Test.Unit.Spikes | |
{ | |
public class ClientConfigurationJsonConverterSpecs | |
{ | |
public class when_serializing_the_client_configuration_to_a_JSON_string : ContextSpecification | |
{ | |
private string ClientConfigurationJson; | |
private ClientConfiguration ClientConfiguration; | |
protected override void When() | |
{ | |
ClientConfiguration = new ClientConfiguration(); | |
ClientConfigurationJson = new ClientConfigurationJsonConverter().Serialize(ClientConfiguration); | |
} | |
[Test] | |
public void it_should_return_a_non_null_string() | |
{ | |
ClientConfigurationJson.ShouldNotBeNull(); | |
} | |
} | |
public class when_deserializing_the_client_configuration : ContextSpecification | |
{ | |
private string ClientConfigurationJson; | |
private ClientConfiguration OriginalClientConfiguration; | |
private ClientConfiguration DeserializedClientConfiguration; | |
protected override void When() | |
{ | |
OriginalClientConfiguration = new ClientConfiguration(); | |
ClientConfigurationJson = new ClientConfigurationJsonConverter().Serialize(OriginalClientConfiguration); | |
DeserializedClientConfiguration = new ClientConfigurationJsonConverter().Deserialize(ClientConfigurationJson); | |
} | |
[Test] | |
public void it_should_be_identical_to_the_original_client_configuration_that_was_serialized() | |
{ | |
foreach (var property in typeof(ClientConfiguration).GetProperties()) | |
{ | |
Console.Out.WriteLine("Validating property: {0}", property.Name); | |
property.GetValue(DeserializedClientConfiguration, null) | |
.ShouldEqual(property.GetValue(OriginalClientConfiguration, null)); | |
} | |
} | |
} | |
} | |
public class ClientConfigurationJsonConverter | |
{ | |
public string Serialize(ClientConfiguration clientConfiguration) | |
{ | |
return Converter.Serialize(clientConfiguration, ConvertValidationPatternsToRegEx); | |
} | |
private bool ConvertValidationPatternsToRegEx(string name, ref object value) | |
{ | |
if (name.Contains("WarnOnSerialNumberPatterns")) | |
{ | |
var palmPattern = value.ToString(); | |
value = TAUtil.ConvertPalmPatternToRegex(palmPattern); | |
} | |
else if (name.Contains("TrackNumValidationPatterns")) | |
{ | |
var palmPattern = value.ToString(); | |
value = TAUtil.ConvertPalmPatternToRegex(palmPattern); | |
} | |
return true; | |
} | |
public ClientConfiguration Deserialize(string json) | |
{ | |
return Converter.Deserialize<ClientConfiguration>(json); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment