Class: Tryouts::EnhancedParser

Inherits:
Parsers::BaseParser show all
Defined in:
lib/tryouts/parsers/enhanced_parser.rb

Overview

Enhanced parser using Prism’s native comment extraction for robust parsing

The EnhancedParser is the default parser that provides syntax-aware comment extraction by leveraging Ruby’s official Prism parser. This approach eliminates common parsing issues found in regex-based parsers, particularly with complex Ruby syntax.

## Key Benefits over LegacyParser

### 1. HEREDOC Safety

  • Uses Prism’s ‘parse_comments()` to extract only actual Ruby comments

  • Automatically excludes content inside string literals, HEREDOCs, and interpolation

  • Prevents false positive expectation detection

### 2. Inline Comment Handling

  • Correctly handles lines with both code and comments

  • Supports multiple comments per line with proper positioning

  • Emits separate tokens for code and comment content

### 3. Syntax Awareness

  • Leverages Ruby’s official parser for accurate code understanding

  • Handles complex Ruby syntax edge cases reliably

  • More robust than regex-based parsing approaches

### 4. Performance

  • Uses optimized C-based Prism parsing for comment extraction

  • Efficient handling of large files with complex syntax

## Pattern Matching Uses Ruby 3.4+ pattern matching (case/in) for token classification, providing clean, modern syntax for expectation type detection.

Examples:

Basic usage

parser = Tryouts::EnhancedParser.new(source_code, file_path)
testrun = parser.parse
puts testrun.test_cases.length

Problematic code that EnhancedParser handles correctly

source = <<~RUBY
  ## Test HEREDOC handling
  sql = <<~SQL
    SELECT * FROM users
    -- This is NOT a tryout comment
    #=> This is NOT a tryout expectation
  SQL
  puts sql.length
  #=> Integer  # This IS a real expectation
RUBY

See Also:

Since:

  • 3.2.0

Instance Attribute Summary

Attributes inherited from Parsers::BaseParser

#warnings

Instance Method Summary collapse

Methods inherited from Parsers::BaseParser

#initialize

Methods included from Parsers::SharedMethods

#add_context_to_block, #add_warning, #block_has_content?, #build_setup, #build_teardown, #build_test_case, #calculate_block_range, #calculate_end_line, #classify_blocks, #classify_potential_descriptions_with_boundaries, #collect_unnamed_test_warning, #extract_code_content, #extract_pure_code_from_blocks, #find_test_case_boundaries, #find_test_case_end, #group_into_test_blocks, #handle_syntax_errors, #has_following_test_pattern?, #is_expectation_type?, #new_test_block, #parse_expectation, #parse_ruby_line, #process_test_blocks, #validate_strict_mode, #warnings

Constructor Details

This class inherits a constructor from Tryouts::Parsers::BaseParser

Instance Method Details

#parseTryouts::Testrun

Parse source code into a Testrun using Prism-based comment extraction

This method provides the main parsing logic that converts raw Ruby source code containing tryout syntax into structured test cases. Uses Prism’s native comment extraction to avoid HEREDOC parsing issues.

Returns:

  • (Tryouts::Testrun)

    Structured test data with setup, test cases, teardown, and warnings

Raises:

Since:

  • 3.2.0



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tryouts/parsers/enhanced_parser.rb', line 72

def parse
  return handle_syntax_errors if @prism_result.failure?

  # Use inhouse comment extraction instead of line-by-line regex parsing
  # This automatically excludes HEREDOC content!
  tokens           = tokenize_content_with_inhouse_extraction
  test_boundaries  = find_test_case_boundaries(tokens)
  tokens           = classify_potential_descriptions_with_boundaries(tokens, test_boundaries)
  test_blocks      = group_into_test_blocks(tokens)
  process_test_blocks(test_blocks)
end