Remix.run Logo
Joker_vD 4 days ago

Pfft, you know how tests for functions like this:

    func MonthToString(month int) string {
        switch month {
        case 1: return "January"
        case 2: return "February"
        ...
        case 10: return "October"
        case 12: return "December"
        default: panic(fmt.Errorf("invalid month number: %d", month))
        }
    }
are usually written? You take the switch's body, shove it into the test function, and then replace "case/return" with regexp to "assert.Equal" or something:

    func TestMonthToString(t *testing.T) {
        assert.Equal(t, "January", MonthToString(1))
        assert.Equal(t, "February", MonthToString(2))
        ...
        assert.Equal(t, "October", MonthToString(10))
        assert.Equal(t, "December", MonthToString(12))
        assert.PanicsWithError(t, "invalid month number: 13", func() { MonthToString(13) })
    }
Look ma, we got that sweet 100% code coverage!
gus_massa 4 days ago | parent | next [-]

The solution is to add integration testing,

  for (i=1, i<13, i++) {
    assert.Equal(t, i, StringToMonth(MonthToString(i)))
  }
the reverse composition is harder to test.
debugnik 4 days ago | parent [-]

Did you mean property testing?

BobbyTables2 4 days ago | parent | prev [-]

I feel attacked! (:>

nasretdinov 4 days ago | parent [-]

Nice nick name. What happened to the first one?

BobbyTables3 3 days ago | parent [-]

It got dropped.

nasretdinov 3 days ago | parent [-]

Again??