Пример #1
0
        public void WriteSymbolWithSymbolTable(String tableSym, String newSym, String expectedText)
        {
            var symbol = "symbols:[\"s1\", \"" + tableSym + "\"]";
            var text   =
                SystemSymbols.IonSymbolTable + "::" +
                "{" +
                symbol +
                "}\n" +
                "$10\n" +
                "$11\n";

            var ionValueFactory = new ValueFactory();
            var datagram        = IonLoader.Default.Load(text);

            datagram.Add(ionValueFactory.NewSymbol(newSym));

            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            datagram.WriteTo(textWriter);
            textWriter.Finish();
            var expected = expectedText;
            var actual   = sw.ToString();

            Assert.AreEqual(expected, actual);
        }
Пример #2
0
 /// <inheritdoc/>
 public IIonWriter Create(IonSerializationOptions options, Stream stream)
 {
     return(IonTextWriterBuilder.Build(
                new StreamWriter(stream),
                IonTextOptions.Json
                ));
 }
Пример #3
0
        public void RespectPrettyTextSerializationFormat()
        {
            var serializer =
                new IonSerializer(new IonSerializationOptions {
                Format = IonSerializationFormat.PRETTY_TEXT
            });
            MemoryStream testStream = (MemoryStream)serializer.Serialize(TestObjects.John);
            string       testString = System.Text.Encoding.UTF8.GetString(testStream.ToArray());

            IIonValue    ionValue             = IonLoader.Default.Load(TestObjects.JohnIonText).GetElementAt(0);
            StringWriter expectedStringWriter = new StringWriter();

            using (var writer =
                       IonTextWriterBuilder.Build(expectedStringWriter, new IonTextOptions {
                PrettyPrint = true
            }))
            {
                ionValue.WriteTo(writer);
                writer.Finish();
            }

            string expectedString = expectedStringWriter.ToString();

            Assert.AreEqual(expectedString, testString);
        }
Пример #4
0
        public void TestMiscMethods()
        {
            // coverage for Digest(), IsInStruct, SetFieldName(), AddTypeAnnotation()
            StringWriter   stringWriter = new StringWriter();
            IIonHashWriter ihw          = IonHashWriterBuilder
                                          .Standard()
                                          .WithHasherProvider(new IdentityIonHasherProvider())
                                          .WithWriter(IonTextWriterBuilder.Build(stringWriter))
                                          .Build();

            TestUtil.AssertEquals(new byte[] { }, ihw.Digest(), "Digests don't match.");

            ihw.WriteNull();
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x0f, 0x0e }, ihw.Digest(), "Digests don't match.");

            ihw.StepIn(IonType.List);
            Assert.ThrowsException <InvalidOperationException>(ihw.Digest);

            ihw.WriteInt(5);
            Assert.ThrowsException <InvalidOperationException>(ihw.Digest);

            ihw.StepOut();
            TestUtil.AssertEquals(new byte[] { 0x0b, 0xb0, 0x0b, 0x20, 0x05, 0x0e, 0x0e }, ihw.Digest(), "Digests don't match.");

            ihw.WriteNull();
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x0f, 0x0e }, ihw.Digest(), "Digests don't match.");

            Assert.IsFalse(ihw.IsInStruct);

            ihw.StepIn(IonType.Struct);
            Assert.IsTrue(ihw.IsInStruct);

            ihw.SetFieldName("hello");
            ihw.AddTypeAnnotation("ion");
            ihw.AddTypeAnnotation("hash");
            ihw.WriteSymbol("world");

            ihw.StepOut();
            Assert.IsFalse(ihw.IsInStruct);
            TestUtil.AssertEquals(new byte[] { 0x0b, 0xd0,
                                               0x0c, 0x0b, 0x70, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0c, 0x0e, // hello:
                                               0x0c, 0x0b, 0xe0,
                                               0x0c, 0x0b, 0x70, 0x69, 0x6f, 0x6e, 0x0c, 0x0e,             // ion::
                                               0x0c, 0x0b, 0x70, 0x68, 0x61, 0x73, 0x68, 0x0c, 0x0e,       // hash::
                                               0x0c, 0x0b, 0x70, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0c, 0x0e, // world
                                               0x0c, 0x0e,
                                               0x0e },
                                  ihw.Digest(),
                                  "Digests don't match.");

            ihw.Finish();

            //Commented out following assertion because it is failing
            //https://github.com/amzn/ion-hash-dotnet/issues/5
            //Assert.AreEqual("null [5] null {hello:ion::hash::world}", stringWriter);
        }
Пример #5
0
        public void TestExtraStepOut()
        {
            IIonHashWriter ihw = IonHashWriterBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithWriter(IonTextWriterBuilder.Build(new StringWriter()))
                                 .Build();

            Assert.ThrowsException <InvalidOperationException>(ihw.StepOut);
        }
Пример #6
0
        public static Stream MakeIonClob(string clob)
        {
            var stream = new MemoryStream();
            var writer = IonTextWriterBuilder.Build(new StreamWriter(stream));

            writer.WriteClob(Encoding.ASCII.GetBytes(clob));
            writer.Flush();
            writer.Finish();
            stream.Position = 0;
            return(stream);
        }
Пример #7
0
        public void WriteSymbolWithSymbolTableOutOfRange()
        {
            var datagram = SymTabUtils.DatagramWithOutOfRangeSymbolsInSymbolTable();

            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            // Should throw exception as sid 12 in datagram exceeds the max ID of the symbol table currently in scope
            datagram.WriteTo(textWriter);
            textWriter.Finish();
        }
