-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_doc_ruby_examples.rb
More file actions
69 lines (58 loc) · 2.45 KB
/
Copy pathcheck_doc_ruby_examples.rb
File metadata and controls
69 lines (58 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
# frozen_string_literal: true
# Verify the ```ruby doc examples against the built binding (#50 phase 5).
#
# The Ruby tabs document outputs with `# =>` comments, e.g.
# Disarm.transliterate("Київ", lang: :uk) # => "Kyiv"
# This script loads the compiled gem and, for every such line, evaluates the
# Disarm.* call and checks it against the documented value — the Ruby analogue of
# the Sybil (Python) and cargo (Rust) doc gates. It is lenient about trailing
# prose in the comment (`# => true (Cyrillic 'а')`): when the expected side does
# not parse as a literal, the call is still run (so a raise is caught) and only
# the value comparison is skipped. Lines without a `# =>` (setup, intentional
# error demos) are ignored — those are covered by RSpec.
#
# Usage: ruby scripts/check_doc_ruby_examples.rb
# Requires the gem to be built (rake compile) and on the load path.
root = File.expand_path("..", __dir__)
$LOAD_PATH.unshift(File.join(root, "bindings", "ruby", "lib"))
require "disarm"
checked = 0
failures = []
# docs/plans/ is gitignored working notes, absent in CI. Scanning it turns this gate
# red for one developer while CI stays green.
local_only = File.join(root, "docs", "plans")
Dir.glob(File.join(root, "docs", "**", "*.md")).sort.each do |md|
next if md.start_with?("#{local_only}/")
File.read(md).scan(/^[ \t]*```ruby\n(.*?)\n[ \t]*```/m) do |(block)|
block.each_line do |raw|
line = raw.strip
# Only lines that call Disarm.* AND document an expected value.
next unless line.include?("Disarm.") && line =~ /\A(.+?)\s*#\s*=>\s*(.+?)\s*\z/
expr = Regexp.last_match(1).strip
expected_src = Regexp.last_match(2).strip
next if expr.empty?
checked += 1
begin
got = eval(expr) # rubocop:disable Security/Eval — trusted, our own docs
rescue StandardError, SyntaxError => e
failures << "#{File.basename(md)}: `#{expr}` raised #{e.class}: #{e.message}"
next
end
begin
want = eval(expected_src) # the `# =>` literal
rescue StandardError, SyntaxError
next # trailing prose after the literal — call ran, skip value check
end
next if got == want
failures << "#{File.basename(md)}: `#{expr}` => #{got.inspect}, documented #{want.inspect}"
end
end
end
puts "checked #{checked} ruby doc expressions"
if failures.empty?
puts "all ruby doc examples ok"
else
failures.each { |f| warn "FAIL #{f}" }
exit 1
end