204 lines
3.8 KiB
Go
204 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func TestSimpleFileSimpleExpression(t *testing.T) {
|
|
fileContents := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
|
luaExpr := `*1.5`
|
|
|
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
if modifiedContent != expected {
|
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
|
}
|
|
}
|
|
|
|
func TestSimpleFileVExpression(t *testing.T) {
|
|
fileContents := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
|
luaExpr := `v1 * 1.5`
|
|
|
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
if modifiedContent != expected {
|
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
|
}
|
|
}
|
|
|
|
func TestSimpleFileTableVExpression(t *testing.T) {
|
|
fileContents := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
|
luaExpr := `v[1] * 1.5`
|
|
|
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
if modifiedContent != expected {
|
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
|
}
|
|
}
|
|
|
|
func TestComplexFileSimpleExpression(t *testing.T) {
|
|
fileContents := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
<item>
|
|
<value>200</value>
|
|
</item>
|
|
<item> <value>300</value> </item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
<item>
|
|
<value>300</value>
|
|
</item>
|
|
<item> <value>450</value> </item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
|
luaExpr := `*1.5`
|
|
|
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
if modifiedContent != expected {
|
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
|
}
|
|
}
|
|
|
|
func TestComplexFileVExpression(t *testing.T) {
|
|
fileContents := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
<item>
|
|
<value>200</value>
|
|
</item>
|
|
<item> <value>300</value> </item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
<item>
|
|
<value>300</value>
|
|
</item>
|
|
<item> <value>450</value> </item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
|
luaExpr := `v1 * 1.5`
|
|
|
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
if modifiedContent != expected {
|
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
|
}
|
|
}
|
|
|
|
func TestComplexFileTableVExpression(t *testing.T) {
|
|
fileContents := `
|
|
<config>
|
|
<item>
|
|
<value>100</value>
|
|
</item>
|
|
<item>
|
|
<value>200</value>
|
|
</item>
|
|
<item> <value>300</value> </item>
|
|
</config>
|
|
`
|
|
expected := `
|
|
<config>
|
|
<item>
|
|
<value>150</value>
|
|
</item>
|
|
<item>
|
|
<value>300</value>
|
|
</item>
|
|
<item> <value>450</value> </item>
|
|
</config>
|
|
`
|
|
|
|
regex := regexp.MustCompile(`<value>(\d+)</value>`)
|
|
luaExpr := `v[1] * 1.5`
|
|
|
|
modifiedContent, err := process(fileContents, regex, luaExpr)
|
|
if err != nil {
|
|
t.Fatalf("Error processing file: %v", err)
|
|
}
|
|
|
|
if modifiedContent != expected {
|
|
t.Fatalf("Expected modified content to be %v, but got %v", expected, modifiedContent)
|
|
}
|
|
} |