|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe CSVParser::Field do |
| 4 | + describe "#to_date" do |
| 5 | + subject(:to_date) { field.to_date } |
| 6 | + |
| 7 | + let(:field) { described_class.new(value, "A", 2, "date") } |
| 8 | + |
| 9 | + context "when value is nil" do |
| 10 | + let(:value) { nil } |
| 11 | + |
| 12 | + it { should be_nil } |
| 13 | + end |
| 14 | + |
| 15 | + context "when value is blank" do |
| 16 | + let(:value) { "" } |
| 17 | + |
| 18 | + it { should be_nil } |
| 19 | + end |
| 20 | + |
| 21 | + context "when value is not a date" do |
| 22 | + let(:value) { "not a date" } |
| 23 | + |
| 24 | + it { should be_nil } |
| 25 | + end |
| 26 | + |
| 27 | + context "with format DD/MM/YYYY" do |
| 28 | + let(:value) { "01/02/2025" } |
| 29 | + |
| 30 | + it { should eq(Date.new(2025, 2, 1)) } |
| 31 | + end |
| 32 | + |
| 33 | + context "with format YYYY-MM-DD" do |
| 34 | + let(:value) { "2025-02-01" } |
| 35 | + |
| 36 | + it { should eq(Date.new(2025, 2, 1)) } |
| 37 | + end |
| 38 | + |
| 39 | + context "with format YYYYMMDD" do |
| 40 | + let(:value) { "20250201" } |
| 41 | + |
| 42 | + it { should eq(Date.new(2025, 2, 1)) } |
| 43 | + end |
| 44 | + |
| 45 | + context "with format DD/MM/YY (2-digit year)" do |
| 46 | + let(:value) { "01/02/25" } |
| 47 | + |
| 48 | + it { should be_nil } |
| 49 | + end |
| 50 | + |
| 51 | + context "with format YY-MM-DD (2-digit year)" do |
| 52 | + let(:value) { "25-02-01" } |
| 53 | + |
| 54 | + it { should be_nil } |
| 55 | + end |
| 56 | + |
| 57 | + context "with format YYMMDD (2-digit year)" do |
| 58 | + let(:value) { "250201" } |
| 59 | + |
| 60 | + it { should be_nil } |
| 61 | + end |
| 62 | + |
| 63 | + context "with a 3-digit year" do |
| 64 | + let(:value) { "01/02/999" } |
| 65 | + |
| 66 | + it { should be_nil } |
| 67 | + end |
| 68 | + |
| 69 | + context "with an impossible date" do |
| 70 | + let(:value) { "31/02/2025" } |
| 71 | + |
| 72 | + it { should be_nil } |
| 73 | + end |
| 74 | + |
| 75 | + context "with an invalid month" do |
| 76 | + let(:value) { "01/13/2025" } |
| 77 | + |
| 78 | + it { should be_nil } |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments