require 'rails_helper'

describe DayTimeMinutes do
  describe "initialization" do
    it "can be initialized using a string" do
      described_class.new('17:48').number.should eq 17 * 60 + 48
    end

    it "is empty when initialized with empty string" do
      described_class.new('').number.should be nil
    end

    it "is empty when initialized with nil" do
      described_class.new(nil).number.should be nil
    end
  end

  describe '#inspect' do
    it "works with sub hour float values and rounds by minute" do
      described_class.new(47.56).inspect.should eq "00:48"
      described_class.new(47.46).inspect.should eq "00:47"
    end
  end

  describe "#to_s" do
    it "is the same as inspect" do
      described_class.new(7).to_s.should eq "00:07"
    end
  end

  describe "comparison" do
    subject { described_class.new 78 }
    describe "equality" do
      it "works with numbers and object as base" do
        (subject == 78 ).should be true
        (subject == 79 ).should be false
        (subject == 77 ).should be false

        (subject == '01:18' ).should be true
        (subject == '00:54' ).should be false
        (subject == '78:00' ).should be false
      end

      it "works the other way around" do
        (78 == subject).should be true
        (79 == subject).should be false
        (77 == subject).should be false

        # cannot coerce strings aparently
        ('01:18' == subject.to_s).should be true
        ('00:54' == subject.to_s).should be false
        ('78:00' == subject.to_s).should be false
      end

      it "works with objects" do
        (subject == described_class.new(5) ).should be false
        (subject == described_class.new(78) ).should be true
        (described_class.new(nil) == described_class.new('') ).should be true
      end

      it "works agains different class objects" do
        DayTimeMinutes.new(2).should eq DayTimeSeconds.new(120)
        DayTimeSeconds.new(120).should eq DayTimeMinutes.new(2)
      end
    end

    describe ">" do
      it "works with numbers and object as base" do
        (subject > 77 ).should be true
        (subject > 78 ).should be false
        (subject > 79 ).should be false

        (subject > '01:17' ).should be true
        (subject > '01:18' ).should be false
        (subject > '16:05' ).should be false
      end

      it "works the other way around" do
        (77 > subject).should be false
        (78 > subject).should be false
        (79 > subject).should be true

        ('01:17' > subject.to_s).should be false
        ('01:18' > subject.to_s).should be false
        ('16:05' > subject.to_s).should be true
      end
    end
  end
end
