How to use isNil method of got Package

Best Got code snippet using got.isNil

schema_test.go

Source:schema_test.go Github

copy

Full Screen

1// Copyright 2011, 2012, 2013 Canonical Ltd.2// Licensed under the AGPLv3, see LICENCE file for details.3package schema_test4import (5 "math"6 "testing"7 . "launchpad.net/gocheck"8 "launchpad.net/juju-core/schema"9)10func Test(t *testing.T) {11 TestingT(t)12}13type S struct{}14var _ = Suite(&S{})15type Dummy struct{}16func (d *Dummy) Coerce(value interface{}, path []string) (coerced interface{}, err error) {17 return "i-am-dummy", nil18}19var aPath = []string{"<pa", "th>"}20func (s *S) TestConst(c *C) {21 sch := schema.Const("foo")22 out, err := sch.Coerce("foo", aPath)23 c.Assert(err, IsNil)24 c.Assert(out, Equals, "foo")25 out, err = sch.Coerce(42, aPath)26 c.Assert(out, IsNil)27 c.Assert(err, ErrorMatches, `<path>: expected "foo", got 42`)28 out, err = sch.Coerce(nil, aPath)29 c.Assert(out, IsNil)30 c.Assert(err, ErrorMatches, `<path>: expected "foo", got nothing`)31}32func (s *S) TestAny(c *C) {33 sch := schema.Any()34 out, err := sch.Coerce("foo", aPath)35 c.Assert(err, IsNil)36 c.Assert(out, Equals, "foo")37 out, err = sch.Coerce(nil, aPath)38 c.Assert(err, IsNil)39 c.Assert(out, Equals, nil)40}41func (s *S) TestOneOf(c *C) {42 sch := schema.OneOf(schema.Const("foo"), schema.Const(42))43 out, err := sch.Coerce("foo", aPath)44 c.Assert(err, IsNil)45 c.Assert(out, Equals, "foo")46 out, err = sch.Coerce(42, aPath)47 c.Assert(err, IsNil)48 c.Assert(out, Equals, 42)49 out, err = sch.Coerce("bar", aPath)50 c.Assert(out, IsNil)51 c.Assert(err, ErrorMatches, `<path>: unexpected value "bar"`)52}53func (s *S) TestBool(c *C) {54 sch := schema.Bool()55 out, err := sch.Coerce(true, aPath)56 c.Assert(err, IsNil)57 c.Assert(out, Equals, true)58 out, err = sch.Coerce(false, aPath)59 c.Assert(err, IsNil)60 c.Assert(out, Equals, false)61 out, err = sch.Coerce(1, aPath)62 c.Assert(out, IsNil)63 c.Assert(err, ErrorMatches, "<path>: expected bool, got 1")64 out, err = sch.Coerce(nil, aPath)65 c.Assert(out, IsNil)66 c.Assert(err, ErrorMatches, "<path>: expected bool, got nothing")67}68func (s *S) TestInt(c *C) {69 sch := schema.Int()70 out, err := sch.Coerce(42, aPath)71 c.Assert(err, IsNil)72 c.Assert(out, Equals, int64(42))73 out, err = sch.Coerce(int8(42), aPath)74 c.Assert(err, IsNil)75 c.Assert(out, Equals, int64(42))76 out, err = sch.Coerce(true, aPath)77 c.Assert(out, IsNil)78 c.Assert(err, ErrorMatches, "<path>: expected int, got true")79 out, err = sch.Coerce(nil, aPath)80 c.Assert(out, IsNil)81 c.Assert(err, ErrorMatches, "<path>: expected int, got nothing")82}83func (s *S) TestForceInt(c *C) {84 sch := schema.ForceInt()85 out, err := sch.Coerce(42, aPath)86 c.Assert(err, IsNil)87 c.Assert(out, Equals, int(42))88 out, err = sch.Coerce(int8(42), aPath)89 c.Assert(err, IsNil)90 c.Assert(out, Equals, int(42))91 out, err = sch.Coerce(float32(42), aPath)92 c.Assert(err, IsNil)93 c.Assert(out, Equals, int(42))94 out, err = sch.Coerce(float64(42), aPath)95 c.Assert(err, IsNil)96 c.Assert(out, Equals, int(42))97 out, err = sch.Coerce(42.66, aPath)98 c.Assert(err, IsNil)99 c.Assert(out, Equals, int(42))100 // If an out of range value is provided, that value is truncated,101 // generating unexpected results, but no error is raised.102 out, err = sch.Coerce(float64(math.MaxInt64+1), aPath)103 c.Assert(err, IsNil)104 out, err = sch.Coerce(true, aPath)105 c.Assert(out, IsNil)106 c.Assert(err, ErrorMatches, "<path>: expected number, got true")107 out, err = sch.Coerce(nil, aPath)108 c.Assert(out, IsNil)109 c.Assert(err, ErrorMatches, "<path>: expected number, got nothing")110}111func (s *S) TestFloat(c *C) {112 sch := schema.Float()113 out, err := sch.Coerce(float32(1.0), aPath)114 c.Assert(err, IsNil)115 c.Assert(out, Equals, float64(1.0))116 out, err = sch.Coerce(float64(1.0), aPath)117 c.Assert(err, IsNil)118 c.Assert(out, Equals, float64(1.0))119 out, err = sch.Coerce(true, aPath)120 c.Assert(out, IsNil)121 c.Assert(err, ErrorMatches, "<path>: expected float, got true")122 out, err = sch.Coerce(nil, aPath)123 c.Assert(out, IsNil)124 c.Assert(err, ErrorMatches, "<path>: expected float, got nothing")125}126func (s *S) TestString(c *C) {127 sch := schema.String()128 out, err := sch.Coerce("foo", aPath)129 c.Assert(err, IsNil)130 c.Assert(out, Equals, "foo")131 out, err = sch.Coerce(true, aPath)132 c.Assert(out, IsNil)133 c.Assert(err, ErrorMatches, "<path>: expected string, got true")134 out, err = sch.Coerce(nil, aPath)135 c.Assert(out, IsNil)136 c.Assert(err, ErrorMatches, "<path>: expected string, got nothing")137}138func (s *S) TestSimpleRegexp(c *C) {139 sch := schema.SimpleRegexp()140 out, err := sch.Coerce("[0-9]+", aPath)141 c.Assert(err, IsNil)142 c.Assert(out, Equals, "[0-9]+")143 out, err = sch.Coerce(1, aPath)144 c.Assert(out, IsNil)145 c.Assert(err, ErrorMatches, "<path>: expected regexp string, got 1")146 out, err = sch.Coerce("[", aPath)147 c.Assert(out, IsNil)148 c.Assert(err, ErrorMatches, `<path>: expected valid regexp, got "\["`)149 out, err = sch.Coerce(nil, aPath)150 c.Assert(out, IsNil)151 c.Assert(err, ErrorMatches, `<path>: expected regexp string, got nothing`)152}153func (s *S) TestList(c *C) {154 sch := schema.List(schema.Int())155 out, err := sch.Coerce([]int8{1, 2}, aPath)156 c.Assert(err, IsNil)157 c.Assert(out, DeepEquals, []interface{}{int64(1), int64(2)})158 out, err = sch.Coerce(42, aPath)159 c.Assert(out, IsNil)160 c.Assert(err, ErrorMatches, "<path>: expected list, got 42")161 out, err = sch.Coerce(nil, aPath)162 c.Assert(out, IsNil)163 c.Assert(err, ErrorMatches, "<path>: expected list, got nothing")164 out, err = sch.Coerce([]interface{}{1, true}, aPath)165 c.Assert(out, IsNil)166 c.Assert(err, ErrorMatches, `<path>\[1\]: expected int, got true`)167}168func (s *S) TestMap(c *C) {169 sch := schema.Map(schema.String(), schema.Int())170 out, err := sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)171 c.Assert(err, IsNil)172 c.Assert(out, DeepEquals, map[interface{}]interface{}{"a": int64(1), "b": int64(2)})173 out, err = sch.Coerce(42, aPath)174 c.Assert(out, IsNil)175 c.Assert(err, ErrorMatches, "<path>: expected map, got 42")176 out, err = sch.Coerce(nil, aPath)177 c.Assert(out, IsNil)178 c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")179 out, err = sch.Coerce(map[int]int{1: 1}, aPath)180 c.Assert(out, IsNil)181 c.Assert(err, ErrorMatches, "<path>: expected string, got 1")182 out, err = sch.Coerce(map[string]bool{"a": true}, aPath)183 c.Assert(out, IsNil)184 c.Assert(err, ErrorMatches, `<path>\.a: expected int, got true`)185 // First path entry shouldn't have dots in an error message.186 out, err = sch.Coerce(map[string]bool{"a": true}, nil)187 c.Assert(out, IsNil)188 c.Assert(err, ErrorMatches, `a: expected int, got true`)189}190func (s *S) TestStringMap(c *C) {191 sch := schema.StringMap(schema.Int())192 out, err := sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)193 c.Assert(err, IsNil)194 c.Assert(out, DeepEquals, map[string]interface{}{"a": int64(1), "b": int64(2)})195 out, err = sch.Coerce(42, aPath)196 c.Assert(out, IsNil)197 c.Assert(err, ErrorMatches, "<path>: expected map, got 42")198 out, err = sch.Coerce(nil, aPath)199 c.Assert(out, IsNil)200 c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")201 out, err = sch.Coerce(map[int]int{1: 1}, aPath)202 c.Assert(out, IsNil)203 c.Assert(err, ErrorMatches, "<path>: expected string, got 1")204 out, err = sch.Coerce(map[string]bool{"a": true}, aPath)205 c.Assert(out, IsNil)206 c.Assert(err, ErrorMatches, `<path>\.a: expected int, got true`)207 // First path entry shouldn't have dots in an error message.208 out, err = sch.Coerce(map[string]bool{"a": true}, nil)209 c.Assert(out, IsNil)210 c.Assert(err, ErrorMatches, `a: expected int, got true`)211}212func assertFieldMap(c *C, sch schema.Checker) {213 out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B"}, aPath)214 c.Assert(err, IsNil)215 c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "b": "B", "c": "C"})216 out, err = sch.Coerce(42, aPath)217 c.Assert(out, IsNil)218 c.Assert(err, ErrorMatches, "<path>: expected map, got 42")219 out, err = sch.Coerce(nil, aPath)220 c.Assert(out, IsNil)221 c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")222 out, err = sch.Coerce(map[string]interface{}{"a": "A", "b": "C"}, aPath)223 c.Assert(out, IsNil)224 c.Assert(err, ErrorMatches, `<path>\.b: expected "B", got "C"`)225 out, err = sch.Coerce(map[string]interface{}{"b": "B"}, aPath)226 c.Assert(out, IsNil)227 c.Assert(err, ErrorMatches, `<path>\.a: expected "A", got nothing`)228 // b is optional229 out, err = sch.Coerce(map[string]interface{}{"a": "A"}, aPath)230 c.Assert(err, IsNil)231 c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "c": "C"})232 // First path entry shouldn't have dots in an error message.233 out, err = sch.Coerce(map[string]bool{"a": true}, nil)234 c.Assert(out, IsNil)235 c.Assert(err, ErrorMatches, `a: expected "A", got true`)236}237func (s *S) TestFieldMap(c *C) {238 fields := schema.Fields{239 "a": schema.Const("A"),240 "b": schema.Const("B"),241 "c": schema.Const("C"),242 }243 defaults := schema.Defaults{244 "b": schema.Omit,245 "c": "C",246 }247 sch := schema.FieldMap(fields, defaults)248 assertFieldMap(c, sch)249 out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)250 c.Assert(err, IsNil)251 c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "b": "B", "c": "C"})252 out, err = sch.Coerce(map[string]interface{}{"a": "A", "d": "D"}, aPath)253 c.Assert(err, IsNil)254 c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "c": "C"})255}256func (s *S) TestFieldMapDefaultInvalid(c *C) {257 fields := schema.Fields{258 "a": schema.Const("A"),259 }260 defaults := schema.Defaults{261 "a": "B",262 }263 sch := schema.FieldMap(fields, defaults)264 _, err := sch.Coerce(map[string]interface{}{}, aPath)265 c.Assert(err, ErrorMatches, `<path>.a: expected "A", got "B"`)266}267func (s *S) TestStrictFieldMap(c *C) {268 fields := schema.Fields{269 "a": schema.Const("A"),270 "b": schema.Const("B"),271 "c": schema.Const("C"),272 }273 defaults := schema.Defaults{274 "b": schema.Omit,275 "c": "C",276 }277 sch := schema.StrictFieldMap(fields, defaults)278 assertFieldMap(c, sch)279 out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)280 c.Assert(out, IsNil)281 c.Assert(err, ErrorMatches, `<path>.d: expected nothing, got "D"`)282}283func (s *S) TestSchemaMap(c *C) {284 fields1 := schema.FieldMap(schema.Fields{285 "type": schema.Const(1),286 "a": schema.Const(2),287 }, nil)288 fields2 := schema.FieldMap(schema.Fields{289 "type": schema.Const(3),290 "b": schema.Const(4),291 }, nil)292 sch := schema.FieldMapSet("type", []schema.Checker{fields1, fields2})293 out, err := sch.Coerce(map[string]int{"type": 1, "a": 2}, aPath)294 c.Assert(err, IsNil)295 c.Assert(out, DeepEquals, map[string]interface{}{"type": 1, "a": 2})296 out, err = sch.Coerce(map[string]int{"type": 3, "b": 4}, aPath)297 c.Assert(err, IsNil)298 c.Assert(out, DeepEquals, map[string]interface{}{"type": 3, "b": 4})299 out, err = sch.Coerce(map[string]int{}, aPath)300 c.Assert(out, IsNil)301 c.Assert(err, ErrorMatches, `<path>\.type: expected supported selector, got nothing`)302 out, err = sch.Coerce(map[string]int{"type": 2}, aPath)303 c.Assert(out, IsNil)304 c.Assert(err, ErrorMatches, `<path>\.type: expected supported selector, got 2`)305 out, err = sch.Coerce(map[string]int{"type": 3, "b": 5}, aPath)306 c.Assert(out, IsNil)307 c.Assert(err, ErrorMatches, `<path>\.b: expected 4, got 5`)308 out, err = sch.Coerce(42, aPath)309 c.Assert(out, IsNil)310 c.Assert(err, ErrorMatches, `<path>: expected map, got 42`)311 out, err = sch.Coerce(nil, aPath)312 c.Assert(out, IsNil)313 c.Assert(err, ErrorMatches, `<path>: expected map, got nothing`)314 // First path entry shouldn't have dots in an error message.315 out, err = sch.Coerce(map[string]int{"a": 1}, nil)316 c.Assert(out, IsNil)317 c.Assert(err, ErrorMatches, `type: expected supported selector, got nothing`)318}...

Full Screen

Full Screen

isNil

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if a == nil {4 fmt.Println("a is nil")5 } else {6 fmt.Println("a is not nil")7 }8 fmt.Println(reflect.ValueOf(a).IsNil())9}

Full Screen

Full Screen

isNil

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if reflect.ValueOf(a).IsNil() {4 fmt.Println("a is nil")5 } else {6 fmt.Println("a is not nil")7 }8}

Full Screen

Full Screen

isNil

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(reflect.ValueOf(a).IsNil())4 fmt.Println(reflect.ValueOf(b).IsNil())5 fmt.Println(reflect.ValueOf(c).IsNil())6 fmt.Println(reflect.ValueOf(d).IsNil())7}

Full Screen

Full Screen

isNil

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if a == nil {4 fmt.Println("a is nil")5 } else {6 fmt.Println("a is not nil")7 }8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful