コード例 #1
0
 public FakeDbaseSchema()
 {
     Fields = new[]
     {
         DbaseField.CreateInt32Field(new DbaseFieldName(nameof(Id)), new DbaseFieldLength(4))
     };
 }
コード例 #2
0
        // shared by dbase int16, nullable int16

        public static int?ReadAsNullableInt32(this BinaryReader reader, DbaseField field)
        {
            int?result = default;

            if (reader.PeekChar() == '\0')
            {
                var read = reader.ReadBytes(field.Length.ToInt32());
                if (read.Length != field.Length.ToInt32())
                {
                    throw new EndOfStreamException(
                              $"Unable to read beyond the end of the stream. Expected stream to have {field.Length.ToInt32()} byte(s) available but only found {read.Length} byte(s) as part of reading field {field.Name.ToString()}."
                              );
                }
            }
            else
            {
                var unpadded = reader.ReadLeftPaddedString(field.Name.ToString(), field.Length.ToInt32(), ' ');

                result = int.TryParse(unpadded,
                                      DbaseNumber.IntegerNumberStyle,
                                      CultureInfo.InvariantCulture, out var parsed)
                    ? (int?)parsed
                    : default;
            }

            return(result);
        }
コード例 #3
0
        public static void WriteAsNullableDecimal(this BinaryWriter writer, DbaseField field, NumberFormatInfo provider, decimal?value)
        {
            if (value.HasValue)
            {
                var unpadded = value.Value.ToString("F", provider);
                if (unpadded.Length < field.Length.ToInt32() && field.DecimalCount.ToInt32() > 0)
                {
                    // Pad with decimal zeros if space left.
                    var parts = unpadded.Split(provider.NumberDecimalSeparator.Single());
                    if (parts.Length == 2 && parts[1].Length < field.DecimalCount.ToInt32())
                    {
                        unpadded = string.Concat(
                            unpadded,
                            new string(
                                '0',
                                field.DecimalCount.ToInt32() - parts[1].Length
                                )
                            );
                    }
                }

                writer.WriteLeftPaddedString(unpadded, field.Length.ToInt32(), ' ');
            }
            else
            {
                writer.Write(new string(' ', field.Length.ToInt32()).ToCharArray());
                // or writer.Write(new byte[Field.Length]); // to determine
            }
        }
コード例 #4
0
        // shared by dbase decimal, nullable decimal

        public static decimal?ReadAsNullableDecimal(this BinaryReader reader, DbaseField field, NumberFormatInfo provider)
        {
            decimal?result = default;

            if (reader.PeekChar() == '\0')
            {
                var read = reader.ReadBytes(field.Length.ToInt32());
                if (read.Length != field.Length.ToInt32())
                {
                    throw new EndOfStreamException(
                              $"Unable to read beyond the end of the stream. Expected stream to have {field.Length.ToInt32()} byte(s) available but only found {read.Length} byte(s) as part of reading field {field.Name.ToString()}."
                              );
                }
            }
            else
            {
                var unpadded = reader.ReadLeftPaddedString(field.Name.ToString(), field.Length.ToInt32(), ' ');

                result = decimal.TryParse(unpadded, DbaseNumber.DoubleNumberStyle, provider, out var parsed)
                    ? (decimal?)parsed
                    : default;
            }

            return(result);
        }
コード例 #5
0
        public DbaseLogical(DbaseField field, bool?value = null) : base(field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (field.FieldType != DbaseFieldType.Logical)
            {
                throw new ArgumentException(
                          $"The field {field.Name}'s type must be logical to use it as a logical field.", nameof(field));
            }

            if (field.DecimalCount.ToInt32() != 0)
            {
                throw new ArgumentException(
                          $"The logical field {field.Name}'s decimal count must be 0 to use it as a logical field.",
                          nameof(field));
            }

            if (field.Length.ToInt32() != 1)
            {
                throw new ArgumentException(
                          $"The logical field {field.Name}'s length must be 1 to use it as a logical field.",
                          nameof(field));
            }

            Value = value;
        }
コード例 #6
0
 public FakeDbaseSchema()
 {
     Fields = new[]
     {
         DbaseField.CreateNumberField(new DbaseFieldName(nameof(Id)), new DbaseFieldLength(4), new DbaseDecimalCount(0))
     };
 }
コード例 #7
0
ファイル: DbaseSingle.cs プロジェクト: yreynhout/shaperon
        public DbaseSingle(DbaseField field, float?value = null) : base(field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (field.FieldType != DbaseFieldType.Float)
            {
                throw new ArgumentException(
                          $"The field {field.Name} 's type must be float to use it as a single field.", nameof(field));
            }

            if (field.Length < MinimumLength || field.Length > MaximumLength)
            {
                throw new ArgumentException(
                          $"The field {field.Name} 's length ({field.Length}) must be between {MinimumLength} and {MaximumLength}.",
                          nameof(field));
            }

            Provider = new NumberFormatInfo
            {
                NumberDecimalDigits    = DbaseDecimalCount.Min(MaximumDecimalCount, field.DecimalCount).ToInt32(),
                NumberDecimalSeparator = "."
            };

            Value = value;
        }
コード例 #8
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateSingleFieldReturnsExpectedResult()
        {
            var name                  = _fixture.Create <DbaseFieldName>();
            var length                = _fixture.GenerateDbaseSingleLength();
            var decimalCount          = _fixture.GenerateDbaseSingleDecimalCount(length);
            var positiveIntegerDigits = decimalCount != new DbaseDecimalCount(0)
                ? new DbaseIntegerDigits(length.ToInt32() - 1 - decimalCount.ToInt32())
                : new DbaseIntegerDigits(length.ToInt32());
            var negativeIntegerDigits = positiveIntegerDigits != new DbaseIntegerDigits(0)
                ? positiveIntegerDigits.Minus(new DbaseIntegerDigits(1))
                : new DbaseIntegerDigits(0);

            var result = DbaseField.CreateSingleField(
                name,
                length,
                decimalCount);

            Assert.Equal(name, result.Name);
            Assert.Equal(DbaseFieldType.Float, result.FieldType);
            Assert.Equal(ByteOffset.Initial, result.Offset);
            Assert.Equal(length, result.Length);
            Assert.Equal(decimalCount, result.DecimalCount);
            Assert.Equal(positiveIntegerDigits, result.PositiveIntegerDigits);
            Assert.Equal(negativeIntegerDigits, result.NegativeIntegerDigits);
        }
コード例 #9
0
 private bool Equals(DbaseField other) =>
 other != null &&
 Name.Equals(other.Name) &&
 FieldType.Equals(other.FieldType) &&
 Offset.Equals(other.Offset) &&
 Length.Equals(other.Length) &&
 DecimalCount.Equals(other.DecimalCount);
コード例 #10
0
        public DbaseField After(DbaseField field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            return(new DbaseField(Name, FieldType, field.Offset.Plus(field.Length), Length, DecimalCount));
        }
コード例 #11
0
        public void ReadingCharacterWithDateTimeValueHasExpectedResult()
        {
            var value  = _fixture.Create <DateTime>();
            var fields = new[]
            {
                new DbaseField(
                    _fixture.Create <DbaseFieldName>(),
                    DbaseFieldType.DateTime,
                    ByteOffset.Initial,
                    new DbaseFieldLength(15),
                    new DbaseDecimalCount(0)
                    )
            };
            var values = new DbaseFieldValue[]
            {
                new DbaseDateTime(
                    fields[0],
                    value
                    )
            };

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    new AnonymousDbaseRecord(values).Write(writer);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    fields[0] = new DbaseField(
                        fields[0].Name,
                        DbaseFieldType.Character,
                        ByteOffset.Initial,
                        new DbaseFieldLength(15),
                        new DbaseDecimalCount(0)
                        );

                    var sut = new AnonymousDbaseRecord(fields);

                    //Act
                    sut.Read(reader);

                    Assert.False(sut.IsDeleted);
                    Assert.Equal(new DbaseFieldValue[]
                    {
                        new DbaseDateTime(
                            fields[0],
                            value
                            )
                    }, sut.Values, new DbaseFieldValueEqualityComparer());
                }
            }
        }
コード例 #12
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateLogicalFieldValueReturnsExpectedResult()
        {
            var sut = DbaseField.CreateLogicalField(_fixture.Create <DbaseFieldName>());

            var result = sut.CreateFieldValue();

            Assert.Equal(sut, result.Field);
            Assert.IsType <DbaseBoolean>(result);
        }
コード例 #13
0
 public static void WriteAsNullableString(this BinaryWriter writer, DbaseField field, string value)
 {
     if (value == null)
     {
         writer.Write(new byte[field.Length.ToInt32()]);
     }
     else
     {
         writer.WriteRightPaddedString(value, field.Length.ToInt32(), ' ');
     }
 }
コード例 #14
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateSingleFieldThrowsWhenDecimalCountGreaterThanZeroAndLengthSmallerThan3(int smallerThan3)
        {
            var name         = _fixture.Create <DbaseFieldName>();
            var length       = new DbaseFieldLength(smallerThan3);
            var decimalCount = new DbaseDecimalCount(1);

            Assert.Throws <ArgumentException>(() => DbaseField.CreateSingleField(
                                                  name,
                                                  length,
                                                  decimalCount));
        }
コード例 #15
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateSingleFieldThrowsWhenDecimalCountGreaterThanOrEqualToLength(int plus)
        {
            var name         = _fixture.Create <DbaseFieldName>();
            var length       = _fixture.GenerateDbaseSingleLength();
            var decimalCount = new DbaseDecimalCount(length.ToInt32()).Plus(new DbaseDecimalCount(plus));

            Assert.Throws <ArgumentException>(() => DbaseField.CreateSingleField(
                                                  name,
                                                  length,
                                                  decimalCount));
        }
コード例 #16
0
        public void DateTimeHoursMinutesSecondsAndMillisecondsAreRemovedUponConstruction()
        {
            var field = new DbaseField(
                _fixture.Create <DbaseFieldName>(),
                DbaseFieldType.Date,
                _fixture.Create <ByteOffset>(),
                new DbaseFieldLength(8),
                new DbaseDecimalCount(0));
            var sut = new DbaseDate(field, new DateTime(1, 1, 1, 1, 1, 1, 1));

            Assert.Equal(new DateTime(1, 1, 1, 0, 0, 0, 0), sut.Value);
        }
コード例 #17
0
        public void AcceptsStringValueReturnsExpectedResult(int length, string value, bool accepted)
        {
            var sut = new DbaseCharacter(
                DbaseField.CreateCharacterField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length)
                    ));

            var result = sut.AcceptsValue(value);

            Assert.Equal(accepted, result);
        }
コード例 #18
0
        public void AcceptsDateTimeOffsetValueReturnsExpectedResult(int length, DbaseCharacterOptions options, DateTimeOffset value, bool accepted)
        {
            var sut = new DbaseCharacter(
                DbaseField.CreateCharacterField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length)
                    ), options: options);

            var result = sut.AcceptsValue(value);

            Assert.Equal(accepted, result);
        }
コード例 #19
0
 public static void WriteAsNullableInt32(this BinaryWriter writer, DbaseField field, int?value)
 {
     if (value.HasValue)
     {
         var unpadded = DbaseInt32.FormatAsString(value.Value);
         writer.WriteLeftPaddedString(unpadded, field.Length.ToInt32(), ' ');
     }
     else
     {
         writer.Write(new string(' ', field.Length.ToInt32()).ToCharArray());
         // or writer.Write(new byte[Field.Length]); // to determine
     }
 }
コード例 #20
0
        public void TryGetValueAsDateTimeOffsetReturnsExpectedResult(int length, DbaseCharacterOptions options, string value, bool gotten, DateTimeOffset gottenValueAsDateTimeOffset)
        {
            var sut = new DbaseCharacter(
                DbaseField.CreateCharacterField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length)
                    ), value, options);

            var result = sut.TryGetValueAsDateTimeOffset(out var valueAsDateTime);

            Assert.Equal(gotten, result);
            Assert.Equal(gottenValueAsDateTimeOffset, valueAsDateTime);
        }
