# frozen_string_literal: true 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 "skip ERB comments" do code = <<-'CODE' <%# This is a great comment! %>