Refactored test helper methods for reuse

This commit is contained in:
Damon Maneice 2018-02-07 08:50:33 -05:00
Родитель d0360071f0
Коммит f2dbb38a04
5 изменённых файлов: 49 добавлений и 73 удалений

Просмотреть файл

@ -8,7 +8,7 @@ describe "collections" do
end
it "does not include emoji outside of description" do
metadata = metadata_for_collection(collection) || {}
metadata = metadata_for(collections_dir, collection) || {}
fields = %w[created_by display_name collection]
fields.each do |field|
@ -19,8 +19,8 @@ describe "collections" do
end
end
it "has a valid items" do
metadata = metadata_for_collection(collection) || {}
it "has valid items" do
metadata = metadata_for(collections_dir, collection) || {}
items = metadata["items"]
invalid_slugs = []
items.each do |item|
@ -66,7 +66,7 @@ describe "collections" do
end
it "has expected metadata in Jekyll front matter" do
metadata = metadata_for_collection(collection)
metadata = metadata_for(collections_dir, collection)
refute_empty metadata, "expected some metadata for collection"
metadata.each_key do |key|
@ -81,7 +81,7 @@ describe "collections" do
end
it "uses the right file name for specified image" do
metadata = metadata_for_collection(collection)
metadata = metadata_for(collections_dir, collection)
if metadata
paths = image_paths_for_collection(collection)
@ -98,7 +98,7 @@ describe "collections" do
end
it "has a valid body" do
body = body_for_collection(collection)
body = body_for(collections_dir, collection)
assert body && (1...MAX_BODY_LENGTH).cover?(body.length),
"must have a body no more than #{MAX_BODY_LENGTH} characters " \
@ -106,7 +106,7 @@ describe "collections" do
end
it "has valid created_by value" do
metadata = metadata_for_collection(collection) || {}
metadata = metadata_for(collections_dir, collection) || {}
if metadata["created_by"]
assert metadata["created_by"].match(USERNAME_REGEX),

Просмотреть файл

@ -49,27 +49,3 @@ end
def possible_image_file_names_for_collection(collection)
COLLECTION_IMAGE_EXTENSIONS.map { |ext| "#{collection}#{ext}" }
end
def metadata_for_collection(collection)
path = File.join(collections_dir, collection, "index.md")
return unless File.file?(path)
parts = File.read(path).split("---", 3)
return unless parts.size >= 2
begin
YAML.safe_load(parts[1])
rescue Psych::SyntaxError => ex
flunk "invalid YAML: #{ex.message}"
end
end
def body_for_collection(collection)
path = File.join(collections_dir, collection, "index.md")
return "" unless File.file?(path)
parts = File.read(path).split("---", 3)
return "" unless parts.size >= 2
parts[2]
end

Просмотреть файл

@ -19,3 +19,27 @@ def valid_uri_scheme?(scheme)
%w[http https].include?(scheme.downcase)
end
def metadata_for(dir, name)
path = File.join(dir, name, "index.md")
return unless File.file?(path)
parts = File.read(path).split("---", 3)
return unless parts.size >= 2
begin
YAML.safe_load(parts[1])
rescue Psych::SyntaxError => ex
flunk "invalid YAML: #{ex.message}"
end
end
def body_for(dir, name)
path = File.join(dir, name, "index.md")
return "" unless File.file?(path)
parts = File.read(path).split("---", 3)
return "" unless parts.size >= 2
parts[2]
end

Просмотреть файл

