Skip to content

Commit

Permalink
using testify for tests and unexporting someTool
Browse files Browse the repository at this point in the history
  • Loading branch information
Amrit Singh committed Jan 29, 2025
1 parent 050cc90 commit d78ff1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
6 changes: 4 additions & 2 deletions tools/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package tools

import (
"context"
"fmt"
"errors"
)

const ErrInvalidTool = "invalid_tool"

// Tool is a tool for the llm agent to interact with different applications.
type Tool interface {
Name() string
Expand All @@ -20,5 +22,5 @@ func (tb *Kit) UseTool(ctx context.Context, toolName string, toolArgs string) (s
return tool.Call(ctx, toolArgs)
}
}
return "", fmt.Errorf("invalid tool %v", toolName)
return "", errors.New(ErrInvalidTool)
}
39 changes: 19 additions & 20 deletions tools/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,44 @@ package tools
import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

type SomeTool struct{}
type someTool struct{}

func (st *SomeTool) Name() string {
func (st *someTool) Name() string {
return "An awesome tool"
}

func (st *SomeTool) Description() string {
func (st *someTool) Description() string {
return "This tool is awesome"
}

func (st *SomeTool) Call(ctx context.Context, _ string) (string, error) {
func (st *someTool) Call(ctx context.Context, _ string) (string, error) {
if ctx.Err() != nil {
return "", ctx.Err()
}
return "test", nil
}

func TestTool(t *testing.T) {
func TestToolWithTestify(t *testing.T) {

Check failure on line 27 in tools/tool_test.go

View workflow job for this annotation

GitHub Actions / Lint

TestToolWithTestify's subtests should call t.Parallel (tparallel)
t.Parallel()
kit := Kit{
&someTool{},
}

// Test when the tool exists
t.Run("Tool Exists in Kit", func(t *testing.T) {

Check failure on line 34 in tools/tool_test.go

View workflow job for this annotation

GitHub Actions / Lint

Function TestToolWithTestify missing the call to method parallel in the test run (paralleltest)
t.Parallel()
kit := Kit{
&SomeTool{},
}
_, err := kit.UseTool(context.Background(), "An awesome tool", "test")
if err != nil {
t.Errorf("Error using tool: %v", err)
}
result, err := kit.UseTool(context.Background(), "An awesome tool", "test")
assert.NoError(t, err)
assert.Equal(t, "test", result)
})

// Test when the tool does not exist
t.Run("Tool Does Not Exist in Kit", func(t *testing.T) {

Check failure on line 41 in tools/tool_test.go

View workflow job for this annotation

GitHub Actions / Lint

Function TestToolWithTestify missing the call to method parallel in the test run (paralleltest)
t.Parallel()
kit := Kit{
&SomeTool{},
}
_, err := kit.UseTool(context.Background(), "A tool that does not exist", "test")
if err == nil {
t.Errorf("Expected error, got nil")
}
assert.Error(t, err)
assert.Equal(t, ErrInvalidTool, err.Error())
})
}

0 comments on commit d78ff1f

Please sign in to comment.