Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-04-22 01:04:20 +0300
committerRobert Speicher <rspeicher@gmail.com>2015-04-25 21:40:40 +0300
commita8e6656481fecce33339ab1e9ad42a6b9f0ed1bf (patch)
tree758f36e802a6b5652b1e07655b84fcc609eab8c1 /spec/models/commit_range_spec.rb
parent2c8bfedb205d2da016f64ef8eb9d24a47d5f88d6 (diff)
Add CommitRange class
Diffstat (limited to 'spec/models/commit_range_spec.rb')
-rw-r--r--spec/models/commit_range_spec.rb155
1 files changed, 155 insertions, 0 deletions
diff --git a/spec/models/commit_range_spec.rb b/spec/models/commit_range_spec.rb
new file mode 100644
index 00000000000..2d1c1bd1049
--- /dev/null
+++ b/spec/models/commit_range_spec.rb
@@ -0,0 +1,155 @@
+require 'spec_helper'
+
+describe CommitRange do
+ let(:sha_from) { 'f3f85602' }
+ let(:sha_to) { 'e86e1013' }
+
+ let(:range) { described_class.new("#{sha_from}...#{sha_to}") }
+ let(:range2) { described_class.new("#{sha_from}..#{sha_to}") }
+
+ it 'raises ArgumentError when given an invalid range string' do
+ expect { described_class.new("Foo") }.to raise_error
+ end
+
+ describe '#to_a' do
+ context 'when valid' do
+ let(:commit1) { double('commit1') }
+ let(:commit2) { double('commit2') }
+
+ before do
+ expect(range).to receive(:valid_commits?).and_return(true)
+ allow(range).to receive(:commit).with(sha_from).and_return(commit1)
+ allow(range).to receive(:commit).with(sha_to).and_return(commit2)
+ end
+
+ it 'returns an Array of Commits' do
+ expect(range.to_a).to eq [commit1, commit2]
+ end
+ end
+
+ context 'when invalid' do
+ before do
+ expect(range).to receive(:valid_commits?).and_return(false)
+ end
+
+ it 'returns [nil, nil]' do
+ expect(range.to_a).to eq [nil, nil]
+ end
+ end
+ end
+
+ describe '#to_s' do
+ context 'with short IDs' do
+ it 'is correct for three-dot syntax' do
+ expect(range.to_s).to eq "#{sha_from[0..7]}...#{sha_to[0..7]}"
+ end
+
+ it 'is correct for two-dot syntax' do
+ expect(range2.to_s).to eq "#{sha_from[0..7]}..#{sha_to[0..7]}"
+ end
+ end
+
+ context 'with full IDs' do
+ it 'is correct for three-dot syntax' do
+ expect(range.to_s(short: false)).to eq "#{sha_from}...#{sha_to}"
+ end
+
+ it 'is correct for two-dot syntax' do
+ expect(range2.to_s(short: false)).to eq "#{sha_from}..#{sha_to}"
+ end
+ end
+ end
+
+ describe '#reference_title' do
+ it 'returns the correct String' do
+ expect(range.reference_title).to eq "Commits #{sha_from} through #{sha_to}"
+ end
+ end
+
+ describe '#to_param' do
+ it 'includes the correct keys' do
+ expect(range.to_param.keys).to eq %i(from to)
+ end
+
+ it 'includes the correct values for a three-dot range' do
+ expect(range.to_param).to eq({from: sha_from, to: sha_to})
+ end
+
+ it 'includes the correct values for a two-dot range' do
+ expect(range2.to_param).to eq({from: sha_from + '^', to: sha_to})
+ end
+ end
+
+ describe '#inclusive?' do
+ it 'is false for three-dot ranges' do
+ expect(range).not_to be_inclusive
+ end
+
+ it 'is true for two-dot ranges' do
+ expect(range2).to be_inclusive
+ end
+ end
+
+ describe '#valid_commits?' do
+ context 'without a project' do
+ it 'returns nil' do
+ expect(range.valid_commits?).to be_nil
+ end
+ end
+
+ it 'accepts an optional project argument' do
+ project1 = double('project1').as_null_object
+ project2 = double('project2').as_null_object
+
+ # project1 gets assigned through the accessor, but ignored when not given
+ # as an argument to `valid_commits?`
+ expect(project1).not_to receive(:present?)
+ range.project = project1
+
+ # project2 gets passed to `valid_commits?`
+ expect(project2).to receive(:present?).and_return(false)
+
+ range.valid_commits?(project2)
+ end
+
+ context 'with a project' do
+ let(:project) { double('project', repository: double('repository')) }
+
+ context 'with a valid repo' do
+ before do
+ expect(project).to receive(:valid_repo?).and_return(true)
+ range.project = project
+ end
+
+ it 'is false when `sha_from` is invalid' do
+ expect(project.repository).to receive(:commit).with(sha_from).and_return(false)
+ expect(project.repository).not_to receive(:commit).with(sha_to)
+ expect(range).not_to be_valid_commits
+ end
+
+ it 'is false when `sha_to` is invalid' do
+ expect(project.repository).to receive(:commit).with(sha_from).and_return(true)
+ expect(project.repository).to receive(:commit).with(sha_to).and_return(false)
+ expect(range).not_to be_valid_commits
+ end
+
+ it 'is true when both `sha_from` and `sha_to` are valid' do
+ expect(project.repository).to receive(:commit).with(sha_from).and_return(true)
+ expect(project.repository).to receive(:commit).with(sha_to).and_return(true)
+ expect(range).to be_valid_commits
+ end
+ end
+
+ context 'without a valid repo' do
+ before do
+ expect(project).to receive(:valid_repo?).and_return(false)
+ range.project = project
+ end
+
+ it 'returns false' do
+ expect(range).not_to be_valid_commits
+ end
+ end
+ end
+ end
+end