public void InvalidValue_ThrowsInvalidBencodeException(IBencodeParser bparser, BString someKey) { bparser.Parse <BString>(Arg.Any <BencodeReader>()).Returns(someKey); bparser.Parse(Arg.Any <BencodeReader>()).Throws <BencodeException>(); var parser = new BDictionaryParser(bparser); Action action = () => parser.ParseString("di42ee"); action.Should().Throw <InvalidBencodeException <BDictionary> >().WithMessage("*Could not parse dictionary value*"); }
public void DuplicateKey_ThrowsInvalidBencodeException(IBencodeParser bparser, BString someKey, BString someValue) { bparser.Parse <BString>(Arg.Any <BencodeReader>()).Returns(someKey, someKey); bparser.Parse(Arg.Any <BencodeReader>()).Returns(someValue); var parser = new BDictionaryParser(bparser); Action action = () => parser.ParseString("di42ee"); action.Should().Throw <InvalidBencodeException <BDictionary> >().WithMessage("*The dictionary already contains the key*"); }
/// <summary> /// Parses a bencoded file into an <see cref="IBObject"/> of type <typeparamref name="T"/>. /// </summary> /// <param name="parser"></param> /// <param name="filePath">The path to the file to parse.</param> /// <returns>The parsed object.</returns> public static T Parse <T>(this IBencodeParser parser, string filePath) where T : class, IBObject { using (var stream = File.OpenRead(filePath)) { return(parser.Parse <T>(stream)); } }
/// <summary> /// Parses a bencoded string into an <see cref="IBObject"/> of type <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam> /// <param name="parser"></param> /// <param name="bencodedString">The bencoded string to parse.</param> /// <returns>The parsed object.</returns> public static T ParseString <T>(this IBencodeParser parser, string bencodedString) where T : class, IBObject { using (var stream = bencodedString.AsStream(parser.Encoding)) { return(parser.Parse <T>(stream)); } }
/// <summary> /// Parses a bencoded array of bytes into an <see cref="IBObject"/> of type <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam> /// <param name="parser"></param> /// <param name="bytes">The bencoded bytes to parse.</param> /// <returns>The parsed object.</returns> public static T Parse <T>(this IBencodeParser parser, byte[] bytes) where T : class, IBObject { using (var stream = new MemoryStream(bytes)) { return(parser.Parse <T>(stream)); } }
/// <summary> /// Parses a bencoded file into an <see cref="IBObject"/>. /// </summary> /// <param name="parser"></param> /// <param name="filePath">The path to the file to parse.</param> /// <returns>The parsed object.</returns> public static IBObject Parse(this IBencodeParser parser, string filePath) { using (var stream = File.OpenRead(filePath)) { return(parser.Parse(stream)); } }
/// <summary> /// Parses a bencoded array of bytes into an <see cref="IBObject"/>. /// </summary> /// <param name="parser"></param> /// <param name="bytes">The bencoded bytes to parse.</param> /// <returns>The parsed object.</returns> public static IBObject Parse(this IBencodeParser parser, byte[] bytes) { using (var stream = new MemoryStream(bytes)) { return(parser.Parse(stream)); } }
/// <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 IBencodeParser parser, string bencodedString) { using (var stream = bencodedString.AsStream(parser.Encoding)) { return(parser.Parse(stream)); } }
/// <summary> /// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam> /// <param name="parser"></param> /// <param name="stream">The stream to parse.</param> /// <returns>The parsed object.</returns> public static T Parse <T>(this IBencodeParser parser, Stream stream) where T : class, IBObject { using (var reader = new BencodeReader(stream, leaveOpen: true)) { return(parser.Parse <T>(reader)); } }
/// <summary> /// Parses a stream into an <see cref="IBObject"/>. /// </summary> /// <param name="parser"></param> /// <param name="stream">The stream to parse.</param> /// <returns>The parsed object.</returns> public static IBObject Parse(this IBencodeParser parser, Stream stream) { using (var reader = new BencodeReader(stream, leaveOpen: true)) { return(parser.Parse(reader)); } }
public void MissingEndChar_ThrowsInvalidBencodeException(string bencode, IBencodeParser bparser, BString someKey, IBObject someValue) { // Arrange bparser.Parse <BString>(Arg.Any <BencodeReader>()) .Returns(someKey); bparser.Parse(Arg.Any <BencodeReader>()) .Returns(someValue) .AndSkipsAhead(bencode.Length - 1); // Act var parser = new BDictionaryParser(bparser); Action action = () => parser.ParseString(bencode); // Assert action.Should().Throw <InvalidBencodeException <BDictionary> >().WithMessage("*Missing end character of object*"); }
private static void SetupBencodeParser(IBencodeParser bparser, string bencode, BString key, IBObject value, bool hasEndChar) { bparser.Parse <BString>(Arg.Any <BencodeStream>()) .Returns(key); bparser.Parse(Arg.Any <BencodeStream>()) .Returns(value) .AndDoes(x => { // Set stream position to end of list, skipping all "parsed" content var stream = x.Arg <BencodeStream>(); stream.Position += Math.Max(1, bencode.Length - 1); if (hasEndChar) { stream.Position--; } }); }
public void CanParseSimple(string bencode, IBencodeParser bparser) { // Arange var key = new BString("key"); var value = new BString("value"); bparser.Parse <BString>(Arg.Any <BencodeReader>()) .Returns(key); bparser.Parse(Arg.Any <BencodeReader>()) .Returns(value) .AndSkipsAhead(bencode.Length - 2); // Act var parser = new BDictionaryParser(bparser); var bdictionary = parser.ParseString(bencode); // Assert bdictionary.Count.Should().Be(1); bdictionary.Should().ContainKey(key); bdictionary[key].Should().BeSameAs(value); }
private static void SetupBencodeParser(IBencodeParser bparser, string bencode, BString key, IBObject value, bool hasEndChar) { bparser.Parse<BString>(Arg.Any<BencodeStream>()) .Returns(key); bparser.Parse(Arg.Any<BencodeStream>()) .Returns(value) .AndDoes(x => { // Set stream position to end of list, skipping all "parsed" content var stream = x.Arg<BencodeStream>(); stream.Position += Math.Max(1, bencode.Length - 1); if (hasEndChar) stream.Position--; }); }
public void CanParseSimple(string bencode, IBencodeParser bparser) { // Arrange var bstring = new BString("test"); bparser.Parse(Arg.Any <BencodeReader>()) .Returns(bstring) .AndSkipsAhead(bencode.Length - 2); // Act var parser = new BListParser(bparser); var blist = parser.ParseString(bencode); // Assert blist.Count.Should().Be(1); blist[0].Should().BeOfType <BString>(); blist[0].Should().BeSameAs(bstring); bparser.Received(1).Parse(Arg.Any <BencodeReader>()); }