mirror of
https://github.com/IllusionMan1212/gorc.git
synced 2026-10-24 02:12:33 +00:00
fix/test: properly parse tag values with escape characters. all tests pass
This commit is contained in:
+45
-1
@@ -39,6 +39,33 @@ func (m *IRCMessage) setTimestamp() {
|
|||||||
m.Timestamp = fmt.Sprintf("[%02d:%02d]", now.Hour(), now.Minute())
|
m.Timestamp = fmt.Sprintf("[%02d:%02d]", now.Hour(), now.Minute())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func escapeCharacters(char rune) string {
|
||||||
|
switch char {
|
||||||
|
case 's':
|
||||||
|
return " "
|
||||||
|
case 'n':
|
||||||
|
return "\n"
|
||||||
|
case 'r':
|
||||||
|
return "\r"
|
||||||
|
case 'b':
|
||||||
|
return "\b"
|
||||||
|
case 't':
|
||||||
|
return "\t"
|
||||||
|
case 'f':
|
||||||
|
return "\f"
|
||||||
|
case ':':
|
||||||
|
return ";"
|
||||||
|
case '\\':
|
||||||
|
fallthrough
|
||||||
|
case '\'':
|
||||||
|
fallthrough
|
||||||
|
case '"':
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
return string(char)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func parseTags(rawTags string) Tags {
|
func parseTags(rawTags string) Tags {
|
||||||
tags := make(Tags, 0)
|
tags := make(Tags, 0)
|
||||||
rawTagsSlice := strings.Split(rawTags, ";")
|
rawTagsSlice := strings.Split(rawTags, ";")
|
||||||
@@ -51,7 +78,24 @@ func parseTags(rawTags string) Tags {
|
|||||||
|
|
||||||
// we need to make sure a "value" exists before accessing str[1]
|
// we need to make sure a "value" exists before accessing str[1]
|
||||||
if len(str) > 1 {
|
if len(str) > 1 {
|
||||||
value = str[1]
|
rawValue := str[1]
|
||||||
|
for idx := 0; idx < len(rawValue); {
|
||||||
|
// if we have a backslash at the end of the string
|
||||||
|
// we need to ignore it
|
||||||
|
if idx == len(rawValue)-1 && rawValue[idx] == '\\' {
|
||||||
|
idx += 2
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if rawValue[idx] == '\\' {
|
||||||
|
value += escapeCharacters(rune(rawValue[idx+1]))
|
||||||
|
idx += 2
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
value += string(rawValue[idx])
|
||||||
|
idx++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags[key] = value
|
tags[key] = value
|
||||||
|
|||||||
@@ -341,7 +341,6 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: fails
|
|
||||||
t.Run("Test escaped tags", func(t *testing.T) {
|
t.Run("Test escaped tags", func(t *testing.T) {
|
||||||
testMessage := "@a=b\\\\and\\nk;c=72\\s45;d=gh\\:764 foo"
|
testMessage := "@a=b\\\\and\\nk;c=72\\s45;d=gh\\:764 foo"
|
||||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||||
@@ -401,7 +400,6 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: FAIL
|
|
||||||
t.Run("Test single param with preceeding :", func(t *testing.T) {
|
t.Run("Test single param with preceeding :", func(t *testing.T) {
|
||||||
testMessage := ":src JOIN :#chan"
|
testMessage := ":src JOIN :#chan"
|
||||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||||
@@ -652,7 +650,6 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: FAIL
|
|
||||||
t.Run("Test yaml encoding with slashes", func(t *testing.T) {
|
t.Run("Test yaml encoding with slashes", func(t *testing.T) {
|
||||||
testMessage := "@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND"
|
testMessage := "@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND"
|
||||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||||
@@ -749,7 +746,6 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: FAIL
|
|
||||||
t.Run("Test tags. they should be parsed char-at-a-time", func(t *testing.T) {
|
t.Run("Test tags. they should be parsed char-at-a-time", func(t *testing.T) {
|
||||||
testMessage := "@tag1=value\\\\ntest COMMAND"
|
testMessage := "@tag1=value\\\\ntest COMMAND"
|
||||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||||
@@ -771,7 +767,6 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: FAIl
|
|
||||||
t.Run("Test tag escape for char that doesn't need it", func(t *testing.T) {
|
t.Run("Test tag escape for char that doesn't need it", func(t *testing.T) {
|
||||||
testMessage := "@tag1=value\\1 COMMAND"
|
testMessage := "@tag1=value\\1 COMMAND"
|
||||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||||
@@ -789,7 +784,6 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: FAIL
|
|
||||||
t.Run("Test slash at end of tag", func(t *testing.T) {
|
t.Run("Test slash at end of tag", func(t *testing.T) {
|
||||||
testMessage := "@tag1=value1\\ COMMAND"
|
testMessage := "@tag1=value1\\ COMMAND"
|
||||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||||
|
|||||||
Reference in New Issue
Block a user