How to use NewEnumSupport method of types Package

Best Ginkgo code snippet using types.NewEnumSupport

Run Ginkgo automation tests on LambdaTest cloud grid

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

enum_support.go

Source: enum_support.go Github

copy
1package types
2
3import "encoding/json"
4
5type EnumSupport struct {
6	toString map[uint]string
7	toEnum   map[string]uint
8	maxEnum  uint
9}
10
11func NewEnumSupport(toString map[uint]string) EnumSupport {
12	toEnum, maxEnum := map[string]uint{}, uint(0)
13	for k, v := range toString {
14		toEnum[v] = k
15		if maxEnum < k {
16			maxEnum = k
17		}
18	}
19	return EnumSupport{toString: toString, toEnum: toEnum, maxEnum: maxEnum}
20}
21
22func (es EnumSupport) String(e uint) string {
23	if e > es.maxEnum {
24		return es.toString[0]
25	}
26	return es.toString[e]
27}
28
29func (es EnumSupport) UnmarshJSON(b []byte) (uint, error) {
30	var dec string
31	if err := json.Unmarshal(b, &dec); err != nil {
32		return 0, err
33	}
34	out := es.toEnum[dec] // if we miss we get 0 which is what we want anyway
35	return out, nil
36}
37
38func (es EnumSupport) MarshJSON(e uint) ([]byte, error) {
39	if e == 0 || e > es.maxEnum {
40		return json.Marshal(nil)
41	}
42	return json.Marshal(es.toString[e])
43}
44
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 NewEnumSupport code on LambdaTest Cloud Grid

Execute automation tests with NewEnumSupport 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)