-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from BiggerNoise/feature/user-token-feature
Add User Tokens to Roundhouse
- Loading branch information
Showing
11 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
74 changes: 74 additions & 0 deletions
74
product/roundhouse.tests/infrastructure.app/tokens/UserTokenParserSpecs.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using roundhouse.infrastructure.app; | ||
|
||
namespace roundhouse.tests.infrastructure.app.tokens | ||
{ | ||
using roundhouse.infrastructure.app.tokens; | ||
using System.IO; | ||
|
||
public class UserTokenParserSpecs | ||
{ | ||
[Concern(typeof(UserTokenParser))] | ||
public class when_parsing_from_text : TinySpec | ||
{ | ||
protected static object result; | ||
|
||
public override void Context() | ||
{ | ||
} | ||
|
||
public override void Because() | ||
{ | ||
} | ||
|
||
[Observation] | ||
public void if_given_keyvalues_should_parse_to_dictionary() | ||
{ | ||
var dictionary = UserTokenParser.Parse("UserId=123;UserName=Some Name"); | ||
dictionary.should_be_an_instance_of<Dictionary<string, string>>(); | ||
dictionary.should_only_contain( | ||
new KeyValuePair<string, string>("UserId", "123"), | ||
new KeyValuePair<string, string>("UserName", "Some Name")); | ||
} | ||
|
||
[Observation] | ||
public void if_given_filepath_with_keyvalues_should_parse_to_dictionary() | ||
{ | ||
var filename = Path.GetTempFileName() + ".txt"; | ||
File.WriteAllText(filename, "UserId=123" + Environment.NewLine + "UserName=Some Name"); | ||
try | ||
{ | ||
var dictionary = UserTokenParser.Parse(filename); | ||
dictionary.should_be_an_instance_of<Dictionary<string, string>>(); | ||
dictionary.should_only_contain( | ||
new KeyValuePair<string, string>("UserId", "123"), | ||
new KeyValuePair<string, string>("UserName", "Some Name")); | ||
} | ||
finally | ||
{ | ||
File.Delete(filename); | ||
} | ||
} | ||
[Observation] | ||
public void if_given_wrong_syntax_text_without_equals_sign_should_throw_format_exception() | ||
{ | ||
Action action = () => | ||
{ | ||
var dictionary = UserTokenParser.Parse("UserId123User&NameSome Name"); | ||
}; | ||
action.should_throw_an<FormatException>(); | ||
} | ||
[Observation] | ||
public void if_given_empty_text_should_throw_argument_null_exception() | ||
{ | ||
Action action = () => | ||
{ | ||
var dictionary = UserTokenParser.Parse(""); | ||
}; | ||
action.should_throw_an<ArgumentNullException>(); | ||
} | ||
} | ||
|
||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace roundhouse.infrastructure.app.tokens | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
public class UserTokenParser | ||
{ | ||
public static Dictionary<string, string> Parse(string option) | ||
{ | ||
if (String.IsNullOrEmpty(option)) | ||
throw new ArgumentNullException("option"); | ||
|
||
var textToParse = option; | ||
var pairs = textToParse.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (pairs.Length == 1 && File.Exists(textToParse)) | ||
{ | ||
textToParse = File.ReadAllText(textToParse); | ||
pairs = textToParse.Split(new string[] { ";",Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); | ||
} | ||
|
||
if (pairs.Any(p => !p.Contains("="))) throw new FormatException("Wrong format"); | ||
|
||
return pairs.ToDictionary(p => p.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[0], | ||
p => p.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1]); | ||
} | ||
} | ||
} |
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
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