Pratik
Pratik

Reputation: 17

reflect.DeepEqual returns false

I was trying to equate two objects to check for my unit testing and to equate two objects I was using reflect.DeepEqual function but reflect.DeepEqual is returning false evenif the output and expected objects are equal

Below is my struct:

type Packages struct {
    AppGroup    string   `json:"appGroup"`
    AppManifest string   `json:"appManifest"`
    CmdArgs     string   `json:"cmdArgs"`
    FileName    string   `json:"fileName"`
    ImageList   []string `json:"imageList"`
    ImageNames  []string `json:"imageNames"`
    Name        string   `json:"name"`
    Requires    []string `json:"requires"`
    Source      string   `json:"source"`
    StartUp     string   `json:"startUp"`
    AppCategory string   `json:"appCategory"`
    Type        string   `json:"type"`
    Version     string   `json:"version"`
}

Below is EmlStartupManifest() method:

var EmlStartupManifest = func() ([]model.Packages, error) {
    data := model.EMMODEL{}
    packageData := []model.Packages{}
    yamlFile, err := FileReadEmlStartupManifest()
    if err != nil {
        return packageData, err
    }

    j, err := yaml.YAMLToJSON(yamlFile)
    if err != nil {
        return packageData, err
    }

    json.Unmarshal([]byte(string(j)), &data)

    for _, installer := range data.Installers {
        if installer.Name == provider.INSTALLER_NAME {
            packageData = installer.Packages
        }
    }

    return packageData, nil
}

And this is my test method:

var emlStartupManifest_output2 = new([]model.Packages)
func Test_EmlStartupManifest(t *testing.T) {
    tests := []struct {
        name         string
        expected     []model.Packages
        mockFunc     func()
        expectingErr bool
    }{
        {
            name:     "Check EML Startup Manifest error with non-existent file",
            expected: *emlStartupManifest_output2,
            mockFunc: func() {
                FileReadEmlStartupManifest = func() ([]byte, error) {
                    yamlFile, err := ioutil.ReadFile("../test/data/dummy.yaml")
                    return yamlFile, err
                }
            },
            expectingErr: true,
        },
                {
            name:     "Check EML Startup Manifest error with corrupted yaml file",
            expected: *emlStartupManifest_output2,
            mockFunc: func() {
                FileReadEmlStartupManifest = func() ([]byte, error) {
                    yamlFile, err := ioutil.ReadFile("../test/data/corrupt.yaml")
                    return yamlFile, err
                }
            },
            expectingErr: true,
        },

    }
    for _, tc := range tests {
        t.Run(tc.name, func(tt *testing.T) {
            tc.mockFunc()
            output, err := EmlStartupManifest()
            eq := reflect.DeepEqual(output, tc.expected)
            log.Println(eq)
            fmt.Printf("Output: %v Expected: %v %v %v\n", output, tc.expected, reflect.TypeOf(output), reflect.TypeOf(tc.expected))
            if !eq {
                t.Errorf("Curr tc: %v", tc.name)
                t.Errorf("Output %q not equal to expected %q", output, tc.expected)
            }
            if err != nil && tc.expectingErr == false {
                t.Errorf("Curr tc: %v", tc.name)
                t.Errorf("not expected error but got the error")
            }
            if err == nil && tc.expectingErr == true {
                t.Errorf("Curr tc: %v", tc.name)
                t.Errorf("expected error but did not get the error")
            }

        })
    }
}

Evenif the output and expected is same Deepequal is returning false. Below is my output: [![enter image description here](https://i.sstatic.net/m4tTt.png)](https://i.sstatic.net/m4tTt.png)

Can anyone tell why is this happening and what would be the possible solution?..... Thanks!

Upvotes: -1

Views: 174

Answers (1)

Miki Tebeka
Miki Tebeka

Reputation: 13850

I guess that you are comparing a nil slice to an empty slice. They look the same, but not considered the same.

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var a []int                          // nil slice
    b := []int{}                         // empty initialized slice
    fmt.Println(reflect.DeepEqual(a, b)) // false
}

Upvotes: 0

Related Questions