aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/code_statistics_calculator_test.rb
blob: 8a2f0294d04bef03ca70ce21d98b1d79d64aad39 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
require "abstract_unit"
require "rails/code_statistics_calculator"

class CodeStatisticsCalculatorTest < ActiveSupport::TestCase
  def setup
    @code_statistics_calculator = CodeStatisticsCalculator.new
  end

  test "calculate statistics using #add_by_file_path" do
    code = <<-RUBY
      def foo
        puts 'foo'
        # bar
      end
    RUBY

    temp_file "stats.rb", code do |path|
      @code_statistics_calculator.add_by_file_path path

      assert_equal 4, @code_statistics_calculator.lines
      assert_equal 3, @code_statistics_calculator.code_lines
      assert_equal 0, @code_statistics_calculator.classes
      assert_equal 1, @code_statistics_calculator.methods
    end
  end

  test "count number of methods in MiniTest file" do
    code = <<-RUBY
      class FooTest < ActionController::TestCase
        test 'expectation' do
          assert true
        end

        def test_expectation
          assert true
        end
      end
    RUBY

    temp_file "foo_test.rb", code do |path|
      @code_statistics_calculator.add_by_file_path path
      assert_equal 2, @code_statistics_calculator.methods
    end
  end

  test "add statistics to another using #add" do
    code_statistics_calculator_1 = CodeStatisticsCalculator.new(1, 2, 3, 4)
    @code_statistics_calculator.add(code_statistics_calculator_1)

    assert_equal 1, @code_statistics_calculator.lines
    assert_equal 2, @code_statistics_calculator.code_lines
    assert_equal 3, @code_statistics_calculator.classes
    assert_equal 4, @code_statistics_calculator.methods

    code_statistics_calculator_2 = CodeStatisticsCalculator.new(2, 3, 4, 5)
    @code_statistics_calculator.add(code_statistics_calculator_2)

    assert_equal 3, @code_statistics_calculator.lines
    assert_equal 5, @code_statistics_calculator.code_lines
    assert_equal 7, @code_statistics_calculator.classes
    assert_equal 9, @code_statistics_calculator.methods
  end

  test "accumulate statistics using #add_by_io" do
    code_statistics_calculator_1 = CodeStatisticsCalculator.new(1, 2, 3, 4)
    @code_statistics_calculator.add(code_statistics_calculator_1)

    code = <<-'CODE'
      def foo
        puts 'foo'
      end

      def bar; end
      class A; end
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :rb)

    assert_equal 7, @code_statistics_calculator.lines
    assert_equal 7, @code_statistics_calculator.code_lines
    assert_equal 4, @code_statistics_calculator.classes
    assert_equal 6, @code_statistics_calculator.methods
  end

  test "calculate number of Ruby methods" do
    code = <<-'CODE'
      def foo
        puts 'foo'
      end

      def bar; end

      class Foo
        def bar(abc)
        end
      end
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :rb)

    assert_equal 3, @code_statistics_calculator.methods
  end

  test "calculate Ruby LOCs" do
    code = <<-'CODE'
      def foo
        puts 'foo'
      end

      # def bar; end

      class A < B
      end
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :rb)

    assert_equal 8, @code_statistics_calculator.lines
    assert_equal 5, @code_statistics_calculator.code_lines
  end

  test "calculate number of Ruby classes" do
    code = <<-'CODE'
      class Foo < Bar
        def foo
          puts 'foo'
        end
      end

      class Z; end

      # class A
      # end
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :rb)

    assert_equal 2, @code_statistics_calculator.classes
  end

  test "skip Ruby comments" do
    code = <<-'CODE'
=begin
      class Foo
        def foo
          puts 'foo'
        end
      end
=end

      # class A
      # end
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :rb)

    assert_equal 10, @code_statistics_calculator.lines
    assert_equal 0, @code_statistics_calculator.code_lines
    assert_equal 0, @code_statistics_calculator.classes
    assert_equal 0, @code_statistics_calculator.methods
  end

  test "calculate number of JS methods" do
    code = <<-'CODE'
      function foo(x, y, z) {
        doX();
      }

      $(function () {
        bar();
      })

      var baz = function ( x ) {
      }
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :js)

    assert_equal 3, @code_statistics_calculator.methods
  end

  test "calculate JS LOCs" do
    code = <<-'CODE'
      function foo()
        alert('foo');
      end

      // var b = 2;

      var a = 1;
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :js)

    assert_equal 7, @code_statistics_calculator.lines
    assert_equal 4, @code_statistics_calculator.code_lines
  end

  test "skip JS comments" do
    code = <<-'CODE'
      /*
       * var f = function () {
       1 / 2;
      }
      */

      // call();
      //
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :js)

    assert_equal 8, @code_statistics_calculator.lines
    assert_equal 0, @code_statistics_calculator.code_lines
    assert_equal 0, @code_statistics_calculator.classes
    assert_equal 0, @code_statistics_calculator.methods
  end

  test "calculate number of CoffeeScript methods" do
    code = <<-'CODE'
      square = (x) -> x * x

      math =
        cube: (x) -> x * square x

      fill = (container, liquid = "coffee") ->
        "Filling the #{container} with #{liquid}..."

      $('.shopping_cart').bind 'click', (event) =>
        @customer.purchase @cart
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :coffee)

    assert_equal 4, @code_statistics_calculator.methods
  end

  test "calculate CoffeeScript LOCs" do
    code = <<-'CODE'
      # Assignment:
      number   = 42
      opposite = true

      ###
      CoffeeScript Compiler v1.4.0
      Released under the MIT License
      ###

      # Conditions:
      number = -42 if opposite
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :coffee)

    assert_equal 11, @code_statistics_calculator.lines
    assert_equal 3, @code_statistics_calculator.code_lines
  end

  test "calculate number of CoffeeScript classes" do
    code = <<-'CODE'
      class Animal
        constructor: (@name) ->

        move: (meters) ->
          alert @name + " moved #{meters}m."

      class Snake extends Animal
        move: ->
          alert "Slithering..."
          super 5

      # class Horse
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :coffee)

    assert_equal 2, @code_statistics_calculator.classes
  end

  test "skip CoffeeScript comments" do
    code = <<-'CODE'
###
class Animal
  constructor: (@name) ->

  move: (meters) ->
    alert @name + " moved #{meters}m."
  ###

  # class Horse
  alert 'hello'
    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :coffee)

    assert_equal 10, @code_statistics_calculator.lines
    assert_equal 1, @code_statistics_calculator.code_lines
    assert_equal 0, @code_statistics_calculator.classes
    assert_equal 0, @code_statistics_calculator.methods
  end

  test "count rake tasks" do
    code = <<-'CODE'
      task :test_task do
        puts 'foo'
      end

    CODE

    @code_statistics_calculator.add_by_io(StringIO.new(code), :rake)

    assert_equal 4, @code_statistics_calculator.lines
    assert_equal 3, @code_statistics_calculator.code_lines
    assert_equal 0, @code_statistics_calculator.classes
    assert_equal 0, @code_statistics_calculator.methods
  end

  private
    def temp_file(name, content)
      dir = File.expand_path "../fixtures/tmp", __FILE__
      path = "#{dir}/#{name}"

      FileUtils.mkdir_p dir
      File.write path, content

      yield path
    ensure
      FileUtils.rm_rf path
    end
end