Exemple #1
0
 /// <summary>
 /// Parses a byte array into an <see cref="IBObject"/>.
 /// </summary>
 /// <param name="parser"></param>
 /// <param name="bytes">The bytes to parse.</param>
 /// <returns>The parsed object.</returns>
 public static IBObject Parse(this IBObjectParser parser, byte[] bytes)
 {
     using (var stream = new MemoryStream(bytes))
     {
         return(parser.Parse(stream));
     }
 }
Exemple #2
0
        public void Add_ParserWithNonIBObjectType_ThrowsArgumentException(Type type, IBObjectParser parser)
        {
            var    list   = new BObjectParserList();
            Action action = () => list.Add(type, parser);

            action.ShouldThrow <ArgumentException>("because only IBObject types are allowed");
        }
        public void Add_ParserWithNonIBObjectType_ThrowsArgumentException(Type type, IBObjectParser parser)
        {
            var list = new BObjectParserList();
            Action action = () => list.Add(type, parser);

            action.ShouldThrow<ArgumentException>("because only IBObject types are allowed");
        }
Exemple #4
0
 /// <summary>
 /// Parses a byte array into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
 /// </summary>
 /// <param name="parser"></param>
 /// <param name="bytes">The bytes to parse.</param>
 /// <returns>The parsed object.</returns>
 public static T Parse <T>(this IBObjectParser <T> parser, byte[] bytes) where T : IBObject
 {
     using (var stream = new MemoryStream(bytes))
     {
         return(parser.Parse(stream));
     }
 }
Exemple #5
0
 /// <summary>
 /// Parses a bencoded string into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
 /// </summary>
 /// <param name="parser"></param>
 /// <param name="bencodedString">The bencoded string to parse.</param>
 /// <returns>The parsed object.</returns>
 public static T ParseString <T>(this IBObjectParser <T> parser, string bencodedString) where T : IBObject
 {
     using (var stream = bencodedString.AsStream(parser.Encoding))
     {
         return(parser.Parse(stream));
     }
 }
Exemple #6
0
 /// <summary>
 /// Parses a bencoded string into an <see cref="IBObject"/>.
 /// </summary>
 /// <param name="parser"></param>
 /// <param name="bencodedString">The bencoded string to parse.</param>
 /// <returns>The parsed object.</returns>
 public static IBObject ParseString(this IBObjectParser parser, string bencodedString)
 {
     using (var stream = bencodedString.AsStream(parser.Encoding))
     {
         return(parser.Parse(stream));
     }
 }
        public void Parse_String_CallsOverriddenParse(IBObjectParser <IBObject> parserMock)
        {
            var parser = new MockBObjectParser(parserMock);

            parser.ParseString("bencoded string");

            parserMock.Received().Parse(Arg.Any <BencodeReader>());
        }
        public void Add_GenericParser_ContainsOnlyThatParser(IBObjectParser<IBObject> parser)
        {
            var list = new BObjectParserList();
            list.Add(parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Value == parser);
        }
        public void Add_GenericParser_AddedWithGenericTypeAsKey(IBObjectParser<IBObject> parser)
        {
            var list = new BObjectParserList();
            list.Add(parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Key == typeof(IBObject));
        }
        public void FirstCharDigit_CallsStringParser(string bencode, IBObjectParser<BString> stringParser)
        {
            var bparser = new BencodeParser();
            bparser.Parsers.AddOrReplace(stringParser);
            bparser.ParseString(bencode);

            stringParser.Received(1).Parse(Arg.Any<BencodeStream>());
        }
        public void Add_ParserWithType_ReplacesExistingOfSameGenericType(IBObjectParser parser)
        {
            var list = new BObjectParserList();
            list.Add(typeof(BString), parser);
            list.Add(typeof(BString), parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Value == parser);
        }
        public void Add_GenericParser_ReplacesExistingOfSameGenericType(IBObjectParser<IBObject> parser)
        {
            var list = new BObjectParserList();
            list.Add(parser);
            list.Add(parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Value == parser);
        }
        public void FirstChar_D_CallsNumberParser(string bencode, IBObjectParser <BDictionary> dictionaryParser)
        {
            var bparser = new BencodeParser();

            bparser.Parsers.AddOrReplace(dictionaryParser);
            bparser.ParseString(bencode);

            dictionaryParser.Received(1).Parse(Arg.Any <BencodeStream>());
        }
        public void FirstChar_L_CallsNumberParser(string bencode, IBObjectParser <BList> listParser)
        {
            var bparser = new BencodeParser();

            bparser.Parsers.AddOrReplace(listParser);
            bparser.ParseString(bencode);

            listParser.Received(1).Parse(Arg.Any <BencodeStream>());
        }
