|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "Weave-Toolkit/internal/tools" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCalculatorTool(t *testing.T) { |
| 15 | + calculator := &tools.CalculatorTool{} |
| 16 | + |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + args tools.CalculatorArgs |
| 20 | + expected float64 |
| 21 | + hasError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "加法运算", |
| 25 | + args: tools.CalculatorArgs{ |
| 26 | + Operation: "add", |
| 27 | + A: 10, |
| 28 | + B: 20, |
| 29 | + }, |
| 30 | + expected: 30, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "减法运算", |
| 34 | + args: tools.CalculatorArgs{ |
| 35 | + Operation: "subtract", |
| 36 | + A: 50, |
| 37 | + B: 30, |
| 38 | + }, |
| 39 | + expected: 20, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "乘法运算", |
| 43 | + args: tools.CalculatorArgs{ |
| 44 | + Operation: "multiply", |
| 45 | + A: 5, |
| 46 | + B: 6, |
| 47 | + }, |
| 48 | + expected: 30, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "除法运算", |
| 52 | + args: tools.CalculatorArgs{ |
| 53 | + Operation: "divide", |
| 54 | + A: 100, |
| 55 | + B: 4, |
| 56 | + }, |
| 57 | + expected: 25, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "除零错误", |
| 61 | + args: tools.CalculatorArgs{ |
| 62 | + Operation: "divide", |
| 63 | + A: 10, |
| 64 | + B: 0, |
| 65 | + }, |
| 66 | + hasError: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "无效操作", |
| 70 | + args: tools.CalculatorArgs{ |
| 71 | + Operation: "invalid", |
| 72 | + A: 10, |
| 73 | + B: 20, |
| 74 | + }, |
| 75 | + hasError: true, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + argsJSON, err := json.Marshal(tt.args) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + result, err := calculator.Execute(context.Background(), argsJSON) |
| 85 | + |
| 86 | + if tt.hasError { |
| 87 | + assert.Error(t, err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + var calcResult tools.CalculatorResult |
| 94 | + err = json.Unmarshal(result, &calcResult) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + assert.Equal(t, tt.expected, calcResult.Result) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestCalculatorToolWithOperands(t *testing.T) { |
| 103 | + calculator := &tools.CalculatorTool{} |
| 104 | + |
| 105 | + tests := []struct { |
| 106 | + name string |
| 107 | + operands []float64 |
| 108 | + operation string |
| 109 | + expected float64 |
| 110 | + }{ |
| 111 | + { |
| 112 | + name: "使用操作数数组进行加法", |
| 113 | + operands: []float64{15, 25}, |
| 114 | + operation: "add", |
| 115 | + expected: 40, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "使用操作数数组进行乘法", |
| 119 | + operands: []float64{3, 7}, |
| 120 | + operation: "multiply", |
| 121 | + expected: 21, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + args := tools.CalculatorArgs{ |
| 128 | + Operation: tt.operation, |
| 129 | + Operands: tt.operands, |
| 130 | + } |
| 131 | + |
| 132 | + argsJSON, err := json.Marshal(args) |
| 133 | + require.NoError(t, err) |
| 134 | + |
| 135 | + result, err := calculator.Execute(context.Background(), argsJSON) |
| 136 | + require.NoError(t, err) |
| 137 | + |
| 138 | + var calcResult tools.CalculatorResult |
| 139 | + err = json.Unmarshal(result, &calcResult) |
| 140 | + require.NoError(t, err) |
| 141 | + |
| 142 | + assert.Equal(t, tt.expected, calcResult.Result) |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments