2

I use the VSCode generation for test file of my project,

currenlty it generate the folloing structure

tests := []struct {
        name    string
        args    args
        wantOut ZTR
    }{
        name: "test123",
        args: args{
            ztrFile: "./testdata/ztrfile.yaml",
        },
        wantOut: “ZTR.Modules",
    }

The test should cover parse of yaml and testing the properties

Here it calles to parse file

for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if gotOut := parseFile(tt.args.ztrFile); !reflect.DeepEqual(gotOut, tt.wantOut) {
                t.Errorf("parseFile() = %v, want %v", gotOut, tt.wantOut)
            }
        })

This is the struct

type Modules struct {
    Name       string
    Type       string
    cwd       string     `yaml:”cwd,omitempty"`
}

Not sure what I need to put here to make it work, I try to play with the types but Im getting errors

        {
            name: "test123",
            args: args{
                mtaFile: "./testdata/ztrfile.yaml",
            },
            wantOut: “ZTR.Modules",
        }

The errors I got is

message: 'cannot use "test123" (type string) as type struct { name string; args args; wantOut ZTR } in array or slice literal' at: '41,3' source: '' code: 'undefined'

3
  • Which is line 41 of your source? Commented Mar 29, 2018 at 13:46
  • @Flimzy - name: "test123”, and I’ve also error on wantOut: “ZTR.Modules”, Commented Mar 29, 2018 at 13:46
  • 1
    You're missing some braces. Remember, you're giving it entries in a slice of structs. Commented Mar 29, 2018 at 13:50

1 Answer 1

4

Your tests declaration is incorrect. You need to provide a slice of structs, but you're providing just keys/values:

tests := []struct {
    name    string
    args    args
    wantOut ZTR
}{
    name: "test123",
    args: args{
        mtaFile: "./testdata/ztrfile.yaml",
    },
    wantOut: “ZTR.Modules",
}

should be:

tests := []struct {
    name    string
    args    args
    wantOut ZTR
}{
    {
        name: "test123",
        args: args{
            mtaFile: "./testdata/ztrfile.yaml",
        },
        wantOut: “ZTR.Modules",
    },
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot this solve the issue, last question now I’ve error in the wantOut , what I should put there ? for example if the name of the modules should be like name=“just name” type=“justType”
I usderstad that this is what I want to verify expected but how should I put it there ?
It's hard to answer your question without knowing what the error is.
if I need to verify the output of the parse of the structure, how should I put it there , any example will help
When I put wantOut: Modules, I got error type module is not expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.