File: generator.rb

package info (click to toggle)
ruby-rr 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,424 kB
  • sloc: ruby: 11,405; makefile: 7
file content (120 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require File.expand_path('../../generator', __FILE__)
require File.expand_path('../../test_case/generator', __FILE__)

module TestFile
  class Generator
    include ::Generator

    attr_accessor \
      :include_rr_before_test_framework,
      :autorequire_gems,
      :directory

    attr_reader :project, :index, :prelude, :requires

    def setup(project, index)
      super
      @project = project
      self.include_rr_before_test_framework = project.include_rr_before_test_framework
      self.autorequire_gems = project.autorequire_gems
      @requires = []
      @prelude = ""
      @index = index
      @body = ""
    end

    def add_to_requires(path)
      @requires << path
    end

    def add_to_prelude(string)
      @prelude << string + "\n"
    end

    def add_to_body(string)
      @body << string + "\n"
    end

    def add_test_case(content=nil, &block)
      if content.nil?
        test_case = test_case_generator.call(self, &block)
        content = test_case.string
      end
      @body << content + "\n"
    end

    def add_working_test_case
      add_test_case do |test_case|
        test_case.add_test <<-EOT
          object = Object.new
          mock(object).foo
          object.foo
        EOT
        yield test_case if block_given?
      end
    end

    def call
      path = File.join(directory, "#{filename_prefix}.rb")
      if RR.debug?
        puts "Test file path: #{path}"
      end
      File.open(path, 'w') do |f|
        if RR.debug?
          puts "~ Test file contents ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
          puts content
          puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        end
        f.write(content)
      end
    end

    def test_case_generator
      @test_case_generator ||= TestCase::Generator.factory
    end

    def add_test_case_with_adapter_tests
      add_test_case do |test_case|
        test_case.include_adapter_tests
        yield test_case if block_given?
      end
    end

    def add_working_test_case
      add_test_case do |test_case|
        test_case.add_working_test
        yield test_case if block_given?
      end
    end

    def add_working_test_case_with_adapter_tests
      add_working_test_case do |test_case|
        test_case.include_adapter_tests
        yield test_case if block_given?
      end
    end

    private

    def all_requires
      project.requires_with_rr(requires)
    end

    def content
      prelude_lines = []

      if @prelude
        prelude_lines << @prelude
      end

      require_lines = project.require_lines(all_requires)
      prelude_lines.concat(require_lines)

      join_lines(prelude_lines) + @body
    end

    def join_lines(lines)
      lines.map { |line| line + "\n" }.join
    end
  end
end