Пример #8
0
            internal override void Traverse(IIonReader reader, TestIonHasherProvider hasherProvider)
            {
                this.hasherProvider = hasherProvider;
                MemoryStream   ms     = new MemoryStream();
                StreamWriter   sw     = new StreamWriter(ms);
                IIonWriter     writer = IonTextWriterBuilder.Build(sw);
                IIonHashWriter ihw    = new IonHashWriter(writer, this.hasherProvider);

                ihw.WriteValues(reader);
                ihw.Digest();
                ihw.Dispose();
            }
Пример #9
0
        public void QuotedSymbolAndFieldName()
        {
            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            textWriter.StepIn(IonType.Struct);
            textWriter.SetFieldName("true");
            textWriter.AddTypeAnnotation("ion");
            textWriter.WriteSymbol("null");
            textWriter.StepOut();

            Assert.AreEqual("{'true':ion::'null'}", sw.ToString());
        }
Пример #10
0
        public void NoQuotedSymbolAndFieldName()
        {
            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            textWriter.StepIn(IonType.Struct);
            textWriter.SetFieldName("hello");
            textWriter.AddTypeAnnotation("ion");
            textWriter.WriteSymbol("world");
            textWriter.StepOut();

            Assert.AreEqual("{hello:ion::world}", sw.ToString());
        }
 /// <inheritdoc/>
 public IIonWriter Create(IonSerializationOptions options, Stream stream)
 {
     return(options.Format switch
     {
         BINARY => IonBinaryWriterBuilder.Build(stream),
         TEXT => IonTextWriterBuilder.Build(new StreamWriter(stream)),
         PRETTY_TEXT => IonTextWriterBuilder.Build(
             new StreamWriter(stream),
             new IonTextOptions {
             PrettyPrint = true
         }),
         _ => throw new InvalidOperationException($"Format {options.Format} not supported"),
     });
Пример #12
0
        public void Test(TestValue tv, string s)
        {
            IIonHashWriter hashWriter = null;

            try
            {
                hashWriter = IonHashWriterBuilder.Standard()
                             .WithWriter(IonTextWriterBuilder.Build(new StringWriter()))
                             .WithHasherProvider(HasherProvider)
                             .Build();
                hashWriter.WriteValues(IonReaderBuilder.Build(s));
            }
            catch (IonException e)
            {
                if (tv.IsValidIon())
                {
                    throw e;
                }
            }

            IIonHashReader hashReader = null;

            try
            {
                hashReader = IonHashReaderBuilder.Standard()
                             .WithReader(IonReaderBuilder.Build(s))
                             .WithHasherProvider(HasherProvider)
                             .Build();
                hashReader.MoveNext();
                hashReader.MoveNext();
            }
            catch (IonException e)
            {
                if (tv.IsValidIon())
                {
                    throw e;
                }
            }

            if (tv.validIon == null || tv.validIon.Value == true)
            {
                TestUtil.AssertEquals(
                    hashWriter.Digest(),
                    hashReader.Digest(),
                    "Reader/writer hashes for line |" + tv.AsIon() + "| as |" + s + "| don't match");
            }
        }
Пример #13
0
        public IonSystemLite(IonTextWriterBuilder textWriterBuilder, PrivateIonBinaryWriterBuilder binaryWriterBuilder,
                             IonReaderBuilder readerBuilder)
        {
            _readerBuilder = readerBuilder;
            var catalog = textWriterBuilder.Catalog;

            //make sure we're on the same catalog here
            Debug.Assert(catalog == binaryWriterBuilder.Catalog);
            Debug.Assert(catalog == readerBuilder.Catalog);

            Context            = new ContainerlessContext(this);
            Catalog            = catalog;
            _systemSymbolTable = binaryWriterBuilder.InitialSymbolTable;
            Debug.Assert(_systemSymbolTable.IsSystem);
            binaryWriterBuilder.SymtabValueFactory = this;

            _textWriterBuilder   = textWriterBuilder.Immutable();
            _binaryWriterBuilder = binaryWriterBuilder.Immutable();
        }
Пример #14
0
        private static void RoundTrip_AssertText(IIonValue datagram, ISymbolTable readerTable)
        {
            var sw     = new StringWriter();
            var writer = IonTextWriterBuilder.Build(sw, new IonTextOptions {
                PrettyPrint = true
            }, readerTable.GetImportedTables());

            datagram.WriteTo(writer);
            writer.Finish();
            var text = sw.ToString();

            Console.WriteLine(text);
            var catalog   = Symbols.GetReaderCatalog(readerTable);
            var datagram2 = IonLoader.WithReaderOptions(new ReaderOptions {
                Catalog = catalog
            }).Load(text);

            AssertDatagramEquivalent(datagram, datagram2);
        }
Пример #15
0
        /// <summary>
        /// Apply the same writing logic to binary and text writers and assert the accuracy
        /// </summary>
        protected void AssertReaderWriter(Action <IIonReader> assertReader, Action <IIonWriter> writerFunc)
        {
            //bin
            using (var s = new MemoryStream())
            {
                var binWriter = IonBinaryWriterBuilder.Build(s);
                writerFunc(binWriter);
                s.Seek(0, SeekOrigin.Begin);
                var binReader = IonReaderBuilder.Build(s);
                assertReader(binReader);
            }

            //text
            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            writerFunc(textWriter);
            var str        = sw.ToString();
            var textReader = IonReaderBuilder.Build(str);

            assertReader(textReader);
        }
Пример #16
0
 public void Initialize()
 {
     this.sw         = new StringWriter();
     this.jsonWriter = IonTextWriterBuilder.Build(this.sw, IonTextOptions.Json);
     this.value      = factory.NewEmptyStruct();
 }