Skip to content

Commit beadf51

Browse files
authored
docs: another round of examples for today (#132)
1 parent 60d88cb commit beadf51

2 files changed

Lines changed: 434 additions & 76 deletions

File tree

examples_test.go

Lines changed: 217 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"errors"
77
"fmt"
88
"io/fs"
9+
"math"
910
"os"
11+
"regexp"
1012
"strconv"
1113
"strings"
1214
"time"
@@ -51,7 +53,11 @@ func (c *myContainer[T]) Contains(item T) bool {
5153
}
5254

5355
func (c *myContainer[T]) Empty() bool {
54-
return len(c.items) == 0
56+
return c.Size() == 0
57+
}
58+
59+
func (c *myContainer[T]) Size() int {
60+
return len(c.items)
5561
}
5662

5763
type score int
@@ -64,6 +70,32 @@ func (s score) Equal(other score) bool {
6470
return s == other
6571
}
6672

73+
type scores []score
74+
75+
func (s scores) Min() score {
76+
min := s[0]
77+
for i := 1; i < len(s); i++ {
78+
if s[i] < min {
79+
min = s[i]
80+
}
81+
}
82+
return min
83+
}
84+
85+
func (s scores) Max() score {
86+
max := s[0]
87+
for i := 1; i < len(s); i++ {
88+
if s[i] > max {
89+
max = s[i]
90+
}
91+
}
92+
return max
93+
}
94+
95+
func (s scores) Len() int {
96+
return len(s)
97+
}
98+
6799
type employee struct {
68100
first string
69101
last string
@@ -359,23 +391,59 @@ func ExampleFilePathValid() {
359391
// Output:
360392
}
361393

362-
// Greater
394+
func ExampleGreater() {
395+
Greater(t, 30, 42)
396+
// Output:
397+
}
363398

364-
// GreaterEq
399+
func ExampleGreaterEq() {
400+
GreaterEq(t, 30.1, 30.3)
401+
// Output:
402+
}
365403

366-
// InDelta
404+
func ExampleInDelta() {
405+
InDelta(t, 30.5, 30.54, .1)
406+
// Output:
407+
}
367408

368-
// InDeltaSlice
409+
func ExampleInDeltaSlice() {
410+
nums := []int{51, 48, 55, 49, 52}
411+
base := []int{52, 44, 51, 51, 47}
412+
InDeltaSlice(t, nums, base, 5)
413+
// Output:
414+
}
369415

370-
// Len
416+
func ExampleLen() {
417+
nums := []int{1, 3, 5, 9}
418+
Len(t, 4, nums)
419+
// Output:
420+
}
371421

372-
// Length
422+
func ExampleLength() {
423+
s := scores{89, 93, 91, 99, 88}
424+
Length(t, 5, s)
425+
// Output:
426+
}
373427

374-
// Less
428+
func ExampleLess() {
429+
// compare using < operator
430+
s := score(50)
431+
Less(t, 66, s)
432+
// Output:
433+
}
375434

376-
// LessEq
435+
func ExampleLessEq() {
436+
s := score(50)
437+
LessEq(t, 50, s)
438+
// Output:
439+
}
377440

378-
// Lesser
441+
func ExampleLesser() {
442+
// compare using .Less method
443+
s := score(50)
444+
Lesser(t, 66, s)
445+
// Output:
446+
}
379447

380448
// MapContainsKey
381449
func ExampleMapContainsKey() {
@@ -437,61 +505,172 @@ func ExampleMapEq() {
437505

438506
// MapNotEmpty
439507

440-
// Max
508+
func ExampleMax() {
509+
s := scores{89, 88, 91, 90, 87}
510+
Max[score](t, 91, s)
511+
// Output:
512+
}
441513

442-
// Min
514+
func ExampleMin() {
515+
s := scores{89, 88, 90, 91}
516+
Min[score](t, 88, s)
517+
// Output:
518+
}
443519

444-
// Negative
520+
func ExampleNegative() {
521+
Negative(t, -9)
522+
// Output:
523+
}
445524

446-
// Nil
525+
func ExampleNil() {
526+
var e *employee
527+
Nil(t, e)
528+
// Output:
529+
}
447530

448-
// NoError
531+
func ExampleNoError() {
532+
var err error
533+
NoError(t, err)
534+
// Output:
535+
}
449536

450-
// NonNegative
537+
func ExampleNonNegative() {
538+
NonNegative(t, 4)
539+
// Output:
540+
}
451541

452-
// NonPositive
542+
func ExampleNonPositive() {
543+
NonPositive(t, -3)
544+
// Output:
545+
}
453546

454-
// NonZero
547+
func ExampleNonZero() {
548+
NonZero(t, .001)
549+
// Output:
550+
}
455551

456-
// NotContains
552+
func ExampleNotContains() {
553+
c := newContainer("mage", "warrior", "priest", "paladin", "hunter")
554+
NotContains[string](t, "rogue", c)
555+
// Output:
556+
}
457557

458-
// NotEmpty
558+
func ExampleNotEmpty() {
559+
c := newContainer("one", "two", "three")
560+
NotEmpty(t, c)
561+
// Output:
562+
}
459563

460-
// NotEq
564+
func ExampleNotEq() {
565+
NotEq(t, "one", "two")
566+
// Output:
567+
}
461568

462-
// NotEqFunc
569+
func ExampleNotEqFunc() {
570+
NotEqFunc(t, 4.1, 5.2, func(a, b float64) bool {
571+
return math.Round(a) == math.Round(b)
572+
})
573+
// Output:
574+
}
463575

464-
// NotEqOp
576+
func ExampleNotEqOp() {
577+
NotEqOp(t, 1, 2)
578+
// Output:
579+
}
465580

466-
// NotEqual
581+
func ExampleNotEqual() {
582+
e1 := &employee{first: "alice"}
583+
e2 := &employee{first: "bob"}
584+
NotEqual(t, e1, e2)
585+
// Output:
586+
}
467587

468-
// NotNil
588+
func ExampleNotNil() {
589+
e := &employee{first: "bob"}
590+
NotNil(t, e)
591+
// Output:
592+
}
469593

470-
// One
594+
func ExampleOne() {
595+
One(t, 1)
596+
// Output:
597+
}
471598

472-
// Positive
599+
func ExamplePositive() {
600+
Positive(t, 42)
601+
// Output:
602+
}
473603

474-
// RegexCompiles
604+
func ExampleRegexCompiles() {
605+
RegexCompiles(t, `[a-z]{7}`)
606+
// Output:
607+
}
475608

476-
// RegexCompilesPOSIX
609+
func ExampleRegexCompilesPOSIX() {
610+
RegexCompilesPOSIX(t, `[a-z]{3}`)
611+
// Output:
612+
}
477613

478-
// RegexMatch
614+
func ExampleRegexMatch() {
615+
re := regexp.MustCompile(`[a-z]{6}`)
616+
RegexMatch(t, re, "cookie")
617+
// Output:
618+
}
479619

480-
// Size
620+
func ExampleSize() {
621+
c := newContainer("pie", "brownie", "cake", "cookie")
622+
Size(t, 4, c)
623+
// Output:
624+
}
481625

482-
// SliceContains
626+
func ExampleSliceContains() {
627+
drinks := []string{"ale", "lager", "cider", "wine"}
628+
SliceContains(t, drinks, "cider")
629+
// Output:
630+
}
483631

484-
// SliceContainsAll
632+
func ExampleSliceContainsAll() {
633+
nums := []int{2, 4, 6, 7, 8}
634+
SliceContainsAll(t, nums, []int{7, 8, 2, 6, 4})
635+
// Output:
636+
}
637+
638+
func ExampleSliceContainsEqual() {
639+
dave := &employee{first: "dave", id: 8}
640+
armon := &employee{first: "armon", id: 2}
641+
mitchell := &employee{first: "mitchell", id: 1}
642+
employees := []*employee{dave, armon, mitchell}
643+
SliceContainsEqual(t, employees, &employee{first: "dave", id: 8})
644+
// Output:
645+
}
485646

486-
// SliceContainsEqual
647+
func ExampleSliceContainsFunc() {
648+
// comparing slice to element of same type
649+
words := []string{"UP", "DoWn", "LefT", "RiGHT"}
650+
SliceContainsFunc(t, words, "left", func(a, b string) bool {
651+
return strings.EqualFold(a, b)
652+
})
487653

488-
// SliceContainsFunc
654+
// comparing slice to element of different type
655+
nums := []string{"2", "4", "6", "8"}
656+
SliceContainsFunc(t, nums, 4, func(a string, b int) bool {
657+
return a == strconv.Itoa(b)
658+
})
659+
// Output:
660+
}
489661

490-
// SliceContainsOp
662+
func ExampleSliceContainsOp() {
663+
nums := []int{1, 2, 3, 4, 5}
664+
SliceContainsOp(t, nums, 3)
665+
// Output:
666+
}
491667

492-
// SliceContainsSubset
668+
func ExampleSliceContainsSubset() {
669+
nums := []int{10, 20, 30, 40, 50}
670+
SliceContainsSubset(t, nums, []int{40, 10, 30})
671+
// Output:
672+
}
493673

494-
// SliceEmpty
495674
func ExampleSliceEmpty() {
496675
var ints []int
497676
SliceEmpty(t, ints)

0 commit comments

Comments
 (0)