Skip to content

Commit b49b9c8

Browse files
committed
Add tests for the gist tag.
1 parent 0f565a0 commit b49b9c8

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format progress

jekyll-gist.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ Gem::Specification.new do |spec|
1919

2020
spec.add_development_dependency "bundler", "~> 1.6"
2121
spec.add_development_dependency "rake"
22+
spec.add_development_dependency "rspec"
23+
spec.add_development_dependency "jekyll", "~> 2.0"
2224
end

spec/gist_tag_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'spec_helper'
2+
3+
describe(Jekyll::Gist::GistTag) do
4+
let(:doc) { doc_with_content(content) }
5+
let(:content) { "{% gist #{gist} %}" }
6+
let(:output) do
7+
doc.content = content
8+
doc.output = Jekyll::Renderer.new(doc.site, doc).run
9+
end
10+
11+
context "simple gist" do
12+
let(:gist) { 358471 }
13+
14+
it "produces the correct script tag" do
15+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
16+
end
17+
end
18+
19+
context "private gist" do
20+
21+
context "when valid" do
22+
let(:gist) { "mattr-/24081a1d93d2898ecf0f" }
23+
24+
it "produces the correct script tag" do
25+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
26+
end
27+
end
28+
29+
context "with file specified" do
30+
let(:gist) { "mattr-/24081a1d93d2898ecf0f" }
31+
let(:filename) { "myfile.ext" }
32+
let(:content) { "{% gist #{gist} #{filename} %}" }
33+
34+
it "produces the correct script tag" do
35+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>/)
36+
end
37+
end
38+
39+
context "when invalid" do
40+
let(:gist) { "mattr-24081a1d93d2898ecf0f" }
41+
42+
it "raises an error" do
43+
expect(->{ output }).to raise_error
44+
end
45+
end
46+
47+
end
48+
49+
context "no gist id present" do
50+
let(:gist) { "" }
51+
52+
it "raises an error" do
53+
expect(->{ output }).to raise_error
54+
end
55+
end
56+
57+
end

spec/spec_helper.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
TEST_DIR = File.dirname(__FILE__)
2+
TMP_DIR = File.expand_path("../tmp", TEST_DIR)
3+
4+
require 'jekyll'
5+
require File.expand_path("../lib/jekyll-gist.rb", TEST_DIR)
6+
7+
Jekyll.logger.log_level = :error
8+
STDERR.reopen(test(?e, '/dev/null') ? '/dev/null' : 'NUL:')
9+
10+
RSpec.configure do |config|
11+
config.treat_symbols_as_metadata_keys_with_true_values = true
12+
config.run_all_when_everything_filtered = true
13+
config.filter_run :focus
14+
config.order = 'random'
15+
16+
def tmp_dir(*files)
17+
File.join(TMP_DIR, *files)
18+
end
19+
20+
def source_dir(*files)
21+
tmp_dir('source', *files)
22+
end
23+
24+
def dest_dir(*files)
25+
tmp_dir('dest', *files)
26+
end
27+
28+
def doc_with_content(content, opts = {})
29+
my_site = site
30+
Jekyll::Document.new(source_dir('_test/doc.md'), {site: my_site, collection: collection(my_site)})
31+
end
32+
33+
def collection(site, label = 'test')
34+
Jekyll::Collection.new(site, label)
35+
end
36+
37+
def site(opts = {})
38+
conf = Jekyll::Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, opts.merge({
39+
"source" => source_dir,
40+
"destination" => dest_dir
41+
}))
42+
Jekyll::Site.new(conf)
43+
end
44+
end

0 commit comments

Comments
 (0)