Module: Trailblazer::Core::Utils::ConvertOperationTest

Defined in:
lib/trailblazer/core/utils/convert_operation_test.rb

Overview

TODO: add skip

Class Method Summary collapse

Class Method Details

.call(filepath, target:, source_gem:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
89
90
# File 'lib/trailblazer/core/utils/convert_operation_test.rb', line 11

def self.call(filepath, target:, source_gem:)
  within_marker = false
  within_ignore = false
  within_ctx_to_result = false
  within_keep = false

  op_test =
  File.foreach(filepath).collect do |line|
    if line.match(/#:[\w]+/) # FIXME: we don't use this!
      within_marker = true
    end
    if line.match(/#:.+ end/)
      within_marker = false
    end

    if line.match(/#~keep$/)
      within_keep = true
    end
    if line.match(/#~keep end/)
      within_keep = false
    end

    if line.match(/#~ignore/)
      within_ignore = true
    end
    if line.match(/#~ignore end/)
      within_ignore = false
    end

    if line.match(/#~ctx_to_result/)
      within_ctx_to_result = true
    end
    if line.match(/#~ctx_to_result end/)
      within_ctx_to_result = false
    end

    if within_ignore
      # puts "@@@@@ #{line.inspect}"
      line = ""
    elsif within_keep
      line
    else
      if match = line.match(/#!hint (.+)/)
        ws = line.match(/^(\s+)/) # Find out number of whitespace before code starts.

        line = (" " * ws[1].size) + match[1] + "\n"
      else
        line = line.sub("< Trailblazer::Activity::Railway", "< Trailblazer::Operation")
        line = line.sub("< Trailblazer::Activity::FastTrack", "< Trailblazer::Operation")
        line = line.gsub("::Activity", "::Operation")

      # if within_marker
        line = line.sub("signal, (ctx, _) =", "result =")
        if within_ctx_to_result
          line = line.sub("ctx[", "result[")
        end

        if match = line.match(/(Trailblazer::Operation\.\(([\w:]+), ?)/)
          activity = match[2]
          line = line.sub(match[0], "#{activity}.(")
        end

        if match = line.match(/(\s+)puts signal.+(:\w+)>/)
          semantic = match[2]
          line = "#{match[1]}result.success? # => #{semantic == ":success" ?  true : false}\n"
        end
      end
      # end

      line = line.sub("assert_equal ctx", "assert_equal result")
      line = line.sub("assert_equal signal", "assert_equal result.event")
    end

    line
  end

  op_test.insert(0, "# THIS FILE IS AUTOGENERATED FROM #{source_gem}/#{filepath}\n")

  File.write target, op_test.join("")
end