コード例 #21
0
        public void TrySetValueAsNullableInt16ReturnsExpectedResult(int length, short?value, bool expected)
        {
            var sut = new DbaseNumber(
                DbaseField.CreateNumberField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            var result = sut.TrySetValueAsNullableInt16(value);

            Assert.Equal(expected, result);
        }
コード例 #22
0
        public void TrySetValueAsInt32ReturnsExpectedResult(int length, int value, bool expected)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            var result = sut.TrySetValueAsInt32(value);

            Assert.Equal(expected, result);
        }
コード例 #23
0
        public void TrySetValueAsNullableDateTimeOffsetReturnsExpectedResult(int length, DbaseCharacterOptions options,
                                                                             DateTimeOffset?value, bool expected)
        {
            var sut = new DbaseCharacter(
                DbaseField.CreateCharacterField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length)
                    ), options: options);

            var result = sut.TrySetValueAsNullableDateTimeOffset(value);

            Assert.Equal(expected, result);
        }
コード例 #24
0
        public void AcceptsNullableInt32ValueReturnsExpectedResult(int length, int?value, bool accepted)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            var result = sut.AcceptsValue(value);

            Assert.Equal(accepted, result);
        }
コード例 #25
0
        public void AcceptsInt16ValueReturnsExpectedResult(int length, short value, bool accepted)
        {
            var sut = new DbaseNumber(
                DbaseField.CreateNumberField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            var result = sut.AcceptsValue(value);

            Assert.Equal(accepted, result);
        }
コード例 #26
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateDateTimeFieldValueReturnsExpectedResult()
        {
            var sut = new DbaseField(
                _fixture.Create <DbaseFieldName>(),
                DbaseFieldType.DateTime,
                _fixture.Create <ByteOffset>(),
                new DbaseFieldLength(15),
                new DbaseDecimalCount(0));

            var result = sut.CreateFieldValue();

            Assert.Equal(sut, result.Field);
            Assert.IsType <DbaseDateTime>(result);
        }
コード例 #27
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateDateTimeFieldReturnsExpectedResult()
        {
            var name = _fixture.Create <DbaseFieldName>();

            var result = DbaseField.CreateDateTimeField(name);

            Assert.Equal(name, result.Name);
            Assert.Equal(DbaseFieldType.DateTime, result.FieldType);
            Assert.Equal(ByteOffset.Initial, result.Offset);
            Assert.Equal(new DbaseFieldLength(15), result.Length);
            Assert.Equal(new DbaseDecimalCount(0), result.DecimalCount);
            Assert.Equal(new DbaseIntegerDigits(0), result.PositiveIntegerDigits);
            Assert.Equal(new DbaseIntegerDigits(0), result.NegativeIntegerDigits);
        }
コード例 #28
0
        public void TryGetValueAsInt32ReturnsExpectedResult(int length, double?value, bool gotten, int gottenValueAsInt32)
        {
            var sut = new DbaseNumber(
                DbaseField.CreateNumberField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ), value);

            var result = sut.TryGetValueAsInt32(out var valueAsInt32);

            Assert.Equal(gotten, result);
            Assert.Equal(gottenValueAsInt32, valueAsInt32);
        }
コード例 #29
0
        public void DateTimeMillisecondsAreRemovedUponSet()
        {
            var field = new DbaseField(
                _fixture.Create <DbaseFieldName>(),
                DbaseFieldType.DateTime,
                _fixture.Create <ByteOffset>(),
                new DbaseFieldLength(15),
                new DbaseDecimalCount(0));
            var sut = new DbaseDateTime(field);

            sut.Value = new DateTime(1, 1, 1, 1, 1, 1, 1);

            Assert.Equal(new DateTime(1, 1, 1, 1, 1, 1, 0), sut.Value);
        }
コード例 #30
0
        public void TryGetValueAsNullableInt16ReturnsExpectedResult(int length, float?value, bool gotten, short?gottenValueAsInt16)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ), value);

            var result = sut.TryGetValueAsNullableInt16(out var valueAsInt16);

            Assert.Equal(gotten, result);
            Assert.Equal(gottenValueAsInt16, valueAsInt16);
        }