Class: TRuby::DocsExampleVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/docs_example_verifier.rb

Overview

Verifies code examples extracted from documentation.

Performs:

  • Syntax validation (parsing)

  • Type checking (for .trb examples)

  • Compilation (generates Ruby output)

Examples:

verifier = DocsExampleVerifier.new
results = verifier.verify_file("docs/getting-started.md")
results.each { |r| puts "#{r.status}: #{r.file_path}:#{r.line_number}" }

Defined Under Namespace

Classes: VerificationResult

Instance Method Summary collapse

Constructor Details

#initializeDocsExampleVerifier

Returns a new instance of DocsExampleVerifier.



48
49
50
51
# File 'lib/t_ruby/docs_example_verifier.rb', line 48

def initialize
  @extractor = DocsExampleExtractor.new
  @compiler = TRuby::Compiler.new
end

Instance Method Details

Print results to stdout

Parameters:

  • results (Array<VerificationResult>)

    Verification results

  • verbose (Boolean) (defaults to: false)

    Show passing tests too



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/t_ruby/docs_example_verifier.rb', line 110

def print_results(results, verbose: false)
  results.each do |result|
    next if result.pass? && !verbose

    status_icon = case result.status
                  when :pass then "\e[32m✓\e[0m"
                  when :fail then "\e[31m✗\e[0m"
                  when :skip then "\e[33m○\e[0m"
                  end

    puts "#{status_icon} #{result.file_path}:#{result.line_number}"

    result.errors&.each do |error|
      puts "    #{error}"
    end
  end

  summary_data = summary(results)
  puts
  puts "Results: #{summary_data[:passed]} passed, #{summary_data[:failed]} failed, #{summary_data[:skipped]} skipped"
  puts "Pass rate: #{summary_data[:pass_rate]}%"
end

#summary(results) ⇒ Hash

Generate a summary report

Parameters:

Returns:

  • (Hash)

    Summary statistics



96
97
98
99
100
101
102
103
104
# File 'lib/t_ruby/docs_example_verifier.rb', line 96

def summary(results)
  {
    total: results.size,
    passed: results.count(&:pass?),
    failed: results.count(&:fail?),
    skipped: results.count(&:skip?),
    pass_rate: results.empty? ? 0 : (results.count(&:pass?).to_f / results.size * 100).round(2),
  }
end

#verify_example(example) ⇒ VerificationResult

Verify a single code example

Parameters:

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/t_ruby/docs_example_verifier.rb', line 75

def verify_example(example)
  return skip_result(example, "Marked as skip-verify") unless example.should_verify?

  case example.language
  when "trb"
    verify_trb_example(example)
  when "ruby"
    verify_ruby_example(example)
  when "rbs"
    verify_rbs_example(example)
  else
    skip_result(example, "Unknown language: #{example.language}")
  end
rescue StandardError => e
  fail_result(example, ["Exception: #{e.message}"])
end

#verify_file(file_path) ⇒ Array<VerificationResult>

Verify all examples in a file

Parameters:

  • file_path (String)

    Path to the markdown file

Returns:



57
58
59
60
# File 'lib/t_ruby/docs_example_verifier.rb', line 57

def verify_file(file_path)
  examples = @extractor.extract_from_file(file_path)
  examples.map { |example| verify_example(example) }
end

#verify_glob(pattern) ⇒ Array<VerificationResult>

Verify all examples from multiple files

Parameters:

  • pattern (String)

    Glob pattern

Returns:



66
67
68
69
# File 'lib/t_ruby/docs_example_verifier.rb', line 66

def verify_glob(pattern)
  examples = @extractor.extract_from_glob(pattern)
  examples.map { |example| verify_example(example) }
end