spec/spec_helper.rb in nanoc-4.8.10 vs spec/spec_helper.rb in nanoc-4.8.11

- old
+ new

@@ -8,13 +8,15 @@ require 'nanoc' require 'nanoc/cli' require 'nanoc/spec' +require 'ddbuffer' require 'timecop' require 'rspec/its' require 'fuubar' +require 'yard' Nanoc::CLI.setup RSpec.configure do |c| c.include(Nanoc::Spec::Helper) @@ -272,9 +274,67 @@ s end failure_message_when_negated do |_actual| "expected that proc would not send notification #{name.inspect} with args #{expected_args.inspect}" + end +end + +RSpec::Matchers.define :have_correct_yard_examples do |_name, *_expected_args| + chain :in_file do |file| + root_dir = File.expand_path(__dir__ + '/..') + YARD.parse(root_dir + '/' + file) + end + + match do |actual| + examples = + P(actual).tags(:example).flat_map do |example| + # Classify + lines = example.text.lines.map do |line| + [line =~ /^\s*# ?=>/ ? :result : :code, line] + end + + # Join + pieces = [] + lines.each do |line| + if !pieces.empty? && pieces.last.first == line.first + pieces.last.last << line.last + else + pieces << line + end + end + lines = pieces.map(&:last) + + # Collect + lines.each_slice(2).to_a + end + + b = binding + executed_examples = examples.map do |pair| + { + input: pair.first, + expected: eval(pair.last.match(/# ?=>(.*)/)[1], b), + actual: eval(pair.first, b), + } + end + + @failing_examples = executed_examples.reject { |ex| ex[:expected] == ex[:actual] } + + @failing_examples.empty? + end + + failure_message do |_actual| + parts = + @failing_examples.map do |ex| + format( + "%s\nexpected to be\n %s\nbut was\n %s", + ex[:input], + ex[:expected].inspect, + ex[:actual].inspect, + ) + end + + parts.join("\n\n---\n\n") end end RSpec::Matchers.define :leak_open_files do |_name, *_expected_args| # Some remarks: