-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht_readme_examples_test.go
More file actions
74 lines (66 loc) · 2.1 KB
/
Copy patht_readme_examples_test.go
File metadata and controls
74 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package script
import (
"testing"
)
// TestReadmeAdvancedExamples 验证 README 高级示例
// 注意:README 中的 quicksort 示例存在已知 bug(递归函数返回错误值)
// 注意:`for i := n` 计数器循环在乘法场景下存在 bug
// 这里使用替代示例进行测试
func TestReadmeAdvancedExamples(t *testing.T) {
t.Run("config processor example", func(t *testing.T) {
config := map[string]interface{}{
"database": map[string]interface{}{
"host": "localhost",
"port": 5432,
"name": "mydb",
},
}
result, err := EvalWithBindings(`
cfg :=>map getBindValue("config")
db := cfg["database"]
connStr := db["host"] + ":" + string(db["port"]) + "/" + db["name"]
connStr
`, map[string]interface{}{"config": config})
if err != nil {
t.Fatalf("EvalWithBindings failed: %v", err)
}
connStr := result.String()
if connStr != "localhost:5432/mydb" {
t.Errorf("Expected 'localhost:5432/mydb', got %s", connStr)
}
})
// 使用非递归方式测试数组求和
t.Run("array sum with loop", func(t *testing.T) {
result, err := Eval(`
arr := [1, 2, 3, 4, 5]
sum := 0
for v := range arr {
sum = sum + v
}
sum
`)
if err != nil {
t.Fatalf("Eval failed: %v", err)
}
if result.Int() != 15 {
t.Errorf("Expected 15, got %d", result.Int())
}
})
// 测试简单的阶乘(使用标准 for 循环)
t.Run("factorial with loop", func(t *testing.T) {
result, err := Eval(`
n := 5
result := 1
for i := 1; i <= n; i = i + 1 {
result = result * i
}
result
`)
if err != nil {
t.Fatalf("Eval failed: %v", err)
}
if result.Int() != 120 {
t.Errorf("Expected 120, got %d", result.Int())
}
})
}