Class: Tryouts::CLI::TestRunState

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/cli/formatters/test_run_state.rb

Overview

Immutable state tracking for test runs using modern Ruby Data.define

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emptyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 17

def self.empty
  new(
    total_tests: 0,
    passed: 0,
    failed: 0,
    errors: 0,
    files_completed: 0,
    total_files: 0,
    current_file: nil,
    current_test: nil,
    start_time: nil,
  )
end

.initial(total_files: 0) ⇒ Object



31
32
33
34
35
36
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 31

def self.initial(total_files: 0)
  empty.with(
    total_files: total_files,
    start_time: Time.now,
  )
end

Instance Method Details

#completion_percentageObject



115
116
117
118
119
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 115

def completion_percentage
  return 0 if total_files == 0

  (files_completed.to_f / total_files * 100).round(1)
end

#elapsed_timeObject

Computed properties



91
92
93
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 91

def elapsed_time
  start_time ? Time.now - start_time : 0
end

#files_remainingObject



111
112
113
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 111

def files_remaining
  total_files - files_completed
end

#has_issues?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 103

def has_issues?
  issues_count > 0
end

#issues_countObject



95
96
97
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 95

def issues_count
  failed + errors
end

#passed_countObject



99
100
101
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 99

def passed_count
  total_tests - issues_count
end

#tests_run?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 107

def tests_run?
  total_tests > 0
end

#update_from_event(event_type, *args, **_kwargs) ⇒ Object

Update state based on formatter events using pattern matching



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tryouts/cli/formatters/test_run_state.rb', line 39

def update_from_event(event_type, *args, **_kwargs)
  case event_type
  in :phase_header
    _, file_count, level = args
    if level == 0 && file_count
      with(total_files: file_count, start_time: Time.now)
    else
      self
    end

  in :file_start
    file_path   = args[0]
    pretty_path = Console.pretty_path(file_path)
    with(current_file: pretty_path)

  in :file_end
    with(
      files_completed: files_completed + 1,
      current_file: nil,
    )

  in :test_start
    test_case = args[0]
    desc      = test_case.description.to_s
    desc      = "test #{args[1]}" if desc.empty?
    with(current_test: desc)

  in :test_end
    with(current_test: nil)

  in :test_result
    result_packet = args[0]
    updated_state = with(total_tests: total_tests + 1)

    case result_packet.status
    when :passed
      updated_state.with(passed: passed + 1)
    when :failed
      updated_state.with(failed: failed + 1)
    when :error
      updated_state.with(errors: errors + 1)
    else
      updated_state
    end

  else
    # Unknown event, return unchanged state
    self
  end
end