@ -37,7 +37,7 @@ describe "topics" do
end
it "uses the right format for 'released'" do
metadata = metadata_for(topic) || ""
metadata = metadata_for(topics_dir, topic) || ""
if released = metadata["released"]
text = released.to_s.gsub(/[\d+,\s]/, "").strip
@ -55,7 +55,7 @@ describe "topics" do
end
it "ends 'released' with a number" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
if metadata["released"]
number_regex = /\d\z/
@ -65,7 +65,7 @@ describe "topics" do
end
it "ends 'short_description' with punctuation" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
if metadata["short_description"]
punctuation_regex = /[.?!]\z/
@ -75,7 +75,7 @@ describe "topics" do
end
it "does not include emoji outside of description" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
fields = %w[created_by display_name released short_description related aliases topic]
fields.each do |field|
@ -87,7 +87,7 @@ describe "topics" do
end
it "has a valid GitHub URL" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
if metadata["github_url"]
uri = URI.parse(metadata["github_url"])
@ -98,7 +98,7 @@ describe "topics" do
end
it "has a valid URL" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
if metadata["url"]
uri = URI.parse(metadata["url"])
@ -107,7 +107,7 @@ describe "topics" do
end
it "has a valid Wikipedia URL" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
if metadata["wikipedia_url"]
uri = URI.parse(metadata["wikipedia_url"])
@ -156,7 +156,7 @@ describe "topics" do
end
it "has a matching topic key" do
metadata = metadata_for(topic)
metadata = metadata_for(topics_dir, topic)
if metadata
assert_equal topic, metadata["topic"],
@ -165,8 +165,8 @@ describe "topics" do
end
it "has a short_description that differs from the body" do
metadata = metadata_for(topic) || {}
body = body_for(topic)
metadata = metadata_for(topics_dir, topic) || {}
body = body_for(topics_dir, topic)
if metadata["short_description"]
refute_equal body.strip, metadata["short_description"].strip,
@ -181,7 +181,7 @@ describe "topics" do
end
it "uses the right file name for specified logo" do
metadata = metadata_for(topic)
metadata = metadata_for(topics_dir, topic)
if metadata
paths = image_paths_for(topic)
@ -247,7 +247,7 @@ describe "topics" do
end
it "has expected metadata in Jekyll front matter" do
metadata = metadata_for(topic)
metadata = metadata_for(topics_dir, topic)
refute_empty metadata, "expected some metadata for topic"
metadata.each_key do |key|
@ -262,7 +262,7 @@ describe "topics" do
end
it "has a valid body" do
body = body_for(topic)
body = body_for(topics_dir, topic)
assert body && (1...MAX_BODY_LENGTH).cover?(body.length),
"must have a body no more than #{MAX_BODY_LENGTH} characters " \
@ -270,7 +270,7 @@ describe "topics" do
end
it "has a valid short_description" do
metadata = metadata_for(topic) || {}
metadata = metadata_for(topics_dir, topic) || {}
if metadata["short_description"]
valid_range = 1...MAX_SHORT_DESCRIPTION_LENGTH
@ -282,8 +282,8 @@ describe "topics" do
end
it "follows the Topic Page Style Guide" do
text = body_for(topic)
metadata = metadata_for(topic)
text = body_for(topics_dir, topic)
metadata = metadata_for(topics_dir, topic)
end_punctuation = %w[. , ; :] + [" "]
month_abbreviations = %w[Jan Feb Mar Apr Jun Jul Aug Sep Oct Nov Dec]
day_ordinals = %w[1st 2nd 3rd 1th 2th 3th 4th 5th 6th 7th 8th 9th]

Просмотреть файл

@ -55,44 +55,20 @@ def possible_image_file_names_for(topic)
TOPIC_IMAGE_EXTENSIONS.map { |ext| "#{topic}#{ext}" }
end
def metadata_for(topic)
path = File.join(topics_dir, topic, "index.md")
return unless File.file?(path)
parts = File.read(path).split("---", 3)
return unless parts.size >= 2
begin
YAML.safe_load(parts[1])
rescue Psych::SyntaxError => ex
flunk "invalid YAML: #{ex.message}"
end
end
def related_topics_for(topic)
metadata = metadata_for(topic)
metadata = metadata_for(topics_dir, topic)
return [] unless metadata
return [] unless metadata["related"]
metadata["related"].split(",")
end
def aliases_for(topic)
metadata = metadata_for(topic)
metadata = metadata_for(topics_dir, topic)
return [] unless metadata
return [] unless metadata["aliases"]
metadata["aliases"].split(",")
end
def body_for(topic)
path = File.join(topics_dir, topic, "index.md")
return "" unless File.file?(path)
parts = File.read(path).split("---", 3)
return "" unless parts.size >= 2
parts[2]
end
def assert_oxford_comma(text)
return unless text