Module: RspecSprint::Fixers::LetItBe::Formatter

Defined in:
lib/rspec_sprint/fixers/let_it_be/formatter.rb

Overview

dry-run レポートを端末向けに整形する。

見出しは「変換可能候補の件数」と「対象外の let{create} 件数(lazy/let! 比)」。上限 share は実収量と乖離しうる(候補が参照する factory のコストの大半が let! や直接 create 由来のことがある)ため、誤誘導する「推定削減」ではなく「上限値・実収量ではない」と明示して補足に降格する。

Class Method Summary collapse

Class Method Details

.format(report) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rspec_sprint/fixers/let_it_be/formatter.rb', line 15

def format(report)
  return none(report.let_bang_count) if report.candidates.empty?

  lines = [
    "rspec-sprint fix --dry-run (let_it_be)",
    "変換可能候補(lazy let{create}): #{report.candidates.size}",
    let_bang_line(report.let_bang_count),
    ""
  ].compact
  report.candidates.each do |c|
    lines << "#{c.file_path}:#{c.line}"
    lines << "  - let(:#{c.let_name}) { create(:#{c.factory_name}) }"
    lines << "  + let_it_be(:#{c.let_name}) { create(:#{c.factory_name}) }"
    lines << ""
  end
  lines << "参考(上限値・実収量ではない): 候補が参照する factory は factory time の最大 #{pct(report)}%。" \
           "実際の削減は適用+再実行で測定(未実施)。"
  lines << "注意: これは dry-run です。適用はまだ行いません(緑のまま速くなるか実測する verify は次段)。"
  lines.join("\n")
end

.let_bang_line(count) ⇒ Object



46
47
48
49
50
# File 'lib/rspec_sprint/fixers/let_it_be/formatter.rb', line 46

def let_bang_line(count)
  return nil unless count.to_i.positive?

  "対象外: let!{create} #{count} 件(eager→once でセマンティクスが変わるため安全変換不可。apply モードでは verify 付きで対応:`rspec-sprint fix let-it-be`)"
end

.none(let_bang_count) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rspec_sprint/fixers/let_it_be/formatter.rb', line 36

def none(let_bang_count)
  if let_bang_count.to_i.positive?
    "変換可能な lazy let{create} 候補は 0 件。" \
      "ただし let!{create} が #{let_bang_count} 件あります" \
      "(eager→once でセマンティクスが変わるため安全変換不可。apply モードでは verify 付きで対応:`rspec-sprint fix let-it-be`)。"
  else
    "該当する let{create} 候補は見つかりませんでした(単一 create 本体の let のみ対象)。"
  end
end

.pct(report) ⇒ Object



52
53
54
# File 'lib/rspec_sprint/fixers/let_it_be/formatter.rb', line 52

def pct(report)
  (report.upper_bound_share * 100).round
end