Class: Tryouts::ExpectationEvaluators::RegexMatch
- Defined in:
- lib/tryouts/expectation_evaluators/regex_match.rb
Overview
Evaluator for regex match expectations using syntax: #=~> /pattern/
PURPOSE:
- Validates that the test result matches a regular expression pattern
- Supports all Ruby regex features (anchors, flags, groups, etc.)
- Provides pattern matching for string validation in documentation-style tests
SYNTAX: #=~> /pattern/flags Examples:
"hello world" #=~> /hello/ # Pass: basic pattern match
"user@example.com" #=~> /^[^@]+@[^@]+$/ # Pass: email validation pattern
"Phone: 555-1234" #=~> /\d{3}-\d{4}/ # Pass: phone number pattern
"HELLO WORLD" #=~> /hello/i # Pass: case insensitive match
"hello world" #=~> /goodbye/ # Fail: pattern not found
IMPLEMENTATION DETAILS:
- Converts actual_result to string using to_s for regex matching
- Uses Ruby's =~ operator for pattern matching (returns match position or nil)
- Evaluates expectation content to resolve regex patterns with flags
- Expected display shows pattern.inspect (e.g., "/hello/i", "/\d+/")
- Actual display shows stringified result for pattern comparison
DESIGN DECISIONS:
- Always converts to string (allows regex matching on any object)
- Uses =~ operator (Ruby standard) rather than match? for compatibility
- Pattern evaluation supports dynamic regex construction
- Displays pattern.inspect for clear regex representation
Instance Attribute Summary
Attributes inherited from Base
#context, #expectation, #test_case
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Tryouts::ExpectationEvaluators::Base
Class Method Details
.handles?(expectation_type) ⇒ Boolean
37 38 39 |
# File 'lib/tryouts/expectation_evaluators/regex_match.rb', line 37 def self.handles?(expectation_type) expectation_type == :regex_match end |
Instance Method Details
#evaluate(actual_result = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tryouts/expectation_evaluators/regex_match.rb', line 41 def evaluate(actual_result = nil) expectation_result = ExpectationResult.from_result(actual_result) pattern = eval_expectation_content(@expectation.content, expectation_result) # Auto-detect exceptions and use message for regex matching # This allows #=~> /pattern/ to work naturally with exception messages # Note: error variable is already available in context (set in evaluate_expectations) string_result = if actual_result.is_a?(Exception) actual_result. # Match against error message else actual_result.to_s # Normal case: convert to string end match_result = string_result =~ pattern build_result( passed: !match_result.nil?, actual: string_result, expected: pattern.inspect, ) rescue StandardError => ex handle_evaluation_error(ex, actual_result) end |