How to use BuildTree method of internal Package

Best Ginkgo code snippet using internal.BuildTree

Run Ginkgo automation tests on LambdaTest cloud grid

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

suite_test.go

Source: suite_test.go Github

copy
1package internal_test
2
3import (
4	"fmt"
5	"io"
6
7	. "github.com/onsi/ginkgo/v2"
8	"github.com/onsi/ginkgo/v2/internal"
9	"github.com/onsi/ginkgo/v2/internal/interrupt_handler"
10	"github.com/onsi/ginkgo/v2/internal/parallel_support"
11	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
12	"github.com/onsi/ginkgo/v2/types"
13	. "github.com/onsi/gomega"
14)
15
16var _ = Describe("Suite", func() {
17	It("is heavily integration tested over in internal_integration", func() {
18	})
19
20	var suite *internal.Suite
21	var failer *internal.Failer
22	var reporter *FakeReporter
23	var writer *internal.Writer
24	var outputInterceptor *FakeOutputInterceptor
25	var interruptHandler *interrupt_handler.InterruptHandler
26	var conf types.SuiteConfig
27	var rt *RunTracker
28	var client parallel_support.Client
29
30	BeforeEach(func() {
31		failer = internal.NewFailer()
32		reporter = &FakeReporter{}
33		writer = internal.NewWriter(io.Discard)
34		outputInterceptor = NewFakeOutputInterceptor()
35		client = nil
36		interruptHandler = interrupt_handler.NewInterruptHandler(0, client)
37		DeferCleanup(interruptHandler.Stop)
38		conf = types.SuiteConfig{
39			ParallelTotal:   1,
40			ParallelProcess: 1,
41		}
42		rt = NewRunTracker()
43		suite = internal.NewSuite()
44	})
45
46	Describe("Constructing Trees", func() {
47		Describe("PhaseBuildTopLevel vs PhaseBuildTree", func() {
48			var err1, err2, err3 error
49			BeforeEach(func() {
50				err1 = suite.PushNode(N(ntCon, "a top-level container", func() {
51					rt.Run("traversing outer")
52					err2 = suite.PushNode(N(ntCon, "a nested container", func() {
53						rt.Run("traversing nested")
54						err3 = suite.PushNode(N(ntIt, "an it", rt.T("running it")))
55					}))
56				}))
57			})
58
59			It("only traverses top-level containers when told to BuildTree", func() {
60				fmt.Fprintln(GinkgoWriter, "HELLO!")
61				Ω(rt).Should(HaveTrackedNothing())
62				Ω(suite.BuildTree()).Should(Succeed())
63				Ω(rt).Should(HaveTracked("traversing outer", "traversing nested"))
64
65				rt.Reset()
66				suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
67				Ω(rt).Should(HaveTracked("running it"))
68
69				Ω(err1).ShouldNot(HaveOccurred())
70				Ω(err2).ShouldNot(HaveOccurred())
71				Ω(err3).ShouldNot(HaveOccurred())
72			})
73		})
74
75		Describe("InRunPhase", func() {
76			It("returns true when in the run phase and false when not in the run phase", func() {
77				falsey := true
78				truey := false
79
80				err := suite.PushNode(N(ntCon, "a top-level container", func() {
81					falsey = suite.InRunPhase()
82					suite.PushNode(N(ntIt, "an it", func() {
83						truey = suite.InRunPhase()
84					}))
85				}))
86
87				Ω(suite.BuildTree()).Should(Succeed())
88				suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
89
90				Ω(err).ShouldNot(HaveOccurred())
91				Ω(truey).Should(BeTrue())
92				Ω(falsey).Should(BeFalse())
93			})
94		})
95
96		Context("when pushing nodes during PhaseRun", func() {
97			var pushNodeErrDuringRun error
98
99			BeforeEach(func() {
100				err := suite.PushNode(N(ntCon, "a top-level container", func() {
101					suite.PushNode(N(ntIt, "an it", func() {
102						rt.Run("in it")
103						pushNodeErrDuringRun = suite.PushNode(N(ntIt, "oops - illegal operation", cl, rt.T("illegal")))
104					}))
105				}))
106
107				Ω(err).ShouldNot(HaveOccurred())
108				Ω(suite.BuildTree()).Should(Succeed())
109			})
110
111			It("errors", func() {
112				suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
113				Ω(pushNodeErrDuringRun).Should(HaveOccurred())
114				Ω(rt).Should(HaveTracked("in it"))
115			})
116
117		})
118
119		Context("when the user attempts to fail during PhaseBuildTree", func() {
120			BeforeEach(func() {
121				suite.PushNode(N(ntCon, "a top-level container", func() {
122					failer.Fail("boom", cl)
123					panic("simulate ginkgo panic")
124				}))
125			})
126
127			It("errors", func() {
128				err := suite.BuildTree()
129				Ω(err.Error()).Should(ContainSubstring(cl.String()))
130				Ω(err.Error()).Should(ContainSubstring("simulate ginkgo panic"))
131			})
132		})
133
134		Context("when the user panics during PhaseBuildTree", func() {
135			BeforeEach(func() {
136				suite.PushNode(N(ntCon, "a top-level container", func() {
137					panic("boom")
138				}))
139			})
140
141			It("errors", func() {
142				err := suite.BuildTree()
143				Ω(err).Should(HaveOccurred())
144				Ω(err.Error()).Should(ContainSubstring("boom"))
145			})
146		})
147
148		Describe("Suite Nodes", func() {
149			Context("when pushing suite nodes at the top level", func() {
150				BeforeEach(func() {
151					err := suite.PushNode(N(types.NodeTypeBeforeSuite))
152					Ω(err).ShouldNot(HaveOccurred())
153
154					err = suite.PushNode(N(types.NodeTypeAfterSuite))
155					Ω(err).ShouldNot(HaveOccurred())
156				})
157
158				Context("when pushing more than one BeforeSuite node", func() {
159					It("errors", func() {
160						err := suite.PushNode(N(types.NodeTypeBeforeSuite))
161						Ω(err).Should(HaveOccurred())
162
163						err = suite.PushNode(N(types.NodeTypeSynchronizedBeforeSuite))
164						Ω(err).Should(HaveOccurred())
165					})
166				})
167
168				Context("when pushing more than one AfterSuite node", func() {
169					It("errors", func() {
170						err := suite.PushNode(N(types.NodeTypeAfterSuite))
171						Ω(err).Should(HaveOccurred())
172
173						err = suite.PushNode(N(types.NodeTypeSynchronizedAfterSuite))
174						Ω(err).Should(HaveOccurred())
175					})
176				})
177			})
178
179			Context("when pushing a serial node in an ordered container", func() {
180				Context("when the outer-most ordered container is marked serial", func() {
181					It("succeeds", func() {
182						var errors = make([]error, 3)
183						errors[0] = suite.PushNode(N(ntCon, "top-level-container", Ordered, Serial, func() {
184							errors[1] = suite.PushNode(N(ntCon, "inner-container", func() {
185								errors[2] = suite.PushNode(N(ntIt, "it", Serial, func() {}))
186							}))
187						}))
188						Ω(errors[0]).ShouldNot(HaveOccurred())
189						Ω(suite.BuildTree()).Should(Succeed())
190						Ω(errors[1]).ShouldNot(HaveOccurred())
191						Ω(errors[2]).ShouldNot(HaveOccurred())
192					})
193				})
194
195				Context("when the outer-most ordered container is not marked serial", func() {
196					It("errors", func() {
197						var errors = make([]error, 3)
198						errors[0] = suite.PushNode(N(ntCon, "top-level-container", Ordered, func() {
199							errors[1] = suite.PushNode(N(ntCon, "inner-container", func() {
200								errors[2] = suite.PushNode(N(ntIt, "it", Serial, cl, func() {}))
201							}))
202						}))
203						Ω(errors[0]).ShouldNot(HaveOccurred())
204						Ω(suite.BuildTree()).Should(Succeed())
205						Ω(errors[1]).ShouldNot(HaveOccurred())
206						Ω(errors[2]).Should(MatchError(types.GinkgoErrors.InvalidSerialNodeInNonSerialOrderedContainer(cl, ntIt)))
207					})
208				})
209			})
210
211			Context("when pushing BeforeAll and AfterAll nodes", func() {
212				Context("in an ordered container", func() {
213					It("succeeds", func() {
214						var errors = make([]error, 3)
215						errors[0] = suite.PushNode(N(ntCon, "top-level-container", Ordered, func() {
216							errors[1] = suite.PushNode(N(types.NodeTypeBeforeAll, func() {}))
217							errors[2] = suite.PushNode(N(types.NodeTypeAfterAll, func() {}))
218						}))
219						Ω(errors[0]).ShouldNot(HaveOccurred())
220						Ω(suite.BuildTree()).Should(Succeed())
221						Ω(errors[1]).ShouldNot(HaveOccurred())
222						Ω(errors[2]).ShouldNot(HaveOccurred())
223					})
224				})
225
226				Context("anywhere else", func() {
227					It("errors", func() {
228						var errors = make([]error, 3)
229						errors[0] = suite.PushNode(N(ntCon, "top-level-container", func() {
230							errors[1] = suite.PushNode(N(types.NodeTypeBeforeAll, cl, func() {}))
231							errors[2] = suite.PushNode(N(types.NodeTypeAfterAll, cl, func() {}))
232						}))
233						Ω(errors[0]).ShouldNot(HaveOccurred())
234						Ω(suite.BuildTree()).Should(Succeed())
235						Ω(errors[1]).Should(MatchError(types.GinkgoErrors.SetupNodeNotInOrderedContainer(cl, types.NodeTypeBeforeAll)))
236						Ω(errors[2]).Should(MatchError(types.GinkgoErrors.SetupNodeNotInOrderedContainer(cl, types.NodeTypeAfterAll)))
237					})
238				})
239			})
240
241			Context("when pushing a suite node during PhaseBuildTree", func() {
242				It("errors", func() {
243					var pushSuiteNodeErr error
244					err := suite.PushNode(N(ntCon, "top-level-container", func() {
245						pushSuiteNodeErr = suite.PushNode(N(types.NodeTypeBeforeSuite, cl))
246					}))
247
248					Ω(err).ShouldNot(HaveOccurred())
249					Ω(suite.BuildTree()).Should(Succeed())
250					Ω(pushSuiteNodeErr).Should(HaveOccurred())
251				})
252			})
253
254			Context("when pushing a suite node during PhaseRun", func() {
255				It("errors", func() {
256					var pushSuiteNodeErr error
257					err := suite.PushNode(N(ntIt, "top-level it", func() {
258						pushSuiteNodeErr = suite.PushNode(N(types.NodeTypeBeforeSuite, cl))
259					}))
260
261					Ω(err).ShouldNot(HaveOccurred())
262					Ω(suite.BuildTree()).Should(Succeed())
263					suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
264					Ω(pushSuiteNodeErr).Should(HaveOccurred())
265				})
266			})
267		})
268
269		Describe("Cleanup Nodes", func() {
270			Context("when pushing a cleanup node during PhaseTopLevel", func() {
271				It("errors", func() {
272					err := suite.PushNode(N(types.NodeTypeCleanupInvalid, cl))
273					Ω(err).Should(MatchError(types.GinkgoErrors.PushingCleanupNodeDuringTreeConstruction(cl)))
274				})
275			})
276
277			Context("when pushing a cleanup node during PhaseBuildTree", func() {
278				It("errors", func() {
279					var errors = make([]error, 2)
280					errors[0] = suite.PushNode(N(ntCon, "container", func() {
281						errors[1] = suite.PushNode(N(types.NodeTypeCleanupInvalid, cl))
282					}))
283					Ω(errors[0]).ShouldNot(HaveOccurred())
284					Ω(suite.BuildTree()).Should(Succeed())
285					Ω(errors[1]).Should(MatchError(types.GinkgoErrors.PushingCleanupNodeDuringTreeConstruction(cl)))
286				})
287			})
288
289			Context("when pushing a cleanup node in a ReportBeforeEach node", func() {
290				It("errors", func() {
291					var errors = make([]error, 4)
292					reportBeforeEachNode, _ := internal.NewReportBeforeEachNode(func(_ types.SpecReport) {
293						errors[3] = suite.PushNode(N(types.NodeTypeCleanupInvalid, cl))
294					}, types.NewCodeLocation(0))
295
296					errors[0] = suite.PushNode(N(ntCon, "container", func() {
297						errors[1] = suite.PushNode(reportBeforeEachNode)
298						errors[2] = suite.PushNode(N(ntIt, "test"))
299					}))
300					Ω(errors[0]).ShouldNot(HaveOccurred())
301
302					Ω(suite.BuildTree()).Should(Succeed())
303					Ω(errors[1]).ShouldNot(HaveOccurred())
304					Ω(errors[2]).ShouldNot(HaveOccurred())
305
306					suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
307					Ω(errors[3]).Should(MatchError(types.GinkgoErrors.PushingCleanupInReportingNode(cl, types.NodeTypeReportBeforeEach)))
308				})
309			})
310
311			Context("when pushing a cleanup node in a ReportAfterEach node", func() {
312				It("errors", func() {
313					var errors = make([]error, 4)
314					reportAfterEachNode, _ := internal.NewReportAfterEachNode(func(_ types.SpecReport) {
315						errors[3] = suite.PushNode(N(types.NodeTypeCleanupInvalid, cl))
316					}, types.NewCodeLocation(0))
317
318					errors[0] = suite.PushNode(N(ntCon, "container", func() {
319						errors[1] = suite.PushNode(N(ntIt, "test"))
320						errors[2] = suite.PushNode(reportAfterEachNode)
321					}))
322					Ω(errors[0]).ShouldNot(HaveOccurred())
323
324					Ω(suite.BuildTree()).Should(Succeed())
325					Ω(errors[1]).ShouldNot(HaveOccurred())
326					Ω(errors[2]).ShouldNot(HaveOccurred())
327
328					suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
329					Ω(errors[3]).Should(MatchError(types.GinkgoErrors.PushingCleanupInReportingNode(cl, types.NodeTypeReportAfterEach)))
330				})
331			})
332
333			Context("when pushing a cleanup node in a ReportAfterSuite node", func() {
334				It("errors", func() {
335					var errors = make([]error, 4)
336					reportAfterSuiteNode, _ := internal.NewReportAfterSuiteNode("report", func(_ types.Report) {
337						errors[3] = suite.PushNode(N(types.NodeTypeCleanupInvalid, cl))
338					}, types.NewCodeLocation(0))
339
340					errors[0] = suite.PushNode(N(ntCon, "container", func() {
341						errors[2] = suite.PushNode(N(ntIt, "test"))
342					}))
343					errors[1] = suite.PushNode(reportAfterSuiteNode)
344					Ω(errors[0]).ShouldNot(HaveOccurred())
345					Ω(errors[1]).ShouldNot(HaveOccurred())
346
347					Ω(suite.BuildTree()).Should(Succeed())
348					Ω(errors[2]).ShouldNot(HaveOccurred())
349
350					suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
351					Ω(errors[3]).Should(MatchError(types.GinkgoErrors.PushingCleanupInReportingNode(cl, types.NodeTypeReportAfterSuite)))
352				})
353			})
354
355			Context("when pushing a cleanup node within a cleanup node", func() {
356				It("errors", func() {
357					var errors = make([]error, 3)
358					errors[0] = suite.PushNode(N(ntIt, "It", func() {
359						cleanupNode, _ := internal.NewCleanupNode(nil, types.NewCustomCodeLocation("outerCleanup"), func() {
360							innerCleanupNode, _ := internal.NewCleanupNode(nil, cl, func() {})
361							errors[2] = suite.PushNode(innerCleanupNode)
362						})
363						errors[1] = suite.PushNode(cleanupNode)
364					}))
365					Ω(errors[0]).ShouldNot(HaveOccurred())
366					Ω(suite.BuildTree()).Should(Succeed())
367					suite.Run("suite", Labels{}, "/path/to/suite", failer, reporter, writer, outputInterceptor, interruptHandler, client, conf)
368					Ω(errors[1]).ShouldNot(HaveOccurred())
369					Ω(errors[2]).Should(MatchError(types.GinkgoErrors.PushingCleanupInCleanupNode(cl)))
370				})
371			})
372		})
373
374		Describe("ReportEntries", func() {
375			Context("when adding a report entry outside of the run phase", func() {
376				It("errors", func() {
377					entry, err := internal.NewReportEntry("name", cl)
378					Ω(err).ShouldNot(HaveOccurred())
379					err = suite.AddReportEntry(entry)
380					Ω(err).Should(MatchError(types.GinkgoErrors.AddReportEntryNotDuringRunPhase(cl)))
381					suite.BuildTree()
382					err = suite.AddReportEntry(entry)
383					Ω(err).Should(MatchError(types.GinkgoErrors.AddReportEntryNotDuringRunPhase(cl)))
384				})
385			})
386		})
387
388		When("using when", func() {
389			It("prepends 'when' to the test name", func() {
390				Ω(CurrentSpecReport().FullText()).Should(ContainSubstring(" when using when prepends"))
391			})
392		})
393	})
394})
395
Full Screen

