Skip to content

Commit

Permalink
Add command for generating test files (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Sep 26, 2024
1 parent 5ab1b47 commit 5408a98
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
25 changes: 25 additions & 0 deletions internal/super/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,22 @@ var GenerateScriptCommand = &command.Command{
RunS: generateScript,
}

var GenerateTestCommand = &command.Command{
Cmd: &cobra.Command{
Use: "test <name>",
Short: "Generate a Cadence test template",
Example: "flow generate test SomeTest",
Args: cobra.ExactArgs(1),
},
Flags: &generateFlags,
RunS: generateTest,
}

func init() {
GenerateContractCommand.AddToParent(GenerateCommand)
GenerateTransactionCommand.AddToParent(GenerateCommand)
GenerateScriptCommand.AddToParent(GenerateCommand)
GenerateTestCommand.AddToParent(GenerateCommand)
}

func generateContract(
Expand Down Expand Up @@ -120,3 +132,16 @@ func generateScript(
err = g.Create(generator.ScriptTemplate{Name: name})
return nil, err
}

func generateTest(
args []string,
_ command.GlobalFlags,
logger output.Logger,
_ flowkit.Services,
state *flowkit.State,
) (result command.Result, err error) {
g := generator.NewGenerator("", state, logger, false, true)
name := util.StripCDCExtension(args[0])
err = g.Create(generator.TestTemplate{Name: name})
return nil, err
}
3 changes: 2 additions & 1 deletion internal/super/generator/contract_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func (c ContractTemplate) GetChildren() []TemplateItem {

return []TemplateItem{
TestTemplate{
Name: fmt.Sprintf("%s_test", c.Name),
Name: fmt.Sprintf("%s_test", c.Name),
TemplatePath: "contract_init_test.cdc.tmpl",
Data: map[string]interface{}{
"ContractName": c.Name,
},
Expand Down
8 changes: 8 additions & 0 deletions internal/super/generator/templates/empty_test.cdc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test

access(all) let account = Test.createAccount()

access(all) fun testExample() {
// Test something
Test.expect(true, true)
}
2 changes: 1 addition & 1 deletion internal/super/generator/test_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (o TestTemplate) GetName() string {
// GetTemplate returns an empty string for scripts and transactions
func (o TestTemplate) GetTemplatePath() string {
if o.TemplatePath == "" {
return "contract_init_test.cdc.tmpl"
return "empty_test.cdc.tmpl"
}

return o.TemplatePath
Expand Down

0 comments on commit 5408a98

Please sign in to comment.