github.com/thorsphere/lpcode
Advanced tools
+7
-1
@@ -76,3 +76,2 @@ # lpcode | ||
| func (code *Code) KeyedElement(a *KeyedElementArgs) *Code | ||
| func (code *Code) TestVarDecl(tv []TestVar) *Code | ||
| func (code *Code) Format() error | ||
@@ -82,2 +81,9 @@ func (code *Code) String() string | ||
| The package includes support for generating test variable declarations through the TestVarDecl method. Test variables are predefined values with associated types and names that can be reused across multiple test cases to ensure consistency. The FindTestVar helper function allows you to retrieve a specific test variable by its type from an array of TestVar structs. | ||
| ```go | ||
| func (code *Code) TestVarDecl(tv []TestVar) *Code | ||
| func FindTestVar(t string, tv []TestVar) (*TestVar, error) | ||
| ``` | ||
| ## Example | ||
@@ -84,0 +90,0 @@ |
+27
-1
@@ -6,4 +6,8 @@ // Copyright (c) 2023-2026 thorsphere. | ||
| import "fmt" | ||
| import ( | ||
| "fmt" // fmt | ||
| "github.com/thorsphere/tserr" // tserr | ||
| ) | ||
| type TestVar struct { | ||
@@ -47,1 +51,23 @@ T string // type of testvariable | ||
| } | ||
| // findTestVar is a helper function that takes t as a string representing a type, iterates | ||
| // through the array of TestVar structs tv, and checks if the type of any TestVar matches t. | ||
| // It returns a pointer to the corresponding TestVar struct for that type. | ||
| // It returns nil and an error if the type is not found in the predefined test cases | ||
| // or if there is an issue with the input. | ||
| func FindTestVar(t string, tv []TestVar) (*TestVar, error) { | ||
| // Return nil and an error in case tv is nil | ||
| if tv == nil { | ||
| return nil, tserr.NilPtr() | ||
| } | ||
| // Iterate over testvars and return the one that matches the type t | ||
| for _, v := range tv { | ||
| // Check if the type of the current TestVar matches t | ||
| if v.T == t { | ||
| // If a match is found, return a pointer to the corresponding TestVar struct | ||
| return &v, nil | ||
| } | ||
| } | ||
| // Return nil and an error if the type is not found in the predefined test variables | ||
| return nil, tserr.NotExistent(fmt.Sprintf("test variable of type %v", t)) | ||
| } |