public CommandParserRepository() { mParsers = new Dictionary <string, Func <string[], IParseResult> > { { "ins", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); var status = ParseResultStatus.InvalidMoney; var money = Money.Unknown; var count = 1; if (it.MoveNext()) { int v; if (!int.TryParse(it.Current, out v)) { return(new ParseErrorResult(ParseResultStatus.InvalidMoney)); } var m = MoneyResolver.Resolve(v); if (m.Status != MoneyStatus.Available) { return(new ParseErrorResult(ParseResultStatus.InvalidMoney)); } status = ParseResultStatus.Success; money = m.Type; } if (it.MoveNext()) { if (!int.TryParse(it.Current, out count)) { return(new ParseErrorResult(ParseResultStatus.InvalidMoney)); } if (count <= 0 || count > 100) { return(new ParseErrorResult(ParseResultStatus.InvalidMoney)); } } return(new MoneyInsertionParseResult { Status = status, Money = money, Count = count, }); } }, { "eject", (token) => { return(new MoneyEjectParseResult { Status = ParseResultStatus.Success, }); } }, { "show", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); if (it.MoveNext()) { var subCommand = it.Current; switch (subCommand) { case "amount": return(new ShowAmountParseResult { Status = ParseResultStatus.Success, }); case "item": return(new ShowItemParseResult { Status = ParseResultStatus.Success, }); } } return(new ParseErrorResult(ParseResultStatus.NotSupportedCommand)); } }, { "help", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); var result = new HelpParseResult { Status = ParseResultStatus.Success }; if (it.MoveNext()) { result.Command = it.Current; } return(result); } }, }; }
public void _ヘルプ表示依頼を処理する_コマンド指定() { var repo = new Ninject.StandardKernel() .BindPurchaseContext() .BindRunnerRepository() .Get<IRunnerRepository>() ; var result = new HelpParseResult { Command = "ins"}; var content = ConsoleAppHelper.ListHelpContents() .Where(c => c.Command == result.Command).FirstOrDefault(); var it = (new string[] {content.Usage, content.Description}).GetEnumerator(); var runner = repo.FindRunner(result, (message) => { Assert.That(it.MoveNext(), Is.True); Assert.That(message, Is.Not.Null.And.Not.Empty); Assert.That(message, Is.EqualTo(it.Current)); }); runner(); Assert.That(it.MoveNext(), Is.False); }
public CommandParserRepository() { mParsers = new Dictionary<string, Func<string[], IParseResult>> { { "ins", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); var status = ParseResultStatus.InvalidMoney; var money = Money.Unknown; var count = 1; if (it.MoveNext()) { int v; if (! int.TryParse(it.Current, out v)) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } var m = MoneyResolver.Resolve(v); if (m.Status != MoneyStatus.Available) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } status = ParseResultStatus.Success; money = m.Type; } if (it.MoveNext()) { if (! int.TryParse(it.Current, out count)) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } if (count <= 0 || count > 100) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } } return new MoneyInsertionParseResult { Status = status, Money = money, Count = count, }; } }, { "eject", (token) => { return new MoneyEjectParseResult { Status = ParseResultStatus.Success, }; } }, { "show", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); if (it.MoveNext()) { var subCommand = it.Current; switch (subCommand) { case "amount": return new ShowAmountParseResult { Status = ParseResultStatus.Success, }; case "item": return new ShowItemParseResult { Status = ParseResultStatus.Success, }; } } return new ParseErrorResult(ParseResultStatus.NotSupportedCommand); } }, { "help", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); var result = new HelpParseResult { Status = ParseResultStatus.Success }; if (it.MoveNext()) { result.Command = it.Current; } return result; } }, }; }
public CommandParserRepository() { mParsers = new Dictionary<string, Func<string[], IParseResult>> { { "ins", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); var status = ParseResultStatus.InvalidMoney; var money = Money.Unknown; var count = 1; if (it.MoveNext()) { int v; if (! int.TryParse(it.Current, out v)) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } var m = MoneyResolver.Resolve(v); if (m.Status != MoneyStatus.Available) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } status = ParseResultStatus.Success; money = m.Type; } if (it.MoveNext()) { if (! int.TryParse(it.Current, out count)) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } if (count <= 0) { return new ParseErrorResult(ParseResultStatus.InvalidMoney); } } return new MoneyInsertionParseResult { Status = status, Money = money, Count = count, }; } }, { "eject", (token) => { return new MoneyEjectParseResult { Status = ParseResultStatus.Success, }; } }, { "show", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); if (it.MoveNext()) { var subCommand = it.Current; switch (subCommand) { case "amount": return new ShowAmountParseResult { Status = ParseResultStatus.Success, }; case "item": return new ShowItemParseResult { Status = ParseResultStatus.Success, }; } } return new ParseErrorResult(ParseResultStatus.NotSupportedCommand); } }, { "buy", (tokens) => { var tmp = tokens .Skip(1) .Select(s => { int p; return new { Valid = int.TryParse(s, out p), Position = p}; }) ; if (tmp.All(t => t.Valid)) { var positions = tmp .Select(p => p.Position) .ToArray() ; return new PurchaseParseResult { Status = ParseResultStatus.Success, Positions = positions }; } else { return new ParseErrorResult(ParseResultStatus.InvalidArgs); } } }, { "help", (tokens) => { var it = tokens.Skip(1).GetEnumerator(); var result = new HelpParseResult { Status = ParseResultStatus.Success }; if (it.MoveNext()) { result.Command = it.Current; } return result; } }, }; }