Skip to content

Commit 185491f

Browse files
committed
Add test cases
1 parent 2ebffa7 commit 185491f

7 files changed

Lines changed: 160 additions & 32 deletions

File tree

‎.rspec‎

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

‎.travis.yml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ rvm:
55
branches:
66
only:
77
- master
8-
before_script: bundle exec rake generate
9-
script: bundle exec rake release
8+
script: bundle exec rspec spec
109
env:
1110
global:
1211
secure: KLuKTR30DpaXXh3Vr0LoSYhXn7OsPL/yR5yoRUKB7C6MKxPFt+5gHzdPbXcx/hrcheXkQad4elxkco8qQiKWe4hNeorQFssbsvVSe2P5s2GMaYJma+WdEwDlYwsjpP98xIQ4v5vRamXehfpB8jy/n+mlvdGAIiXA/d1ka9BpkJI=

‎Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ gem 'travis'
99
group :development do
1010
gem 'spidr', '~> 0.4'
1111
gem 'validate-website', '~> 0.9'
12+
gem 'rspec'
1213
end

‎Gemfile.lock‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ GEM
2121
execjs
2222
coffee-script-source (1.8.0)
2323
colorator (0.1)
24+
diff-lcs (1.2.5)
2425
ethon (0.7.2)
2526
ffi (>= 1.3.0)
2627
execjs (2.2.2)
@@ -141,6 +142,18 @@ GEM
141142
ffi (>= 0.5.0)
142143
rdiscount (2.1.7)
143144
redcarpet (3.1.2)
145+
rspec (3.1.0)
146+
rspec-core (~> 3.1.0)
147+
rspec-expectations (~> 3.1.0)
148+
rspec-mocks (~> 3.1.0)
149+
rspec-core (3.1.7)
150+
rspec-support (~> 3.1.0)
151+
rspec-expectations (3.1.2)
152+
diff-lcs (>= 1.2.0, < 2.0)
153+
rspec-support (~> 3.1.0)
154+
rspec-mocks (3.1.3)
155+
rspec-support (~> 3.1.0)
156+
rspec-support (3.1.2)
144157
safe_yaml (1.0.4)
145158
sass (3.4.9)
146159
slop (3.6.0)
@@ -181,6 +194,7 @@ DEPENDENCIES
181194
github-pages
182195
kramdown
183196
rake (~> 10.0)
197+
rspec
184198
spidr (~> 0.4)
185199
travis
186200
validate-website (~> 0.9)

‎_plugins/generators.rb‎

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
module RSS
2-
class Generator < Jekyll::Generator
3-
ROW_MATCHER = /^-\s*\[(?<title>[^\]]+)\]\((?<url>[^)]+)\){:\s*.article(?<data>(\s*data-\w+="[^"]+")+)\s*}\s*$/
4-
DATA_MATCHER = /data-(?<key>\w+)="(?<value>[^"]+)"/
5-
def generate(site)
6-
@index_page = site.pages.find { |page| page.name == "index.md" }
7-
@feed_page = site.pages.find { |page| page.name == "feed.xml" }
8-
@rss_page = site.pages.find { |page| page.name == "rss.xml" }
9-
@feed_page.data['articles'] = articles
10-
@rss_page.data['articles'] = articles
1+
require 'jekyll'
2+
3+
class RSSGenerator < Jekyll::Generator
4+
ROW_MATCHER = /^-\s*\[(?<title>[^\]]+)\]\((?<url>[^)]+)\){:\s*.article(?<data>(\s*data-\w+="[^"]+")+)\s*}\s*$/
5+
DATA_MATCHER = /data-(?<key>\w+)="(?<value>[^"]+)"/
6+
def generate(site)
7+
@index_page = site.pages.find { |page| page.name == "index.md" }
8+
site.pages.each do |page|
9+
page.data['articles'] = articles if %w(feed.xml rss.xml).include?(page.name)
1110
end
11+
end
1212

