示例#1
0
        public void op_Item_stringNull()
        {
            using (var obj = new StreamWriterDictionary())
            {
                // ReSharper disable AccessToDisposedClosure
                Assert.Throws <ArgumentNullException>(() => obj.Item(null as string));

                // ReSharper restore AccessToDisposedClosure
            }
        }
示例#2
0
        public void op_Item_FileInfo()
        {
            using (var file = new TempFile())
            {
                using (var obj = new StreamWriterDictionary())
                {
                    obj.Item(file.Info).Write("example");
                }

                file.Info.Refresh();
                Assert.True(file.Info.Exists);
            }
        }
示例#3
0
        public void op_Dispose()
        {
            using (var file = new TempFile())
            {
                var obj = new StreamWriterDictionary();

                obj.Dispose();

                // ReSharper disable AccessToDisposedClosure
                Assert.Throws <InvalidOperationException>(() => obj.Item(file.Info));

                // ReSharper restore AccessToDisposedClosure
            }
        }
示例#4
0
        public void op_Item_string_string_FileModeTruncate_FileAccessReadWrite_FileShareWrite()
        {
            using (var file = new TempFile())
            {
                using (var obj = new StreamWriterDictionary("example"))
                {
                    obj.Item(file.Info.FullName, "one", FileMode.Truncate, FileAccess.ReadWrite, FileShare.Write).Write("two");
                }

                var expected = "one{0}two".FormatWith(Environment.NewLine);
                var actual   = File.ReadAllText(file.Info.FullName);

                Assert.Equal(expected, actual);
            }
        }
示例#5
0
        public void op_Item_string_stringNull_FileModeTruncate_FileAccessReadWrite_FileShareWrite()
        {
            using (var file = new TempFile())
            {
                using (var obj = new StreamWriterDictionary())
                {
                    obj.Item(file.Info.FullName, null, FileMode.Truncate, FileAccess.ReadWrite, FileShare.Write);
                }

                var expected = string.Empty;
                var actual   = File.ReadAllText(file.Info.FullName);

                Assert.Equal(expected, actual);
            }
        }
示例#6
0
        public void op_Item_string_string()
        {
            using (var file = new TempFile())
            {
                using (var obj = new StreamWriterDictionary())
                {
                    obj.Item(file.Info.FullName, "one").Write("two");
                }

                var expected = "one{0}two".FormatWith(Environment.NewLine);
                var actual   = File.ReadAllText(file.Info.FullName);

                Assert.Equal(expected, actual);
            }
        }
示例#7
0
        public void op_Item_string_stringEmpty_FileModeCreate_FileAccessReadWrite_FileShareWrite()
        {
            using (var file = new TempFile())
            {
                file.Info.Delete();

                using (var obj = new StreamWriterDictionary())
                {
                    obj.Item(file.Info.FullName, string.Empty, FileMode.Create, FileAccess.ReadWrite, FileShare.Write);
                }

                var expected = Environment.NewLine;
                var actual   = File.ReadAllText(file.Info.FullName);

                Assert.Equal(expected, actual);
            }
        }
示例#8
0
        public void op_Item_string_caseInsensitive()
        {
            using (var temp = new TempDirectory())
            {
                var lower = temp.Info.ToFile("abc");
                var upper = temp.Info.ToFile("ABC");
                using (var obj = new StreamWriterDictionary())
                {
                    obj.Item(lower.FullName).Write("example");
                    obj.Item(upper.FullName).Write("example");
                }

                const int expected = 1;
                var       actual   = temp.Info.GetFiles().Length;

                Assert.Equal(expected, actual);
            }
        }
示例#9
0
        public int Create(IEnumerable <KeyStringDictionary> data)
        {
            if (null == data)
            {
                throw new ArgumentNullException("data");
            }

            if (Destination.Exists)
            {
                throw new IOException("\"{0}\" already exists.".FormatWith(Destination.FullName));
            }

            Count     = 0;
            Stopwatch = new Stopwatch();
            Stopwatch.Start();

            using (var temp = new TempFile(CurrentTempDirectory.Location))
            {
                using (var writers = new StreamWriterDictionary())
                {
                    foreach (var entry in data)
                    {
                        Count++;
                        if (null == writers.FirstLine)
                        {
                            writers.FirstLine = Csv.Header(entry);
                        }

                        writers.Item(temp.Info).WriteLine(Csv.Line(entry));
                    }
                }

                if (Count.IsNot(0))
                {
                    temp.Info.CopyTo(Destination);

                    Destination.SetDate(Modified);
                }
            }

            Stopwatch.Stop();
            return(Count);
        }
示例#10
0
        public TempCsvFile(IEnumerable <KeyStringDictionary> data)
            : base(CurrentTempDirectory.Location)
        {
            if (null == data)
            {
                throw new ArgumentNullException("data");
            }

            using (var writers = new StreamWriterDictionary())
            {
                foreach (var entry in data)
                {
                    if (null == writers.FirstLine)
                    {
                        writers.FirstLine = Csv.Header(entry);
                    }

                    writers.Item(Info).WriteLine(Csv.Line(entry));
                }
            }
        }
示例#11
0
        public void op_Item_string_string_FileModeAppend_FileAccessWrite_FileShareWrite_whenCalledAgain()
        {
            using (var file = new TempFile())
            {
                file.Info.Delete();

                using (var obj = new StreamWriterDictionary("example"))
                {
                    obj.Item(file.Info.FullName, "one", FileMode.Append, FileAccess.Write, FileShare.Write).WriteLine("two");
                }

                using (var obj = new StreamWriterDictionary("example"))
                {
                    obj.Item(file.Info.FullName, "one", FileMode.Append, FileAccess.Write, FileShare.Write).Write("three");
                }

                var expected = "one{0}two{0}three".FormatWith(Environment.NewLine);
                var actual   = File.ReadAllText(file.Info.FullName);

                Assert.Equal(expected, actual);
            }
        }