processor/structs_test.go GO 146 lines View on github.com → Search inside
1// SPDX-License-Identifier: MIT23package processor45import (6	"encoding/json"7	"slices"8	"strings"9	"testing"10)1112func TestCheckDuplicates(t *testing.T) {13	c := CheckDuplicates{14		hashes: make(map[int64][][]byte),15	}1617	c.Add(1, []byte("hash"))18	c.Add(1, []byte("hash2"))1920	if !c.Check(1, []byte("hash")) {21		t.Error("Expected match")22	}2324	if !c.Check(1, []byte("hash2")) {25		t.Error("Expected match")26	}2728	if c.Check(2, []byte("hash")) {29		t.Error("Expected no match")30	}3132	if c.Check(1, []byte("hash3")) {33		t.Error("Expected no match")34	}35}3637func TestMatch(t *testing.T) {38	trie := &Trie{}39	trie.InsertClose(TString, []byte("'"), []byte("'"))40	trie.InsertClose(TString, []byte("'''"), []byte("'''"))4142	testCases := []struct {43		token        []byte44		expectType   int45		expectDepth  int46		expectClosed []byte47	}{48		{49			token:        []byte("'"),50			expectType:   TString,51			expectDepth:  0,52			expectClosed: []byte("'"),53		},54		{55			token:        []byte("-"),56			expectType:   0,57			expectDepth:  0,58			expectClosed: []byte{},59		},60		{61			token:        []byte("'-"),62			expectType:   TString,63			expectDepth:  1,64			expectClosed: []byte("'"),65		},66		{67			token:        []byte("''"),68			expectType:   TString,69			expectDepth:  0,70			expectClosed: []byte("'"),71		},72		{73			token:        []byte("'''"),74			expectType:   1,75			expectDepth:  2,76			expectClosed: []byte("'''"),77		},78		{79			token:        []byte("'''a'''"),80			expectType:   TString,81			expectDepth:  3,82			expectClosed: []byte("'''"),83		},84	}8586	for _, tc := range testCases {87		typ, depth, closed := trie.Match(tc.token)88		if typ != tc.expectType {89			t.Errorf("\"%v\" matched wrong type, want: %v, got: %v", string(tc.token), tc.expectType, typ)90		}91		if depth != tc.expectDepth {92			t.Errorf("\"%v\" matched wrong depth, want: %v, got: %v", string(tc.token), tc.expectDepth, depth)93		}94		if !slices.Equal(tc.expectClosed, closed) {95			t.Errorf("\"%v\" matched wrong closed, want: %v, got: %v", string(tc.token), tc.expectClosed, closed)96		}97	}98}99100func TestFileJobJSONIgnoreFields(t *testing.T) {101	job := FileJob{102		Language:           "Dockerfile",103		PossibleLanguages:  []string{"Dockerfile"},104		Filename:           "Dockerfile",105		Extension:          "Dockerfile",106		Location:           "Dockerfile",107		Symlocation:        "",108		Content:            []uint8{1, 2, 3},109		Bytes:              248,110		Lines:              14,111		Code:               11,112		Comment:            0,113		Blank:              3,114		Complexity:         1,115		ComplexityLine:     []int64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},116		WeightedComplexity: 1,117		Hash:               nil,118		Callback:           &linecounter{},119		Binary:             false,120		Minified:           false,121		Generated:          false,122		EndPoint:           0,123		Uloc:               0,124		LineLength:         []int{1, 2, 3},125	}126127	data, err := json.MarshalIndent(&job, "", "\t")128	if err != nil {129		t.Fatal(err)130	}131132	jsonStr := string(data)133	if strings.Contains(jsonStr, `"Content": `) {134		t.Errorf("Content should be ignored")135	}136	if strings.Contains(jsonStr, `"ComplexityLine": `) {137		t.Errorf("ComplexityLine should be ignored")138	}139	if strings.Contains(jsonStr, `"Callback": `) {140		t.Errorf("Callback should be ignored")141	}142	if strings.Contains(jsonStr, `"LineLength": `) {143		t.Errorf("LineLength should be ignored")144	}145}

Code quality findings 3

String to byte slice conversion inside loop allocates a new slice each iteration; convert once before the loop
info correctness string-to-byte-in-loop
expectClosed: []byte("'''"),
String to byte slice conversion inside loop allocates a new slice each iteration; convert once before the loop
info correctness string-to-byte-in-loop
token: []byte("'''a'''"),
String to byte slice conversion inside loop allocates a new slice each iteration; convert once before the loop
info correctness string-to-byte-in-loop
expectClosed: []byte("'''"),

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.