mirror of
https://github.com/wahyd4/redhat.git
synced 2026-08-09 04:25:53 +10:00
150 lines
3.1 KiB
Go
150 lines
3.1 KiB
Go
package redhat
|
|
|
|
import (
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileAnalyser_parseContent(t *testing.T) {
|
|
file, _ := os.Open("test_files/simple_input.txt", )
|
|
closedFile, _ := os.Open("test_files/valid_input.txt", )
|
|
closedFile.Close()
|
|
type args struct {
|
|
file *os.File
|
|
handler HandleLine
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fileAnalyser *FileAnalyser
|
|
args args
|
|
want map[string]int
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "can parse file and separate words by whitespace",
|
|
fileAnalyser: &FileAnalyser{
|
|
file: file,
|
|
dataRows: make([]dataRow, 0),
|
|
},
|
|
args: args{
|
|
file: file,
|
|
handler: processLineWithWhiteSpace,
|
|
},
|
|
want: map[string]int{
|
|
"Help": 1,
|
|
"Some": 1,
|
|
"future": 1,
|
|
"libraries": 1,
|
|
"shape": 1,
|
|
"of": 2,
|
|
"the": 2,
|
|
"Go": 3,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "fail to parse the file due to some error occurred when read the file",
|
|
fileAnalyser: &FileAnalyser{
|
|
file: closedFile,
|
|
dataRows: make([]dataRow, 0),
|
|
},
|
|
args: args{
|
|
file: closedFile,
|
|
handler: processLineWithWhiteSpace,
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := tt.fileAnalyser.parseContent(tt.args.file, tt.args.handler)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("parseContent() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("parseContent() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFileAnalyser_AnalyseData(t *testing.T) {
|
|
file, _ := os.Open("test_files/simple_input.txt", )
|
|
closedFile, _ := os.Open("test_files/valid_input.txt", )
|
|
closedFile.Close()
|
|
tests := []struct {
|
|
name string
|
|
fileAnalyser *FileAnalyser
|
|
wantErr bool
|
|
wantDataRows []dataRow
|
|
}{
|
|
{
|
|
name: "can parse and analyse text, with correct order and count",
|
|
fileAnalyser: &FileAnalyser{
|
|
file: file,
|
|
dataRows: make([]dataRow, 0),
|
|
},
|
|
wantErr: false,
|
|
wantDataRows: []dataRow{
|
|
{
|
|
word: "Go",
|
|
count: 3,
|
|
},
|
|
{
|
|
word: "the",
|
|
count: 2,
|
|
},
|
|
{
|
|
word: "of",
|
|
count: 2,
|
|
},
|
|
|
|
{
|
|
word: "shape",
|
|
count: 1,
|
|
},
|
|
{
|
|
word: "libraries",
|
|
count: 1,
|
|
},
|
|
{
|
|
word: "future",
|
|
count: 1,
|
|
},
|
|
{
|
|
word: "Some",
|
|
count: 1,
|
|
},
|
|
{
|
|
word: "Help",
|
|
count: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "can not parse and analyse the file, if the file has been closed before process",
|
|
fileAnalyser: &FileAnalyser{
|
|
file: closedFile,
|
|
dataRows: make([]dataRow, 0),
|
|
},
|
|
wantErr: true,
|
|
wantDataRows: []dataRow{},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := tt.fileAnalyser.AnalyseData(); (err != nil) != tt.wantErr {
|
|
t.Errorf("AnalyseData() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
|
|
if got := tt.fileAnalyser.dataRows; !reflect.DeepEqual(got, tt.wantDataRows) {
|
|
t.Errorf("Datarows = %v, want %v", got, tt.wantDataRows)
|
|
}
|
|
})
|
|
}
|
|
}
|