From 7a6673181731f087d866ec9dec3aa57e09f4580f Mon Sep 17 00:00:00 2001 From: Jean-Marc MEESSEN Date: Thu, 11 Jun 2020 21:56:38 +0200 Subject: [PATCH] Finished braketedData processing --- cmd/braketedData.go | 24 ++++++++++++---- cmd/braketedData_test.go | 59 ++++++++++++++++++++++++++++++++++------ cmd/parse_line.go | 5 ++-- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/cmd/braketedData.go b/cmd/braketedData.go index f4e675c..6f1236c 100644 --- a/cmd/braketedData.go +++ b/cmd/braketedData.go @@ -19,17 +19,26 @@ import ( "strings" ) -func getBraketedData(inputLine, braketType string) (text, cleanedLine string) { +//The BraketType type is used to define the enumeration +type BraketType int + +//Enumeration of the valid Bracket Types +const ( + COMMENT BraketType = iota + QSL +) + +func getBraketedData(inputLine string, braketType BraketType) (braketedData, cleanedLine string) { // Get substring between two strings. a := "" b := "" //TODO: refactor that as a switch statement to exclude non supported bracket types - if braketType == "COMMENT" { + if braketType == COMMENT { a = "<" b = ">" } - if braketType == "QSL" { + if braketType == QSL { a = "[" b = "]" } @@ -43,8 +52,11 @@ func getBraketedData(inputLine, braketType string) (text, cleanedLine string) { return "",inputLine } posFirstAdjusted := posFirst + 1 - if posFirstAdjusted >= posLast { + if posFirstAdjusted > posLast { return "",inputLine - } - return inputLine[posFirstAdjusted:posLast], inputLine + } + + braketedData = inputLine[posFirstAdjusted:posLast] + cleanedLine = strings.Replace(inputLine, a + braketedData + b, "",1) + return braketedData, cleanedLine } \ No newline at end of file diff --git a/cmd/braketedData_test.go b/cmd/braketedData_test.go index b661019..d79f81c 100644 --- a/cmd/braketedData_test.go +++ b/cmd/braketedData_test.go @@ -5,21 +5,62 @@ import "testing" func Test_getBraketedData(t *testing.T) { type args struct { inputLine string - braketType string + braketType BraketType } tests := []struct { - name string - args args - wantText string - wantCleanedLine string + name string + args args + wantBraketedData string + wantCleanedLine string }{ - // TODO: Add test cases. + { + "Happy case: comment", + args{ inputLine: "aaaa bbbbb", braketType: COMMENT}, + "bracketed text", + "aaaa bbbbb", + }, + { + "Happy case: QSL", + args{ inputLine: "aaaa [bracketed text] bbbbb", braketType: QSL}, + "bracketed text", + "aaaa bbbbb", + }, + { + "Happy case: nothing", + args{ inputLine: "aaaa bbbbb cccccc", braketType: QSL}, + "", + "aaaa bbbbb cccccc", + }, + { + "Empty brackets", + args{ inputLine: "aaaa <> bbbbb", braketType: COMMENT}, + "", + "aaaa bbbbb", + }, + { + "Brackets at right", + args{ inputLine: "aaaa bbbbb ", braketType: COMMENT}, + "bracketed text", + "aaaa bbbbb ", + }, + { + "concatenated", + args{ inputLine: "aaaabbbbb", braketType: COMMENT}, + "bracketed text", + "aaaabbbbb", + }, + { + "duplicated", + args{ inputLine: "aaaa bbbbb < double > cccc", braketType: COMMENT}, + "bracketed text", + "aaaa bbbbb < double > cccc", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotText, gotCleanedLine := getBraketedData(tt.args.inputLine, tt.args.braketType) - if gotText != tt.wantText { - t.Errorf("getBraketedData() gotText = %v, want %v", gotText, tt.wantText) + gotBraketedData, gotCleanedLine := getBraketedData(tt.args.inputLine, tt.args.braketType) + if gotBraketedData != tt.wantBraketedData { + t.Errorf("getBraketedData() gotBraketedData = %v, want %v", gotBraketedData, tt.wantBraketedData) } if gotCleanedLine != tt.wantCleanedLine { t.Errorf("getBraketedData() gotCleanedLine = %v, want %v", gotCleanedLine, tt.wantCleanedLine) diff --git a/cmd/parse_line.go b/cmd/parse_line.go index 38413cd..c7baa54 100644 --- a/cmd/parse_line.go +++ b/cmd/parse_line.go @@ -50,14 +50,13 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg //TODO: what happens when we have <> or when there are multiple comments //TODO: Refactor this! it is ugly - comment,inputStr := getBraketedData(inputStr, "COMMENT") + comment,inputStr := getBraketedData(inputStr, COMMENT) if comment != "" { logLine.Comment = comment - inputStr = strings.Replace(inputStr, "<" + comment + ">", "",1) fmt.Println("Cleaned input string: ", inputStr) } - comment,inputStr = getBraketedData(inputStr, "QSL") + comment,inputStr = getBraketedData(inputStr, QSL) if comment != "" { logLine.QSLmsg = comment inputStr = strings.Replace(inputStr, "[" + comment + "]", "",1)