File: string_spec.rb

package info (click to toggle)
ruby-timeliness 0.3.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 216 kB
  • sloc: ruby: 1,456; makefile: 4
file content (70 lines) | stat: -rw-r--r-- 2,352 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
describe Timeliness::CoreExt, 'String' do
  # Test values taken from ActiveSupport unit tests for compatibility

  describe "#to_time" do
    it 'should convert valid string to Time object in default zone' do
      expect("2005-02-27 23:50".to_time).to eq Time.utc(2005, 2, 27, 23, 50)
    end

    it 'should convert ISO 8601 string to Time object' do
      expect("2005-02-27T23:50:19.275038".to_time).to eq Time.utc(2005, 2, 27, 23, 50, 19, 275038)
    end

    context "with :local" do
      it 'should convert valid string to local time' do
        expect("2005-02-27 23:50".to_time(:local)).to eq Time.local(2005, 2, 27, 23, 50)
      end

      it 'should convert ISO 8601 string to local time' do
        expect("2005-02-27T23:50:19.275038".to_time(:local)).to eq Time.local(2005, 2, 27, 23, 50, 19, 275038)
      end
    end

    it 'should convert valid future string to Time object' do
      expect("2039-02-27 23:50".to_time(:local)).to eq Time.local(2039, 2, 27, 23, 50)
    end

    it 'should convert valid future string to Time object' do
      expect("2039-02-27 23:50".to_time).to eq DateTime.civil(2039, 2, 27, 23, 50)
    end

    it 'should convert empty string to nil' do
      expect(''.to_time).to be_nil
    end
  end

  describe "#to_datetime" do
    it 'should convert valid string to DateTime object' do
      expect("2039-02-27 23:50".to_datetime).to eq DateTime.civil(2039, 2, 27, 23, 50)
    end

    it 'should convert to DateTime object with UTC offset' do
      expect("2039-02-27 23:50".to_datetime.offset).to eq 0
    end

    it 'should convert ISO 8601 string to DateTime object' do
      datetime = DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00")
      expect("2039-02-27T23:50:19.275038-04:00".to_datetime).to eq datetime
    end

    it 'should use Rubys default start value' do
      # Taken from ActiveSupport unit tests. Not sure on the implication.
      expect("2039-02-27 23:50".to_datetime.start).to eq ::Date::ITALY
    end

    it 'should convert empty string to nil' do
      expect(''.to_datetime).to be_nil
    end
  end

  describe "#to_date" do
    it 'should convert string to Date object' do
      expect("2005-02-27".to_date).to eq Date.new(2005, 2, 27)
    end

    it 'should convert empty string to nil' do
      expect(''.to_date).to be_nil
    end
  end

end