refactor: Rename clean function to cleanMessage for clarity and consistency

The code changes in `aviation_parser.go` and `aviation_parser_test.go` rename the `clean` function to `cleanMessage` to improve clarity and consistency in function naming. This change ensures that the function's purpose is more accurately reflected in its name, making it easier for developers to understand and maintain the code.
This commit is contained in:
windyboy
2024-07-24 14:38:32 +08:00
parent 5a61d52fc4
commit 15f15ec146
2 changed files with 55 additions and 3 deletions
+2 -2
View File
@@ -164,7 +164,7 @@ func Parse(rawText string) (*domain.ParsedMessage, error) {
return &message, nil return &message, nil
} }
func clean(text string) string { func cleanMessage(text string) string {
cleanedText := emptyLineRemove.ReplaceAllString(text, "") cleanedText := emptyLineRemove.ReplaceAllString(text, "")
cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n") cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n")
if match := bodyOnly.FindStringSubmatch(cleanText); len(match) > 1 { if match := bodyOnly.FindStringSubmatch(cleanText); len(match) > 1 {
@@ -179,7 +179,7 @@ func clean(text string) string {
func ParseHeader(fullMessage string) (domain.ParsedMessage, error) { func ParseHeader(fullMessage string) (domain.ParsedMessage, error) {
log := utils.GetSugaredLogger() log := utils.GetSugaredLogger()
fullMessage = clean(fullMessage) fullMessage = cleanMessage(fullMessage)
lines := strings.Split(fullMessage, "\n") lines := strings.Split(fullMessage, "\n")
if len(lines) < 3 { if len(lines) < 3 {
+53 -1
View File
@@ -17,7 +17,7 @@ GG ZBTJZXZX
(ARR-CES5470-ZBTJ-ZSHC1614) (ARR-CES5470-ZBTJ-ZSHC1614)
NNNN` NNNN`
It("should get a clean body text", func() { It("should get a clean body text", func() {
body := clean(message) body := cleanMessage(message)
expected := `ZCZC TMQ2530 141614 expected := `ZCZC TMQ2530 141614
GG ZBTJZXZX GG ZBTJZXZX
141614 ZSHCZTZX 141614 ZSHCZTZX
@@ -247,4 +247,56 @@ NNNN
}) })
}) })
}) })
Describe("Utility Functions", func() {
It("should clean text correctly", func() {
text := `ZCZC TMQ2530 141614
1234
4567
NNNN`
expect := "ZCZC TMQ2530 141614\n1234\n 4567"
cleaned := cleanMessage(text)
Expect(cleaned).To(Equal(expect))
})
It("should parse start indicator correctly", func() {
line := "ZCZC TMQ2530 141614"
startIndicator, messageID, dateTime, err := parseStartIndicator(line)
Expect(err).ToNot(HaveOccurred())
Expect(startIndicator).To(Equal("ZCZC"))
Expect(messageID).To(Equal("TMQ2530"))
Expect(dateTime).To(Equal("141614"))
})
It("should return error for invalid start indicator line", func() {
line := "Invalid Line"
_, _, _, err := parseStartIndicator(line)
Expect(err).To(HaveOccurred())
})
It("should parse priority and primary address correctly", func() {
line := "QU TSNZPCA"
priority, primary := parsePriorityAndPrimary(line)
Expect(priority).To(Equal("QU"))
Expect(primary).To(Equal("TSNZPCA"))
})
It("should return empty strings for invalid priority and primary address line", func() {
line := "Invalid-Line"
priority, primary := parsePriorityAndPrimary(line)
Expect(priority).To(BeEmpty())
Expect(primary).To(BeEmpty())
})
It("should parse remaining lines correctly", func() {
lines := []string{"QU PEKUDCA TSNUOCA TSNZPCA TSNUFCA", ".SELOZKE 170999", "BEGIN PART 01"}
secondaryAddresses, originator, originatorDateTime, bodyAndFooter := parseRemainingLines(lines)
Expect(secondaryAddresses).To(Equal([]string{"QU PEKUDCA TSNUOCA TSNZPCA TSNUFCA"}))
Expect(originator).To(Equal("SELOZKE"))
Expect(originatorDateTime).To(Equal("170999"))
Expect(bodyAndFooter).To(Equal("BEGIN PART 01\n"))
})
})
}) })