MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / TestNew

Function TestNew

strings/guid/guid_test.go:8–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

6)
7
8func TestNew(t *testing.T) {
9 t.Run("check for allowed characters", func(t *testing.T) {
10 allowedChars := "0123456789abcdef-"
11 guid, err := New()
12 if err != nil {
13 t.Errorf(`the test failed, an error occurred: %s`, err.Error())
14 }
15
16 for _, char := range guid {
17 if !strings.Contains(allowedChars, string(char)) {
18 t.Errorf(`allowed only "%s" characters, but got %v`, allowedChars, char)
19 }
20 }
21 })
22
23 t.Run("check string length", func(t *testing.T) {
24 guid, err := New()
25 if err != nil {
26 t.Errorf(`the test failed, an error occurred: %s`, err.Error())
27 }
28
29 if len(guid) != len(pattern) {
30 t.Errorf(`the length of the string should be "%d", but got %d`, len(pattern), len(guid))
31 }
32 })
33
34 t.Run("check for version index", func(t *testing.T) {
35 expected := "4"
36 versionIndex := strings.Index(pattern, expected)
37 guid, err := New()
38 if err != nil {
39 t.Errorf(`the test failed, an error occurred: %s`, err.Error())
40 }
41
42 result := string(guid[versionIndex])
43
44 if expected != result {
45 t.Errorf(`at the index %d should be %s, but got %s`, versionIndex, expected, result)
46 }
47 })
48
49 t.Run("check the number of dashes", func(t *testing.T) {
50 expected := strings.Count(pattern, "-")
51 guid, err := New()
52 if err != nil {
53 t.Errorf(`the test failed, an error occurred: %s`, err.Error())
54 }
55
56 result := strings.Count(guid, "-")
57
58 if expected != result {
59 t.Errorf(`the length of the string should be "%d", but got %d`, len(pattern), len(guid))
60 }
61 })
62}
63
64func BenchmarkNew(b *testing.B) {
65 for i := 0; i < b.N; i++ {

Callers

nothing calls this directly

Calls 3

ContainsMethod · 0.80
NewFunction · 0.70
CountMethod · 0.45

Tested by

no test coverage detected