Ejemplo n.º 1
0
        public void ShouldBeDateTime()
        {
            var dt = new DateTime(2000, 1, 1, 11, 12, 13);

            var testSubject = new TypeElector();
            var xs          = new[]
            {
                DateTime.MinValue.ToString(),
                DateTime.MaxValue.ToString(),
                dt.ToLongDateString(),
                dt.ToShortDateString(),
                dt.ToString("g"),
                dt.ToString("G"),
                dt.ToString("M"),
                dt.ToString("o"),
                dt.ToString("R"),
                dt.ToString("s"),
                dt.ToString("u"),
                dt.ToString("U"),
                dt.ToString("Y"),
            };

            testSubject.Elect(xs).Should().Be(typeof(DateTime));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(DateTime?));
        }
Ejemplo n.º 2
0
        public void ShouldBeBool()
        {
            var testSubject = new TypeElector();
            var xs          = new[] { "true", "false", "TRUE", "FalsE" };

            testSubject.Elect(xs).Should().Be(typeof(bool));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(bool?));
        }
Ejemplo n.º 3
0
        public void ShouldBeULong()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                "0",
                ulong.MinValue.ToString(),
                ulong.MaxValue.ToString(),
            };

            testSubject.Elect(xs).Should().Be(typeof(ulong));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(ulong?));
        }
Ejemplo n.º 4
0
        public void ShouldBeInt()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                "-1",
                "0",
                int.MinValue.ToString(),
                int.MaxValue.ToString(),
            };

            testSubject.Elect(xs).Should().Be(typeof(int));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(int?));
        }
Ejemplo n.º 5
0
        public void ShouldBeTimeSpan()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                "01:23:45",
                "987.01:23:45",
                TimeSpan.Zero.ToString(),
                TimeSpan.MinValue.ToString(),
                TimeSpan.MaxValue.ToString(),
            };

            testSubject.Elect(xs).Should().Be(typeof(TimeSpan));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(TimeSpan?));
        }
Ejemplo n.º 6
0
        public void ShouldBeChar()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                "0",
                "1",
                "a",
                "Z",
                char.MinValue.ToString(),
                char.MaxValue.ToString(),
            };

            testSubject.Elect(xs).Should().Be(typeof(char));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(char?));
        }
Ejemplo n.º 7
0
        public void ShouldBeDouble()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                "-1",
                "0",
                float.MinValue.ToString(),
                float.MaxValue.ToString(),
                double.Epsilon.ToString(),
                // XXX: say what!?!  MinValue and MaxValue cause string
                // to be elected, not double.
                //double.MinValue.ToString(),
                //double.MaxValue.ToString(),
            };

            testSubject.Elect(xs).Should().Be(typeof(double));
            testSubject.Elect(xs.Append(string.Empty).ToArray()).Should().Be(typeof(double?));
        }
Ejemplo n.º 8
0
        public void EmptyStringsShouldStillBeString()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                "dogfish",
                string.Empty,
            };

            testSubject.Elect(xs).Should().Be(typeof(string));
        }
Ejemplo n.º 9
0
        public DelimitedHeader Elect(Stream stream)
        {
            var table = this.ParseBeginningOfFile(
                new CharReader(stream),
                this.delimiter)
                        .ToArray();

            var typeElector     = new TypeElector();
            var electionResults = new List <Tuple <Type, Type> >();

            for (int i = 0; i < table[0].Length; i++)
            {
                var typeOfAllColumns         = typeElector.Elect(table.Select(x => x[i]));
                var typeOfColumnsMinusHeader = typeElector.Elect(table.Skip(1).Select(x => x[i]));

                electionResults.Add(
                    Tuple.Create(typeOfAllColumns, typeOfColumnsMinusHeader));
            }

            var hasHeader = electionResults.Any(x => x.Item1 != x.Item2);

            if (hasHeader)
            {
                var columnNames = table[0];
                return(new DelimitedHeader(
                           columnNames.Zip(electionResults, (x, y) => new DelimitedColumn()
                {
                    Name = x, Type = y.Item2
                })));
            }

            return(new DelimitedHeader(
                       electionResults.Select((x, i) => new DelimitedColumn()
            {
                Name = String.Format("Column{0}", i), Type = x.Item1
            })));
        }
Ejemplo n.º 10
0
        public void AllNullAndEmptyShouldBeString()
        {
            var testSubject = new TypeElector();
            var xs          = new[]
            {
                null,
                string.Empty,
                " ",
                "\t",
                "\r\n",
                "    ",
            };

            testSubject.Elect(xs).Should().Be(typeof(string));
        }
Ejemplo n.º 11
0
        public void CompleteMixShouldBeString()
        {
            var dt = new DateTime(2000, 1, 1, 11, 12, 13);

            var testSubject = new TypeElector();
            var xs          = new[]
            {
                bool.TrueString,
                int.MinValue.ToString(),
                Guid.Empty.ToString(),
                dt.ToString("o"),
            };

            testSubject.Elect(xs).Should().Be(typeof(string));
        }