public void SkipsComments() { const string s = @"BT 21 32 Td %A comment here 0 Tr ET"; var input = StringBytesTestConverter.Convert(s, false); var result = parser.Parse(1, input.Bytes, log); Assert.Equal(4, result.Count); Assert.Equal(BeginText.Value, result[0]); var moveLine = Assert.IsType <MoveToNextLineWithOffset>(result[1]); Assert.Equal(21, moveLine.Tx); Assert.Equal(32, moveLine.Ty); var renderingMode = Assert.IsType <SetTextRenderingMode>(result[2]); Assert.Equal(TextRenderingMode.Fill, renderingMode.Mode); Assert.Equal(EndText.Value, result[3]); }
public void CorrectlyWritesOperations() { var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Parser", "SimpleGoogleDocPageContent.txt"); var content = File.ReadAllText(path); var input = StringBytesTestConverter.Convert(content, false); var result = parser.Parse(1, input.Bytes, log); var replacementRegex = new Regex(@"\s(\.\d+)\b"); using (var stream = new MemoryStream()) { foreach (var operation in result) { operation.Write(stream); } var text = OtherEncodings.BytesAsLatin1String(stream.ToArray()); text = LineEndingsToWhiteSpace(text); content = LineEndingsToWhiteSpace(content); content = replacementRegex.Replace(content, " 0$1"); Assert.Equal(content, text); } }
public void HandlesEscapedLineBreaks() { const string s = @"q 1 0 0 1 48 434 cm BT 0.0001 Tc 19 0 0 19 0 0 Tm /Tc1 1 Tf ( \(sleep 1; printf ""QUIT\\r\\n""\) | ) Tj ET Q"; var input = StringBytesTestConverter.Convert(s, false); var result = parser.Parse(1, input.Bytes, log); Assert.Equal(9, result.Count); Assert.IsType <Push>(result[0]); Assert.IsType <ModifyCurrentTransformationMatrix>(result[1]); Assert.IsType <BeginText>(result[2]); Assert.IsType <SetCharacterSpacing>(result[3]); Assert.IsType <SetTextMatrix>(result[4]); Assert.IsType <SetFontAndSize>(result[5]); var text = Assert.IsType <ShowText>(result[6]); Assert.IsType <EndText>(result[7]); Assert.IsType <Pop>(result[8]); Assert.Equal(@" (sleep 1; printf ""QUIT\r\n"") | ", text.Text); }
private PdfTokenScanner GetScanner(string s, TestObjectLocationProvider locationProvider = null) { var input = StringBytesTestConverter.Convert(s, false); return(new PdfTokenScanner(input.Bytes, locationProvider ?? new TestObjectLocationProvider(), new TestFilterProvider(), NoOpEncryptionHandler.Instance)); }
public void CorrectlyWritesSmallTextContent() { const string s = @"BT /F13 48 Tf 20 38 Td 1 Tr 2 w (ABC) Tj ET"; var input = StringBytesTestConverter.Convert(s, false); var result = parser.Parse(1, input.Bytes, log); using (var stream = new MemoryStream()) { foreach (var operation in result) { operation.Write(stream); } var text = OtherEncodings.BytesAsLatin1String(stream.ToArray()); text = LineEndingsToWhiteSpace(text).Trim(); var expected = LineEndingsToWhiteSpace(s); Assert.Equal(expected, text); } }
public void ScansSpecificationSimpleDictionaryExampleContents() { const string s = @"/Type /Example /Subtype /DictionaryExample /Version 0.01 /IntegerItem 12 /StringItem(a string)"; var tokens = new List <IToken>(); var scanner = scannerFactory(StringBytesTestConverter.Convert(s, false).Bytes); while (scanner.MoveNext()) { tokens.Add(scanner.CurrentToken); } AssertCorrectToken <NameToken, CosName>(tokens[0], CosName.TYPE); AssertCorrectToken <NameToken, CosName>(tokens[1], CosName.Create("Example")); AssertCorrectToken <NameToken, CosName>(tokens[2], CosName.SUBTYPE); AssertCorrectToken <NameToken, CosName>(tokens[3], CosName.Create("DictionaryExample")); AssertCorrectToken <NameToken, CosName>(tokens[4], CosName.VERSION); AssertCorrectToken <NumericToken, decimal>(tokens[5], 0.01m); AssertCorrectToken <NameToken, CosName>(tokens[6], CosName.Create("IntegerItem")); AssertCorrectToken <NumericToken, decimal>(tokens[7], 12m); AssertCorrectToken <NameToken, CosName>(tokens[8], CosName.Create("StringItem")); AssertCorrectToken <StringToken, string>(tokens[9], "a string"); }
public void SecondPartInvalidFormatFalse() { var input = StringBytesTestConverter.Convert("85 9t", false); var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var _); Assert.False(result); }
public void TryReadIncorrectFormatMultiplePartsFalse() { var input = StringBytesTestConverter.Convert("76362 100 1000", false); var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var _); Assert.False(result); }
public void NullInputBytesThrows() { var input = StringBytesTestConverter.Convert("11 0 obj", false); Action action = () => FileTrailerParser.GetFirstCrossReferenceOffset(null, new CoreTokenScanner(input.Bytes), false); Assert.Throws <ArgumentNullException>(action); }
public void CanParseAfmFile() { var input = StringBytesTestConverter.Convert(CourierAfmSnippet, false); var metrics = AdobeFontMetricsParser.Parse(input.Bytes, false); Assert.NotNull(metrics); }
public void NullScannerThrows() { var input = StringBytesTestConverter.Convert("11 0 obj", false); Action action = () => parser.GetFirstCrossReferenceOffset(input.Bytes, null, false); Assert.Throws <ArgumentNullException>(action); }
public void CurrentByteIsNotEndOfLineFalse() { var input = StringBytesTestConverter.Convert("\r something \n", false); var result = tokenizer.TryTokenize((byte)'\0', input.Bytes, out var _); Assert.False(result); }
public void InvalidFirstCharacter_ReturnsFalse(string s) { var input = StringBytesTestConverter.Convert(s); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.False(result); Assert.Null(token); }
public void CannotTokenizeInvalidBytes(string s) { var input = StringBytesTestConverter.Convert(s); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.False(result); Assert.Null(token); }
public void HandlesUtf16Strings(string s, string expected) { var input = StringBytesTestConverter.Convert(s); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(expected, AssertHexToken(token).Data); }
public void HandlesDash() { var input = StringBytesTestConverter.Convert("-"); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(0m, AssertNumericToken(token).Data); }
public void ParsesValidNumbers(string s, decimal expected) { var input = StringBytesTestConverter.Convert(s); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(expected, AssertNumericToken(token).Data); }
public void IgnoreInvalidSingleHex() { var input = StringBytesTestConverter.Convert("/Invalid#Z"); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal("Invalid#Z", AssertNameToken(token).Data); }
public void WritesEscapedCharactersToOutput(string input, string expected) { var bytes = StringBytesTestConverter.Convert(input); var result = tokenizer.TryTokenize(bytes.First, bytes.Bytes, out var token); Assert.True(result); Assert.Equal(expected, AssertStringToken(token).Data); }
public void EndsNameFollowingInvalidHex() { var input = StringBytesTestConverter.Convert("/Hex#/Name"); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal("Hex#", AssertNameToken(token).Data); }
public void CorrectlyExtractsOperations() { var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Parser", "SimpleGoogleDocPageContent.txt"); var content = File.ReadAllText(path); var input = StringBytesTestConverter.Convert(content, false); var result = parser.Parse(1, input.Bytes, log); Assert.NotEmpty(result); }
public void ReadsName_NotAtForwardSlash_Throws() { const string s = " /Type"; var input = StringBytesTestConverter.Convert(s); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var _); Assert.False(result); }
public void ReadsValidPdfNames(string s, string expected) { var input = StringBytesTestConverter.Convert(s); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(expected, AssertNameToken(token).Data.Name); }
public void HandleDoubleDashedNumber() { // This is a really weird format but seen in the wild. PDF, shine on, you crazy diamond. var input = StringBytesTestConverter.Convert("--10.25"); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(-10.25m, AssertNumericToken(token).Data); }
public void TextNullReturnsNullToken() { var input = StringBytesTestConverter.Convert("null "); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(NullToken.Instance, token); }
public void CurrentByteIsEndOfLineTrue() { var input = StringBytesTestConverter.Convert("\n", false); var result = tokenizer.TryTokenize((byte)'\n', input.Bytes, out var token); Assert.True(result); Assert.Same(EndOfLineToken.Token, token); }
public void TryTokenizeWhitespaceFalse() { var input = StringBytesTestConverter.Convert(" something"); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.False(result); Assert.Null(token); }
public void ValidTrue() { var input = StringBytesTestConverter.Convert("12 32", false); var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var definition); Assert.True(result); Assert.Equal(12, definition.FirstNumber); Assert.Equal(32, definition.Count); }
public void OnlyParsesNumberPart() { var input = StringBytesTestConverter.Convert("135.6654/Type"); var result = tokenizer.TryTokenize(input.First, input.Bytes, out var token); Assert.True(result); Assert.Equal(135.6654m, AssertNumericToken(token).Data); Assert.Equal('/', (char)input.Bytes.CurrentByte); }
public void ValidWithLongTrue() { var input = StringBytesTestConverter.Convert("214748364700 6", false); var result = TableSubsectionDefinition.TryRead(log, input.Bytes, out var definition); Assert.True(result); Assert.Equal(214748364700L, definition.FirstNumber); Assert.Equal(6, definition.Count); }