Пример #1
0
        internal IEnumerable<string[]> Parse(Notification note)
        {
            var parsed = new List<string[]>();

            var length = _commandLineArguments.Count();

            for (var i = 0; i < length; i++)
            {
                var fst = _commandLineArguments.ElementAt(i);
                var snd = i < length - 1 ? _commandLineArguments.ElementAt(i + 1) : _commandLineArguments.ElementAt(0);
                var sndIsCmd = IsCommand(snd);

                string cmd;

                if (TryParseCommand(fst, out cmd))
                {
                    var value = sndIsCmd
                        ? String.Empty
                        : snd;

                    parsed.Add(new[] { cmd, value });
                    continue;
                }

                if (sndIsCmd)
                    continue;

                note.AddError(String.Format("Expected a command but got \'{0}\'.", i == 0 ? fst : snd));
                break;
            }

            return parsed;
        }
Пример #2
0
        public void Parse_Rainy_Tests(string args, string err)
        {
            var a = new NoteArguments(args.Split(';'), new List<ArgInfo>());
            var note = new Notification();
            var parsed = a.Parse(note);

            Check.That(note.HasErrors).IsTrue();
            Check.That(note.Errors.Count()).IsEqualTo(1);
            Check.That(note.Errors.First()).IsEqualTo(err);
        }
Пример #3
0
        public void Parse_Happy_Tests(string args, string expected, int expectedCount)
        {
            var a = new NoteArguments(args.Split(';'), new List<ArgInfo>());
            var note = new Notification();
            var parsed = a.Parse(note);

            var expectedParsed = expected.Split(';').Select(x => x.Split('='));

            Check.That(parsed.Count()).IsEqualTo(expectedCount);
            for (var i = 0; i < parsed.Count(); i++)
            {
                Check.That(parsed.ElementAt(i)).ContainsExactly(expectedParsed.ElementAt(i));
            }
            Check.That(note.IsValid).IsTrue();
        }
Пример #4
0
        public Notification Validation()
        {
            var note = new Notification();

            var parsed = Parse(note);

            if (note.HasErrors)
                return note;

            CheckNoDuplicates(parsed, note);
            CheckAllRequired(parsed, note);
            CheckAllDefined(parsed, note);

            if (note.IsValid)
                _dictionary = parsed.ToDictionary(x => x.First(), x => x.Skip(1).First());

            return note;
        }
Пример #5
0
        private void CheckNoDuplicates(IEnumerable<string[]> args, Notification note)
        {
            var duplicates = args
                .Where(x => _definedCommands.Contains(x.First()))
                .GroupBy(x => x.First())
                .Where(x => x.Count() > 1)
                .Select(x => x.Key)
                .Distinct();

            foreach (var duplicate in duplicates)
            {
                note.AddError(String.Format("Duplicate command \'{0}\'.", duplicate));
            }
        }
Пример #6
0
 private void CheckAllRequired(IEnumerable<string[]> args, Notification note)
 {
     foreach (var arg in _requiredCommands.Where(x => !args.Select(arg => arg.First()).Contains(x)))
     {
         note.AddError(String.Format("Required command \'{0}\' not in argument list.", arg));
     }
 }
Пример #7
0
 private void CheckAllDefined(IEnumerable<string[]> args, Notification note)
 {
     foreach (var arg in args.Select(x => x.First()).Distinct().Where(arg => !_definedCommands.Contains(arg)))
     {
         note.AddError(String.Format("Command \'{0}\' not defined.", arg));
     }
 }