How to use run method of main Package

Best Mock code snippet using main.run

Run Mock automation tests on LambdaTest cloud grid

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

strategy_cover_test.go

Source: strategy_cover_test.go Github

copy
1package main
2
3import (
4	"context"
5	"io/ioutil"
6	"log"
7	"os"
8	"path/filepath"
9	"reflect"
10	"sort"
11	"testing"
12)
13
14func TestCoverStrategyTestsToRun(t *testing.T) {
15	var (
16		gomod = []byte(`module cover-strategy-test-run
17
18		go 1.13
19		`)
20		mainFile = []byte(`package main
21
22		import (
23			"fmt"
24                        "cover-strategy-test-run/pkga"
25		)
26
27		var a, b int = 10, 20
28
29		func main() {
30			fmt.Printf("%+v\n", add(a, b))
31		}
32
33		func sub(a, b int) int {
34			return pkga.Sub(a, b)
35		}
36		`)
37		fileA = []byte(`package main
38
39		func add(a, b int) int {
40			return a + b
41		}
42
43		func mul(a, b int) int {
44			return a * b
45		}
46		`)
47		mainTestFile = []byte(`package main
48
49		import (
50			"testing"
51		)
52
53		func TestAdd(t *testing.T) {
54			if add(3, 4) != 7 {
55				t.Error("add unexpected result")
56			}
57		}
58
59		func TestMul(t *testing.T) {
60			if mul(10, 5) != 50 {
61				t.Error("mul unexpected result")
62			}
63		}
64		`)
65
66		pkgAFileA = []byte(`package pkga
67
68		func Div(a, b int) int {
69			return a / b
70		}
71
72		func Sub(a, b int) int {
73			return a - b
74		}
75		`)
76		pkgATestFile = []byte(`package pkga
77
78		import (
79			"testing"
80		)
81
82		func TestDiv(t *testing.T) {
83			if Div(3, 2) != 1 {
84				t.Error("Div unexpected result")
85			}
86		}
87
88		func TestSub(t *testing.T) {
89			if Sub(10, 5) != 5 {
90				t.Error("Sub unexpected result")
91			}
92		}`)
93
94		fileAAddDouble = []byte(`package main
95
96		func add(a, b int) int {
97			return a + b
98		}
99
100		func mul(a, b int) int {
101			return a * b
102		}
103
104		func double(a int) int {
105			return 2 * a
106		}
107		`)
108		mainTestFileAddDouble = []byte(`package main
109
110		import (
111			"testing"
112		)
113
114		func TestAdd(t *testing.T) {
115			if add(3, 4) != 7 {
116				t.Error("add unexpected result")
117			}
118		}
119
120		func TestMul(t *testing.T) {
121			if mul(10, 5) != 50 {
122				t.Error("mul unexpected result")
123			}
124		}
125
126		func TestDouble(t *testing.T) {
127			if double(1) != 2 {
128				t.Error("double unexpected result")
129			}
130		}
131		`)
132		fileAChangeMul = []byte(`package main
133
134		func add(a, b int) int {
135			return a + b
136		}
137
138		func mul(a, b int) int {
139			return a * b * 1
140		}
141
142		func double(a int) int {
143			return 2 * a
144		}
145		`)
146		pkgAfileAChangeSub = []byte(`package pkga
147
148		func Div(a, b int) int {
149			return a / b
150		}
151
152		func Sub(a, b int) int {
153			return a - b + 0
154		}`)
155		pkgATestFileUpdate = []byte(`package pkga
156
157		import (
158			"testing"
159		)
160
161		func TestDiv(t *testing.T) {
162			if Div(6, 2) != 3 {
163				t.Error("Div unexpected result")
164			}
165		}
166
167		func TestSub(t *testing.T) {
168			if Sub(10, 5) != 5 {
169				t.Error("Sub unexpected result")
170			}
171		}`)
172
173		testAddProf = []byte(`mode: set
174cover-strategy-test-run/pkga/file_a.go:3.26,5.4 1 0
175cover-strategy-test-run/pkga/file_a.go:7.26,9.4 1 0
176cover-strategy-test-run/file_a.go:3.26,5.4 1 1
177cover-strategy-test-run/file_a.go:7.26,9.4 1 0
178cover-strategy-test-run/file_a.go:11.26,13.4 1 0
179cover-strategy-test-run/main.go:10.15,12.4 1 0
180cover-strategy-test-run/main.go:14.26,16.4 1 0
181`)
182		testDivProf = []byte(`mode: set
183cover-strategy-test-run/pkga/file_a.go:3.26,5.4 1 1
184cover-strategy-test-run/pkga/file_a.go:7.26,9.4 1 0
185cover-strategy-test-run/file_a.go:3.26,5.4 1 0
186cover-strategy-test-run/file_a.go:7.26,9.4 1 0
187cover-strategy-test-run/file_a.go:11.26,13.4 1 0
188cover-strategy-test-run/main.go:10.15,12.4 1 0
189cover-strategy-test-run/main.go:14.26,16.4 1 0
190`)
191
192		testDoubleProf = []byte(`mode: set
193cover-strategy-test-run/file_a.go:3.26,5.4 1 0
194cover-strategy-test-run/file_a.go:7.26,9.4 1 0
195cover-strategy-test-run/file_a.go:11.26,13.4 1 1
196cover-strategy-test-run/main.go:10.15,12.4 1 0
197cover-strategy-test-run/main.go:14.26,16.4 1 0
198cover-strategy-test-run/pkga/file_a.go:3.26,5.4 1 0
199cover-strategy-test-run/pkga/file_a.go:7.26,9.4 1 0
200`)
201
202		testMulProf = []byte(`mode: set
203cover-strategy-test-run/main.go:10.15,12.4 1 0
204cover-strategy-test-run/main.go:14.26,16.4 1 0
205cover-strategy-test-run/file_a.go:3.26,5.4 1 0
206cover-strategy-test-run/file_a.go:7.26,9.4 1 1
207cover-strategy-test-run/file_a.go:11.26,13.4 1 0
208cover-strategy-test-run/pkga/file_a.go:3.26,5.4 1 0
209cover-strategy-test-run/pkga/file_a.go:7.26,9.4 1 0
210`)
211		testSubProf = []byte(`mode: set
212cover-strategy-test-run/pkga/file_a.go:3.26,5.4 1 0
213cover-strategy-test-run/pkga/file_a.go:7.26,9.4 1 1
214cover-strategy-test-run/file_a.go:3.26,5.4 1 0
215cover-strategy-test-run/file_a.go:7.26,9.4 1 0
216cover-strategy-test-run/file_a.go:11.26,13.4 1 0
217cover-strategy-test-run/main.go:10.15,12.4 1 0
218cover-strategy-test-run/main.go:14.26,16.4 1 0
219`)
220	)
221	// setup
222	testDir := filepath.Join(os.TempDir(), "test_cover_strategy_tests_to_run")
223	gitCmdRun := NewGitCmd(testDir)
224
225	pkgAFileAPath := filepath.Join("pkga", "file_a.go")
226	pkgATestFileAPath := filepath.Join("pkga", "file_a_test.go")
227
228	files := map[string][]byte{
229		"go.mod":  gomod,
230		"main.go": mainFile, "file_a.go": fileA, "main_test.go": mainTestFile,
231		pkgAFileAPath: pkgAFileA, pkgATestFileAPath: pkgATestFile,
232	}
233	logger := log.New(os.Stdout, "gtr-cover-strategy-test:", log.Ltime)
234	setup := func() *CoverStrategy {
235		setupTestGitDir(t,
236			testDir, files,
237			[]string{
238				"go.mod", "main.go", "main_test.go", "file_a.go",
239				pkgAFileAPath, pkgATestFileAPath,
240			},
241		)
242		// TODO test with different configurations
243		return NewCoverStrategy(true, testDir, logger)
244	}
245	// teardown
246	defer func() {
247		if !t.Failed() {
248			// clean tmp dir on test success
249			_ = os.RemoveAll(testDir)
250		}
251	}()
252	cases := []struct {
253		desc            string
254		setup, tearDown func() error
255		outTests        []string
256		err             error
257	}{
258		{
259			desc: "No changes in files, no cover profiles",
260			outTests: []string{"cover-strategy-test-run.TestAdd",
261				"cover-strategy-test-run.TestMul",
262				"cover-strategy-test-run/pkga.TestDiv",
263				"cover-strategy-test-run/pkga.TestSub"},
264		},
265		{
266			desc: "Add func double in file_a.go",
267			setup: func() error {
268				return ioutil.WriteFile(
269					filepath.Join(testDir, "file_a.go"), fileAAddDouble, 0600)
270			},
271			tearDown: func() error {
272				files := [][]byte{
273					[]byte("cover-strategy-test-run.TestAdd"),
274					testAddProf,
275					[]byte("cover-strategy-test-run.TestMul"),
276					testMulProf,
277					[]byte("cover-strategy-test-run.TestDouble"),
278					testDoubleProf,
279					[]byte("cover-strategy-test-run_pkga.TestSub"),
280					testSubProf,
281					[]byte("cover-strategy-test-run_pkga.TestDiv"),
282					testDivProf,
283				}
284				for i := 0; i < len(files); i += 2 {
285					err := ioutil.WriteFile(
286						filepath.Join(testDir, ".gtr", string(files[i])),
287						files[i+1], 0600)
288					if err != nil {
289						return err
290					}
291				}
292				return gitCmdRun("commit", "-am", "changes")
293			},
294			outTests: nil,
295		},
296		{
297			desc: "Add test to Double",
298			setup: func() error {
299				return ioutil.WriteFile(
300					filepath.Join(testDir, "main_test.go"),
301					mainTestFileAddDouble, 0600)
302			},
303			tearDown: func() error {
304				return gitCmdRun("commit", "-am", "commit changes") // Test
305			},
306			outTests: []string{"cover-strategy-test-run.TestDouble"},
307		},
308		{
309			desc: "Update mul func in file_a.go and sub in pkga/file_a.go",
310			setup: func() error {
311				err := ioutil.WriteFile(
312					filepath.Join(testDir, "file_a.go"),
313					fileAChangeMul, 0600)
314				if err != nil {
315					return err
316				}
317				return ioutil.WriteFile(
318					filepath.Join(testDir, pkgAFileAPath),
319					pkgAfileAChangeSub, 0600)
320			},
321			tearDown: func() error {
322				return gitCmdRun("commit", "-am", "commit changes")
323			},
324			outTests: []string{"cover-strategy-test-run.TestMul",
325				"cover-strategy-test-run/pkga.TestSub"},
326		},
327		{
328			desc: "Update in pkga",
329			setup: func() error {
330				return ioutil.WriteFile(
331					filepath.Join(testDir, pkgATestFileAPath),
332					pkgATestFileUpdate, 0600)
333			},
334			tearDown: func() error {
335				return gitCmdRun("commit", "-am", "commit changes") // Test
336			},
337			outTests: []string{"cover-strategy-test-run/pkga.TestDiv"},
338		},
339	}
340	coverStrategy := setup()
341	for i, tc := range cases {
342		// setup()
343		execTestHelper(t, i, tc.desc, tc.setup)
344		_, testsList, _, err := coverStrategy.TestsToRun(context.Background())
345		// teardown()
346		execTestHelper(t, i, tc.desc, tc.tearDown)
347		if isUnexpectedErr(t, i, tc.desc, tc.err, err) {
348			continue
349		}
350		// TODO test packages are handled
351		sort.Strings(tc.outTests)
352		sort.Strings(testsList)
353		if !reflect.DeepEqual(tc.outTests, testsList) {
354			t.Errorf("case [%d] %s\nexpected Tests %+v\ngot %+v", i, tc.desc, tc.outTests, testsList)
355		}
356
357	}
358
359}
360
361func TestFindAllTestInDir(t *testing.T) {
362	var (
363		moduleName = "find-all.tests"
364		gomod      = []byte(`module find-all.tests
365
366		go 1.13
367		`)
368		mainFile = []byte(`package main
369
370		import (
371			"fmt"
372                        "find-all.tests/pkga"
373		)
374
375		var a, b int = 10, 20
376
377		func main() {
378			fmt.Printf("%+v\n", add(a, b))
379		}
380
381		func sub(a, b int) int {
382			return pkga.Sub(a, b)
383		}
384		`)
385		fileA = []byte(`package main
386
387		func add(a, b int) int {
388			return a + b
389		}
390
391		func mul(a, b int) int {
392			return a * b
393		}
394		`)
395		mainTestFile = []byte(`package main
396
397		import (
398			"testing"
399		)
400
401		func TestAdd(t *testing.T) {
402			if add(3, 4) != 7 {
403				t.Error("add unexpected result")
404			}
405		}
406
407		func TestMul(t *testing.T) {
408			if mul(10, 5) != 50 {
409				t.Error("mul unexpected result")
410			}
411		}
412		`)
413
414		pkgAFileA = []byte(`package pkga
415
416		func Div(a, b int) int {
417			return a / b
418		}
419
420		func Sub(a, b int) int {
421			return a - b
422		}
423		func Test(a, b int) bool {
424			return a > b
425		}
426		`)
427		pkgATestFile = []byte(`package pkga
428
429		import (
430			"testing"
431		)
432
433		func TestDiv(t *testing.T) {
434			if Div(3, 2) != 1 {
435				t.Error("Div unexpected result")
436			}
437		}
438
439		func TestSub(t *testing.T) {
440			if Sub(10, 5) != 5 {
441				t.Error("Sub unexpected result")
442			}
443		}
444                type TT struct {}
445        	func (t *TT) TestNot(a int) int {
446                        return a
447		}
448		`)
449	)
450
451	// setup
452	testDir := filepath.Join(os.TempDir(), "test_find_all_tests_in_dir")
453
454	pkgAFileAPath := filepath.Join("pkga", "file_a.go")
455	pkgATestFileAPath := filepath.Join("pkga", "file_a_test.go")
456
457	files := map[string][]byte{
458		"go.mod":  gomod,
459		"main.go": mainFile, "file_a.go": fileA, "main_test.go": mainTestFile,
460		pkgAFileAPath: pkgAFileA, pkgATestFileAPath: pkgATestFile,
461	}
462	setup := func() {
463		setupTestGitDir(t,
464			testDir, files,
465			[]string{
466				"go.mod", "main.go", "main_test.go", "file_a.go",
467				pkgAFileAPath, pkgATestFileAPath,
468			},
469		)
470	}
471	// teardown
472	defer func() {
473		if !t.Failed() {
474			// clean tmp dir on test success
475			_ = os.RemoveAll(testDir)
476		}
477	}()
478	cases := []struct {
479		desc            string
480		setup, tearDown func() error
481		outTests        []string
482		err             error
483	}{
484		{
485			desc: "Find all test which are runnable by go test",
486			outTests: []string{"find-all.tests.TestAdd",
487				"find-all.tests.TestMul",
488				"find-all.tests/pkga.TestDiv",
489				"find-all.tests/pkga.TestSub"},
490		},
491	}
492	setup()
493	for i, tc := range cases {
494		// setup()
495		execTestHelper(t, i, tc.desc, tc.setup)
496		tests, err := findAllTestInDir(context.Background(), moduleName, testDir)
497		// teardown()
498		execTestHelper(t, i, tc.desc, tc.tearDown)
499		if isUnexpectedErr(t, i, tc.desc, tc.err, err) {
500			continue
501		}
502		// TODO test packages are handled
503		sort.Strings(tc.outTests)
504		sort.Strings(tests)
505		if !reflect.DeepEqual(tc.outTests, tests) {
506			t.Errorf("case [%d] %s\nexpected Tests %+v\ngot %+v", i, tc.desc, tc.outTests, tests)
507		}
508
509	}
510
511}
512
Full Screen

