PageRenderTime 42ms CodeModel.GetById 13ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/TestSpss/SpssVariablesCollectionTest.cs

#
C# | 220 lines | 198 code | 12 blank | 10 comment | 0 complexity | c218c112130d12a031dc77052ebf666b MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using Microsoft.VisualStudio.TestTools.UnitTesting;
  4
  5namespace Spss.Testing
  6{
  7	/// <summary>
  8	///This is a test class for Spss.SpssVariablesCollection and is intended
  9	///to contain all Spss.SpssVariablesCollection Unit Tests
 10	///</summary>
 11	[TestClass()]
 12    [DeploymentItem("x86", "x86")]
 13    [DeploymentItem("x64", "x64")]
 14	public class SpssVariablesCollectionTest : TestBase
 15	{
 16
 17
 18		private TestContext testContextInstance;
 19
 20		/// <summary>
 21		///Gets or sets the test context which provides
 22		///information about and functionality for the current test run.
 23		///</summary>
 24		public TestContext TestContext
 25		{
 26			get
 27			{
 28				return testContextInstance;
 29			}
 30			set
 31			{
 32				testContextInstance = value;
 33			}
 34		}
 35
 36
 37		[TestMethod]
 38		public void Count()
 39		{
 40			Assert.AreEqual(SpssSafeWrapperTest.Test1VarNames.Length, docRead.Variables.Count);
 41		}
 42
 43		[TestMethod]
 44		public void Enumerate()
 45		{
 46			int i = 0;
 47			foreach (SpssVariable var in docRead.Variables)
 48			{
 49				Assert.IsNotNull(var);
 50				Assert.IsTrue(i < SpssSafeWrapperTest.Test1VarNames.Length, "Too many variables!");
 51				Assert.AreEqual(SpssSafeWrapperTest.Test1VarNames[i], var.Name);
 52				i++;
 53			}
 54			Assert.AreEqual(SpssSafeWrapperTest.Test1VarNames.Length, i);
 55		}
 56		[TestMethod]
 57		public void IndexerByName()
 58		{
 59			SpssVariable var = docRead.Variables[SpssSafeWrapperTest.Test1VarNames[0]];
 60			Assert.IsNotNull(var);
 61			Assert.AreEqual(SpssSafeWrapperTest.Test1VarNames[0], var.Name);
 62		}
 63		[TestMethod]
 64		[ExpectedException(typeof(KeyNotFoundException))]
 65		public void IndexerByNameBad()
 66		{
 67			SpssVariable var = docRead.Variables["some bad var name"];
 68		}
 69		[TestMethod]
 70		[ExpectedException(typeof(KeyNotFoundException))]
 71		public void IndexerByNameEmpty()
 72		{
 73			SpssVariable var = docRead.Variables[string.Empty];
 74		}
 75		[TestMethod]
 76		[ExpectedException(typeof(ArgumentNullException))]
 77		public void IndexerByNameNull()
 78		{
 79			SpssVariable var = docRead.Variables[null];
 80		}
 81		[TestMethod]
 82		public void IndexerByOrdinal()
 83		{
 84			SpssVariable var = docRead.Variables[0];
 85			Assert.IsNotNull(var);
 86			Assert.AreEqual(SpssSafeWrapperTest.Test1VarNames[0], var.Name);
 87		}
 88		[TestMethod]
 89		[ExpectedException(typeof(ArgumentOutOfRangeException))]
 90		public void IndexerByOrdinalNegative()
 91		{
 92			SpssVariable var = docRead.Variables[-1];
 93		}
 94		[TestMethod]
 95		[ExpectedException(typeof(ArgumentOutOfRangeException))]
 96		public void IndexerByOrdinalTooHigh()
 97		{
 98			SpssVariable var = docRead.Variables[4096]; // just some insanely high number
 99		}
100		[TestMethod]
101		public void SameVarObjectRepeatedly()
102		{
103			SpssVariable var1 = docRead.Variables[0];
104			SpssVariable var2 = docRead.Variables[0];
105			Assert.AreSame(var1, var2);
106		}
107		[TestMethod]
108		[ExpectedException(typeof(ArgumentNullException))]
109		public void AddNull()
110		{
111			docRead.Variables.Add(null);
112		}
113		[TestMethod]
114		[ExpectedException(typeof(ArgumentNullException))]
115		public void InsertNull()
116		{
117			docRead.Variables.Insert(0, null);
118		}
119		[TestMethod]
120		[ExpectedException(typeof(SpssVariableNameConflictException))]
121		public void AddVarWithSameName()
122		{
123			SpssVariable var1 = new SpssStringVariable();
124			var1.Name = "var1";
125			docWrite.Variables.Add(var1);
126
127			SpssVariable var2 = new SpssStringVariable();
128			var2.Name = "Var1";
129			docWrite.Variables.Add(var2);
130		}
131		[TestMethod]
132		[ExpectedException(typeof(SpssVariableNameConflictException))]
133		public void RenameVarToConflictingName()
134		{
135			SpssVariable var1 = new SpssStringVariable();
136			var1.Name = "var1";
137			docWrite.Variables.Add(var1);
138
139			SpssVariable var2 = new SpssStringVariable();
140			var2.Name = "var2";
141			docWrite.Variables.Add(var2);
142
143			// Now rename one variable to match the name of the other.
144			// Capitalization differences should still cause an exception.
145			var2.Name = "Var1";
146		}
147		[TestMethod]
148		public void AddVariableToNewFile()
149		{
150			SpssVariable var = new SpssStringVariable();
151			var.Name = "var1";
152			docWrite.Variables.Add(var);
153			Assert.AreEqual(1, docWrite.Variables.Count);
154		}
155		[TestMethod]
156		public void CommitVariableToNewFile()
157		{
158			try
159			{
160				AddVariableToNewFile();
161			}
162			catch (Exception)
163			{
164				Assert.Inconclusive("AddVariableToNewFile() failed.");
165			}
166			docWrite.CommitDictionary();
167		}
168		[TestMethod]
169		[ExpectedException(typeof(InvalidOperationException))]
170		public void AddVariableAfterCommit()
171		{
172			try
173			{
174				CommitVariableToNewFile();
175			}
176			catch (Exception)
177			{
178				Assert.Inconclusive("AddVariableToNewFileAndCommit() failed.");
179			}
180			SpssVariable var = new SpssStringVariable();
181			var.Name = "anothervar";
182			docWrite.Variables.Add(var);
183		}
184		[TestMethod]
185		public void CommitVariableWithLabels()
186		{
187			SpssNumericVariable var = new SpssNumericVariable();
188			var.Name = "var1";
189			var.ValueLabels.Add(1, "Never");
190			var.ValueLabels.Add(2, "Rarely");
191			docWrite.Variables.Add(var);
192			docWrite.CommitDictionary();
193			docWrite.Close();
194		}
195		[TestMethod]
196		[ExpectedException(typeof(InvalidOperationException))]
197		public void ChangeLabelAfterCommit()
198		{
199			try
200			{
201				CommitVariableWithLabels();
202			}
203			catch (Exception)
204			{
205				Assert.Inconclusive("CommitVariableWithLabels() failed.");
206			}
207			SpssNumericVariable var = (SpssNumericVariable)docWrite.Variables[0];
208			var.ValueLabels[6] = "break";
209		}
210		[TestMethod]
211		public void LookUpLoadedLabels()
212		{
213			SpssNumericVariable var = (SpssNumericVariable)docRead.Variables["numLabels"];
214			Assert.AreEqual(23, var.ValueLabels.Count);
215			Assert.AreEqual("fattening", var.ValueLabels[1]);
216		}
217	}
218
219
220}