Beispiel #1
0
        public void canRead_length()
        {
            var reader = new StringReader("abc");

            reader.CanRead(1).Should().BeTrue();
            reader.CanRead(2).Should().BeTrue();
            reader.CanRead(3).Should().BeTrue();
            reader.CanRead(4).Should().BeFalse();
            reader.CanRead(5).Should().BeFalse();
        }
Beispiel #2
0
        public void CanRead()
        {
            var reader = new StringReader("abc");

            reader.CanRead().Should().BeTrue();
            reader.Skip();             // 'a'
            reader.CanRead().Should().BeTrue();
            reader.Skip();             // 'b'
            reader.CanRead().Should().BeTrue();
            reader.Skip();             // 'c'
            reader.CanRead().Should().BeFalse();
        }
Beispiel #3
0
        public static bool TryParseOffer(Discord.IUser user, Discord.IUser target, string input, out TradeOffer offer)
        {
            offer = null;

            input = input.Trim();
            var reader = new StringReader(input);

            // item,amount item,amount item

            bool requested = false;
            bool remainder = false;

            if (!reader.CanRead())
            {
                return(false); // throw new Exception("Expected a string to read but returned null");
            }
            var offered  = new Dictionary <string, int>();
            var requests = new Dictionary <string, int>();

            while (reader.CanRead())
            {
                string arg;
                int    amount = 1;

                if (!reader.GetRemaining().Contains(' '))
                {
                    arg       = reader.GetRemaining();
                    remainder = true;
                }
                else
                {
                    arg = reader.ReadUntil(' ');
                }

                if (arg == Separator)
                {
                    if (requested)
                    {
                        return(false); // throw new Exception("Offer separator has already been specified");
                    }
                    requested = true;
                    continue;
                }

                if (arg.Contains(','))
                {
                    if (arg.Count(x => x == ',') > 1)
                    {
                        return(false); // throw new Exception("Expected a single comma separator");
                    }
                    int.TryParse(arg[arg.IndexOf(',')..], out amount);
Beispiel #4
0
        public void TestParseGreedyString()
        {
            var reader = new StringReader("Hello world! This is a test.");

            Arguments.GreedyString().Parse(reader).Should().BeEquivalentTo("Hello world! This is a test.");
            reader.CanRead().Should().Be(false);
        }
Beispiel #5
0
 public IEnumerable <CommandNode <TSource> > GetRelevantNodes(StringReader input)
 {
     if (_literals.Count > 0)
     {
         var cursor = input.Cursor;
         while (input.CanRead() && input.Peek() != ' ')
         {
             input.Skip();
         }
         var text = input.String.Substring(cursor, input.Cursor - cursor);
         input.Cursor = cursor;
         if (_literals.TryGetValue(text, out var literal))
         {
             yield return(literal);
         }
         else
         {
             foreach (var node in _arguments.Values)
             {
                 yield return(node);
             }
         }
     }
     else
     {
         foreach (var node in _arguments.Values)
         {
             yield return(node);
         }
     }
 }
        public void Parse()
        {
            var reader = new StringReader("15");

            Float().Parse(reader).Should().Be(15f);
            reader.CanRead().Should().Be(false);
        }
        public void Parse()
        {
            var reader = new StringReader("15");

            Arguments.Double().Parse(reader).Should().Be(15.0);
            reader.CanRead().Should().Be(false);
        }
Beispiel #8
0
        public void UuidArgument_ShouldHaveTrailingCharacters()
        {
            // Arrange
            UuidArgument  argument = new UuidArgument();
            IStringReader reader   = new StringReader("1-2-3-4-5!!!");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsTrue(reader.CanRead(3));
        }
Beispiel #9
0
        public void TeamArgument_StringReaderShouldHaveTrailingCharacters()
        {
            // Arrange
            TeamArgument  argument = new TeamArgument();
            IStringReader reader   = new StringReader("foo!@#");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsTrue(reader.CanRead(3));
        }
        public void StringReader_ShouldHaveTrailingCharacters()
        {
            // Arrange
            ResourceLocationArgument argument = new ResourceLocationArgument();
            IStringReader            reader   = new StringReader("foo:barBAZ");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsTrue(reader.CanRead());
        }
        public void EntityArgument_ParseShouldSucceed_WithUuid()
        {
            // Arrange
            EntityArgument argument = new EntityArgument();
            IStringReader  reader   = new StringReader("1-2-3-4-5");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsFalse(reader.CanRead());
        }
        public void EntityArgument_ParseShouldSucceed_WithPlayername()
        {
            // Arrange
            EntityArgument argument = new EntityArgument();
            IStringReader  reader   = new StringReader("Steve");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsFalse(reader.CanRead());
        }
        public void ScoreHolderArgument_ShouldNotRead_SpaceCharacter()
        {
            // Arrange
            ScoreHolderArgument argument = new ScoreHolderArgument();
            IStringReader       reader   = new StringReader("foo bar");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsTrue(reader.CanRead(4));
        }
Beispiel #14
0
        public void MessageArgument_ParseShouldSucceed_WithInvalidSelectorType()
        {
            // Arrange
            MessageArgument argument = new MessageArgument();
            IStringReader   reader   = new StringReader("Hello @m! This is a test.");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsFalse(reader.CanRead());
        }
Beispiel #15
0
        public void MessageArgument_ParseShouldSucceed()
        {
            // Arrange
            MessageArgument argument = new MessageArgument();
            IStringReader   reader   = new StringReader("Hello world! This is a test.");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsFalse(reader.CanRead());
        }
        public void StringArgument_WithTypeWord_HasTrailingCharacters()
        {
            // Arrange
            StringArgument argument = new StringArgument(StringType.WORD);
            IStringReader  reader   = new StringReader("foo!@#");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsTrue(reader.CanRead(3));
        }
        public void StringArgument_ParseShouldSucceed_WithTypeGreedy()
        {
            // Arrange
            StringArgument argument = new StringArgument(StringType.GREEDY);
            IStringReader  reader   = new StringReader("Hello world! This is a test.");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsFalse(reader.CanRead());
        }
Beispiel #18
0
        private int Parse(StringReader reader)
        {
            var start = reader.Cursor;

            if (reader.CanRead(Literal.Length))
            {
                var end = start + Literal.Length;
                if (reader.String.Substring(start, end - start).Equals(Literal))
                {
                    reader.Cursor = end;
                    if (!reader.CanRead() || reader.Peek() == ' ')
                    {
                        return(end);
                    }
                    else
                    {
                        reader.Cursor = start;
                    }
                }
            }
            return(-1);
        }
        public void SwizzleArgument_ShouldNotRead_SpaceCharacter()
        {
            // Arrange
            HashSet <char> characters = new HashSet <char>()
            {
                'a', 'b', 'c'
            };
            SwizzleArgument argument = new SwizzleArgument(characters);
            IStringReader   reader   = new StringReader("ab c");

            // Act
            argument.Parse(reader, out _);

            // Assert
            Assert.IsTrue(reader.CanRead(2));
        }
Beispiel #20
0
        public InputResult TryParse(Input input)
        {
            var result = new InputResult();

            result.Source = input.Text;
            string text = input.Text;

            // name parameters
            if (text.StartsWith(Name, CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase))
            {
                Logger.Debug($"Reading input: \"{text}\"");
                result.IsSuccess = true;
                result.Input     = this;

                var reader = new StringReader(text);
                reader.Skip(Name.Length);
                reader.SkipWhiteSpace();

                while (reader.CanRead())
                {
                    // TODO: Handle required arguments here.
                    string arg = reader.ReadUnquotedString();
                    reader.SkipWhiteSpace();
                    Logger.Debug($"Parsing argument: \"{arg}\"");

                    if (!string.IsNullOrWhiteSpace(arg))
                    {
                        result.Args.Add(arg);
                    }
                    else
                    {
                        result.IsSuccess = false;
                        break;
                    }
                }
            }
            else
            {
                result.IsSuccess = false;
            }

            Logger.Debug($"Parsed {Name} ({result.IsSuccess})");
            return(result);
        }
Beispiel #21
0
        // the input given would have to be: --name value
        // -alias value
        public bool TryParse(string input, out object result)
        {
            result = null;
            var reader = new StringReader(input);

            bool isExplicit = false;
            bool isStart    = true;
            bool isArray    = true;

            string name   = ""; // --NAME VALUE
            var    values = new List <object>();

            var value = new StringBuilder(); // this is the builder for each value.

            while (reader.CanRead())
            {
                char c = reader.Read();

                if (isStart)
                {
                    isStart = false;

                    if (c == '-')
                    {
                        if (reader.Peek() == '-')
                        {
                            isExplicit = true;
                            continue;
                        }
                    }

                    // If any of the aliases are specified, continue.
                    if (Aliases.Contains(reader.ReadString()))
                    {
                        if (Type == null)
                        {
                            return(true);
                        }

                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }

                if (isExplicit)
                {
                    name = reader.ReadUnquotedString();

                    if (name != Name)
                    {
                        return(false);
                    }
                    else
                    {
                        if (Type == null)
                        {
                            return(true);
                        }

                        reader.SkipWhiteSpace(); // skip all of the whitespace between --NAME VALUE
                        continue;
                    }
                }

                if (isArray)
                {
                    if (c == ']') // This is the end of the value, close reader.
                    {
                        reader.Skip();
                        result = values;
                        return(true);
                    }

                    if (c == ',') // This is a value separator, end and attach value.
                    {
                        if (TypeParser.TryParse(Type, value.ToString(), out result))
                        {
                            values.Add(result);
                            value.Clear();
                            continue;
                        }
                    }

                    value.Append(c);
                    continue;
                }

                if (reader.Peek() == '[') // This is the start of the value.
                {
                    isArray = true;
                    continue;
                }

                return(TypeParser.TryParse(Type, reader.ReadString(), out result));
            }

            return(false);
        }