How to use newIdentifierAllocator method of main Package

Best Mock code snippet using main.newIdentifierAllocator

Run Mock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

identifier_allocator.go

Source: identifier_allocator.go Github

copy
1package main
2
3import "strconv"
4
5type identifierAllocator map[string]struct{}
6
7func newIdentifierAllocator(taken []string) identifierAllocator {
8	a := make(identifierAllocator, len(taken))
9	for _, s := range taken {
10		a[s] = struct{}{}
11	}
12	return a
13}
14
15func (o identifierAllocator) allocateIdentifier(want string) string {
16	id := want
17	for i := 2; ; i++ {
18		if _, ok := o[id]; !ok {
19			o[id] = struct{}{}
20			return id
21		}
22		id = want + "_" + strconv.Itoa(i)
23	}
24}
25
Full Screen

mockgen_test.go

Source: mockgen_test.go Github

copy
1package main
2
3import (
4	"fmt"
5	"testing"
6)
7
8func TestMakeArgString(t *testing.T) {
9	testCases := []struct {
10		argNames  []string
11		argTypes  []string
12		argString string
13	}{
14		{
15			argNames:  nil,
16			argTypes:  nil,
17			argString: "",
18		},
19		{
20			argNames:  []string{"arg0"},
21			argTypes:  []string{"int"},
22			argString: "arg0 int",
23		},
24		{
25			argNames:  []string{"arg0", "arg1"},
26			argTypes:  []string{"int", "bool"},
27			argString: "arg0 int, arg1 bool",
28		},
29		{
30			argNames:  []string{"arg0", "arg1"},
31			argTypes:  []string{"int", "int"},
32			argString: "arg0, arg1 int",
33		},
34		{
35			argNames:  []string{"arg0", "arg1", "arg2"},
36			argTypes:  []string{"bool", "int", "int"},
37			argString: "arg0 bool, arg1, arg2 int",
38		},
39		{
40			argNames:  []string{"arg0", "arg1", "arg2"},
41			argTypes:  []string{"int", "bool", "int"},
42			argString: "arg0 int, arg1 bool, arg2 int",
43		},
44		{
45			argNames:  []string{"arg0", "arg1", "arg2"},
46			argTypes:  []string{"int", "int", "bool"},
47			argString: "arg0, arg1 int, arg2 bool",
48		},
49		{
50			argNames:  []string{"arg0", "arg1", "arg2"},
51			argTypes:  []string{"int", "int", "int"},
52			argString: "arg0, arg1, arg2 int",
53		},
54		{
55			argNames:  []string{"arg0", "arg1", "arg2", "arg3"},
56			argTypes:  []string{"bool", "int", "int", "int"},
57			argString: "arg0 bool, arg1, arg2, arg3 int",
58		},
59		{
60			argNames:  []string{"arg0", "arg1", "arg2", "arg3"},
61			argTypes:  []string{"int", "bool", "int", "int"},
62			argString: "arg0 int, arg1 bool, arg2, arg3 int",
63		},
64		{
65			argNames:  []string{"arg0", "arg1", "arg2", "arg3"},
66			argTypes:  []string{"int", "int", "bool", "int"},
67			argString: "arg0, arg1 int, arg2 bool, arg3 int",
68		},
69		{
70			argNames:  []string{"arg0", "arg1", "arg2", "arg3"},
71			argTypes:  []string{"int", "int", "int", "bool"},
72			argString: "arg0, arg1, arg2 int, arg3 bool",
73		},
74		{
75			argNames:  []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
76			argTypes:  []string{"bool", "int", "int", "int", "bool"},
77			argString: "arg0 bool, arg1, arg2, arg3 int, arg4 bool",
78		},
79		{
80			argNames:  []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
81			argTypes:  []string{"int", "bool", "int", "int", "bool"},
82			argString: "arg0 int, arg1 bool, arg2, arg3 int, arg4 bool",
83		},
84		{
85			argNames:  []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
86			argTypes:  []string{"int", "int", "bool", "int", "bool"},
87			argString: "arg0, arg1 int, arg2 bool, arg3 int, arg4 bool",
88		},
89		{
90			argNames:  []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
91			argTypes:  []string{"int", "int", "int", "bool", "bool"},
92			argString: "arg0, arg1, arg2 int, arg3, arg4 bool",
93		},
94		{
95			argNames:  []string{"arg0", "arg1", "arg2", "arg3", "arg4"},
96			argTypes:  []string{"int", "int", "bool", "bool", "int"},
97			argString: "arg0, arg1 int, arg2, arg3 bool, arg4 int",
98		},
99	}
100
101	for i, tc := range testCases {
102		t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
103			s := makeArgString(tc.argNames, tc.argTypes)
104			if s != tc.argString {
105				t.Errorf("result == %q, want %q", s, tc.argString)
106			}
107		})
108	}
109}
110
111func TestNewIdentifierAllocator(t *testing.T) {
112	a := newIdentifierAllocator([]string{"taken1", "taken2"})
113	if len(a) != 2 {
114		t.Fatalf("expected 2 items, got %v", len(a))
115	}
116
117	_, ok := a["taken1"]
118	if !ok {
119		t.Errorf("allocator doesn't contain 'taken1': %#v", a)
120	}
121
122	_, ok = a["taken2"]
123	if !ok {
124		t.Errorf("allocator doesn't contain 'taken2': %#v", a)
125	}
126}
127
128func allocatorContainsIdentifiers(a identifierAllocator, ids []string) bool {
129	if len(a) != len(ids) {
130		return false
131	}
132
133	for _, id := range ids {
134		_, ok := a[id]
135		if !ok {
136			return false
137		}
138	}
139
140	return true
141}
142
143func TestIdentifierAllocator_allocateIdentifier(t *testing.T) {
144	a := newIdentifierAllocator([]string{"taken"})
145
146	t2 := a.allocateIdentifier("taken_2")
147	if t2 != "taken_2" {
148		t.Fatalf("expected 'taken_2', got %q", t2)
149	}
150	expected := []string{"taken", "taken_2"}
151	if !allocatorContainsIdentifiers(a, expected) {
152		t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
153	}
154
155	t3 := a.allocateIdentifier("taken")
156	if t3 != "taken_3" {
157		t.Fatalf("expected 'taken_3', got %q", t3)
158	}
159	expected = []string{"taken", "taken_2", "taken_3"}
160	if !allocatorContainsIdentifiers(a, expected) {
161		t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
162	}
163
164	t4 := a.allocateIdentifier("taken")
165	if t4 != "taken_4" {
166		t.Fatalf("expected 'taken_4', got %q", t4)
167	}
168	expected = []string{"taken", "taken_2", "taken_3", "taken_4"}
169	if !allocatorContainsIdentifiers(a, expected) {
170		t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
171	}
172
173	id := a.allocateIdentifier("id")
174	if id != "id" {
175		t.Fatalf("expected 'id', got %q", id)
176	}
177	expected = []string{"taken", "taken_2", "taken_3", "taken_4", "id"}
178	if !allocatorContainsIdentifiers(a, expected) {
179		t.Fatalf("allocator doesn't contain the expected items - allocator: %#v, expected items: %#v", a, expected)
180	}
181}
182
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)