asciinema.org/spec/models/asciicast_spec.rb

168 lines
4.3 KiB
Ruby
Raw Normal View History

2015-07-20 12:05:38 +00:00
# -*- coding: utf-8 -*-
2014-08-30 17:38:47 +00:00
require 'rails_helper'
require 'tempfile'
2011-11-23 21:30:09 +00:00
describe Asciicast do
describe '.find_by_id_or_secret_token!' do
subject { Asciicast.find_by_id_or_secret_token!(thing) }
context 'for public asciicast' do
let(:asciicast) { create(:asciicast, private: false) }
context 'when looked up by id' do
let(:thing) { asciicast.id }
it { should eq(asciicast) }
end
context 'when looked up by secret token' do
let(:thing) { asciicast.secret_token }
it { should eq(asciicast) }
end
end
context 'for private asciicast' do
let(:asciicast) { create(:asciicast, private: true) }
context 'when looked up by id' do
let(:thing) { asciicast.id }
it 'raises RecordNotFound' do
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when looked up by secret token' do
let(:thing) { asciicast.secret_token }
it { should eq(asciicast) }
end
end
end
describe '.generate_secret_token' do
subject { Asciicast.generate_secret_token }
it { should match(/^[a-z0-9]{25}$/) }
end
describe '.for_category_ordered' do
subject { described_class.for_category_ordered(category, order) }
let!(:asciicast_1) { create(:asciicast, created_at: 2.hours.ago,
views_count: 10,
featured: false) }
let!(:asciicast_2) { create(:asciicast, created_at: 1.hour.ago,
views_count: 20,
featured: true) }
let!(:asciicast_3) { create(:asciicast, created_at: 4.hours.ago,
views_count: 30,
featured: false) }
let!(:asciicast_4) { create(:asciicast, created_at: 3.hours.ago,
views_count: 40,
featured: true) }
context "when category is :all" do
let(:category) { :all }
2015-07-20 12:05:38 +00:00
context "and order is :date" do
let(:order) { :date }
it { should eq([asciicast_2, asciicast_1, asciicast_4, asciicast_3]) }
end
context "and order is :popularity" do
let(:order) { :popularity }
it { should eq([asciicast_4, asciicast_3, asciicast_2, asciicast_1]) }
end
end
context "when category is :featured" do
let(:category) { :featured }
2015-07-20 12:05:38 +00:00
context "and order is :date" do
let(:order) { :date }
it { should eq([asciicast_2, asciicast_4]) }
end
context "and order is :popularity" do
let(:order) { :popularity }
it { should eq([asciicast_4, asciicast_2]) }
end
end
end
describe '#to_param' do
subject { asciicast.to_param }
let(:asciicast) { Asciicast.new(id: 123, secret_token: 'sekrit') }
context 'for public asciicast' do
before do
asciicast.private = false
end
it { should eq('123') }
end
context 'for private asciicast' do
before do
asciicast.private = true
end
it { should eq('sekrit') }
end
end
2012-04-09 18:04:58 +00:00
describe '#stdout' do
context 'for single-file, JSON asciicast' do
let(:asciicast) { create(:asciicast) }
2013-07-08 22:55:31 +00:00
subject { asciicast.stdout.to_a }
2013-08-13 17:27:02 +00:00
it 'is enumerable with [delay, data] pair as every item' do
expect(subject).to eq([
[1.234567, "foo bar"],
[5.678987, "baz qux"],
[3.456789, "żółć jaźń"],
])
end
end
2013-07-08 22:55:31 +00:00
context 'for multi-file, legacy asciicast' do
let(:asciicast) { create(:legacy_asciicast) }
subject { asciicast.stdout.to_a }
it 'is enumerable with [delay, data] pair as every item' do
expect(subject).to eq([
[1.234567, "foobar"],
[0.123456, "baz"],
[2.345678, "qux"],
])
end
2013-07-08 22:55:31 +00:00
end
end
describe '#theme' do
it 'returns proper theme when theme_name is not blank' do
asciicast = described_class.new(theme_name: 'tango')
expect(asciicast.theme.name).to eq('tango')
end
it 'returns nil when theme_name is blank' do
asciicast = described_class.new(theme_name: '')
expect(asciicast.theme).to be(nil)
end
end
2011-11-23 21:30:09 +00:00
end