Add tests
This commit is contained in:
204
main_test.go
Normal file
204
main_test.go
Normal file
@@ -0,0 +1,204 @@
|
||||
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user