From 9a670fdc617a433df4d9f6d440d4b7258d97d5d2 Mon Sep 17 00:00:00 2001 From: naudachu Date: Wed, 21 Jun 2023 21:03:48 +0500 Subject: [PATCH] - simple test for helper function --- helpers/helpers.go | 36 ++++++++++++++++++++++++++++++++++++ {ext => helpers}/xml_test.go | 9 ++++----- 2 files changed, 40 insertions(+), 5 deletions(-) rename {ext => helpers}/xml_test.go (74%) diff --git a/helpers/helpers.go b/helpers/helpers.go index 93c17ba..8a12bfd 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -1,7 +1,9 @@ package helpers import ( + "encoding/xml" "regexp" + "strconv" "strings" ) @@ -22,3 +24,37 @@ func GitNaming(input string) string { // Join words and return return strings.Join(words, "-") } + +type MultistatusObj struct { + XMLName xml.Name `xml:"multistatus"` + Multistatus struct { + XMLName xml.Name `xml:"response"` + Propstat struct { + XMLName xml.Name `xml:"propstat"` + Prop struct { + XMLName xml.Name `xml:"prop"` + FileID struct { + XMLName xml.Name `xml:"fileid"` + ID string `xml:",chardata"` + } + } + } + } +} + +func GetFileIDFromRespBody(str []byte) int { + + var multi MultistatusObj + + err := xml.Unmarshal(str, &multi) + if err != nil { + return 0 + } + + id, err := strconv.Atoi(multi.Multistatus.Propstat.Prop.FileID.ID) + if err != nil { + return 0 + } + + return id +} diff --git a/ext/xml_test.go b/helpers/xml_test.go similarity index 74% rename from ext/xml_test.go rename to helpers/xml_test.go index ba59d5c..30fccee 100644 --- a/ext/xml_test.go +++ b/helpers/xml_test.go @@ -1,7 +1,6 @@ -package ext +package helpers import ( - "log" "testing" ) @@ -12,7 +11,7 @@ const ( // [ ] todo normal test... func TestGetFileID(t *testing.T) { - str, err := getFileIDFromRespBody([]byte(EXAMPLE)) - log.Print(str, err) - + if output := GetFileIDFromRespBody([]byte(EXAMPLE)); output != 33225 { + t.Errorf("Output %q not equal to expected %q", output, 33225) + } }