Exemple #15
0
        public void Add_GenericParser_ContainsOnlyThatParser(IBObjectParser <IBObject> parser)
        {
            var list = new BObjectParserList();

            list.Add(parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Value == parser);
        }
        public void FirstCharDigit_CallsStringParser(string bencode, IBObjectParser <BString> stringParser)
        {
            var bparser = new BencodeParser();

            bparser.Parsers.AddOrReplace(stringParser);
            bparser.ParseString(bencode);

            stringParser.Received(1).Parse(Arg.Any <BencodeStream>());
        }
Exemple #17
0
        public void Add_GenericParser_AddedWithGenericTypeAsKey(IBObjectParser <IBObject> parser)
        {
            var list = new BObjectParserList();

            list.Add(parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Key == typeof(IBObject));
        }
Exemple #18
0
        public void Clear_EmptiesList(IBObjectParser <BString> parser1, IBObjectParser <BNumber> parser2)
        {
            var list = new BObjectParserList {
                parser1, parser2
            };

            list.Clear();

            list.Should().BeEmpty();
        }
Exemple #19
0
        internal static async ValueTask <T> ParseStringAsync <T>(this IBObjectParser <T> parser, string bencodedString) where T : IBObject
        {
            var bytes = Encoding.UTF8.GetBytes(bencodedString).AsMemory();

            var(reader, writer) = new Pipe();
            await writer.WriteAsync(bytes);

            writer.Complete();
            return(await parser.ParseAsync(reader));
        }
Exemple #20
0
        public void Add_ParserWithType_ReplacesExistingOfSameGenericType(IBObjectParser parser)
        {
            var list = new BObjectParserList();

            list.Add(typeof(BString), parser);
            list.Add(typeof(BString), parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Value == parser);
        }
Exemple #21
0
        public void Add_GenericParser_ReplacesExistingOfSameGenericType(IBObjectParser <IBObject> parser)
        {
            var list = new BObjectParserList();

            list.Add(parser);
            list.Add(parser);

            list.Should().HaveCount(1);
            list.Should().ContainSingle(x => x.Value == parser);
        }
        public void Add_WithMultipleTypes_AddsParserForEachType(IBObjectParser parser)
        {
            var types = new[] {typeof (BString), typeof (BNumber), typeof (BList)};

            var list = new BObjectParserList();
            list.Add(types, parser);

            list.Should().HaveCount(3);
            list.Should().OnlyContain(x => x.Value == parser);
        }
Exemple #23
0
        public void Add_WithMultipleTypes_AddsParserForEachType(IBObjectParser parser)
        {
            var types = new[] { typeof(BString), typeof(BNumber), typeof(BList) };

            var list = new BObjectParserList();

            list.Add(types, parser);

            list.Should().HaveCount(3);
            list.Should().OnlyContain(x => x.Value == parser);
        }
        public void Parse_Stream_CallsOverriddenParse(IBObjectParser <IBObject> parserMock)
        {
            var parser = new MockBObjectParser(parserMock);
            var bytes  = Encoding.UTF8.GetBytes("bencoded string");

            using (var stream = new MemoryStream(bytes))
            {
                parser.Parse(stream);
            }

            parserMock.Received().Parse(Arg.Any <BencodeReader>());
        }
        public void FirstChar_D_CallsNumberParser(string bencode, IBObjectParser<BDictionary> dictionaryParser)
        {
            var bparser = new BencodeParser();
            bparser.Parsers.AddOrReplace(dictionaryParser);
            bparser.ParseString(bencode);

            dictionaryParser.Received(1).Parse(Arg.Any<BencodeStream>());
        }
 public MockBObjectParser(IBObjectParser <IBObject> substitute)
 {
     Substitute = substitute;
 }
        public void Clear_EmptiesList(IBObjectParser<BString> parser1, IBObjectParser<BNumber> parser2)
        {
            var list = new BObjectParserList {parser1, parser2};
            list.Clear();

            list.Should().BeEmpty();
        }
        public void FirstChar_L_CallsNumberParser(string bencode, IBObjectParser<BList> listParser)
        {
            var bparser = new BencodeParser();
            bparser.Parsers.AddOrReplace(listParser);
            bparser.ParseString(bencode);

            listParser.Received(1).Parse(Arg.Any<BencodeStream>());
        }