| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"os" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 14 |
"github.com/maxatome/go-testdeep/internal/test" |
| 15 |
) |
| 16 |
|
| 17 |
func TestContext(t *testing.T) { |
| 18 |
nctx := newContext(nil) |
| 19 |
test.EqualStr(t, nctx.Path.String(), "DATA") |
| 20 |
if nctx.OriginalTB != nil { |
| 21 |
t.Error("OriginalTB should be nil") |
| 22 |
} |
| 23 |
|
| 24 |
nctx = newContext(t) |
| 25 |
test.EqualStr(t, nctx.Path.String(), "DATA") |
| 26 |
if nctxt, ok := nctx.OriginalTB.(*testing.T); test.IsTrue(t, ok, "%T", nctx.OriginalTB) { |
| 27 |
if nctxt != t { |
| 28 |
t.Errorf("OriginalTB, got=%p expected=%p", nctxt, t) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
nctx = newContext(Require(t).UseEqual().TestDeepInGotOK()) |
| 33 |
_, ok := nctx.OriginalTB.(*T) |
| 34 |
test.IsTrue(t, ok) |
| 35 |
test.IsTrue(t, nctx.FailureIsFatal) |
| 36 |
test.IsTrue(t, nctx.UseEqual) |
| 37 |
test.IsTrue(t, nctx.TestDeepInGotOK) |
| 38 |
test.EqualStr(t, nctx.Path.String(), "DATA") |
| 39 |
|
| 40 |
nctx = newBooleanContext() |
| 41 |
test.EqualStr(t, nctx.Path.String(), "") |
| 42 |
if nctx.OriginalTB != nil { |
| 43 |
t.Error("OriginalTB should be nil") |
| 44 |
} |
| 45 |
|
| 46 |
if newContextWithConfig(nil, ContextConfig{MaxErrors: -1}).CollectError(nil) != nil { |
| 47 |
t.Errorf("ctx.CollectError(nil) should return nil") |
| 48 |
} |
| 49 |
|
| 50 |
ctx := ContextConfig{} |
| 51 |
if ctx.Equal(DefaultContextConfig) { |
| 52 |
t.Errorf("Empty ContextConfig should be ≠ from DefaultContextConfig") |
| 53 |
} |
| 54 |
ctx.sanitize() |
| 55 |
if !ctx.Equal(DefaultContextConfig) { |
| 56 |
t.Errorf("Sanitized empty ContextConfig should be = to DefaultContextConfig") |
| 57 |
} |
| 58 |
|
| 59 |
ctx.RootName = "PIPO" |
| 60 |
test.EqualStr(t, ctx.OriginalPath(), "PIPO") |
| 61 |
|
| 62 |
nctx = newContext(t) |
| 63 |
nctx.Path = ctxerr.NewPath("BINGO[0].Zip") |
| 64 |
ctx.forkedFromCtx = &nctx |
| 65 |
test.EqualStr(t, ctx.OriginalPath(), "BINGO[0].Zip") |
| 66 |
} |
| 67 |
|
| 68 |
func TestGetMaxErrorsFromEnv(t *testing.T) { |
| 69 |
oldEnv, set := os.LookupEnv(envMaxErrors) |
| 70 |
defer func() { |
| 71 |
if set { |
| 72 |
os.Setenv(envMaxErrors, oldEnv) //nolint: errcheck |
| 73 |
} else { |
| 74 |
os.Unsetenv(envMaxErrors) //nolint: errcheck |
| 75 |
} |
| 76 |
}() |
| 77 |
|
| 78 |
os.Setenv(envMaxErrors, "") //nolint: errcheck |
| 79 |
test.EqualInt(t, getMaxErrorsFromEnv(), 10) |
| 80 |
|
| 81 |
os.Setenv(envMaxErrors, "aaa") //nolint: errcheck |
| 82 |
test.EqualInt(t, getMaxErrorsFromEnv(), 10) |
| 83 |
|
| 84 |
os.Setenv(envMaxErrors, "-8") //nolint: errcheck |
| 85 |
test.EqualInt(t, getMaxErrorsFromEnv(), -8) |
| 86 |
} |