From 3383c74f52ef58c379435a8ad872dd752fe888bb Mon Sep 17 00:00:00 2001 From: Jakub Pavlik Date: Mon, 15 Jul 2024 23:05:11 +0200 Subject: [PATCH] liturgical law: meaningful example names ref #101 --- Gemfile | 1 + Gemfile.lock | 2 + spec/liturgical_law_spec.rb | 75 ++++++++++++++++++++++--------------- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/Gemfile b/Gemfile index 78713719..76dfdc8a 100644 --- a/Gemfile +++ b/Gemfile @@ -23,4 +23,5 @@ group :test do gem 'cucumber', '~> 2.99' gem 'simplecov' gem 'backports', '~> 3.18' + gem 'markly' end diff --git a/Gemfile.lock b/Gemfile.lock index 049d7882..a047fac2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -83,6 +84,7 @@ DEPENDENCIES backports (~> 3.18) cucumber (~> 2.99) i18n + markly rake roman-numerals rspec diff --git a/spec/liturgical_law_spec.rb b/spec/liturgical_law_spec.rb index 37ebabe9..43241c25 100644 --- a/spec/liturgical_law_spec.rb +++ b/spec/liturgical_law_spec.rb @@ -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