hexops / autogold

Automatically update your Go tests
Other
285 stars 17 forks source link

want-data is partly dynamic and has to be adapated to got before equality-check #14

Closed fabstu closed 3 years ago

fabstu commented 3 years ago

I have want-data for which part of got is dynamic.

My example is this. This is the want-data:

    - root:
        items:
        - download:
            ilias: Algos 2
            name: Algos1
            options:
              download_folder: ""
        path: 02-ss2021
    path: %s

I read this data, do want := fmt.Sprintf(wantRaw, path) and then compare with assert.Equal:

    - root:
        items:
        - download:
            ilias: Algos 2
            name: Algos1
            options:
              download_folder: ""
        path: 02-ss2021
    path: /var/folders/rw/35q09zqj5yv5pz4wwg3yjfkm0000gn/T/TestSetup_basic278973801/001

I can't see how to do this with autogold.Equal at the moment. Any ideas? I guess generics would be an easy solution but that's still a long time out. :-)

slimsag commented 3 years ago

Can you help me to understand how you would write your test with a regular Go test (or unit test generally)?

It seems like you're producing a non-deterministic string/[]byte output, and cannot compare that with anything in a stable way - so it's hard to imagine what the desired behavior here is exactly

fabstu commented 3 years ago

The template contains a %s. I read my current want-template from disk, run fmt.Sprintf(wantTemplate with the value for this run and then do the comparison based on the sprintf-output.

Thinking about this, autogold is supposed to automatically generate the want, but my sprintf relies on my placing the %s manually. This manual step seems incompatible with Autogold. I think I will replace the pre-known value in got with a placeholder, so autogold only sees the placeholder and does not have to be modified.

Good idea! 👍

A step where autogold calls a foreign method to modify got before storing it as want in -update seems possible, but that makes autogold way more complicated than it has to be. I can simply modify got before it reaches that point.