Add TestShareFormatWord test

main
Leonid Maslakov 5 months ago
parent f52d37a3a4
commit 8f9424b616
  1. 8
      highlight_share_nums_test.go
  2. 31
      highlight_share_word.go
  3. 104
      highlight_share_word_test.go

@ -32,6 +32,10 @@ func TestShareFormatNumber(t *testing.T) {
Input: "\ntest\n",
ExpectResult: "\ntest\n",
},
{
Input: "Русский\nтекст\n",
ExpectResult: "Русский\nтекст\n",
},
{
Input: "100",
ExpectResult: "<span class='" + StyleValue + "'>100</span>",
@ -56,10 +60,6 @@ func TestShareFormatNumber(t *testing.T) {
Input: "<span class='" + StyleOperator + "'>+</span>100",
ExpectResult: "<span class='" + StyleOperator + "'>+</span><span class='" + StyleValue + "'>100</span>",
},
{
Input: "Русский\nтекст\n",
ExpectResult: "Русский\nтекст\n",
},
}
testFunc := func(text string) string {

@ -21,7 +21,8 @@ package highlight
var defaultKeywordChars = []string{
"", " ", "\t", "\n",
"<", ">", ":", ";",
"(", ")", "{", "}",
"=", "+", "-", "*", "/", "&", "^",
"(", ")", "[", "]", "{", "}",
}
var defaultOperatorChars = []string{
@ -44,18 +45,18 @@ var defaultOperatorChars = []string{
// Processes keywords, commands and var types.
// Content inside <span>....</span> is ignored.
// It is assumed that all HTML tags except <span>....</span> were shielded.
func formatWord(line string, command string, cmdStartChars []string, cmdEndChars []string, styleClass string) string {
func formatWord(text string, command string, cmdStartChars []string, cmdEndChars []string, styleClass string) string {
result := ""
lineRune := []rune(line)
lineLen := len(lineRune)
textRune := []rune(text)
textLen := len(textRune)
commandLen := len(command)
otherSpanTagOpen := 0
skip := 0
for i := range lineRune {
char := string(lineRune[i])
for i := range textRune {
char := string(textRune[i])
// Skip
if skip != 0 {
@ -67,33 +68,33 @@ func formatWord(line string, command string, cmdStartChars []string, cmdEndChars
lastChar := ""
if i != 0 {
lastChar = string(lineRune[i-1])
lastChar = string(textRune[i-1])
}
// Get command end char
cmdEndChar := ""
if lineLen > i+commandLen {
cmdEndChar = string(lineRune[i+commandLen])
if textLen > i+commandLen {
cmdEndChar = string(textRune[i+commandLen])
}
// Get sub string
subLine := ""
if lineLen > i+commandLen-1 {
subLine = string(lineRune[i : i+commandLen])
if textLen > i+commandLen-1 {
subLine = string(textRune[i : i+commandLen])
}
// Find <span
if lineLen > i+5 {
if string(lineRune[i:i+5]) == "<span" {
if textLen > i+5 {
if string(textRune[i:i+5]) == "<span" {
otherSpanTagOpen = otherSpanTagOpen + 1
}
}
// Find </span>
if lineLen > i+7 {
if string(lineRune[i:i+7]) == "</span>" {
if textLen > i+7 {
if string(textRune[i:i+7]) == "</span>" {
otherSpanTagOpen = otherSpanTagOpen - 1
}
}

@ -0,0 +1,104 @@
// Copyright (C) 2022 Leonid Maslakov.
// This file is part of Highlight.
// Highlight is free software: you can redistribute it
// and/or modify it under the terms of the
// GNU Affero Public License as published by the
// Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// Highlight is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero Public License for more details.
// You should have received a copy of the GNU Affero Public License along with Highlight.
// If not, see <https://www.gnu.org/licenses/>.
package highlight
import (
"testing"
)
func TestShareFormatWordKeyword(t *testing.T) {
testData := []testDataType{
{
Input: "",
ExpectResult: "",
},
{
Input: "\ntest\n",
ExpectResult: "\ntest\n",
},
{
Input: "Русский\nтекст\n",
ExpectResult: "Русский\nтекст\n",
},
{
Input: "COMMAND",
ExpectResult: "<span class='" + StyleKeyword + "'>COMMAND</span>",
},
{
Input: "КОМАНДА",
ExpectResult: "<span class='" + StyleKeyword + "'>КОМАНДА</span>",
},
{
Input: "123COMMAND123",
ExpectResult: "123COMMAND123",
},
{
Input: " COMMAND",
ExpectResult: " <span class='" + StyleKeyword + "'>COMMAND</span>",
},
{
Input: "myVar:=COMMAND(123)",
ExpectResult: "myVar:=<span class='" + StyleKeyword + "'>COMMAND</span>(123)",
},
}
testFunc := func(text string) string {
for _, command := range []string{"COMMAND", "КОМАНДА"} {
text = formatWord(text, command, defaultKeywordChars, defaultKeywordChars, StyleKeyword)
}
return text
}
runTest(testFunc, testData, t)
}
func TestShareFormatWordOperator(t *testing.T) {
testData := []testDataType{
{
Input: "",
ExpectResult: "",
},
{
Input: "\ntest\n",
ExpectResult: "\ntest\n",
},
{
Input: "Русский\nтекст\n",
ExpectResult: "Русский\nтекст\n",
},
{
Input: "a=2+1",
ExpectResult: "a<span class='"+StyleOperator+"'>=</span>2<span class='"+StyleOperator+"'>+</span>1",
},
{
Input: "c=a+b",
ExpectResult: "c<span class='"+StyleOperator+"'>=</span>a<span class='"+StyleOperator+"'>+</span>b",
},
}
testFunc := func(text string) string {
for _, operator := range []string{"=", "+", "-", "*", "/"} {
text = formatWord(text, operator, defaultOperatorChars, defaultOperatorChars, StyleOperator)
}
return text
}
runTest(testFunc, testData, t)
}
Loading…
Cancel
Save