File: determinator_spec.rb

package info (click to toggle)
ruby-jekyll-last-modified-at 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: ruby: 346; makefile: 4; sh: 3
file content (79 lines) | stat: -rw-r--r-- 2,369 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
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

require 'spec_helper'
require 'tempfile'

describe(Jekyll::LastModifiedAt::Determinator) do
  let(:site_source) { @fixtures_path }
  let(:page_path)   { @fixtures_path.join('_posts').join('1984-03-06-command.md') }
  let(:mod_time)    { Time.new(2019, 11, 17, 15, 35, 32, '+00:00') }
  subject { described_class.new(site_source.to_s, page_path.to_s) }

  it 'determines it is a git repo' do
    expect(subject.git.git_repo?).to eql(true)
    expect(subject.git.site_source).to end_with('spec/fixtures')
    expect(subject.git.top_level_directory).to end_with('/.git')
  end

  it 'knows the last modified date of the file in question' do
    expect(subject.formatted_last_modified_date).to eql('17-Nov-19')
  end

  it 'knows the last modified time (as a time object) of the file' do
    expect(subject.last_modified_at_time).to eql(mod_time)
  end

  it 'knows the last modified time of the file in question' do
    expect(subject.last_modified_at_unix).to eql('1574004932')
  end

  context 'not in a git repo' do
    let(:file) { Tempfile.new('some_file.txt') }
    let(:site_source) { File.dirname(file) }
    let(:page_path)   { file.path }
    let(:mod_time)    { Time.now }

    it 'determines it is not a git repo' do
      expect(subject.git.git_repo?).to eql(false)
      expect(subject.git.site_source).to eql(File.dirname(Tempfile.new))
      expect(subject.git.top_level_directory).to eql(nil)
    end

    it 'uses the write time' do
      expect(subject.last_modified_at_time.to_i).to eql(mod_time.to_i)
    end

    it 'uses the write time for the date, too' do
      expect(subject.formatted_last_modified_date).to eql(mod_time.strftime('%d-%b-%y'))
    end
  end

  context '#to_s' do
    it 'returns the formatted date' do
      expect(subject.to_s).to eql('17-Nov-19')
    end
  end

  context '#to_liquid' do
    it 'returns a Time object' do
      expect(subject.to_liquid).to be_a(Time)
    end

    it 'returns the correct time' do
      expect(subject.to_liquid).to eql(mod_time)
    end
  end

  context 'without a format set' do
    it 'has a default format' do
      expect(subject.format).to eql('%d-%b-%y')
    end
  end

  context 'with a format set' do
    before(:each) { subject.format = '%Y-%m-%d' }
    it 'honors the custom format' do
      expect(subject.format).to eql('%Y-%m-%d')
    end
  end
end