Skip to content

Commit

Permalink
liturgical law: meaningful example names
Browse files Browse the repository at this point in the history
ref #101
  • Loading branch information
igneus committed Jul 15, 2024
1 parent 6c298ac commit 3383c74
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ group :test do
gem 'cucumber', '~> 2.99'
gem 'simplecov'
gem 'backports', '~> 3.18'
gem 'markly'
end
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ GEM
i18n (0.6.9)
jaro_winkler (1.5.4)
json (2.0.2)
markly (0.10.0)
multi_json (1.12.2)
multi_test (0.1.2)
parallel (1.19.2)
Expand Down Expand Up @@ -83,6 +84,7 @@ DEPENDENCIES
backports (~> 3.18)
cucumber (~> 2.99)
i18n
markly
rake
roman-numerals
rspec
Expand Down
75 changes: 45 additions & 30 deletions spec/liturgical_law_spec.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
require 'spec_helper'
require 'markly'

class LiturgicalLawExample
# make RSpec expectations available for the code example
extend RSpec::Matchers
extend RSpec::Core::Pending

class << self
def year
# random year, but stays the same for the whole example/code block
@year ||= rand(1970 .. 3000)
end

def years(from: 1970, to: 2300)
from.upto(to)
end

def years_with(from: 1970, to: 2300)
from
.upto(to)
.select {|y| yield y }
.tap {|result| raise 'no matching year found' if result.empty? }
end
end
end

Dir[File.expand_path('../../liturgical_law/*.md', __FILE__)].each do |path|
describe path, slow: true do
document = File.read path
doc = MarkdownDocument.new document

doc.each_ruby_example do |code, line|
describe "example L#{line}" do
it 'executes without failure' do
cls = Class.new do
# make RSpec expectations available for the code example
extend RSpec::Matchers
extend RSpec::Core::Pending

class << self
def year
# random year, but stays the same for the whole example/code block
@year ||= rand(1970 .. 3000)
end

def years(from: 1970, to: 2300)
from.upto(to)
end

def years_with(from: 1970, to: 2300)
from
.upto(to)
.select {|y| yield y }
.tap {|result| raise 'no matching year found' if result.empty? }
end
end
end

cls.class_eval(code, path, line)

last_paragraph = ''
header_level = 0
Markly.parse(document).each do |node|
case node.type
when :header
last_paragraph = node.to_plaintext
when :paragraph
last_paragraph = node.to_plaintext
when :code_block
next if node.fence_info != 'ruby'

line = node.source_position[:start_line]

context = last_paragraph[0..50].gsub(/\s+/, ' ')
context = context[0..context.rindex(' ')]

it context do
cls = Class.new(LiturgicalLawExample)
cls.class_eval(node.string_content, path, line)
end
end
end
Expand Down

0 comments on commit 3383c74

Please sign in to comment.