Class: Tryouts::ExpectationEvaluators::True

Inherits:
Base
  • Object
show all
Defined in:
lib/tryouts/expectation_evaluators/true.rb

Overview

Evaluator for boolean true expectations using syntax: #==> expression

PURPOSE:

  • Validates that an expression evaluates to exactly true (not truthy)
  • Provides explicit boolean validation for documentation-style tests
  • Distinguishes between true/false and truthy/falsy values

SYNTAX: #==> boolean_expression Examples:

[1, 2, 3]  #==> result.length == 3        # Pass: expression is true
[1, 2, 3]  #==> result.include?(2)        # Pass: expression is true
[]         #==> result.empty?             # Pass: expression is true
[1, 2, 3]  #==> result.empty?             # Fail: expression is false
[1, 2, 3]  #==> result.length             # Fail: expression is 3 (truthy but not true)

BOOLEAN STRICTNESS:

  • Only passes when expression evaluates to exactly true (not truthy)
  • Fails for false, nil, 0, "", [], {}, or any non-true value
  • Uses Ruby's === comparison for exact boolean matching
  • Encourages explicit boolean expressions in documentation

IMPLEMENTATION DETAILS:

  • Expression has access to result and _ variables (actual_result)
  • Expected display shows 'true (exactly)' for clarity
  • Actual display shows the evaluated expression result
  • Distinguishes from regular expectations through strict true matching

DESIGN DECISIONS:

  • Strict true matching prevents accidental truthy value acceptance
  • Clear expected display explains the exact requirement
  • Expression evaluation provides flexible boolean logic testing
  • Part of unified #= prefix convention for all expectation types

Instance Attribute Summary

Attributes inherited from Base

#context, #expectation, #test_case

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Tryouts::ExpectationEvaluators::Base

Class Method Details

.handles?(expectation_type) ⇒ Boolean

Returns:



42
43
44
# File 'lib/tryouts/expectation_evaluators/true.rb', line 42

def self.handles?(expectation_type)
  expectation_type == :true # rubocop:disable Lint/BooleanSymbol
end

Instance Method Details

#evaluate(actual_result = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tryouts/expectation_evaluators/true.rb', line 46

def evaluate(actual_result = nil)
  expectation_result = ExpectationResult.from_result(actual_result)
  expression_result  = eval_expectation_content(@expectation.content, expectation_result)

  build_result(
    passed: expression_result == true,
    actual: expression_result,
    expected: true,
  )
rescue StandardError => ex
  handle_evaluation_error(ex, actual_result)
end