public string Test(Type type, string[] paramStrs) { var inst = type.CreateInstance(ParseParams(paramStrs)); NAssert.NotNull(inst); return(inst.ToString() !); }
public void PropertyAttribute() { var rd = new AttributeReader(); var attrs = rd.GetAttributes <MapValueAttribute>(InfoOf.Member <AttributeReaderTests>(a => a.Property1) !); NAssert.NotNull(attrs); Assert.AreEqual(1, attrs.Length); Assert.AreEqual("TestName", attrs[0].Value); }
public void TypeAttribute() { var rd = new AttributeReader(); var attrs = rd.GetAttributes <TestFixtureAttribute>(typeof(AttributeReaderTests)); NAssert.NotNull(attrs); Assert.AreEqual(1, attrs.Length); Assert.AreEqual(null, attrs[0].Description); }
public void FieldAttribute() { var rd = new XmlAttributeReader(new MemoryStream(Encoding.UTF8.GetBytes(_data))); var attrs = rd.GetAttributes <ColumnAttribute>(InfoOf.Member <XmlReaderTests>(a => a.Field1) !); NAssert.NotNull(attrs); Assert.AreEqual(1, attrs.Length); Assert.AreEqual("TestName", attrs[0].Name); }
public void TypeAttribute() { var rd = new XmlAttributeReader(new MemoryStream(Encoding.UTF8.GetBytes(_data))); var attrs = rd.GetAttributes <TableAttribute>(typeof(XmlReaderTests)); NAssert.NotNull(attrs); Assert.AreEqual(1, attrs.Length); Assert.AreEqual("TestName", attrs[0].Name); }
public void IntConstructor() { using (var h = HGlobal.Create(sizeof(int))) { Assert.AreEqual(sizeof(int), h.Length); NAssert.NotNull(h.Data); Marshal.WriteInt32(h.Data, 100); Assert.AreEqual(100, Marshal.ReadInt32(h.Data)); } }
public void IntConstructor([Values(sizeof(int), sizeof(int) + 1)] int size) { using (var h = HGlobal.Create <int>(size)) { Assert.AreEqual(size, h.Length); NAssert.NotNull(h.Data); Marshal.WriteInt32(h.Data, 100); Assert.AreEqual(100, h.Value); } }
public void DefaultConstructor() { using (var h = HGlobal.Create <int>()) { Assert.AreEqual(sizeof(int), h.Length); NAssert.NotNull(h.Data); Marshal.WriteInt32(h.Data, 100); Assert.AreEqual(100, h.Value); } }
public void PropertyAttribute() { var rd = new XmlAttributeReader(new MemoryStream(Encoding.UTF8.GetBytes(_data))); MappingSchema.Default.AddMetadataReader(rd); var attrs = MappingSchema.Default.GetAttributes <MapValueAttribute>(InfoOf.Member <XmlReaderTests>(a => a.Property1) !); NAssert.NotNull(attrs); Assert.AreEqual(1, attrs.Length); Assert.AreEqual("TestName", attrs[0].Value); }
private static string CreateAndLeakTempDir(string s) { #pragma warning disable IDE0079 // Remove unnecessary suppression #pragma warning disable CA2000 // Dispose objects before losing scope var dir2 = TempData.CreateDirectory(); #pragma warning restore CA2000 // Dispose objects before losing scope #pragma warning restore IDE0079 // Remove unnecessary suppression var dir2Path = dir2.Path; Assert.AreNotEqual(s, dir2Path, "Path should not match"); NAssert.NotNull(dir2.Info, "Info is null"); Assert.IsTrue(dir2.Info.Exists, "Directory should exist"); GC.KeepAlive(dir2); return(dir2Path); }
private static string CreateAndLeakTempFile(string filePath) { #pragma warning disable IDE0079 // Remove unnecessary suppression #pragma warning disable CA2000 // Dispose objects before losing scope var file2 = TempData.CreateFile(); #pragma warning restore CA2000 // Dispose objects before losing scope #pragma warning restore IDE0079 // Remove unnecessary suppression var file2Path = file2.Path; Assert.AreNotEqual(filePath, file2Path, "Path should not match"); NAssert.NotNull(file2.Info, "Info is null"); Assert.IsTrue(file2.Info.Exists, "File should exist"); GC.KeepAlive(file2); return(file2Path); }
public void TestFile() { var tempPath = Path.GetTempPath(); string filePath; using (var file = TempData.CreateFile()) { filePath = file.Path; Assert.IsTrue(File.Exists(filePath), "File should exist"); Assert.That(tempPath, Does.StartWith(tempPath)); var file2 = TempData.CreateFile(); Assert.AreNotEqual(file.Path, file2.Path, "Path should not match"); Assert.IsTrue(file2.Info.Exists, "File should exist"); file2.Dispose(); AssertDisposed(() => file2.Info); } Assert.IsFalse(File.Exists(filePath), "File should NOT exist"); // test for cleanup if leaked { var file2Path = CreateAndLeakTempFile(filePath); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Assert.IsFalse(File.Exists(file2Path), "File should NOT exist"); } // test for SuppressDelete() { var file2 = TempData.CreateFile(); var file2Path = file2.Path; try { file2.SuppressDelete(); Assert.AreNotEqual(filePath, file2Path, "Path should not match"); NAssert.NotNull(file2.Info, "Info is null"); Assert.IsTrue(file2.Info.Exists, "File should exist"); file2.Dispose(); Assert.IsTrue(File.Exists(file2Path), "File should exist"); } finally { File.Delete(file2Path); } } }
public void TestFileWrapperWithDefaultPath() { string filePath; using (var file = new TempData.TempFile()) { filePath = file.Path; NAssert.NotNull(file.Info, "Info is null"); Assert.IsFalse(file.Info.Exists, "File should not exist"); using (File.Create(filePath)) { } file.Info.Refresh(); Assert.IsTrue(file.Info.Exists, "File should exist"); } Assert.IsFalse(File.Exists(filePath), "File should NOT exist"); }
public void TestDirectory() { var tempPath = Path.GetTempPath(); string dirPath; using (var dir = TempData.CreateDirectory()) { dirPath = dir.Path; Assert.IsTrue(Directory.Exists(dirPath), "Directory should exist"); Assert.That(dirPath, Does.StartWith(tempPath)); var dir2 = TempData.CreateDirectory(); Assert.AreNotEqual(dir.Path, dir2.Path, "Path should not match"); Assert.IsTrue(dir2.Info.Exists, "Directory should exist"); dir2.Dispose(); AssertDisposed(() => dir2.Info); } Assert.IsFalse(Directory.Exists(dirPath), "Directory should NOT exist"); // test for cleanup if leaked { var dir2Path = CreateAndLeakTempDir(dirPath); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Assert.IsFalse(Directory.Exists(dir2Path), "Directory should NOT exist"); } // test for SuppressDelete() { var dir2 = TempData.CreateDirectory(); var dir2Path = dir2.Path; try { dir2.SuppressDelete(); Assert.AreNotEqual(dirPath, dir2Path, "Path should not match"); NAssert.NotNull(dir2.Info, "Info is null"); Assert.IsTrue(dir2.Info.Exists, "Directory should exist"); dir2.Dispose(); Assert.IsTrue(Directory.Exists(dir2Path), "Directory should exist"); } finally { Directory.Delete(dir2Path); } } }
public void TestDirectorySpecificPathSubDirectory() { var tempPath = Path.Combine(Path.GetTempPath(), TempData.GetTempName()); Assert.IsFalse(Directory.Exists(tempPath), "Directory should NOT exist"); var dirName = TempData.GetTempName(); var dirPath = Path.Combine(tempPath, dirName); using (var directory = TempData.CreateDirectory(tempPath, dirName)) { Assert.AreEqual(directory.Path, dirPath); NAssert.NotNull(directory.Info, "Info is null"); Assert.IsTrue(directory.Info.Exists, "Directory should exist"); } Assert.IsFalse(File.Exists(dirPath), "Directory should NOT exist"); Directory.Delete(tempPath); }
public void TestFileContent() { string filePath; using (var file = TempData.CreateFile()) { filePath = file.Path; NAssert.NotNull(file.Info, "Info is null"); using (var textWriter = file.Info.AppendText()) textWriter.Write("O La La"); var content = File.ReadAllText(filePath); Assert.AreEqual(content, "O La La"); } Assert.IsFalse(File.Exists(filePath), "File should NOT exist"); }
public void TestFileWrapperWithCustomPath() { var tempPath = Path.GetTempPath(); var fileName = TempData.GetTempName(); var filePath = Path.Combine(tempPath, fileName); using (var file = new TempData.TempFile(filePath)) { Assert.AreEqual(filePath, file.Path); NAssert.NotNull(file.Info, "Info is null"); Assert.IsFalse(file.Info.Exists, "File should not exist"); using (File.Create(filePath)) { } file.Info.Refresh(); Assert.IsTrue(file.Info.Exists, "File should exist"); } Assert.IsFalse(File.Exists(filePath), "File should NOT exist"); }
public void TestEmptyIfNull() { var arr = Array <int> .Empty; var list = new List <int>(); var dic = new Dictionary <int, string>(); var str = ""; Assert.IsNull(arr.NullIfEmpty()); NAssert.NotNull(arr.EmptyIfNull()); Assert.IsNull(dic.NullIfEmpty()); NAssert.NotNull(dic.EmptyIfNull()); Assert.IsNull(list.NullIfEmpty()); NAssert.NotNull(list.EmptyIfNull()); Assert.IsNull(str.NullIfEmpty()); NAssert.NotNull(str.EmptyIfNull()); arr = new int[2]; list.Add(3); dic.Add(1, "A"); str = "B"; NAssert.NotNull(arr.NullIfEmpty()); NAssert.NotNull(arr.EmptyIfNull()); NAssert.NotNull(dic.NullIfEmpty()); NAssert.NotNull(dic.EmptyIfNull()); NAssert.NotNull(list.NullIfEmpty()); NAssert.NotNull(list.EmptyIfNull()); NAssert.NotNull(str.NullIfEmpty()); NAssert.NotNull(str.EmptyIfNull()); arr = null; list = null; dic = null; str = null; Assert.IsNull(arr.NullIfEmpty()); NAssert.NotNull(arr.EmptyIfNull()); Assert.IsNull(dic.NullIfEmpty()); NAssert.NotNull(dic.EmptyIfNull()); Assert.IsNull(list.NullIfEmpty()); NAssert.NotNull(list.EmptyIfNull()); Assert.IsNull(str.NullIfEmpty()); NAssert.NotNull(str.EmptyIfNull()); }
public void ExtractingDefaultClassCtor() { var expected = typeof(User).GetConstructors().First(c => c.GetParameters().Length == 0); var ctor1 = InfoOf.Constructor <User>(); var ctor2 = InfoOf.Constructor(() => new User()); var ctor3 = InfoOf <User> .Constructor(() => new User()); var ctor4 = Expression.New(typeof(User)).Constructor; NAssert.NotNull(ctor1, "#1"); NAssert.NotNull(ctor2, "#2"); NAssert.NotNull(ctor3, "#3"); NAssert.NotNull(ctor4, "#4"); Assert.AreEqual(expected, ctor1); Assert.AreEqual(expected, ctor2); Assert.AreEqual(expected, ctor3); Assert.AreEqual(expected, ctor4); }
public void TestDirectoryNestedContent() { string dirPath; string nestedFile; string nestedDir; using (var dir = TempData.CreateDirectory()) { dirPath = dir.Path; NAssert.NotNull(dir.Info, "Info is null"); nestedDir = dir.Info.CreateSubdirectory("test.dir").FullName; nestedFile = Path.Combine(dirPath, "test.tmp"); File.WriteAllText(nestedFile, "O La La"); var content = File.ReadAllText(nestedFile); Assert.AreEqual(content, "O La La"); } Assert.IsFalse(Directory.Exists(dirPath), "Directory should NOT exist"); Assert.IsFalse(Directory.Exists(nestedDir), "Directory should NOT exist"); Assert.IsFalse(File.Exists(nestedFile), "File should NOT exist"); }
public void TestFileSpecificPath() { var tempPath = Path.GetTempPath(); var fileName = TempData.GetTempName(); var filePath = Path.Combine(tempPath, fileName); using (var file = TempData.CreateFile(tempPath, fileName)) { Assert.AreEqual(file.Path, filePath); NAssert.NotNull(file.Info, "Info is null"); Assert.IsTrue(file.Info.Exists, "File should exist"); } Assert.IsFalse(File.Exists(filePath), "File should NOT exist"); using (var file = TempData.CreateFile(null, fileName)) { Assert.AreEqual(file.Path, filePath); NAssert.NotNull(file.Info, "Info is null"); Assert.IsTrue(file.Info.Exists, "File should exist"); } Assert.IsFalse(File.Exists(filePath), "File should NOT exist"); }