77 "path/filepath"
88 "regexp"
99 "slices"
10+ "strconv"
1011 "strings"
1112 "testing"
1213)
@@ -315,6 +316,50 @@ func TestLineLength(t *testing.T) {
315316 }
316317}
317318
319+ func TestFormatHTML (t * testing.T ) {
320+ t .Parallel ()
321+ output , err := runSCC ("--format" , "html" )
322+ if err != nil {
323+ t .Fatal (err )
324+ }
325+ if ! strings .Contains (output , "<title>scc html output</title>" ) {
326+ t .Fatalf ("html format test failed, output:\n %s" , output )
327+ }
328+ }
329+
330+ func TestFormatHTMLTable (t * testing.T ) {
331+ t .Parallel ()
332+ output , err := runSCC ("--format" , "html-table" )
333+ if err != nil {
334+ t .Fatal (err )
335+ }
336+ if ! strings .Contains (output , `<table id="scc-table">` ) {
337+ t .Fatalf ("html-table format test failed, output:\n %s" , output )
338+ }
339+ }
340+
341+ func TestFormatSQL (t * testing.T ) {
342+ t .Parallel ()
343+ output , err := runSCC ("--format" , "sql" )
344+ if err != nil {
345+ t .Fatal (err )
346+ }
347+ if ! strings .Contains (output , "create table metadata ( -- github.com/boyter/scc" ) {
348+ t .Fatalf ("sql format test failed, output:\n %s" , output )
349+ }
350+ }
351+
352+ func TestFormatSQLInsert (t * testing.T ) {
353+ t .Parallel ()
354+ output , err := runSCC ("--format" , "sql-insert" )
355+ if err != nil {
356+ t .Fatal (err )
357+ }
358+ if ! strings .Contains (output , "begin transaction;\n insert into t values(" ) {
359+ t .Fatalf ("sql-insert format test failed, output:\n %s" , output )
360+ }
361+ }
362+
318363func TestMultipleFormatStdout (t * testing.T ) {
319364 output , err := runSCC ("--format-multi" , "tabular:stdout,html:stdout,csv:stdout,sql:stdout" )
320365 if err != nil {
@@ -519,7 +564,7 @@ func TestDeterministicOutput(t *testing.T) {
519564 if err != nil {
520565 t .Fatal (err )
521566 }
522- for range 20 {
567+ for range 10 {
523568 output2 , err := runSCC ("." )
524569 if err != nil {
525570 t .Fatal (err )
@@ -530,6 +575,152 @@ func TestDeterministicOutput(t *testing.T) {
530575 }
531576}
532577
578+ func TestDuplicates (t * testing.T ) {
579+ for range 10 {
580+ output , err := runSCC ("-f" , "json" , "-d" , "./examples/duplicates/" )
581+ if err != nil {
582+ t .Fatal (err )
583+ }
584+ if ! strings .Contains (output , `"Count":1` ) {
585+ t .Fatalf ("duplicates check failed, output:\n %s" , output )
586+ }
587+ }
588+ }
589+
590+ func TestCountAs (t * testing.T ) {
591+ testCases := []struct {
592+ countAs string
593+ expected []string
594+ }{
595+ {
596+ countAs : "jsp:html" ,
597+ expected : []string {"HTML" },
598+ },
599+ {
600+ countAs : "JsP:html" ,
601+ expected : []string {"HTML" },
602+ },
603+ {
604+ countAs : "jsp:j2" ,
605+ expected : []string {"Jinja" },
606+ },
607+ {
608+ countAs : "jsp:html,new:java" ,
609+ expected : []string {"HTML" , "Java" },
610+ },
611+ {
612+ countAs : "jsp:html,new:C Header" ,
613+ expected : []string {"HTML" , "C Header" },
614+ },
615+ }
616+
617+ for _ , tc := range testCases {
618+ output , err := runSCC ("-f" , "csv" , "--count-as" , tc .countAs , "./examples/countas/" )
619+ if err != nil {
620+ t .Fatal (err )
621+ }
622+ for _ , expectedLang := range tc .expected {
623+ if ! strings .Contains (output , expectedLang + "," ) {
624+ t .Errorf ("count as failed, count as: %s, output:\n %s" , tc .countAs , output )
625+ }
626+ }
627+ }
628+ }
629+
630+ func TestRemapUnknown (t * testing.T ) {
631+ t .Parallel ()
632+ output , err := runSCC ("-f" , "csv" , "--remap-unknown" , "-*- C++ -*-:C Header" , "./examples/remap/unknown" )
633+ if err != nil {
634+ t .Fatal (err )
635+ }
636+ if ! strings .Contains (output , "C Header," ) {
637+ t .Fatalf ("remap unknown failed, output:\n %s" , output )
638+ }
639+ }
640+
641+ func TestRemapAll (t * testing.T ) {
642+ t .Parallel ()
643+ output , err := runSCC ("-f" , "csv" , "--remap-all" , "-*- C++ -*-:C Header" , "./examples/remap/java.java" )
644+ if err != nil {
645+ t .Fatal (err )
646+ }
647+ if ! strings .Contains (output , "C Header," ) {
648+ t .Fatalf ("remap all failed, output:\n %s" , output )
649+ }
650+ }
651+
652+ func TestCocomoProjectType (t * testing.T ) {
653+ projectTypes := []string {"organic" , "semi-detached" , "embedded" , "custom,1,1,1,1" }
654+ for _ , typ := range projectTypes {
655+ output , err := runSCC ("--cocomo-project-type" , typ )
656+ if err != nil {
657+ t .Fatal (err )
658+ }
659+ if ! strings .Contains (output , fmt .Sprintf ("Estimated Cost to Develop (%s)" , typ )) ||
660+ ! strings .Contains (output , fmt .Sprintf ("Estimated Schedule Effort (%s)" , typ )) ||
661+ ! strings .Contains (output , fmt .Sprintf ("Estimated People Required (%s)" , typ )) {
662+ t .Errorf ("check cocomo project type failed: %s" , typ )
663+ }
664+ }
665+ }
666+
667+ func TestCocomoProjectTypeFallback (t * testing.T ) {
668+ unknownTypes := []string {"doesnotexist" , "custom,1,1,1" }
669+ for _ , typ := range unknownTypes {
670+ output , err := runSCC ("--cocomo-project-type" , typ )
671+ if err != nil {
672+ t .Fatal (err )
673+ }
674+ if ! strings .Contains (output , "Estimated Cost to Develop (organic)" ) ||
675+ ! strings .Contains (output , "Estimated Schedule Effort (organic)" ) ||
676+ ! strings .Contains (output , "Estimated People Required (organic)" ) {
677+ t .Errorf ("check cocomo project type fallback failed: %s" , typ )
678+ }
679+ }
680+ }
681+
682+ func TestOutputBytes (t * testing.T ) {
683+ jsonOutput , err := runSCC ("-f" , "json" )
684+ if err != nil {
685+ t .Fatal (err )
686+ }
687+ if ! strings .Contains (jsonOutput , `"Bytes":` ) {
688+ t .Errorf ("json output does not contain `Bytes` field, output:\n %s" , jsonOutput )
689+ }
690+
691+ output , err := runSCC ()
692+ if err != nil {
693+ t .Fatal (err )
694+ }
695+ if ! strings .Contains (output , "megabytes" ) {
696+ t .Errorf ("output does not contain `megabytes`, output:\n %s" , output )
697+ }
698+ }
699+
700+ func TestFileGCCount (t * testing.T ) {
701+ const target = "./examples/duplicates"
702+ files , err := os .ReadDir (target )
703+ if err != nil {
704+ t .Fatal (err )
705+ }
706+
707+ output , err := runSCC ("--file-gc-count" , strconv .Itoa (len (files )- 1 ), "-v" , target )
708+ if err != nil {
709+ t .Fatal (err )
710+ }
711+ if ! strings .Contains (output , "read file limit exceeded GC re-enabled" ) {
712+ t .Errorf ("test file GC count failed, file count: %d, limit: %d" , len (files ), len (files )- 1 )
713+ }
714+
715+ output , err = runSCC ("--file-gc-count" , strconv .Itoa (len (files )+ 1 ), "-v" , target )
716+ if err != nil {
717+ t .Fatal (err )
718+ }
719+ if strings .Contains (output , "read file limit exceeded GC re-enabled" ) {
720+ t .Errorf ("test file GC count failed, file count: %d, limit: %d" , len (files ), len (files )+ 1 )
721+ }
722+ }
723+
533724func TestLanguageNameTruncate (t * testing.T ) {
534725 output , err := runSCC ("examples/language" )
535726 if err != nil {
0 commit comments