main.go

Source: main.go Github

copy
1// Copyright © 2018 Inanc Gumus
2// Learn Go Programming Course
3// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
4//
5// For more tutorials  : https://learngoprogramming.com
6// In-person training  : https://www.linkedin.com/in/inancgumus/
7// Follow me on twitter: https://twitter.com/inancgumus
8
9package main
10
11import (
12	"fmt"
13	"os"
14	"strings"
15	"time"
16)
17
18// ---------------------------------------------------------
19// EXERCISE: Days in a Month
20//
21//  Print the number of days in a given month.
22//
23// RESTRICTIONS
24//  1. On a leap year, february should print 29. Otherwise, 28.
25//
26//     Set your computer clock to 2020 to see whether it works.
27//
28//  2. It should work case-insensitive. See below.
29//
30//     Search on Google: golang pkg strings ToLower
31//
32//  3. Get the current year using the time.Now()
33//
34//     Search on Google: golang pkg time now year
35//
36//
37// EXPECTED OUTPUT
38//
39//  -----------------------------------------
40//  Your solution should not accept invalid months
41//  -----------------------------------------
42//  go run main.go
43//    Give me a month name
44//
45//  go run main.go sheep
46//    "sheep" is not a month.
47//
48//  -----------------------------------------
49//  Your solution should handle the leap years
50//  -----------------------------------------
51//  go run main.go january
52//    "january" has 31 days.
53//
54//  go run main.go february
55//    "february" has 28 days.
56//
57//  go run main.go march
58//    "march" has 31 days.
59//
60//  go run main.go april
61//    "april" has 30 days.
62//
63//  go run main.go may
64//    "may" has 31 days.
65//
66//  go run main.go june
67//    "june" has 30 days.
68//
69//  go run main.go july
70//    "july" has 31 days.
71//
72//  go run main.go august
73//    "august" has 31 days.
74//
75//  go run main.go september
76//    "september" has 30 days.
77//
78//  go run main.go october
79//    "october" has 31 days.
80//
81//  go run main.go november
82//    "november" has 30 days.
83//
84//  go run main.go december
85//    "december" has 31 days.
86//
87//  -----------------------------------------
88//  Your solution should be case insensitive
89//  -----------------------------------------
90//  go run main.go DECEMBER
91//    "DECEMBER" has 31 days.
92//
93//  go run main.go dEcEmBeR
94//    "dEcEmBeR" has 31 days.
95// ---------------------------------------------------------
96
97func main() {
98	a := os.Args
99	l := len(a)
100	y := time.Now().Year()
101
102	if l < 2 {
103		fmt.Println("Give me a month name.")
104	}
105	numberDays := 28
106	leapYear := y%4 == 0 && (y%100 != 0 || y%400 != 0)
107
108	if m := strings.ToLower(a[1]); m == "april" ||
109		m == "june" ||
110		m == "september" ||
111		m == "november" {
112		numberDays = 30
113	} else if m == "january" ||
114		m == "march" ||
115		m == "may" ||
116		m == "july" ||
117		m == "august" ||
118		m == "october" ||
119		m == "december" {
120		numberDays = 31
121	} else if m == "february" && leapYear {
122		numberDays = 29
123	} else {
124		fmt.Printf("%q is not a month.\n", a[1])
125		return
126	}
127	fmt.Printf("%q has %d days .\n", a[1], numberDays)
128}
129
Full Screen

run.go

Source: run.go Github

copy
1package pixelgl
2
3import (
4	"github.com/faiface/mainthread"
5	"github.com/go-gl/glfw/v3.3/glfw"
6	"github.com/pkg/errors"
7)
8
9// Run is essentially the main function of PixelGL. It exists mainly due to the technical
10// limitations of OpenGL and operating systems. In short, all graphics and window manipulating calls
11// must be done from the main thread. Run makes this possible.
12//
13// Call this function from the main function of your application. This is necessary, so that Run
14// runs on the main thread.
15//
16//   func run() {
17//       // interact with Pixel and PixelGL from here (even concurrently)
18//   }
19//
20//   func main() {
21//       pixel.Run(run)
22//   }
23//
24// You can spawn any number of goroutines from your run function and interact with PixelGL
25// concurrently. The only condition is that the Run function is called from your main function.
26func Run(run func()) {
27	err := glfw.Init()
28	if err != nil {
29		panic(errors.Wrap(err, "failed to initialize GLFW"))
30	}
31	defer glfw.Terminate()
32	mainthread.Run(run)
33}
34
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)