1
1
package assert
2
2
3
3
import (
4
+ "flag"
4
5
"fmt"
5
6
"os"
6
7
"reflect"
@@ -16,13 +17,17 @@ import (
16
17
type defInd = uint32
17
18
18
19
const (
20
+ // Plain converts asserts just plain error messages without extra
21
+ // information.
22
+ Plain defInd = 0 + iota
23
+
19
24
// Production (pkg default) is the best asserter for most uses. The
20
25
// assertion violations are treated as Go error values. And only a
21
26
// pragmatic caller info is automatically included into the error message
22
27
// like file name, line number, and caller function, all in one line:
23
28
//
24
29
// copy file: main.go:37: CopyFile(): assertion violation: string shouldn't be empty
25
- Production defInd = 0 + iota
30
+ Production
26
31
27
32
// Development is the best asserter for most development uses. The
28
33
// assertion violations are treated as Go error values. And a formatted
@@ -76,8 +81,12 @@ const (
76
81
Debug
77
82
)
78
83
84
+ type flagAsserter struct {}
85
+
79
86
// Deprecated: use e.g. assert.That(), only default asserter is used.
80
87
var (
88
+ PL = AsserterToError
89
+
81
90
// P is a production Asserter that sets panic objects to errors which
82
91
// allows err2 handlers to catch them.
83
92
P = AsserterToError | AsserterCallerInfo
@@ -97,12 +106,13 @@ var (
97
106
// These two are our indexing system for default asserter. Note also the
98
107
// mutex blew. All of this is done to keep client package race detector
99
108
// cool.
109
+ // Plain
100
110
// Production
101
111
// Development
102
112
// Test
103
113
// TestFull
104
114
// Debug
105
- defAsserter = []Asserter {P , B , T , TF , D }
115
+ defAsserter = []Asserter {PL , P , B , T , TF , D }
106
116
def defInd
107
117
108
118
// mu is package lvl Mutex that is used to cool down race detector of
@@ -112,10 +122,13 @@ var (
112
122
// indexing system we are using for default asserter (above) we are pretty
113
123
// much theard safe.
114
124
mu sync.Mutex
125
+
126
+ asserterFlag flagAsserter
115
127
)
116
128
117
129
func init () {
118
130
SetDefault (Production )
131
+ flag .Var (& asserterFlag , "asserter" , "`asserter`: Plain, Prod, Dev, Debug" )
119
132
}
120
133
121
134
type (
@@ -543,29 +556,31 @@ func SetDefault(i defInd) Asserter {
543
556
544
557
// mapDefInd runtime asserters, that's why test asserts are removed for now.
545
558
var mapDefInd = map [string ]defInd {
546
- "Production" : Production ,
547
- "Development" : Development ,
559
+ "Plain" : Plain ,
560
+ "Prod" : Production ,
561
+ "Dev" : Development ,
548
562
//"Test": Test,
549
563
//"TestFull": TestFull,
550
564
"Debug" : Debug ,
551
565
}
552
566
553
- var MapDefIndToString = map [defInd ]string {
554
- Production : "Production" ,
555
- Development : "Development" ,
567
+ var mapDefIndToString = map [defInd ]string {
568
+ Plain : "Plain" ,
569
+ Production : "Prod" ,
570
+ Development : "Dev" ,
556
571
Test : "Test" ,
557
572
TestFull : "TestFull" ,
558
573
Debug : "Debug" ,
559
574
}
560
575
561
576
func AsserterString () string {
562
- return MapDefIndToString [def ]
577
+ return mapDefIndToString [def ]
563
578
}
564
579
565
- func NewDefInd (v string ) defInd {
580
+ func newDefInd (v string ) defInd {
566
581
ind , found := mapDefInd [v ]
567
582
if ! found {
568
- return Production
583
+ return Plain
569
584
}
570
585
return ind
571
586
}
@@ -601,3 +616,19 @@ func myByteToInt(b []byte) int {
601
616
type Number interface {
602
617
constraints.Float | constraints.Integer
603
618
}
619
+
620
+ // String is part of the flag interfaces
621
+ func (f * flagAsserter ) String () string {
622
+ return AsserterString ()
623
+ }
624
+
625
+ // Get is part of the flag interfaces, getter.
626
+ func (f * flagAsserter ) Get () any {
627
+ return mapDefIndToString [def ]
628
+ }
629
+
630
+ // Set is part of the flag.Value interface.
631
+ func (* flagAsserter ) Set (value string ) error {
632
+ SetDefault (newDefInd (value ))
633
+ return nil
634
+ }
0 commit comments