Skip to content

Commit 9c7c414

Browse files
committed
pkg lvl documentations for err2, assert, and try
1 parent dee8182 commit 9c7c414

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

assert/doc.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,30 @@ add following two lines at the beginning of your unit tests:
1010
alice.Node = root1.Invite(alice.Node, root1.Key, alice.PubKey, 1)
1111
assert.Equal(alice.Len(), 1) // assert anything normally
1212
13-
# Merge Runtime And Test Assertions
13+
# Merge Runtime And Unit Test Assertions
1414
1515
Especially powerful feature is that even if some assertion violation happens
16-
during the execution of called functions not the test it self. See the above
17-
example. If assertion failure happens inside of the Invite() function instead of
18-
the actual Test function, TestInvite, it's still reported correctly as normal
19-
test failure when TestInvite is executed. It doesn't matter how deep the
20-
recursion is, or if parallel test runs are performed. It works just as you
21-
hoped.
16+
during the execution of called functions not the test function itself. See the
17+
above example. If assertion failure happens inside of the Invite() function
18+
instead of the actual test function, TestInvite, it's still reported correctly
19+
as normal test failure when TestInvite unit test is executed. It doesn't matter
20+
how deep the recursion is, or if parallel test runs are performed. It works just
21+
as you hoped.
22+
23+
This is the actual Invite function implementation's first two lines. Even the
24+
assertion line is written more for runtime detection and active comment, it
25+
catches all unit test errors as well:
26+
27+
func (c Chain) Invite(...) {
28+
assert.That(c.isLeaf(invitersKey), "only leaf can invite")
2229
2330
# Call Stack Traversal During tests
2431
2532
The asserter package has super powerful feature. It allows us track assertion
2633
violations over package and even module boundaries. When using err2 assert
27-
package for runtime Asserts and assert violation happens in what ever package
28-
and module, the whole call stack is brougth to unit test logs. Naturally this is
29-
optinal. Only thing you need to do is set proper asserter and call PushTester.
34+
package for runtime Asserts and assert violation happens in whatever package
35+
and module, the whole call stack is brought to unit test logs. Naturally this is
36+
optional. Only thing you need to do is set proper asserter and call PushTester.
3037
3138
// use unit testing asserter
3239
assert.SetDefault(assert.TestFull)
@@ -66,15 +73,19 @@ And assert package's configuration flags are inserted.
6673
# Performance
6774
6875
assert.That's performance is equal to the if-statement thanks for inlining. And
69-
the most of the generics-based versions are about the equally fast.
76+
the most of the generics-based versions are about the equally fast. Practice has
77+
thought that we should prefer other than assert.That because by using detailed
78+
version like assert.Shorter we get precise error messages automatically. Some
79+
also prefer readability of specific asserters.
7080
7181
If your algorithm is performance-critical please run `make bench` in the err2
7282
repo and decide case by case. Also you can make an issue or even PR if you would
7383
like to have something similar like glog.V() function.
7484
75-
# Technical Notes
85+
# Naming
7686
77-
Format string functions need to be own instances because of Go's vet and test
78-
tool integration.
87+
Because performance has been number one requirement and Go's generics cannot
88+
discrete slices, maps and channels we have used naming prefixes accordingly: S =
89+
slice, M = map, C = channel. No prefix is (currently) for the string type.
7990
*/
8091
package assert

doc.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ the try package documentation for more information about the error checks.
6767
6868
# Automatic Stack Tracing
6969
70-
err2 offers optional stack tracing. And yes, it's fully automatic. Just set the
71-
tracers at the beginning your app, e.g. main function, to the stream you want
72-
traces to be written:
70+
err2 offers optional stack tracing. And yes, it's fully automatic. Just call
71+
72+
flag.Parse() # this is enough for err2 pkg to add its flags
73+
74+
at the beginning your app, e.g. main function, or set the tracers
75+
programmatically (before flag.Parse if you are using that):
7376
7477
err2.SetErrorTracer(os.Stderr) // write error stack trace to stderr
7578
or

try/try.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ about err2 and try packager roles can be seen in the FileCopy example:
1010
1111
w := try.To1(os.Create(dst))
1212
defer err2.Handle(&err, func(error) error {
13-
try.To(os.Remove(dst)).Logf()
13+
try.Out(os.Remove(dst)).Logf()
1414
return nil
1515
})
1616
defer w.Close()

0 commit comments

Comments
 (0)