diff --git a/internal/quantitative/leipzig/corpus_test.go b/internal/quantitative/leipzig/corpus_test.go index a80f1f6..ccbbca1 100644 --- a/internal/quantitative/leipzig/corpus_test.go +++ b/internal/quantitative/leipzig/corpus_test.go @@ -36,6 +36,26 @@ func (s *leipzigCorpusTestSuite) TestWithSize() { s.Require().Equal("300K", s.corpus.Size()) } +func (s *leipzigCorpusTestSuite) TestWithYear() { + s.corpus.WithYear("2024") + s.Require().Equal("2024", s.corpus.Year()) +} + +func (s *leipzigCorpusTestSuite) TestWithSource() { + s.corpus.WithSource("news") + s.Require().Equal("news", s.corpus.Source()) +} + +func (s *leipzigCorpusTestSuite) TestWithLanguage() { + s.corpus.WithLanguage("eng") + s.Require().Equal("eng", s.corpus.Language()) +} + +func (s *leipzigCorpusTestSuite) TestWithURL() { + s.corpus.WithURL("https://downloads.wortschatz-leipzig.de/corpora") + s.Require().Equal("https://downloads.wortschatz-leipzig.de/corpora", s.corpus.URL()) +} + func (s *leipzigCorpusTestSuite) TestGetIterator() { s.corpus.WithSize("10K") s.cache = s.corpus.FetchCorpusFile() diff --git a/internal/quantitative/leipzig/file_test.go b/internal/quantitative/leipzig/file_test.go new file mode 100644 index 0000000..e58f98a --- /dev/null +++ b/internal/quantitative/leipzig/file_test.go @@ -0,0 +1,182 @@ +package leipzig + +import ( + "github.com/coreruleset/go-ftw/experimental/corpus" + "reflect" + "testing" +) + +func TestFile_CacheDir(t *testing.T) { + type fields struct { + cacheDir string + filePath string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1", + fields: fields{ + cacheDir: "cacheDir", + filePath: "filePath", + }, + want: "cacheDir", + }, + { + name: "Test 2", + fields: fields{ + cacheDir: "cacheDir2", + filePath: "filePath2", + }, + want: "cacheDir2", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := File{ + cacheDir: tt.fields.cacheDir, + filePath: tt.fields.filePath, + } + if got := f.CacheDir(); got != tt.want { + t.Errorf("CacheDir() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestFile_FilePath(t *testing.T) { + type fields struct { + cacheDir string + filePath string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "Test 1", + fields: fields{ + cacheDir: "cacheDir", + filePath: "filePath", + }, + want: "filePath", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := File{ + cacheDir: tt.fields.cacheDir, + filePath: tt.fields.filePath, + } + if got := f.FilePath(); got != tt.want { + t.Errorf("FilePath() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestFile_WithCacheDir(t *testing.T) { + type fields struct { + cacheDir string + filePath string + } + type args struct { + cacheDir string + } + tests := []struct { + name string + fields fields + args args + want corpus.File + }{ + { + name: "Test 1", + fields: fields{ + cacheDir: "cacheDir1", + filePath: "filePath", + }, + args: args{ + cacheDir: "cacheDir10", + }, + want: File{ + cacheDir: "cacheDir10", + filePath: "filePath", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := File{ + cacheDir: tt.fields.cacheDir, + filePath: tt.fields.filePath, + } + if got := f.WithCacheDir(tt.args.cacheDir); !reflect.DeepEqual(got, tt.want) { + t.Errorf("WithCacheDir() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestFile_WithFilePath(t *testing.T) { + type fields struct { + cacheDir string + filePath string + } + type args struct { + filePath string + } + tests := []struct { + name string + fields fields + args args + want corpus.File + }{ + { + name: "Test 1", + fields: fields{ + cacheDir: "cacheDir", + filePath: "filePath1", + }, + args: args{ + filePath: "filePath2", + }, + want: File{ + cacheDir: "cacheDir", + filePath: "filePath2", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := File{ + cacheDir: tt.fields.cacheDir, + filePath: tt.fields.filePath, + } + if got := f.WithFilePath(tt.args.filePath); !reflect.DeepEqual(got, tt.want) { + t.Errorf("WithFilePath() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewFile(t *testing.T) { + tests := []struct { + name string + want corpus.File + }{ + { + name: "Test 1", + want: File{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewFile(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewFile() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/quantitative/stats_test.go b/internal/quantitative/stats_test.go new file mode 100644 index 0000000..394b864 --- /dev/null +++ b/internal/quantitative/stats_test.go @@ -0,0 +1,309 @@ +package quantitative + +import ( + "github.com/coreruleset/go-ftw/output" + "reflect" + "testing" + "time" +) + +func TestNewQuantitativeStats(t *testing.T) { + tests := []struct { + name string + want *QuantitativeRunStats + }{ + { + name: "Test 1", + want: &QuantitativeRunStats{ + count_: 0, + falsePositives: 0, + falsePositivesPerRule: make(map[int]int), + totalTime: 0, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewQuantitativeStats(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewQuantitativeStats() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestQuantitativeRunStats_Count(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + tests := []struct { + name string + fields fields + want int + }{ + { + name: "Test 1", + fields: fields{ + count_: 1, + }, + want: 1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + if got := s.Count(); got != tt.want { + t.Errorf("Count() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestQuantitativeRunStats_MarshalJSON(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + tests := []struct { + name string + fields fields + want []byte + wantErr bool + }{ + { + name: "Test 1", + fields: fields{ + count_: 1, + totalTime: 1, + falsePositives: 1, + falsePositivesPerRule: map[int]int{920010: 1}, + }, + want: []byte(`{"count":1,"totalTime":1,"falsePositives":1,"falsePositivesPerRule":{"920010":1}}`), + wantErr: false, + }, + { + name: "Test 2", + fields: fields{ + count_: 2, + totalTime: 2, + falsePositives: 2, + falsePositivesPerRule: map[int]int{933100: 2}, + }, + want: []byte(`{"count":2,"totalTime":2,"falsePositives":2,"falsePositivesPerRule":{"933100":2}}`), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + got, err := s.MarshalJSON() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestQuantitativeRunStats_SetTotalTime(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + type args struct { + totalTime time.Duration + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "Test 1", + fields: fields{ + count_: 1, + }, + args: args{ + totalTime: 1, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + s.SetTotalTime(tt.args.totalTime) + }) + } +} + +func TestQuantitativeRunStats_TotalTime(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + tests := []struct { + name string + fields fields + want time.Duration + }{ + { + name: "Test 1", + fields: fields{ + totalTime: 150, + }, + want: 150, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + if got := s.TotalTime(); got != tt.want { + t.Errorf("TotalTime() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestQuantitativeRunStats_addFalsePositive(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + type args struct { + rule int + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "Test 1", + fields: fields{ + count_: 1, + }, + args: args{ + rule: 1, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + s.addFalsePositive(tt.args.rule) + }) + } +} + +func TestQuantitativeRunStats_incrementRun(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + tests := []struct { + name string + fields fields + }{ + { + name: "Test 1", + fields: fields{ + count_: 100, + totalTime: 100, + falsePositives: 100, + falsePositivesPerRule: map[int]int{100: 100}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + s.incrementRun() + }) + } +} + +func TestQuantitativeRunStats_printSummary(t *testing.T) { + type fields struct { + count_ int + totalTime time.Duration + falsePositives int + falsePositivesPerRule map[int]int + } + type args struct { + out *output.Output + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "Test 1", + fields: fields{ + count_: 1, + totalTime: 1, + falsePositives: 1, + falsePositivesPerRule: map[int]int{1: 1}, + }, + args: args{ + out: &output.Output{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &QuantitativeRunStats{ + count_: tt.fields.count_, + totalTime: tt.fields.totalTime, + falsePositives: tt.fields.falsePositives, + falsePositivesPerRule: tt.fields.falsePositivesPerRule, + } + s.printSummary(tt.args.out) + }) + } +}