diff --git a/internal/super/generate.go b/internal/super/generate.go index 389c9ffcc..9a3d2e9e0 100644 --- a/internal/super/generate.go +++ b/internal/super/generate.go @@ -76,10 +76,22 @@ var GenerateScriptCommand = &command.Command{ RunS: generateScript, } +var GenerateTestCommand = &command.Command{ + Cmd: &cobra.Command{ + Use: "test ", + 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( @@ -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 +} diff --git a/internal/super/generator/contract_template.go b/internal/super/generator/contract_template.go index ae78f0263..f8bbb5dd9 100644 --- a/internal/super/generator/contract_template.go +++ b/internal/super/generator/contract_template.go @@ -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, }, diff --git a/internal/super/generator/templates/empty_test.cdc.tmpl b/internal/super/generator/templates/empty_test.cdc.tmpl new file mode 100644 index 000000000..98ea2508a --- /dev/null +++ b/internal/super/generator/templates/empty_test.cdc.tmpl @@ -0,0 +1,8 @@ +import Test + +access(all) let account = Test.createAccount() + +access(all) fun testExample() { + // Test something + Test.expect(true, true) +} \ No newline at end of file diff --git a/internal/super/generator/test_template.go b/internal/super/generator/test_template.go index de936ea27..49e13e718 100644 --- a/internal/super/generator/test_template.go +++ b/internal/super/generator/test_template.go @@ -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