1// SPDX-License-Identifier: MIT23package processor45import (6 "testing"7)89func TestCountStatsJava(t *testing.T) {10 ProcessConstants()11 fileJob := FileJob{12 Language: "Java",13 }1415 fileJob.SetContent(`/* 23 lines 16 code 4 comments 3 blanks */1617/*18* Simple test class19*/20public class Test21{22 int j = 0; // Not counted23 public static void main(String[] args)24 {25 Foo f = new Foo();26 f.bar();2728 }29}3031class Foo32{33 public void bar()34 {35 System.out.println("FooBar"); //Not counted36 }37}`)3839 CountStats(&fileJob)4041 if fileJob.Lines != 23 {42 t.Errorf("Expected 23 lines")43 }4445 if fileJob.Code != 16 {46 t.Errorf("Expected 16 lines got %d", fileJob.Code)47 }4849 if fileJob.Comment != 4 {50 t.Errorf("Expected 4 lines got %d", fileJob.Comment)51 }5253 if fileJob.Blank != 3 {54 t.Errorf("Expected 3 lines got %d", fileJob.Blank)55 }56}5758func TestCountStatsAccuracyCPlusPlus(t *testing.T) {59 ProcessConstants()60 fileJob := FileJob{61 Language: "C++",62 }6364 fileJob.SetContent(`/* 15 lines 7 code 4 comments 4 blanks */6566#include <iostream>676869using namespace std;7071/*72 * Simple test73 */74int main()75{76 cout<<"Hello world"<<endl;77 return 0;78}79`)8081 CountStats(&fileJob)8283 if fileJob.Lines != 15 {84 t.Errorf("Expected 15 lines got %d", fileJob.Lines)85 }8687 if fileJob.Code != 7 {88 t.Errorf("Expected 7 lines got %d", fileJob.Code)89 }9091 if fileJob.Comment != 4 {92 t.Errorf("Expected 4 lines got %d", fileJob.Comment)93 }9495 if fileJob.Blank != 4 {96 t.Errorf("Expected 4 lines got %d", fileJob.Blank)97 }98}99100func TestCountStatsAccuracyRakefile(t *testing.T) {101 ProcessConstants()102 fileJob := FileJob{103 Language: "Rakefile",104 }105106 fileJob.SetContent(`# 10 lines 4 code 2 comments 4 blanks107108# this is a rakefile109110task default: %w[test]111112task :test do # not counted113 ruby "test/unittest.rb"114end115116`)117118 CountStats(&fileJob)119120 if fileJob.Lines != 10 {121 t.Errorf("Expected 10 lines got %d", fileJob.Lines)122 }123124 if fileJob.Code != 4 {125 t.Errorf("Expected 4 lines got %d", fileJob.Code)126 }127128 if fileJob.Comment != 2 {129 t.Errorf("Expected 2 lines got %d", fileJob.Comment)130 }131132 if fileJob.Blank != 4 {133 t.Errorf("Expected 4 lines got %d", fileJob.Blank)134 }135}136137func TestCountStatsAccuracyRuby(t *testing.T) {138 ProcessConstants()139 fileJob := FileJob{140 Language: "Ruby",141 }142143 fileJob.SetContent(`# 20 lines 9 code 8 comments 3 blanks144x = 3145if x < 2146 p = "Smaller"147else148 p = "Bigger"149end150151=begin152 Comments153 Comments154 Comments155 Comments156=end157158# testing.159while x > 2 and x < 10160 x += 1161end162163`)164165 CountStats(&fileJob)166167 if fileJob.Lines != 20 {168 t.Errorf("Expected 20 lines got %d", fileJob.Lines)169 }170171 if fileJob.Code != 9 {172 t.Errorf("Expected 9 lines got %d", fileJob.Code)173 }174175 if fileJob.Comment != 8 {176 t.Errorf("Expected 8 lines got %d", fileJob.Comment)177 }178179 if fileJob.Blank != 3 {180 t.Errorf("Expected 3 lines got %d", fileJob.Blank)181 }182}183184func TestCountStatsAccuracyTokeiTest(t *testing.T) {185 ProcessConstants()186 fileJob := FileJob{187 Language: "Rust",188 }189190 fileJob.SetContent(`// 39 lines 32 code 2 comments 5 blanks191192/* /**/ */193fn main() {194 let start = "/*";195 loop {196 if x.len() >= 2 && x[0] == '*' && x[1] == '/' { // found the */197 break;198 }199 }200}201202fn foo() {203 let this_ends = "a \"test/*.";204 call1();205 call2();206 let this_does_not = /* a /* nested */ comment " */207 "*/another /*test208 call3();209 */";210}211212fn foobar() {213 let does_not_start = // "214 "until here,215 test/*216 test"; // a quote: "217 let also_doesnt_start = /* " */218 "until here,219 test,*/220 test"; // another quote: "221}222223fn foo() {224 let a = 4; // /*225 let b = 5;226 let c = 6; // */227}228229`)230231 CountStats(&fileJob)232233 // 39 lines 32 code 2 comments 5 blanks234 if fileJob.Lines != 39 {235 t.Errorf("Expected 39 lines got %d", fileJob.Lines)236 }237238 if fileJob.Code != 32 {239 t.Errorf("Expected 32 lines got %d", fileJob.Code)240 }241242 if fileJob.Comment != 2 {243 t.Errorf("Expected 2 lines got %d", fileJob.Comment)244 }245246 if fileJob.Blank != 5 {247 t.Errorf("Expected 5 lines got %d", fileJob.Blank)248 }249}
Findings
✓ No findings reported for this file.