From 51bea8d698d30b23795219641976a8f4ee465542 Mon Sep 17 00:00:00 2001 From: Jean-Marc MEESSEN Date: Thu, 10 Sep 2020 22:09:59 +0200 Subject: [PATCH] Add day increment feature --- doc/whats_new.md | 7 +- fleprocess/adif_write.go | 2 +- fleprocess/adif_write_test.go | 1 - fleprocess/load_file.go | 78 +++++++-------- fleprocess/load_file_test.go | 183 +++++++++++++++++++++++++++++++++- fleprocess/parse_line.go | 50 +++++++++- fleprocess/parse_line_test.go | 32 +++++- fleprocess/validate.go | 24 ++++- fleprocess/validate_test.go | 50 ++++++++++ 9 files changed, 378 insertions(+), 49 deletions(-) diff --git a/doc/whats_new.md b/doc/whats_new.md index 5b8b34a..388cca9 100644 --- a/doc/whats_new.md +++ b/doc/whats_new.md @@ -2,7 +2,12 @@ ## v0.1.2 -* Correctly process of optional WWFF keyword(issue #38) +* DATE keyword is now optional +* Date can have several delimiter ("-", "/", ".", or " ") +* Partial dates can be entered ("20-9-6" => "2020-09-06") +* The new (FLE v3) "DAY" keyword is now supported (increment is 10 max) +* Date, band, and mode can be specified on a same line, even with a QSO +* Correctly process of optional WWFF keyword * Correct some typos ## Previous releases diff --git a/fleprocess/adif_write.go b/fleprocess/adif_write.go index 04fcdc6..1222112 100644 --- a/fleprocess/adif_write.go +++ b/fleprocess/adif_write.go @@ -76,7 +76,7 @@ func buildAdif(fullLog []LogLine, isWWFF bool, isSOTA bool) (adifList []string) } } if logLine.Operator != "" { - adifLine.WriteString(adifElement("OPERATOR", logLine.Operator)) + adifLine.WriteString(adifElement("OPERATOR", logLine.Operator)) } if logLine.MyGrid != "" { adifLine.WriteString(adifElement("MY_GRIDSQUARE", logLine.MyGrid)) diff --git a/fleprocess/adif_write_test.go b/fleprocess/adif_write_test.go index 527faf3..9bc637e 100644 --- a/fleprocess/adif_write_test.go +++ b/fleprocess/adif_write_test.go @@ -80,7 +80,6 @@ func Test_buildAdif(t *testing.T) { "ON4KJM/P ON4LY 20200524 1312 20m CW 559 599 WWFF ONFF-0259 ON4KJM JO40eu ", } - type args struct { fullLog []LogLine isWWFF bool diff --git a/fleprocess/load_file.go b/fleprocess/load_file.go index aea6254..30d633d 100644 --- a/fleprocess/load_file.go +++ b/fleprocess/load_file.go @@ -61,8 +61,6 @@ func LoadFile(inputFilename string, isInterpolateTime bool) (filleFullLog []LogL regexpHeaderMyGrid := regexp.MustCompile("(?i)^mygrid ") regexpHeaderQslMsg := regexp.MustCompile("(?i)^qslmsg ") regexpHeaderNickname := regexp.MustCompile("(?i)^nickname ") - regexpHeaderDateMarker := regexp.MustCompile("(?i)^date ") - regexpDatePattern := regexp.MustCompile("^(\\d{2}|\\d{4})[-/ .]\\d{1,2}[-/ .]\\d{1,2}$") headerMyCall := "" headerOperator := "" @@ -71,7 +69,7 @@ func LoadFile(inputFilename string, isInterpolateTime bool) (filleFullLog []LogL headerMyGrid := "" headerQslMsg := "" headerNickname := "" - headerDate := "" + //headerDate := "" lineCount := 0 wrkTimeBlock := InferTimeBlock{} @@ -218,42 +216,42 @@ func LoadFile(inputFilename string, isInterpolateTime bool) (filleFullLog []LogL continue } - // Date with keyword - if regexpHeaderDateMarker.MatchString(eachline) { - errorMsg := "" - myDateList := regexpHeaderDateMarker.Split(eachline, -1) - if len(myDateList[1]) > 0 { - normalizedDate := "" - normalizedDate, errorMsg = NormalizeDate(myDateList[1]) - if len(errorMsg) != 0 { - errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, eachline, errorMsg)) - } else { - headerDate, errorMsg = ValidateDate(normalizedDate) - if len(errorMsg) != 0 { - errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, myDateList[1], errorMsg)) - } - } - } - //If there is no data after the marker, we just skip the data. - continue - } - - //Date, apparently alone on a line? - if regexpDatePattern.MatchString(eachline) { - //We probably have a date, let's normalize it - errorMsg := "" - normalizedDate := "" - normalizedDate, errorMsg = NormalizeDate(eachline) - if len(errorMsg) != 0 { - errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, eachline, errorMsg)) - } else { - headerDate, errorMsg = ValidateDate(normalizedDate) - if len(errorMsg) != 0 { - errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, eachline, errorMsg)) - } - } - continue - } + // // Date with keyword + // if regexpHeaderDateMarker.MatchString(eachline) { + // errorMsg := "" + // myDateList := regexpHeaderDateMarker.Split(eachline, -1) + // if len(myDateList[1]) > 0 { + // normalizedDate := "" + // normalizedDate, errorMsg = NormalizeDate(myDateList[1]) + // if len(errorMsg) != 0 { + // errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, eachline, errorMsg)) + // } else { + // headerDate, errorMsg = ValidateDate(normalizedDate) + // if len(errorMsg) != 0 { + // errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, myDateList[1], errorMsg)) + // } + // } + // } + // //If there is no data after the marker, we just skip the data. + // continue + // } + + // //Date, apparently alone on a line? + // if regexpDatePattern.MatchString(eachline) { + // //We probably have a date, let's normalize it + // errorMsg := "" + // normalizedDate := "" + // normalizedDate, errorMsg = NormalizeDate(eachline) + // if len(errorMsg) != 0 { + // errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, eachline, errorMsg)) + // } else { + // headerDate, errorMsg = ValidateDate(normalizedDate) + // if len(errorMsg) != 0 { + // errorLog = append(errorLog, fmt.Sprintf("Invalid Date at line %d: %s (%s)", lineCount, eachline, errorMsg)) + // } + // } + // continue + // } // **** // ** Process the data block @@ -267,7 +265,7 @@ func LoadFile(inputFilename string, isInterpolateTime bool) (filleFullLog []LogL previousLogLine.MyGrid = headerMyGrid previousLogLine.QSLmsg = headerQslMsg //previousLogLine.QslMsg is redundant previousLogLine.Nickname = headerNickname - previousLogLine.Date = headerDate + //previousLogLine.Date = headerDate //parse a line logline, errorLine := ParseLine(eachline, previousLogLine) diff --git a/fleprocess/load_file_test.go b/fleprocess/load_file_test.go index 525d797..831bf68 100644 --- a/fleprocess/load_file_test.go +++ b/fleprocess/load_file_test.go @@ -199,6 +199,188 @@ func TestLoadFile_happyCase_date(t *testing.T) { os.Remove(temporaryDataFileName) } +func TestLoadFile_happyCase_date2(t *testing.T) { + + //Given + dataArray := make([]string, 0) + dataArray = append(dataArray, "# Header") + dataArray = append(dataArray, "myCall on4kjm/p") + dataArray = append(dataArray, "operator on4kjm") + dataArray = append(dataArray, "nickname Portable") + dataArray = append(dataArray, " ") + dataArray = append(dataArray, " #Log") + dataArray = append(dataArray, "20/5/23") + dataArray = append(dataArray, "40m cw 0950 ik5zve/5 9 5") + dataArray = append(dataArray, "on6zq") + dataArray = append(dataArray, "0954 on4do") + dataArray = append(dataArray, "20-05-25 20m ssb 1000 on4up") + + temporaryDataFileName := createTestFile(dataArray) + + //When + loadedLogFile, isLoadedOK := LoadFile(temporaryDataFileName, true) + + //Then + if !isLoadedOK { + t.Error("Test file could not be correctly processed") + } + if len(loadedLogFile) == 0 { + t.Error("No data loaded") + } + + expectedValue := "ON4KJM/P" + if loadedLogFile[0].MyCall != expectedValue { + t.Errorf("Not the expected MyCall value: %s (expecting %s)", loadedLogFile[0].MyCall, expectedValue) + } + expectedValue = "ON4KJM" + if loadedLogFile[0].Operator != expectedValue { + t.Errorf("Not the expected Operator value: %s (expecting %s)", loadedLogFile[0].Operator, expectedValue) + } + expectedValue = "Portable" + if loadedLogFile[0].Nickname != expectedValue { + t.Errorf("Not the expected eQsl Nickname value: %s (expecting %s)", loadedLogFile[0].Nickname, expectedValue) + } + expectedValue = "IK5ZVE/5" + if loadedLogFile[0].Call != expectedValue { + t.Errorf("Not the expected Call[0] value: %s (expecting %s)", loadedLogFile[0].Call, expectedValue) + } + expectedValue = "0950" + if loadedLogFile[0].Time != expectedValue { + t.Errorf("Not the expected Time[0] value: %s (expecting %s)", loadedLogFile[0].Time, expectedValue) + } + expectedValue = "2020-05-23" + if loadedLogFile[0].Date != expectedValue { + t.Errorf("Not the expected Date[0] value: %s (expecting %s)", loadedLogFile[0].Date, expectedValue) + } + expectedValue = "ON6ZQ" + if loadedLogFile[1].Call != expectedValue { + t.Errorf("Not the expected Call[1] value: %s (expecting %s)", loadedLogFile[1].Call, expectedValue) + } + expectedValue = "0952" + if loadedLogFile[1].Time != expectedValue { + t.Errorf("Not the expected Time[1] value: %s (expecting %s)", loadedLogFile[1].Time, expectedValue) + } + expectedValue = "ON4DO" + if loadedLogFile[2].Call != expectedValue { + t.Errorf("Not the expected Call[2] value: %s (expecting %s)", loadedLogFile[2].Call, expectedValue) + } + expectedValue = "0954" + if loadedLogFile[2].Time != expectedValue { + t.Errorf("Not the expected Time[2] value: %s (expecting %s)", loadedLogFile[2].Time, expectedValue) + } + + // "20-05-25 20m 1000 on4up") + expectedValue = "2020-05-25" + if loadedLogFile[3].Date != expectedValue { + t.Errorf("Not the expected Date[3] value: %s (expecting %s)", loadedLogFile[3].Time, expectedValue) + } + expectedValue = "1000" + if loadedLogFile[3].Time != expectedValue { + t.Errorf("Not the expected Time[3] value: %s (expecting %s)", loadedLogFile[3].Time, expectedValue) + } + expectedValue = "20m" + if loadedLogFile[3].Band != expectedValue { + t.Errorf("Not the expected Band[3] value: %s (expecting %s)", loadedLogFile[3].Band, expectedValue) + } + expectedValue = "ON4UP" + if loadedLogFile[3].Call != expectedValue { + t.Errorf("Not the expected Call[3] value: %s (expecting %s)", loadedLogFile[3].Call, expectedValue) + } + //Clean Up + os.Remove(temporaryDataFileName) +} + +func TestLoadFile_happyCase_day(t *testing.T) { + + //Given + dataArray := make([]string, 0) + dataArray = append(dataArray, "# Header") + dataArray = append(dataArray, "myCall on4kjm/p") + dataArray = append(dataArray, "operator on4kjm") + dataArray = append(dataArray, "nickname Portable") + dataArray = append(dataArray, " ") + dataArray = append(dataArray, " #Log") + dataArray = append(dataArray, "20/5/23") + dataArray = append(dataArray, "40m cw 0950 ik5zve/5 9 5") + dataArray = append(dataArray, "on6zq") + dataArray = append(dataArray, "0954 on4do") + dataArray = append(dataArray, "day ++ 20m 1000 on4up") + + temporaryDataFileName := createTestFile(dataArray) + + //When + loadedLogFile, isLoadedOK := LoadFile(temporaryDataFileName, true) + + //Then + if !isLoadedOK { + t.Error("Test file could not be correctly processed") + } + if len(loadedLogFile) == 0 { + t.Error("No data loaded") + } + + expectedValue := "ON4KJM/P" + if loadedLogFile[0].MyCall != expectedValue { + t.Errorf("Not the expected MyCall value: %s (expecting %s)", loadedLogFile[0].MyCall, expectedValue) + } + expectedValue = "ON4KJM" + if loadedLogFile[0].Operator != expectedValue { + t.Errorf("Not the expected Operator value: %s (expecting %s)", loadedLogFile[0].Operator, expectedValue) + } + expectedValue = "Portable" + if loadedLogFile[0].Nickname != expectedValue { + t.Errorf("Not the expected eQsl Nickname value: %s (expecting %s)", loadedLogFile[0].Nickname, expectedValue) + } + expectedValue = "IK5ZVE/5" + if loadedLogFile[0].Call != expectedValue { + t.Errorf("Not the expected Call[0] value: %s (expecting %s)", loadedLogFile[0].Call, expectedValue) + } + expectedValue = "0950" + if loadedLogFile[0].Time != expectedValue { + t.Errorf("Not the expected Time[0] value: %s (expecting %s)", loadedLogFile[0].Time, expectedValue) + } + expectedValue = "2020-05-23" + if loadedLogFile[0].Date != expectedValue { + t.Errorf("Not the expected Date[0] value: %s (expecting %s)", loadedLogFile[0].Date, expectedValue) + } + expectedValue = "ON6ZQ" + if loadedLogFile[1].Call != expectedValue { + t.Errorf("Not the expected Call[1] value: %s (expecting %s)", loadedLogFile[1].Call, expectedValue) + } + expectedValue = "0952" + if loadedLogFile[1].Time != expectedValue { + t.Errorf("Not the expected Time[1] value: %s (expecting %s)", loadedLogFile[1].Time, expectedValue) + } + expectedValue = "ON4DO" + if loadedLogFile[2].Call != expectedValue { + t.Errorf("Not the expected Call[2] value: %s (expecting %s)", loadedLogFile[2].Call, expectedValue) + } + expectedValue = "0954" + if loadedLogFile[2].Time != expectedValue { + t.Errorf("Not the expected Time[2] value: %s (expecting %s)", loadedLogFile[2].Time, expectedValue) + } + + // "20-05-25 20m 1000 on4up") + expectedValue = "2020-05-25" + if loadedLogFile[3].Date != expectedValue { + t.Errorf("Not the expected Date[3] value: %s (expecting %s)", loadedLogFile[3].Time, expectedValue) + } + expectedValue = "1000" + if loadedLogFile[3].Time != expectedValue { + t.Errorf("Not the expected Time[3] value: %s (expecting %s)", loadedLogFile[3].Time, expectedValue) + } + expectedValue = "20m" + if loadedLogFile[3].Band != expectedValue { + t.Errorf("Not the expected Band[3] value: %s (expecting %s)", loadedLogFile[3].Band, expectedValue) + } + expectedValue = "ON4UP" + if loadedLogFile[3].Call != expectedValue { + t.Errorf("Not the expected Call[3] value: %s (expecting %s)", loadedLogFile[3].Call, expectedValue) + } + //Clean Up + os.Remove(temporaryDataFileName) +} + func TestLoadFile_bad_date(t *testing.T) { //Given @@ -280,7 +462,6 @@ func TestLoadFile_bad_date(t *testing.T) { os.Remove(temporaryDataFileName) } - func TestLoadFile_wrongHeader(t *testing.T) { //Given diff --git a/fleprocess/parse_line.go b/fleprocess/parse_line.go index 3584860..addaf06 100644 --- a/fleprocess/parse_line.go +++ b/fleprocess/parse_line.go @@ -59,8 +59,12 @@ var regexpIsOMname = regexp.MustCompile("^@") var regexpIsGridLoc = regexp.MustCompile("^#") var regexpIsRst = regexp.MustCompile("^[\\d]{1,3}$") var regexpIsFreq = regexp.MustCompile("^[\\d]+\\.[\\d]+$") -var regexpIsSotaKeyWord = regexp.MustCompile("(?i)^sota") -var regexpIsWwffKeyWord = regexp.MustCompile("(?i)^wwff") +var regexpIsSotaKeyWord = regexp.MustCompile("(?i)^sota$") +var regexpIsWwffKeyWord = regexp.MustCompile("(?i)^wwff$") +var regexpDatePattern = regexp.MustCompile("^(\\d{2}|\\d{4})[-/ .]\\d{1,2}[-/ .]\\d{1,2}$") +var regexpIsDateKeyWord = regexp.MustCompile("(?i)^date$") +var regexpDayIncrementPattern = regexp.MustCompile("^\\+*$") +var regexpIsDayKeyword = regexp.MustCompile("(?i)^day$") // ParseLine cuts a FLE line into useful bits func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg string) { @@ -119,6 +123,46 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg continue } + //Date? + if regexpDatePattern.MatchString(element) { + //We probably have a date, let's normalize it + errorTxt := "" + normalizedDate := "" + normalizedDate, errorTxt = NormalizeDate(element) + if len(errorTxt) != 0 { + logLine.Date = normalizedDate + errorMsg = errorMsg + fmt.Sprintf("Invalid Date: %s (%s)", element, errorTxt) + } else { + logLine.Date, errorTxt = ValidateDate(normalizedDate) + if len(errorTxt) != 0 { + errorMsg = errorMsg + fmt.Sprintf("Error %s", errorTxt) + } + } + continue + } + + // The date keyword is not really useful, skip it + if regexpIsDateKeyWord.MatchString(element) { + continue + } + + //Skip the "day" keyword + if regexpIsDayKeyword.MatchString(element) { + continue + } + + //Scan the + part + if regexpDayIncrementPattern.MatchString(element) { + increment := len(element) + fmt.Println(logLine.Date) + newDate, dateError := IncrementDate(logLine.Date, increment) + if dateError != "" { + errorMsg = errorMsg + fmt.Sprintf(dateError) + } + logLine.Date = newDate + continue + } + // Is it a band? isBandElement, bandLowerLimit, bandUpperLimit, _ := IsBand(element) if isBandElement { @@ -257,7 +301,7 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg } //If we come here, we could not make sense of what we found - errorMsg = errorMsg + "Unable to make sense of [" + element + "]." + errorMsg = errorMsg + "Unable to make sense of [" + element + "]. " } diff --git a/fleprocess/parse_line_test.go b/fleprocess/parse_line_test.go index 7eda825..4e91301 100644 --- a/fleprocess/parse_line_test.go +++ b/fleprocess/parse_line_test.go @@ -70,7 +70,7 @@ func TestParseLine(t *testing.T) { { "Wrong mode", args{inputStr: "cww", previousLine: LogLine{Mode: "SSB"}}, - LogLine{Mode: "SSB", RSTsent: "59", RSTrcvd: "59"}, "Unable to make sense of [cww].", + LogLine{Mode: "SSB", RSTsent: "59", RSTrcvd: "59"}, "Unable to make sense of [cww]. ", }, { "Parse OM name", @@ -152,6 +152,36 @@ func TestParseLine(t *testing.T) { args{inputStr: "1230 oe6cud/p onff-0258", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}}, LogLine{Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "59", RSTrcvd: "59", Mode: "FM", ModeType: "PHONE", WWFF: "ONFF-0258"}, "", }, + { + "date processing", + args{inputStr: "20.09.7 1230 oe6cud/p onff-0258", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}}, + LogLine{Date: "2020-09-07", Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "59", RSTrcvd: "59", Mode: "FM", ModeType: "PHONE", WWFF: "ONFF-0258"}, "", + }, + { + "date processing (with keyword)", + args{inputStr: "Date 20.09.7 1230 oe6cud/p onff-0258", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}}, + LogLine{Date: "2020-09-07", Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "59", RSTrcvd: "59", Mode: "FM", ModeType: "PHONE", WWFF: "ONFF-0258"}, "", + }, + { + "date processing - validation error", + args{inputStr: "20.09.34 1230 oe6cud/p onff-0258", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}}, + LogLine{Date: "*2020-09-34", Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "59", RSTrcvd: "59", Mode: "FM", ModeType: "PHONE", WWFF: "ONFF-0258"}, "Error parsing time \"2020-09-34\": day out of range", + }, + { + "date processing - day ", + args{inputStr: "day ++ 1230 oe6cud/p ", previousLine: LogLine{Date: "2020-09-05", Mode: "FM", ModeType: "PHONE"}}, + LogLine{Date: "2020-09-07", Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "59", RSTrcvd: "59", Mode: "FM", ModeType: "PHONE"}, "", + }, + { + "date processing - day (error) ", + args{inputStr: "day +++++++++++ 1230 oe6cud/p ", previousLine: LogLine{Date: "2020-09-05", Mode: "FM", ModeType: "PHONE"}}, + LogLine{Date: "*2020-09-05", Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "59", RSTrcvd: "59", Mode: "FM", ModeType: "PHONE"}, "Invalid day increment, expecting smaller or equal to 10", + }, + { + "date band and mode on the same line)", + args{inputStr: "Date 20.09.7 40m cw 1230 oe6cud/p onff-0258", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}}, + LogLine{Date: "2020-09-07", Mode: "CW", Band: "40m", BandLowerLimit: 7.0, BandUpperLimit: 7.3, Call: "OE6CUD/P", Time: "1230", ActualTime: "1230", RSTsent: "599", RSTrcvd: "599", ModeType: "CW", WWFF: "ONFF-0258"}, "", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/fleprocess/validate.go b/fleprocess/validate.go index e891fc8..4647f5a 100644 --- a/fleprocess/validate.go +++ b/fleprocess/validate.go @@ -142,7 +142,6 @@ func NormalizeDate(inputStr string) (date, errorMsg string) { return "*" + inputStr, errorMsg } - //complete the numbers if shorter than expected ("20" for the first and "0" for the two next) year := s[0] if len(year) == 2 { @@ -194,6 +193,29 @@ func ValidateDate(inputStr string) (ref, errorMsg string) { return wrongInputStr, fmt.Sprint(err) } +//IncrementDate will increment the supplied date by the specified increment. It returns the new date. +func IncrementDate(date string, increment int) (newdate string, err string) { + if date == "" { + return "", "No date to increment" + } + if increment < 1 { + return "*" + date, "Invalid day increment, expecting greater or equal to 1" + } + if 10 < increment { + return "*" + date, "Invalid day increment, expecting smaller or equal to 10" + } + + const RFC3339FullDate = "2006-01-02" + convertedTime, timeErr := time.Parse(RFC3339FullDate, date) + if timeErr != nil { + return "*" + date, "(Internal error) error " + fmt.Sprint(timeErr) + } + // the number of days specified in increment + newDate := convertedTime.AddDate(0, 0, increment) + + return newDate.Format(RFC3339FullDate), "" +} + //IsBand retuns true if the passed input string is a valid string func IsBand(inputStr string) (result bool, lowerLimit, upperLimit float64, altBandName string) { switch strings.ToLower(inputStr) { diff --git a/fleprocess/validate_test.go b/fleprocess/validate_test.go index ccb2052..93ae59a 100644 --- a/fleprocess/validate_test.go +++ b/fleprocess/validate_test.go @@ -455,3 +455,53 @@ func TestNormalizeDate(t *testing.T) { }) } } + +func TestIncrementDate(t *testing.T) { + type args struct { + date string + increment int + } + tests := []struct { + name string + args args + wantNewdate string + wantErr string + }{ + { + "No date", + args{date: "", increment: 2}, + "", "No date to increment", + }, + { + "increment below 0", + args{date: "2020-09-05", increment: 0}, + "*2020-09-05", "Invalid day increment, expecting greater or equal to 1", + }, + { + "increment above 10", + args{date: "2020-09-05", increment: 11}, + "*2020-09-05", "Invalid day increment, expecting smaller or equal to 10", + }, + { + "Invalid date", + args{date: "2020-09-32", increment: 2}, + "*2020-09-32", "(Internal error) error parsing time \"2020-09-32\": day out of range", + }, + { + "happy case", + args{date: "2020-09-05", increment: 2}, + "2020-09-07", "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotNewdate, gotErr := IncrementDate(tt.args.date, tt.args.increment) + if gotNewdate != tt.wantNewdate { + t.Errorf("IncrementDate() gotNewdate = %v, want %v", gotNewdate, tt.wantNewdate) + } + if gotErr != tt.wantErr { + t.Errorf("IncrementDate() gotErr = %v, want %v", gotErr, tt.wantErr) + } + }) + } +}