13-
private
14-
def articles
15-
unless @articles
16-
@articles = []
17-
rows.each do |row|
18-
article = {}
19-
m = row.match(ROW_MATCHER)
20-
m[:data].scan(DATA_MATCHER) do |key, value|
21-
article[key] = value
22-
end
23-
if article["tags"].include?("translated")
24-
article["title"] = "[번역] #{m[:title]}"
25-
else
26-
article["title"] = m[:title]
27-
end
28-
article["url"] = m[:url]
29-
@articles << article
13+
def articles
14+
unless @articles
15+
@articles = []
16+
rows.each do |row|
17+
article = {}
18+
m = row.match(ROW_MATCHER)
19+
m[:data].scan(DATA_MATCHER) do |key, value|
20+
article[key] = value
3021
end
22+
set_title(article, m)
23+
article["url"] = m[:url]
24+
@articles << article
3125
end
32-
@articles
3326
end
27+
@articles
28+
end
29+
30+
private
31+
def rows
32+
@index_page.content.split(/#articles\s*}\n\n/).last.split(/## \[동영상\]/).first.split(/\n/)
33+
end
3434

35-
def rows
36-
@index_page.content.split(/#articles\s*}\n\n/).last.split(/## \[동영상\]/).first.split(/\n/)
35+
def set_title(article, m)
36+
if article["tags"].include?("translated")
37+
article["title"] = "[번역] #{m[:title]}"
38+
else
39+
article["title"] = m[:title]
3740
end
3841
end
3942
end

‎spec/generators_spec.rb‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
describe RSSGenerator do
3+
subject(:generator) { RSSGenerator.new }
4+
it { is_expected.not_to be_nil }
5+
6+
describe "#articles" do
7+
before do
8+
allow(subject).to receive(:rows).and_return([
9+
'- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="translated ruby" data-author="author" }',
10+
'- [title](https://link.to.article){: .article data-date="2015.01.12" data-tags="ruby" data-author="author" }',
11+
])
12+
end
13+
14+
it { expect(subject.articles.size).to be(2) }
15+
it { expect(subject.articles.first["title"]).to eq("[번역] title") }
16+
it { expect(subject.articles.last["title"]).to eq("title") }
17+
18+
end
19+
end

‎spec/spec_helper.rb‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require './_plugins/generators'
2+
# This file was generated by the `rspec --init` command. Conventionally, all
3+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
5+
# file to always be loaded, without a need to explicitly require it in any files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need it.
13+
#
14+
# The `.rspec` file also contains a few flags that are not defaults but that
15+
# users commonly want.
16+
#
17+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18+
RSpec.configure do |config|
19+
# rspec-expectations config goes here. You can use an alternate
20+
# assertion/expectation library such as wrong or the stdlib/minitest
21+
# assertions if you prefer.
22+
config.expect_with :rspec do |expectations|
23+
# This option will default to `true` in RSpec 4. It makes the `description`
24+
# and `failure_message` of custom matchers include text for helper methods
25+
# defined using `chain`, e.g.:
26+
# be_bigger_than(2).and_smaller_than(4).description
27+
# # => "be bigger than 2 and smaller than 4"
28+
# ...rather than:
29+
# # => "be bigger than 2"
30+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31+
end
32+
33+
# rspec-mocks config goes here. You can use an alternate test double
34+
# library (such as bogus or mocha) by changing the `mock_with` option here.
35+
config.mock_with :rspec do |mocks|
36+
# Prevents you from mocking or stubbing a method that does not exist on
37+
# a real object. This is generally recommended, and will default to
38+
# `true` in RSpec 4.
39+
mocks.verify_partial_doubles = true
40+
end
41+
42+
# The settings below are suggested to provide a good initial experience
43+
# with RSpec, but feel free to customize to your heart's content.
44+
=begin
45+
# These two settings work together to allow you to limit a spec run
46+
# to individual examples or groups you care about by tagging them with
47+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
48+
# get run.
49+
config.filter_run :focus
50+
config.run_all_when_everything_filtered = true
51+
52+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
53+
# For more details, see:
54+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
55+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
56+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
57+
config.disable_monkey_patching!
58+
59+
# This setting enables warnings. It's recommended, but in some cases may
60+
# be too noisy due to issues in dependencies.
61+
config.warnings = true
62+
63+
# Many RSpec users commonly either run the entire suite or an individual
64+
# file, and it's useful to allow more verbose output when running an
65+
# individual spec file.
66+
if config.files_to_run.one?
67+
# Use the documentation formatter for detailed output,
68+
# unless a formatter has already been configured
69+
# (e.g. via a command-line flag).
70+
config.default_formatter = 'doc'
71+
end
72+
73+
# Print the 10 slowest examples and example groups at the
74+
# end of the spec run, to help surface which specs are running
75+
# particularly slow.
76+
config.profile_examples = 10
77+
78+
# Run specs in random order to surface order dependencies. If you find an
79+
# order dependency and want to debug it, you can fix the order by providing
80+
# the seed, which is printed after each run.
81+
# --seed 1234
82+
config.order = :random
83+
84+
# Seed global randomization in this process using the `--seed` CLI option.
85+
# Setting this allows you to use `--seed` to deterministically reproduce
86+
# test failures related to randomization by passing the same `--seed` value
87+
# as the one that triggered the failure.
88+
Kernel.srand config.seed
89+
=end
90+
end

0 commit comments

Comments
 (0)