⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textfieldparsertest.cs

📁 大名鼎鼎的mono是.NET平台的跨平台(支持linux
💻 CS
📖 第 1 页 / 共 2 页
字号:
// TextFieldParserTest.cs - NUnit Test Cases for Microsoft.VisualBasic.FileIO.TextFieldParser//// Rolf Bjarne Kvinge  (RKvinge@novell.com)//// // Copyright (C) 2007 Novell, Inc (http://www.novell.com)//// Permission is hereby granted, free of charge, to any person obtaining// a copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to// permit persons to whom the Software is furnished to do so, subject to// the following conditions:// // The above copyright notice and this permission notice shall be// included in all copies or substantial portions of the Software.// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.//#if NET_2_0using System;using System.Collections.Generic;using System.Text;using NUnit.Framework;using Microsoft.VisualBasic.FileIO;using Microsoft.VisualBasic;using System.IO;namespace MonoTests.Microsoft_VisualBasic.FileIO{	[TestFixture]	public class TextFieldParserTest	{		[Test]		public void CloseTest ()		{			TextFieldParser t = new TextFieldParser (new System.IO.MemoryStream());			t.Close ();			t.Close ();		}				[Test]		public void DelimitedTest1 ()		{			string [] delimiters;			string text;						delimiters = new string [] {";"};			text = "a;bb;ccc;dddd" + Constants.vbNewLine + "111;22;3";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				Assert.AreEqual ("a:bb:ccc:dddd", Strings.Join (t.ReadFields (), ":"), "#A1");				Assert.AreEqual ("111:22:3", Strings.Join (t.ReadFields (), ":"), "#A2");				Assert.AreEqual (null, Strings.Join (t.ReadFields (), ":"), "#A3");			}			delimiters = new string [] { ";" };			text = "a;bb;ccc;dddd" + Constants.vbNewLine + "111;22;3";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				Assert.AreEqual ("a:bb:ccc:dddd", Strings.Join (t.ReadFields (), ":"), "#B1");				Assert.AreEqual ("111:22:3", Strings.Join (t.ReadFields (), ":"), "#B2");				Assert.AreEqual (null, Strings.Join (t.ReadFields (), ":"), "#B3");			}			delimiters = new string [] { ";" };			text = "a;bb;ccc;dddd" + Constants.vbNewLine + "\"111;22\";\"3\"";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				Assert.AreEqual ("a:bb:ccc:dddd", Strings.Join (t.ReadFields (), ":"), "#C1");				Assert.AreEqual ("111;22:3", Strings.Join (t.ReadFields (), ":"), "#C2");				Assert.AreEqual (null, Strings.Join (t.ReadFields (), ":"), "#C3");			}			delimiters = new string [] { ";" };			text = "\"";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				try {					t.ReadFields ();					Assert.Fail ("#Dx1 - Expected MalformedLineException");				} catch (MalformedLineException ex) {					Assert.AreEqual ("Line 1 cannot be parsed using the current Delimiters.", ex.Message, "#Dx2");				} catch (Exception ex) {					Helper.RemoveWarning (ex);					Assert.Fail ("#Dx3 - Expected MalformedLineException");				}			}			delimiters = new string [] { "a", "bb"};			text = "a;bb;ccc;dddd" + Constants.vbNewLine + "\"111;22\";\"3\"";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				Assert.AreEqual ("?;?;ccc;dddd", Strings.Join (t.ReadFields (), "?"), "#E1");				try {					t.ReadFields ();					Assert.Fail ("#Ex1 - Expected MalformedLineException");				} catch (MalformedLineException ex) {					Assert.AreEqual ("Line 2 cannot be parsed using the current Delimiters.", ex.Message, "#Ex2");				} catch (Exception ex) {					Helper.RemoveWarning (ex);					Assert.Fail ("#Ex3 - Expected MalformedLineException");				}			}			delimiters = new string [] { ";" };			text = "a\"b\"c";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				Assert.AreEqual ("a\"b\"c", Strings.Join (t.ReadFields (), "?"), "#F1");			}			delimiters = new string [] { "a", "aa", "aaa" };			text = "a";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				Assert.AreEqual ("?", Strings.Join (t.ReadFields (), "?"), "#G1");			}			delimiters = new string [] { "aa", "a", "aaa" };			text = "aaaaaaaaa";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				Assert.AreEqual ("?????", Strings.Join (t.ReadFields (), "?"), "#H1");			}			delimiters = new string [] { ";" };			text = "\"";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				try {					t.ReadFields ();					Assert.Fail ("#Ix1 - Expected MalformedLineException");				} catch (MalformedLineException ex) {					Assert.AreEqual ("Line 1 cannot be parsed using the current Delimiters.", ex.Message, "#Ix2");				} catch (Exception ex) {					Helper.RemoveWarning (ex);					Assert.Fail ("#Ix3 - Expected MalformedLineException");				}			}			delimiters = new string [] { ";" };			text = "\"a";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				try {					t.ReadFields ();					Assert.Fail ("#Jx1 - Expected MalformedLineException");				} catch (MalformedLineException ex) {					Assert.AreEqual ("Line 1 cannot be parsed using the current Delimiters.", ex.Message, "#Jx2");				} catch (Exception ex) {					Helper.RemoveWarning (ex);					Assert.Fail ("#Jx3 - Expected MalformedLineException");				}			}			delimiters = new string [] { "aa", "a", "aaa" };			text = "\"aaaaaaaaa\"";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				Assert.AreEqual ("aaaaaaaaa", Strings.Join (t.ReadFields (), "?"), "#K1");			}			delimiters = new string [] { "aa", "a", "aaa" };			text = "aba";			using (StringReader reader = new StringReader (text))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetDelimiters (delimiters);				t.TextFieldType = FieldType.Delimited;				t.HasFieldsEnclosedInQuotes = true;				Assert.AreEqual ("?b?", Strings.Join (t.ReadFields (), "?"), "#L1");			}		}				[Test]		public void FixedTest1 ()		{			using (StringReader reader = new StringReader ("abcdef" + Constants.vbNewLine + "1234" + Constants.vbNewLine + "ghijklmno" + Constants.vbNewLine))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetFieldWidths (new int [] {1, 3, 2});				t.TextFieldType = FieldType.FixedWidth;				Assert.AreEqual ("a;bcd;ef", Strings.Join (t.ReadFields (), ";"), "#01");				try {					Assert.AreEqual ("1;234", Strings.Join (t.ReadFields (), ";"), "#02");					Assert.Fail ("#E3 - Expected 'MalformedLineException'");				} catch (MalformedLineException ex) {					Assert.AreEqual ("Line 2 cannot be parsed using the current FieldWidths.", ex.Message, "#E1");					Assert.AreEqual (2, ex.LineNumber, "#E2");				} catch (Exception ex) {					Helper.RemoveWarning (ex);					Assert.Fail ("#E4 - Expected 'MalformedLineException'");				}					Assert.AreEqual ("g;hij;kl", Strings.Join (t.ReadFields (), ";"), "#03");			}			using (StringReader reader = new StringReader ("abcdef" + Constants.vbNewLine + "1234" + Constants.vbNewLine + "ghijklmno" + Constants.vbNewLine))			using (TextFieldParser t = new TextFieldParser (reader)) {				t.SetFieldWidths (new int [] {});				t.TextFieldType = FieldType.FixedWidth;				try {					Assert.AreEqual ("a;bcd;ef", Strings.Join (t.ReadFields (), ";"), "#11");					Assert.Fail ("#E12 - Expected 'InvalidOperationException'");				} catch (InvalidOperationException ex) {					Assert.AreEqual ("Unable to read fixed width fields because FieldWidths is Nothing or empty.", ex.Message, "#E11");				} catch (Exception ex) {					Helper.RemoveWarning (ex);					Assert.Fail ("#E13 - Expected 'InvalidOperationException'");				}			}			using (StringReader reader = new StringReader (" bcdef" + Constants.vbNewLine + "1 234" + Constants.vbNewLine + "gh klmno" + Constants.vbNewLine))			using (TextFieldParser t = new TextFieldParser (reader)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -