aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/code_statistics_calculator_test.rb
blob: b3eabf50240157195de6c8908280135702d15363 (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
require 'abstract_unit'
require 'rails/code_statistics_calculator'

class CodeStatisticsCalculatorTest < ActiveSupport::TestCase
  def setup
    @code_statistics_calculator = CodeStatisticsCalculator.new
  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 statistics using #add_by_file_path' do
    tmp_path = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'tmp'))
    FileUtils.mkdir_p(tmp_path)

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

    file_path = "#{tmp_path}/stats.rb"
    File.open(file_path, 'w') { |f| f.write(code) }

    @code_statistics_calculator.add_by_file_path(file_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

    FileUtils.rm_rf(tmp_path)
  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
end