mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-20 08:04:28 +01:00
fixed bug in textstyleparser + added unittests
This commit is contained in:
@@ -11,18 +11,19 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
None int = 0
|
None int = 0
|
||||||
BoldBegin = 1
|
ItalicBegin = 1
|
||||||
BoldEnd = 2
|
ItalicEnd = 2
|
||||||
ItalicBegin = 3
|
BoldBegin = 3
|
||||||
ItalicEnd1 = 4
|
BoldEnd1 = 4
|
||||||
ItalicEnd2 = 5
|
BoldEnd2 = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseMarkdownMessage(message string) (string, []string) {
|
func ParseMarkdownMessage(message string) (string, []string) {
|
||||||
textFormat := Normal
|
textFormat := Normal
|
||||||
textFormatBegin := 0
|
textFormatBegin := 0
|
||||||
textFormatEnd := 0
|
textFormatLength := 0
|
||||||
|
numOfAsterisks := 0
|
||||||
state := None
|
state := None
|
||||||
signalCliFormatStrings := []string{}
|
signalCliFormatStrings := []string{}
|
||||||
fullString := ""
|
fullString := ""
|
||||||
@@ -31,34 +32,36 @@ func ParseMarkdownMessage(message string) (string, []string) {
|
|||||||
|
|
||||||
for i, v := range runes { //iterate through rune
|
for i, v := range runes { //iterate through rune
|
||||||
if v == '*' {
|
if v == '*' {
|
||||||
if state == BoldBegin {
|
if state == ItalicBegin {
|
||||||
if i-1 == textFormatBegin {
|
if i-1 == textFormatBegin {
|
||||||
state = ItalicBegin
|
state = BoldBegin
|
||||||
textFormat = Italic
|
textFormat = Bold
|
||||||
textFormatBegin = i
|
textFormatBegin = i - numOfAsterisks
|
||||||
|
textFormatLength = 0
|
||||||
} else {
|
} else {
|
||||||
state = BoldEnd
|
state = ItalicEnd
|
||||||
textFormatEnd = i - 1
|
|
||||||
}
|
}
|
||||||
} else if state == None {
|
} else if state == None {
|
||||||
state = BoldBegin
|
state = ItalicBegin
|
||||||
textFormat = Bold
|
textFormat = Italic
|
||||||
textFormatBegin = i
|
textFormatBegin = i - numOfAsterisks
|
||||||
} else if state == ItalicBegin {
|
textFormatLength = 0
|
||||||
state = ItalicEnd1
|
} else if state == BoldBegin {
|
||||||
textFormatEnd = i - 1
|
state = BoldEnd1
|
||||||
} else if state == ItalicEnd1 {
|
} else if state == BoldEnd1 {
|
||||||
state = ItalicEnd2
|
state = BoldEnd2
|
||||||
}
|
}
|
||||||
|
numOfAsterisks += 1
|
||||||
} else {
|
} else {
|
||||||
|
textFormatLength += 1
|
||||||
fullString += string(v)
|
fullString += string(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
if state == BoldEnd || state == ItalicEnd2 {
|
if state == ItalicEnd || state == BoldEnd2 {
|
||||||
signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatEnd-textFormatBegin)+":"+textFormat)
|
signalCliFormatStrings = append(signalCliFormatStrings, strconv.Itoa(textFormatBegin)+":"+strconv.Itoa(textFormatLength)+":"+textFormat)
|
||||||
state = None
|
state = None
|
||||||
textFormatBegin = 0
|
textFormatBegin = 0
|
||||||
textFormatEnd = 0
|
textFormatLength = 0
|
||||||
textFormat = Normal
|
textFormat = Normal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
src/utils/textstyleparser_test.go
Normal file
34
src/utils/textstyleparser_test.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
func expectMessageEqual(t *testing.T, message1 string, message2 string) {
|
||||||
|
if message1 != message2 {
|
||||||
|
t.Errorf("got %q, wanted %q", message1, message2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func expectFormatStringsEqual(t *testing.T, formatStrings1 []string, formatStrings2 []string) {
|
||||||
|
if !reflect.DeepEqual(formatStrings1, formatStrings2) {
|
||||||
|
t.Errorf("got %q, wanted %q", formatStrings1, formatStrings2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleMessage1(t *testing.T) {
|
||||||
|
message, signalCliFormatStrings := ParseMarkdownMessage("*italic*")
|
||||||
|
expectMessageEqual(t, message, "italic")
|
||||||
|
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"0:6:ITALIC"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleMessage(t *testing.T) {
|
||||||
|
message, signalCliFormatStrings := ParseMarkdownMessage("*This is a italic message*")
|
||||||
|
expectMessageEqual(t, message, "This is a italic message")
|
||||||
|
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"0:24:ITALIC"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBoldAndItalicMessage(t *testing.T) {
|
||||||
|
message, signalCliFormatStrings := ParseMarkdownMessage("This is a **bold** and *italic* message")
|
||||||
|
expectMessageEqual(t, message, "This is a bold and italic message")
|
||||||
|
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"10:4:BOLD", "19:6:ITALIC"})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user