math_tree_test.go

Source: math_tree_test.go Github

copy
1package parser
2
3import (
4	"testing"
5
6	"github.com/sarulabs/di/v2"
7
8	"github.com/blkmlk/microshell/internal/logger"
9	"github.com/blkmlk/microshell/internal/models"
10	"github.com/stretchr/testify/suite"
11)
12
13type MathTreeExpressionTestSuite struct {
14	suite.Suite
15	ctx SystemContext
16}
17
18func TestMathTree(t *testing.T) {
19	suite.Run(t, new(MathTreeExpressionTestSuite))
20}
21
22func (t *MathTreeExpressionTestSuite) SetupTest() {
23	builder, err := di.NewBuilder()
24	t.Require().NoError(err)
25
26	err = builder.Add(
27		Definition,
28		DefinitionContext,
29		DefinitionScope,
30		DefinitionCommandTree,
31		logger.Definition,
32	)
33	t.Require().NoError(err)
34
35	ctn := builder.Build()
36
37	t.ctx = ctn.Get(DefinitionNameRootScope).(SystemContext)
38}
39
40func (t *MathTreeExpressionTestSuite) TestMathTree_Add() {
41	tree := t.buildTree([]interface{}{"2", OperatorMultiply, "2", OperatorPlus, "2", OperatorMultiply, "3"})
42
43	ctx := t.ctx
44
45	v, err := tree.Value(ctx)
46	t.Require().NoError(err)
47	t.Require().True(v.IsNumber())
48	t.Require().Equal(10, v.Number())
49
50	tree = t.buildTree([]interface{}{"2", OperatorConcatenate, "3"})
51	v, err = tree.Value(ctx)
52	t.Require().NoError(err)
53	t.Require().Equal("23", v.String())
54
55	tree = t.buildTree([]interface{}{"2", OperatorMultiply, "3", OperatorConcatenate, "3"})
56	v, err = tree.Value(ctx)
57	t.Require().NoError(err)
58	t.Require().Equal("63", v.String())
59
60	tree = t.buildTree([]interface{}{"1", OperatorPlus, "3", OperatorConcatenate, "10"})
61	v, err = tree.Value(ctx)
62	t.Require().NoError(err)
63	t.Require().Equal("410", v.String())
64
65	tree = t.buildTree([]interface{}{"1", OperatorPlus, "3", OperatorEqual, "4"})
66	v, err = tree.Value(ctx)
67	t.Require().NoError(err)
68	t.Require().True(v.Bool())
69
70	tree = t.buildTree([]interface{}{"10", OperatorMultiply, "3", OperatorNotEqual, "30"})
71	v, err = tree.Value(ctx)
72	t.Require().NoError(err)
73	t.Require().False(v.Bool())
74
75	tree = t.buildTree([]interface{}{"10", OperatorMultiply, "3", OperatorEqual, "15", OperatorPlus, "20", OperatorMinus, "5"})
76	v, err = tree.Value(ctx)
77	t.Require().NoError(err)
78	t.Require().True(v.Bool())
79
80	tree = t.buildTree([]interface{}{"10", OperatorMultiply, "3", OperatorEqual, "15", OperatorPlus, "20", OperatorMinus, "5"})
81	v, err = tree.Value(ctx)
82	t.Require().NoError(err)
83	t.Require().True(v.Bool())
84
85	tree = t.buildTree([]interface{}{"true", OperatorGreater, "false"})
86	_, err = tree.Value(ctx)
87	t.Require().Error(err)
88
89	tree = t.buildTree([]interface{}{"true", OperatorNotEqual, "false"})
90	v, err = tree.Value(ctx)
91	t.Require().NoError(err)
92	t.Require().True(v.Bool())
93
94	tree = t.buildTree([]interface{}{OperatorNot, "false"})
95	v, err = tree.Value(ctx)
96	t.Require().NoError(err)
97	t.Require().True(v.Bool())
98
99	tree = t.buildTree([]interface{}{"true", OperatorEqual, OperatorNot, "false"})
100	v, err = tree.Value(ctx)
101	t.Require().NoError(err)
102	t.Require().True(v.Bool())
103
104	tree = t.buildTree([]interface{}{OperatorNot, "true", OperatorEqual, "false"})
105	v, err = tree.Value(ctx)
106	t.Require().NoError(err)
107	t.Require().True(v.Bool())
108
109	tree = t.buildTree([]interface{}{"1", OperatorEqual, "1", OperatorEqual, "true"})
110	v, err = tree.Value(ctx)
111	t.Require().NoError(err)
112	t.Require().True(v.Bool())
113
114	tree = t.buildTree([]interface{}{OperatorNot, OperatorNot, "true"})
115	v, err = tree.Value(ctx)
116	t.Require().NoError(err)
117	t.Require().True(v.Bool())
118}
119
120func (t *MathTreeExpressionTestSuite) buildTree(items []interface{}) *mathTree {
121	tree := NewMathTree()
122
123	ctx := t.ctx
124
125	for _, i := range items {
126		switch t := i.(type) {
127		case string:
128			exp := NewStdExpression(true)
129
130			for _, c := range t {
131				exp.Add(ctx, models.Rune(c))
132			}
133
134			tree.Add(exp)
135		case Operator:
136			tree.Add(t)
137		}
138	}
139
140	return tree
141}
142
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

Most used method in

Trigger BuildTree code on LambdaTest Cloud Grid

Execute automation tests with BuildTree on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)