Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlReaderCommonTests.cs
index 4ac6da7d7e5bcc15b5842045507d81e4f776edc1..26f461aeb30ff94cdb2f6dea7afffb0ae06499e8 100644 (file)
@@ -15,6 +15,8 @@ using System.Text;
 using System.Xml;\r
 using System.Xml.Schema;\r
 using System.Xml.XPath;\r
+using System.Threading;\r
+using System.Threading.Tasks;\r
 \r
 using NUnit.Framework;\r
 \r
@@ -197,14 +199,12 @@ namespace MonoTests.System.Xml
                        document.LoadXml (xml);\r
                        xnr = new XmlNodeReader (document);\r
                        method (xnr);\r
-#if NET_2_0\r
 /*\r
                        // XPathNavigatorReader tests\r
                        System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument (new StringReader (xml));\r
                        XmlReader xpr = doc.CreateNavigator ().ReadSubtree ();\r
                        method (xpr);\r
 */\r
-#endif\r
                }\r
 \r
 \r
@@ -1344,6 +1344,7 @@ namespace MonoTests.System.Xml
 \r
                [Test]\r
                [Category ("NotDotNet")]\r
+               [Ignore ("Bug in Microsoft referencesource")]\r
                public void IndexerAndAttributes ()\r
                {\r
                        string xml = @"<?xml version='1.0' standalone='no'?><foo _1='1' _2='2' _3='3' />";\r
@@ -1509,7 +1510,6 @@ namespace MonoTests.System.Xml
                        reader.Read (); // silently returns false\r
                }\r
 \r
-#if NET_2_0\r
                [Test]\r
                public void CreateSimple ()\r
                {\r
@@ -1536,7 +1536,7 @@ namespace MonoTests.System.Xml
                // a bit revised version of bug #78706\r
                public void CreateFromUrlClose ()\r
                {\r
-                       string file = "Test/XmlFiles/78706.xml";\r
+                       string file = Path.Combine (Path.GetTempPath (), "78706.xml");\r
                        try {\r
                                if (!File.Exists (file))\r
                                        File.Create (file).Close ();\r
@@ -1556,7 +1556,7 @@ namespace MonoTests.System.Xml
                // a bit revised version of bug #385638\r
                public void CreateFromUrlClose2 ()\r
                {\r
-                       string file = "Test/XmlFiles/385638.xml";\r
+                       string file = Path.Combine (Path.GetTempPath (), "385638.xml");\r
                        try {\r
                                if (File.Exists (file))\r
                                        File.Delete (file);\r
@@ -1752,6 +1752,19 @@ namespace MonoTests.System.Xml
                        Assert.AreEqual (3, count, "#3");\r
                }\r
 \r
+               [Test, Category("NotWorking")]\r
+               public void ReadToNextSiblingInInitialReadState ()\r
+               {\r
+                       var xml = "<Text name=\"hello\"><Something></Something></Text>";\r
+                       var ms = new MemoryStream(Encoding.Default.GetBytes(xml));\r
+                       var xtr = XmlReader.Create(ms);\r
+\r
+                       Assert.AreEqual(xtr.ReadState, ReadState.Initial);\r
+                       xtr.ReadToNextSibling("Text");\r
+\r
+                       Assert.AreEqual("hello", xtr.GetAttribute("name"));\r
+               }\r
+\r
                [Test]\r
                public void ReadSubtree ()\r
                {\r
@@ -2230,7 +2243,7 @@ namespace MonoTests.System.Xml
                        Assert.AreEqual (XmlNodeType.Text, reader.NodeType, "#2");\r
                        bytesRead = reader.ReadElementContentAsBase64 (fixedSizeBuffer, 0, fixedSizeBuffer.Length);\r
                        Assert.AreEqual (0, bytesRead, "#3");\r
-                       Assert.AreEqual (XmlNodeType.EndElement, reader.NodeType, "#4");\r
+                       Assert.AreEqual (XmlNodeType.None, reader.NodeType, "#4");\r
                }\r
 \r
                [Test]\r
@@ -2265,6 +2278,71 @@ namespace MonoTests.System.Xml
                        Assert.AreEqual (arr [0], ret [0], "#2");\r
                        Assert.AreEqual (arr [1], ret [1], "#3");\r
                }\r
-#endif\r
+\r
+               [Test]\r
+               public void ReadContentAs ()\r
+               {\r
+                       var xr = XmlReader.Create (new StringReader ("<doc a=' 1 '/>"));\r
+                       xr.Read ();\r
+                       xr.MoveToAttribute ("a");\r
+\r
+                       Assert.AreEqual ((Byte) 1, xr.ReadContentAs (typeof (Byte), null), "#1");\r
+                       Assert.AreEqual ((SByte) 1, xr.ReadContentAs (typeof (SByte), null), "#2");\r
+                       Assert.AreEqual ((Int16) 1, xr.ReadContentAs (typeof (Int16), null), "#3");\r
+                       Assert.AreEqual ((UInt16) 1, xr.ReadContentAs (typeof (UInt16), null), "#4");\r
+                       Assert.AreEqual ((Int32) 1, xr.ReadContentAs (typeof (Int32), null), "#5");\r
+                       Assert.AreEqual ((UInt32) 1, xr.ReadContentAs (typeof (UInt32), null), "#6");\r
+                       Assert.AreEqual ((Int64) 1, xr.ReadContentAs (typeof (Int64), null), "#7");\r
+                       Assert.AreEqual ((UInt64) 1, xr.ReadContentAs (typeof (UInt64), null), "#8");\r
+               }\r
+\r
+               [Test]\r
+               [ExpectedException(typeof(InvalidOperationException))]\r
+               public void MustSetAsyncFlag ()\r
+               {\r
+                       var r = XmlReader.Create (new StringReader ("<root/>"));\r
+                       r.ReadAsync ();\r
+               }\r
+\r
+               Exception RunAsync (Action action)\r
+               {\r
+                       var task = Task<Exception>.Run (async () => {\r
+                               try {\r
+                                       action ();\r
+                                       return null;\r
+                               } catch (Exception ex) {\r
+                                       return ex;\r
+                               }\r
+                       });\r
+                       task.Wait ();\r
+                       Assert.That (task.IsCompleted);\r
+                       return task.Result;\r
+               }\r
+\r
+               [Test]\r
+               public void SimpleAsync ()\r
+               {\r
+                       var xml = "<root test=\"monkey\"/>";\r
+                       var task = Task<Exception>.Run (async () => {\r
+                               try {\r
+                                       var s = new XmlReaderSettings ();\r
+                                       s.Async = true;\r
+                                       var r = XmlReader.Create (new StringReader (xml), s);\r
+\r
+                                       Assert.That (await r.ReadAsync ());\r
+                                       Assert.That (r.MoveToFirstAttribute ());\r
+\r
+                                       Assert.AreEqual (await r.GetValueAsync (), "monkey");\r
+                                       r.Close ();\r
+                                       return null;\r
+                               } catch (Exception ex) {\r
+                                       return ex;\r
+                               }\r
+                       });\r
+                       task.Wait ();\r
+                       Assert.That (task.IsCompleted);\r
+                       if (task.Result != null)\r
+                               throw task.Result;\r
+               }\r
        }\r
 }\r