Best Got code snippet using got.Each
expander_test.go
Source:expander_test.go
...11 // Some module and resource addresses and values we'll use repeatedly below.12 singleModuleAddr := addrs.ModuleCall{Name: "single"}13 count2ModuleAddr := addrs.ModuleCall{Name: "count2"}14 count0ModuleAddr := addrs.ModuleCall{Name: "count0"}15 forEachModuleAddr := addrs.ModuleCall{Name: "for_each"}16 singleResourceAddr := addrs.Resource{17 Mode: addrs.ManagedResourceMode,18 Type: "test",19 Name: "single",20 }21 count2ResourceAddr := addrs.Resource{22 Mode: addrs.ManagedResourceMode,23 Type: "test",24 Name: "count2",25 }26 count0ResourceAddr := addrs.Resource{27 Mode: addrs.ManagedResourceMode,28 Type: "test",29 Name: "count0",30 }31 forEachResourceAddr := addrs.Resource{32 Mode: addrs.ManagedResourceMode,33 Type: "test",34 Name: "for_each",35 }36 eachMap := map[string]cty.Value{37 "a": cty.NumberIntVal(1),38 "b": cty.NumberIntVal(2),39 }40 // In normal use, Expander would be called in the context of a graph41 // traversal to ensure that information is registered/requested in the42 // correct sequence, but to keep this test self-contained we'll just43 // manually write out the steps here.44 //45 // The steps below are assuming a configuration tree like the following:46 // - root module47 // - resource test.single with no count or for_each48 // - resource test.count2 with count = 249 // - resource test.count0 with count = 050 // - resource test.for_each with for_each = { a = 1, b = 2 }51 // - child module "single" with no count or for_each52 // - resource test.single with no count or for_each53 // - resource test.count2 with count = 254 // - child module "count2" with count = 255 // - resource test.single with no count or for_each56 // - resource test.count2 with count = 257 // - child module "count2" with count = 258 // - resource test.count2 with count = 259 // - child module "count0" with count = 060 // - resource test.single with no count or for_each61 // - child module for_each with for_each = { a = 1, b = 2 }62 // - resource test.single with no count or for_each63 // - resource test.count2 with count = 264 ex := NewExpander()65 // We don't register the root module, because it's always implied to exist.66 //67 // Below we're going to use braces and indentation just to help visually68 // reflect the tree structure from the tree in the above comment, in the69 // hope that the following is easier to follow.70 //71 // The Expander API requires that we register containing modules before72 // registering anything inside them, so we'll work through the above73 // in a depth-first order in the registration steps that follow.74 {75 ex.SetResourceSingle(addrs.RootModuleInstance, singleResourceAddr)76 ex.SetResourceCount(addrs.RootModuleInstance, count2ResourceAddr, 2)77 ex.SetResourceCount(addrs.RootModuleInstance, count0ResourceAddr, 0)78 ex.SetResourceForEach(addrs.RootModuleInstance, forEachResourceAddr, eachMap)79 ex.SetModuleSingle(addrs.RootModuleInstance, singleModuleAddr)80 {81 // The single instance of the module82 moduleInstanceAddr := addrs.RootModuleInstance.Child("single", addrs.NoKey)83 ex.SetResourceSingle(moduleInstanceAddr, singleResourceAddr)84 ex.SetResourceCount(moduleInstanceAddr, count2ResourceAddr, 2)85 }86 ex.SetModuleCount(addrs.RootModuleInstance, count2ModuleAddr, 2)87 for i1 := 0; i1 < 2; i1++ {88 moduleInstanceAddr := addrs.RootModuleInstance.Child("count2", addrs.IntKey(i1))89 ex.SetResourceSingle(moduleInstanceAddr, singleResourceAddr)90 ex.SetResourceCount(moduleInstanceAddr, count2ResourceAddr, 2)91 ex.SetModuleCount(moduleInstanceAddr, count2ModuleAddr, 2)92 for i2 := 0; i2 < 2; i2++ {93 moduleInstanceAddr := moduleInstanceAddr.Child("count2", addrs.IntKey(i2))94 ex.SetResourceCount(moduleInstanceAddr, count2ResourceAddr, 2)95 }96 }97 ex.SetModuleCount(addrs.RootModuleInstance, count0ModuleAddr, 0)98 {99 // There are no instances of module "count0", so our nested module100 // would never actually get registered here: the expansion node101 // for the resource would see that its containing module has no102 // instances and so do nothing.103 }104 ex.SetModuleForEach(addrs.RootModuleInstance, forEachModuleAddr, eachMap)105 for k := range eachMap {106 moduleInstanceAddr := addrs.RootModuleInstance.Child("for_each", addrs.StringKey(k))107 ex.SetResourceSingle(moduleInstanceAddr, singleResourceAddr)108 ex.SetResourceCount(moduleInstanceAddr, count2ResourceAddr, 2)109 }110 }111 t.Run("root module", func(t *testing.T) {112 // Requesting expansion of the root module doesn't really mean anything113 // since it's always a singleton, but for consistency it should work.114 got := ex.ExpandModule(addrs.RootModule)115 want := []addrs.ModuleInstance{addrs.RootModuleInstance}116 if diff := cmp.Diff(want, got); diff != "" {117 t.Errorf("wrong result\n%s", diff)118 }119 })120 t.Run("resource single", func(t *testing.T) {121 got := ex.ExpandModuleResource(122 addrs.RootModule,123 singleResourceAddr,124 )125 want := []addrs.AbsResourceInstance{126 mustAbsResourceInstanceAddr(`test.single`),127 }128 if diff := cmp.Diff(want, got); diff != "" {129 t.Errorf("wrong result\n%s", diff)130 }131 })132 t.Run("resource count2", func(t *testing.T) {133 got := ex.ExpandModuleResource(134 addrs.RootModule,135 count2ResourceAddr,136 )137 want := []addrs.AbsResourceInstance{138 mustAbsResourceInstanceAddr(`test.count2[0]`),139 mustAbsResourceInstanceAddr(`test.count2[1]`),140 }141 if diff := cmp.Diff(want, got); diff != "" {142 t.Errorf("wrong result\n%s", diff)143 }144 })145 t.Run("resource count0", func(t *testing.T) {146 got := ex.ExpandModuleResource(147 addrs.RootModule,148 count0ResourceAddr,149 )150 want := []addrs.AbsResourceInstance(nil)151 if diff := cmp.Diff(want, got); diff != "" {152 t.Errorf("wrong result\n%s", diff)153 }154 })155 t.Run("resource for_each", func(t *testing.T) {156 got := ex.ExpandModuleResource(157 addrs.RootModule,158 forEachResourceAddr,159 )160 want := []addrs.AbsResourceInstance{161 mustAbsResourceInstanceAddr(`test.for_each["a"]`),162 mustAbsResourceInstanceAddr(`test.for_each["b"]`),163 }164 if diff := cmp.Diff(want, got); diff != "" {165 t.Errorf("wrong result\n%s", diff)166 }167 })168 t.Run("module single", func(t *testing.T) {169 got := ex.ExpandModule(addrs.RootModule.Child("single"))170 want := []addrs.ModuleInstance{171 mustModuleInstanceAddr(`module.single`),172 }173 if diff := cmp.Diff(want, got); diff != "" {174 t.Errorf("wrong result\n%s", diff)175 }176 })177 t.Run("module single resource single", func(t *testing.T) {178 got := ex.ExpandModuleResource(179 mustModuleAddr("single"),180 singleResourceAddr,181 )182 want := []addrs.AbsResourceInstance{183 mustAbsResourceInstanceAddr("module.single.test.single"),184 }185 if diff := cmp.Diff(want, got); diff != "" {186 t.Errorf("wrong result\n%s", diff)187 }188 })189 t.Run("module single resource count2", func(t *testing.T) {190 // Two different ways of asking the same question, which should191 // both produce the same result.192 // First: nested expansion of all instances of the resource across193 // all instances of the module, but it's a single-instance module194 // so the first level is a singleton.195 got1 := ex.ExpandModuleResource(196 mustModuleAddr(`single`),197 count2ResourceAddr,198 )199 // Second: expansion of only instances belonging to a specific200 // instance of the module, but again it's a single-instance module201 // so there's only one to ask about.202 got2 := ex.ExpandResource(203 count2ResourceAddr.Absolute(204 addrs.RootModuleInstance.Child("single", addrs.NoKey),205 ),206 )207 want := []addrs.AbsResourceInstance{208 mustAbsResourceInstanceAddr(`module.single.test.count2[0]`),209 mustAbsResourceInstanceAddr(`module.single.test.count2[1]`),210 }211 if diff := cmp.Diff(want, got1); diff != "" {212 t.Errorf("wrong ExpandModuleResource result\n%s", diff)213 }214 if diff := cmp.Diff(want, got2); diff != "" {215 t.Errorf("wrong ExpandResource result\n%s", diff)216 }217 })218 t.Run("module single resource count2 with non-existing module instance", func(t *testing.T) {219 got := ex.ExpandResource(220 count2ResourceAddr.Absolute(221 // Note: This is intentionally an invalid instance key,222 // so we're asking about module.single[1].test.count2223 // even though module.single doesn't have count set and224 // therefore there is no module.single[1].225 addrs.RootModuleInstance.Child("single", addrs.IntKey(1)),226 ),227 )228 // If the containing module instance doesn't exist then it can't229 // possibly have any resource instances inside it.230 want := ([]addrs.AbsResourceInstance)(nil)231 if diff := cmp.Diff(want, got); diff != "" {232 t.Errorf("wrong result\n%s", diff)233 }234 })235 t.Run("module count2", func(t *testing.T) {236 got := ex.ExpandModule(mustModuleAddr(`count2`))237 want := []addrs.ModuleInstance{238 mustModuleInstanceAddr(`module.count2[0]`),239 mustModuleInstanceAddr(`module.count2[1]`),240 }241 if diff := cmp.Diff(want, got); diff != "" {242 t.Errorf("wrong result\n%s", diff)243 }244 })245 t.Run("module count2 resource single", func(t *testing.T) {246 got := ex.ExpandModuleResource(247 mustModuleAddr(`count2`),248 singleResourceAddr,249 )250 want := []addrs.AbsResourceInstance{251 mustAbsResourceInstanceAddr(`module.count2[0].test.single`),252 mustAbsResourceInstanceAddr(`module.count2[1].test.single`),253 }254 if diff := cmp.Diff(want, got); diff != "" {255 t.Errorf("wrong result\n%s", diff)256 }257 })258 t.Run("module count2 resource count2", func(t *testing.T) {259 got := ex.ExpandModuleResource(260 mustModuleAddr(`count2`),261 count2ResourceAddr,262 )263 want := []addrs.AbsResourceInstance{264 mustAbsResourceInstanceAddr(`module.count2[0].test.count2[0]`),265 mustAbsResourceInstanceAddr(`module.count2[0].test.count2[1]`),266 mustAbsResourceInstanceAddr(`module.count2[1].test.count2[0]`),267 mustAbsResourceInstanceAddr(`module.count2[1].test.count2[1]`),268 }269 if diff := cmp.Diff(want, got); diff != "" {270 t.Errorf("wrong result\n%s", diff)271 }272 })273 t.Run("module count2 module count2", func(t *testing.T) {274 got := ex.ExpandModule(mustModuleAddr(`count2.count2`))275 want := []addrs.ModuleInstance{276 mustModuleInstanceAddr(`module.count2[0].module.count2[0]`),277 mustModuleInstanceAddr(`module.count2[0].module.count2[1]`),278 mustModuleInstanceAddr(`module.count2[1].module.count2[0]`),279 mustModuleInstanceAddr(`module.count2[1].module.count2[1]`),280 }281 if diff := cmp.Diff(want, got); diff != "" {282 t.Errorf("wrong result\n%s", diff)283 }284 })285 t.Run("module count2 module count2 GetDeepestExistingModuleInstance", func(t *testing.T) {286 t.Run("first step invalid", func(t *testing.T) {287 got := ex.GetDeepestExistingModuleInstance(mustModuleInstanceAddr(`module.count2["nope"].module.count2[0]`))288 want := addrs.RootModuleInstance289 if !want.Equal(got) {290 t.Errorf("wrong result\ngot: %s\nwant: %s", got, want)291 }292 })293 t.Run("second step invalid", func(t *testing.T) {294 got := ex.GetDeepestExistingModuleInstance(mustModuleInstanceAddr(`module.count2[1].module.count2`))295 want := mustModuleInstanceAddr(`module.count2[1]`)296 if !want.Equal(got) {297 t.Errorf("wrong result\ngot: %s\nwant: %s", got, want)298 }299 })300 t.Run("neither step valid", func(t *testing.T) {301 got := ex.GetDeepestExistingModuleInstance(mustModuleInstanceAddr(`module.count2.module.count2["nope"]`))302 want := addrs.RootModuleInstance303 if !want.Equal(got) {304 t.Errorf("wrong result\ngot: %s\nwant: %s", got, want)305 }306 })307 t.Run("both steps valid", func(t *testing.T) {308 got := ex.GetDeepestExistingModuleInstance(mustModuleInstanceAddr(`module.count2[1].module.count2[0]`))309 want := mustModuleInstanceAddr(`module.count2[1].module.count2[0]`)310 if !want.Equal(got) {311 t.Errorf("wrong result\ngot: %s\nwant: %s", got, want)312 }313 })314 })315 t.Run("module count2 resource count2 resource count2", func(t *testing.T) {316 got := ex.ExpandModuleResource(317 mustModuleAddr(`count2.count2`),318 count2ResourceAddr,319 )320 want := []addrs.AbsResourceInstance{321 mustAbsResourceInstanceAddr(`module.count2[0].module.count2[0].test.count2[0]`),322 mustAbsResourceInstanceAddr(`module.count2[0].module.count2[0].test.count2[1]`),323 mustAbsResourceInstanceAddr(`module.count2[0].module.count2[1].test.count2[0]`),324 mustAbsResourceInstanceAddr(`module.count2[0].module.count2[1].test.count2[1]`),325 mustAbsResourceInstanceAddr(`module.count2[1].module.count2[0].test.count2[0]`),326 mustAbsResourceInstanceAddr(`module.count2[1].module.count2[0].test.count2[1]`),327 mustAbsResourceInstanceAddr(`module.count2[1].module.count2[1].test.count2[0]`),328 mustAbsResourceInstanceAddr(`module.count2[1].module.count2[1].test.count2[1]`),329 }330 if diff := cmp.Diff(want, got); diff != "" {331 t.Errorf("wrong result\n%s", diff)332 }333 })334 t.Run("module count2 resource count2 resource count2", func(t *testing.T) {335 got := ex.ExpandResource(336 count2ResourceAddr.Absolute(mustModuleInstanceAddr(`module.count2[0].module.count2[1]`)),337 )338 want := []addrs.AbsResourceInstance{339 mustAbsResourceInstanceAddr(`module.count2[0].module.count2[1].test.count2[0]`),340 mustAbsResourceInstanceAddr(`module.count2[0].module.count2[1].test.count2[1]`),341 }342 if diff := cmp.Diff(want, got); diff != "" {343 t.Errorf("wrong result\n%s", diff)344 }345 })346 t.Run("module count0", func(t *testing.T) {347 got := ex.ExpandModule(mustModuleAddr(`count0`))348 want := []addrs.ModuleInstance(nil)349 if diff := cmp.Diff(want, got); diff != "" {350 t.Errorf("wrong result\n%s", diff)351 }352 })353 t.Run("module count0 resource single", func(t *testing.T) {354 got := ex.ExpandModuleResource(355 mustModuleAddr(`count0`),356 singleResourceAddr,357 )358 // The containing module has zero instances, so therefore there359 // are zero instances of this resource even though it doesn't have360 // count = 0 set itself.361 want := []addrs.AbsResourceInstance(nil)362 if diff := cmp.Diff(want, got); diff != "" {363 t.Errorf("wrong result\n%s", diff)364 }365 })366 t.Run("module for_each", func(t *testing.T) {367 got := ex.ExpandModule(mustModuleAddr(`for_each`))368 want := []addrs.ModuleInstance{369 mustModuleInstanceAddr(`module.for_each["a"]`),370 mustModuleInstanceAddr(`module.for_each["b"]`),371 }372 if diff := cmp.Diff(want, got); diff != "" {373 t.Errorf("wrong result\n%s", diff)374 }375 })376 t.Run("module for_each resource single", func(t *testing.T) {377 got := ex.ExpandModuleResource(378 mustModuleAddr(`for_each`),379 singleResourceAddr,380 )381 want := []addrs.AbsResourceInstance{382 mustAbsResourceInstanceAddr(`module.for_each["a"].test.single`),383 mustAbsResourceInstanceAddr(`module.for_each["b"].test.single`),384 }385 if diff := cmp.Diff(want, got); diff != "" {386 t.Errorf("wrong result\n%s", diff)387 }388 })389 t.Run("module for_each resource count2", func(t *testing.T) {390 got := ex.ExpandModuleResource(391 mustModuleAddr(`for_each`),392 count2ResourceAddr,393 )394 want := []addrs.AbsResourceInstance{395 mustAbsResourceInstanceAddr(`module.for_each["a"].test.count2[0]`),396 mustAbsResourceInstanceAddr(`module.for_each["a"].test.count2[1]`),397 mustAbsResourceInstanceAddr(`module.for_each["b"].test.count2[0]`),398 mustAbsResourceInstanceAddr(`module.for_each["b"].test.count2[1]`),399 }400 if diff := cmp.Diff(want, got); diff != "" {401 t.Errorf("wrong result\n%s", diff)402 }403 })404 t.Run("module for_each resource count2", func(t *testing.T) {405 got := ex.ExpandResource(406 count2ResourceAddr.Absolute(mustModuleInstanceAddr(`module.for_each["a"]`)),407 )408 want := []addrs.AbsResourceInstance{409 mustAbsResourceInstanceAddr(`module.for_each["a"].test.count2[0]`),410 mustAbsResourceInstanceAddr(`module.for_each["a"].test.count2[1]`),411 }412 if diff := cmp.Diff(want, got); diff != "" {413 t.Errorf("wrong result\n%s", diff)414 }415 })416 t.Run(`module.for_each["b"] repetitiondata`, func(t *testing.T) {417 got := ex.GetModuleInstanceRepetitionData(418 mustModuleInstanceAddr(`module.for_each["b"]`),419 )420 want := RepetitionData{421 EachKey: cty.StringVal("b"),422 EachValue: cty.NumberIntVal(2),423 }424 if diff := cmp.Diff(want, got, cmp.Comparer(valueEquals)); diff != "" {425 t.Errorf("wrong result\n%s", diff)426 }427 })428 t.Run(`module.count2[0].module.count2[1] repetitiondata`, func(t *testing.T) {429 got := ex.GetModuleInstanceRepetitionData(430 mustModuleInstanceAddr(`module.count2[0].module.count2[1]`),431 )432 want := RepetitionData{433 CountIndex: cty.NumberIntVal(1),434 }435 if diff := cmp.Diff(want, got, cmp.Comparer(valueEquals)); diff != "" {436 t.Errorf("wrong result\n%s", diff)437 }438 })439 t.Run(`module.for_each["a"] repetitiondata`, func(t *testing.T) {440 got := ex.GetModuleInstanceRepetitionData(441 mustModuleInstanceAddr(`module.for_each["a"]`),442 )443 want := RepetitionData{444 EachKey: cty.StringVal("a"),445 EachValue: cty.NumberIntVal(1),446 }447 if diff := cmp.Diff(want, got, cmp.Comparer(valueEquals)); diff != "" {448 t.Errorf("wrong result\n%s", diff)449 }450 })451 t.Run(`test.for_each["a"] repetitiondata`, func(t *testing.T) {452 got := ex.GetResourceInstanceRepetitionData(453 mustAbsResourceInstanceAddr(`test.for_each["a"]`),454 )455 want := RepetitionData{456 EachKey: cty.StringVal("a"),457 EachValue: cty.NumberIntVal(1),458 }459 if diff := cmp.Diff(want, got, cmp.Comparer(valueEquals)); diff != "" {460 t.Errorf("wrong result\n%s", diff)461 }462 })463 t.Run(`module.for_each["a"].test.single repetitiondata`, func(t *testing.T) {464 got := ex.GetResourceInstanceRepetitionData(465 mustAbsResourceInstanceAddr(`module.for_each["a"].test.single`),466 )467 want := RepetitionData{}468 if diff := cmp.Diff(want, got, cmp.Comparer(valueEquals)); diff != "" {469 t.Errorf("wrong result\n%s", diff)470 }471 })...
immutable_test.go
Source:immutable_test.go
...27 t.Fatalf("Get: unexpected result - got %x, want nil", gotVal)28 }29 // Ensure there are no panics when deleting keys from an empty treap.30 testTreap.Delete(key)31 // Ensure the number of keys iterated by ForEach on an empty treap is32 // zero.33 var numIterated int34 testTreap.ForEach(func(k, v []byte) bool {35 numIterated++36 return true37 },38 )39 if numIterated != 0 {40 t.Fatalf("ForEach: unexpected iterate count - got %d, want 0",41 numIterated,42 )43 }44}45// TestImmutableSequential ensures that putting keys into an immutable treap in sequential order works as expected.46func TestImmutableSequential(t *testing.T) {47 t.Parallel()48 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.49 expectedSize := uint64(0)50 numItems := 100051 testTreap := NewImmutable()52 for i := 0; i < numItems; i++ {53 key := serializeUint32(uint32(i))54 testTreap = testTreap.Put(key, key)55 // Ensure the treap length is the expected value.56 if gotLen := testTreap.Len(); gotLen != i+1 {57 t.Fatalf("Len #%d: unexpected length - got %d, want %d",58 i, gotLen, i+1,59 )60 }61 // Ensure the treap has the key.62 if !testTreap.Has(key) {63 t.Fatalf("Has #%d: key %q is not in treap", i, key)64 }65 // Get the key from the treap and ensure it is the expected value.66 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {67 t.Fatalf("Get #%d: unexpected value - got %x, want %x",68 i, gotVal, key,69 )70 }71 // Ensure the expected size is reported.72 expectedSize += nodeFieldsSize + 873 if gotSize := testTreap.Size(); gotSize != expectedSize {74 t.Fatalf("Size #%d: unexpected byte size - got %d, "+75 "want %d", i, gotSize, expectedSize,76 )77 }78 }79 // Ensure the all keys are iterated by ForEach in order.80 var numIterated int81 testTreap.ForEach(func(k, v []byte) bool {82 wantKey := serializeUint32(uint32(numIterated))83 // Ensure the key is as expected.84 if !bytes.Equal(k, wantKey) {85 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",86 numIterated, k, wantKey,87 )88 }89 // Ensure the value is as expected.90 if !bytes.Equal(v, wantKey) {91 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",92 numIterated, v, wantKey,93 )94 }95 numIterated++96 return true97 },98 )99 // Ensure all items were iterated.100 if numIterated != numItems {101 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",102 numIterated, numItems,103 )104 }105 // Delete the keys one-by-one while checking several of the treap functions work as expected.106 for i := 0; i < numItems; i++ {107 key := serializeUint32(uint32(i))108 testTreap = testTreap.Delete(key)109 // Ensure the treap length is the expected value.110 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {111 t.Fatalf("Len #%d: unexpected length - got %d, want %d",112 i, gotLen, numItems-i-1,113 )114 }115 // Ensure the treap no longer has the key.116 if testTreap.Has(key) {117 t.Fatalf("Has #%d: key %q is in treap", i, key)118 }119 // Get the key that no longer exists from the treap and ensure it is nil.120 if gotVal := testTreap.Get(key); gotVal != nil {121 t.Fatalf("Get #%d: unexpected value - got %x, want nil",122 i, gotVal,123 )124 }125 // Ensure the expected size is reported.126 expectedSize -= nodeFieldsSize + 8127 if gotSize := testTreap.Size(); gotSize != expectedSize {128 t.Fatalf("Size #%d: unexpected byte size - got %d, "+129 "want %d", i, gotSize, expectedSize,130 )131 }132 }133}134// TestImmutableReverseSequential ensures that putting keys into an immutable treap in reverse sequential order works as135// expected.136func TestImmutableReverseSequential(t *testing.T) {137 t.Parallel()138 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.139 expectedSize := uint64(0)140 numItems := 1000141 testTreap := NewImmutable()142 for i := 0; i < numItems; i++ {143 key := serializeUint32(uint32(numItems - i - 1))144 testTreap = testTreap.Put(key, key)145 // Ensure the treap length is the expected value.146 if gotLen := testTreap.Len(); gotLen != i+1 {147 t.Fatalf("Len #%d: unexpected length - got %d, want %d",148 i, gotLen, i+1,149 )150 }151 // Ensure the treap has the key.152 if !testTreap.Has(key) {153 t.Fatalf("Has #%d: key %q is not in treap", i, key)154 }155 // Get the key from the treap and ensure it is the expected156 // value.157 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {158 t.Fatalf("Get #%d: unexpected value - got %x, want %x",159 i, gotVal, key,160 )161 }162 // Ensure the expected size is reported.163 expectedSize += nodeFieldsSize + 8164 if gotSize := testTreap.Size(); gotSize != expectedSize {165 t.Fatalf("Size #%d: unexpected byte size - got %d, "+166 "want %d", i, gotSize, expectedSize,167 )168 }169 }170 // Ensure the all keys are iterated by ForEach in order.171 var numIterated int172 testTreap.ForEach(func(k, v []byte) bool {173 wantKey := serializeUint32(uint32(numIterated))174 // Ensure the key is as expected.175 if !bytes.Equal(k, wantKey) {176 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",177 numIterated, k, wantKey,178 )179 }180 // Ensure the value is as expected.181 if !bytes.Equal(v, wantKey) {182 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",183 numIterated, v, wantKey,184 )185 }186 numIterated++187 return true188 },189 )190 // Ensure all items were iterated.191 if numIterated != numItems {192 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",193 numIterated, numItems,194 )195 }196 // Delete the keys one-by-one while checking several of the treap functions work as expected.197 for i := 0; i < numItems; i++ {198 // Intentionally use the reverse order they were inserted here.199 key := serializeUint32(uint32(i))200 testTreap = testTreap.Delete(key)201 // Ensure the treap length is the expected value.202 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {203 t.Fatalf("Len #%d: unexpected length - got %d, want %d",204 i, gotLen, numItems-i-1,205 )206 }207 // Ensure the treap no longer has the key.208 if testTreap.Has(key) {209 t.Fatalf("Has #%d: key %q is in treap", i, key)210 }211 // Get the key that no longer exists from the treap and ensure212 // it is nil.213 if gotVal := testTreap.Get(key); gotVal != nil {214 t.Fatalf("Get #%d: unexpected value - got %x, want nil",215 i, gotVal,216 )217 }218 // Ensure the expected size is reported.219 expectedSize -= nodeFieldsSize + 8220 if gotSize := testTreap.Size(); gotSize != expectedSize {221 t.Fatalf("Size #%d: unexpected byte size - got %d, "+222 "want %d", i, gotSize, expectedSize,223 )224 }225 }226}227// TestImmutableUnordered ensures that putting keys into an immutable treap in no particular order works as expected.228func TestImmutableUnordered(t *testing.T) {229 t.Parallel()230 // Insert a bunch of out-of-order keys while checking several of the treap functions work as expected.231 expectedSize := uint64(0)232 numItems := 1000233 testTreap := NewImmutable()234 for i := 0; i < numItems; i++ {235 // Hash the serialized int to generate out-of-order keys.236 hash := sha256.Sum256(serializeUint32(uint32(i)))237 key := hash[:]238 testTreap = testTreap.Put(key, key)239 // Ensure the treap length is the expected value.240 if gotLen := testTreap.Len(); gotLen != i+1 {241 t.Fatalf("Len #%d: unexpected length - got %d, want %d",242 i, gotLen, i+1,243 )244 }245 // Ensure the treap has the key.246 if !testTreap.Has(key) {247 t.Fatalf("Has #%d: key %q is not in treap", i, key)248 }249 // Get the key from the treap and ensure it is the expected value.250 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {251 t.Fatalf("Get #%d: unexpected value - got %x, want %x",252 i, gotVal, key,253 )254 }255 // Ensure the expected size is reported.256 expectedSize += nodeFieldsSize + uint64(len(key)+len(key))257 if gotSize := testTreap.Size(); gotSize != expectedSize {258 t.Fatalf("Size #%d: unexpected byte size - got %d, "+259 "want %d", i, gotSize, expectedSize,260 )261 }262 }263 // Delete the keys one-by-one while checking several of the treap functions work as expected.264 for i := 0; i < numItems; i++ {265 // Hash the serialized int to generate out-of-order keys.266 hash := sha256.Sum256(serializeUint32(uint32(i)))267 key := hash[:]268 testTreap = testTreap.Delete(key)269 // Ensure the treap length is the expected value.270 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {271 t.Fatalf("Len #%d: unexpected length - got %d, want %d",272 i, gotLen, numItems-i-1,273 )274 }275 // Ensure the treap no longer has the key.276 if testTreap.Has(key) {277 t.Fatalf("Has #%d: key %q is in treap", i, key)278 }279 // Get the key that no longer exists from the treap and ensure it is nil.280 if gotVal := testTreap.Get(key); gotVal != nil {281 t.Fatalf("Get #%d: unexpected value - got %x, want nil",282 i, gotVal,283 )284 }285 // Ensure the expected size is reported.286 expectedSize -= nodeFieldsSize + 64287 if gotSize := testTreap.Size(); gotSize != expectedSize {288 t.Fatalf("Size #%d: unexpected byte size - got %d, "+289 "want %d", i, gotSize, expectedSize,290 )291 }292 }293}294// TestImmutableDuplicatePut ensures that putting a duplicate key into an immutable treap works as expected.295func TestImmutableDuplicatePut(t *testing.T) {296 t.Parallel()297 expectedVal := []byte("testval")298 expectedSize := uint64(0)299 numItems := 1000300 testTreap := NewImmutable()301 for i := 0; i < numItems; i++ {302 key := serializeUint32(uint32(i))303 testTreap = testTreap.Put(key, key)304 expectedSize += nodeFieldsSize + uint64(len(key)+len(key))305 // Put a duplicate key with the the expected final value.306 testTreap = testTreap.Put(key, expectedVal)307 // Ensure the key still exists and is the new value.308 if gotVal := testTreap.Has(key); !gotVal {309 t.Fatalf("Has: unexpected result - got %v, want true",310 gotVal,311 )312 }313 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, expectedVal) {314 t.Fatalf("Get: unexpected result - got %x, want %x",315 gotVal, expectedVal,316 )317 }318 // Ensure the expected size is reported.319 expectedSize -= uint64(len(key))320 expectedSize += uint64(len(expectedVal))321 if gotSize := testTreap.Size(); gotSize != expectedSize {322 t.Fatalf("Size: unexpected byte size - got %d, want %d",323 gotSize, expectedSize,324 )325 }326 }327}328// TestImmutableNilValue ensures that putting a nil value into an immutable treap results in a key being added with an329// empty byte slice.330func TestImmutableNilValue(t *testing.T) {331 t.Parallel()332 key := serializeUint32(0)333 // Put the key with a nil value.334 testTreap := NewImmutable()335 testTreap = testTreap.Put(key, nil)336 // Ensure the key exists and is an empty byte slice.337 if gotVal := testTreap.Has(key); !gotVal {338 t.Fatalf("Has: unexpected result - got %v, want true", gotVal)339 }340 if gotVal := testTreap.Get(key); gotVal == nil {341 t.Fatalf("Get: unexpected result - got nil, want empty slice")342 }343 if gotVal := testTreap.Get(key); len(gotVal) != 0 {344 t.Fatalf("Get: unexpected result - got %x, want empty slice",345 gotVal,346 )347 }348}349// TestImmutableForEachStopIterator ensures that returning false from the ForEach callback on an immutable treap stops350// iteration early.351func TestImmutableForEachStopIterator(t *testing.T) {352 t.Parallel()353 // Insert a few keys.354 numItems := 10355 testTreap := NewImmutable()356 for i := 0; i < numItems; i++ {357 key := serializeUint32(uint32(i))358 testTreap = testTreap.Put(key, key)359 }360 // Ensure ForEach exits early on false return by caller.361 var numIterated int362 testTreap.ForEach(func(k, v []byte) bool {363 numIterated++364 return numIterated != numItems/2365 },366 )367 if numIterated != numItems/2 {368 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",369 numIterated, numItems/2,370 )371 }372}373// TestImmutableSnapshot ensures that immutable treaps are actually immutable by keeping a reference to the previous374// treap, performing a mutation, and then ensuring the referenced treap does not have the mutation applied.375func TestImmutableSnapshot(t *testing.T) {376 t.Parallel()377 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.378 expectedSize := uint64(0)379 numItems := 1000380 testTreap := NewImmutable()381 for i := 0; i < numItems; i++ {382 treapSnap := testTreap...
mutable_test.go
Source:mutable_test.go
...28 t.Fatalf("Get: unexpected result - got %x, want nil", gotVal)29 }30 // Ensure there are no panics when deleting keys from an empty treap.31 testTreap.Delete(key)32 // Ensure the number of keys iterated by ForEach on an empty treap is33 // zero.34 var numIterated int35 testTreap.ForEach(func(k, v []byte) bool {36 numIterated++37 return true38 },39 )40 if numIterated != 0 {41 t.Fatalf("ForEach: unexpected iterate count - got %d, want 0",42 numIterated,43 )44 }45}46// TestMutableReset ensures that resetting an existing mutable treap works as expected.47func TestMutableReset(t *testing.T) {48 t.Parallel()49 // Insert a few keys.50 numItems := 1051 testTreap := NewMutable()52 for i := 0; i < numItems; i++ {53 key := serializeUint32(uint32(i))54 testTreap.Put(key, key)55 }56 // Reset it.57 testTreap.Reset()58 // Ensure the treap length is now 0.59 if gotLen := testTreap.Len(); gotLen != 0 {60 t.Fatalf("Len: unexpected length - got %d, want %d", gotLen, 0)61 }62 // Ensure the reported size is now 0.63 if gotSize := testTreap.Size(); gotSize != 0 {64 t.Fatalf("Size: unexpected byte size - got %d, want 0",65 gotSize,66 )67 }68 // Ensure the treap no longer has any of the keys.69 for i := 0; i < numItems; i++ {70 key := serializeUint32(uint32(i))71 // Ensure the treap no longer has the key.72 if testTreap.Has(key) {73 t.Fatalf("Has #%d: key %q is in treap", i, key)74 }75 // Get the key that no longer exists from the treap and ensure76 // it is nil.77 if gotVal := testTreap.Get(key); gotVal != nil {78 t.Fatalf("Get #%d: unexpected value - got %x, want nil",79 i, gotVal,80 )81 }82 }83 // Ensure the number of keys iterated by ForEach is zero.84 var numIterated int85 testTreap.ForEach(func(k, v []byte) bool {86 numIterated++87 return true88 },89 )90 if numIterated != 0 {91 t.Fatalf("ForEach: unexpected iterate count - got %d, want 0",92 numIterated,93 )94 }95}96// TestMutableSequential ensures that putting keys into a mutable treap in sequential order works as expected.97func TestMutableSequential(t *testing.T) {98 t.Parallel()99 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.100 expectedSize := uint64(0)101 numItems := 1000102 testTreap := NewMutable()103 for i := 0; i < numItems; i++ {104 key := serializeUint32(uint32(i))105 testTreap.Put(key, key)106 // Ensure the treap length is the expected value.107 if gotLen := testTreap.Len(); gotLen != i+1 {108 t.Fatalf("Len #%d: unexpected length - got %d, want %d",109 i, gotLen, i+1,110 )111 }112 // Ensure the treap has the key.113 if !testTreap.Has(key) {114 t.Fatalf("Has #%d: key %q is not in treap", i, key)115 }116 // Get the key from the treap and ensure it is the expected value.117 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {118 t.Fatalf("Get #%d: unexpected value - got %x, want %x",119 i, gotVal, key,120 )121 }122 // Ensure the expected size is reported.123 expectedSize += nodeFieldsSize + 8124 if gotSize := testTreap.Size(); gotSize != expectedSize {125 t.Fatalf("Size #%d: unexpected byte size - got %d, "+126 "want %d", i, gotSize, expectedSize,127 )128 }129 }130 // Ensure the all keys are iterated by ForEach in order.131 var numIterated int132 testTreap.ForEach(func(k, v []byte) bool {133 wantKey := serializeUint32(uint32(numIterated))134 // Ensure the key is as expected.135 if !bytes.Equal(k, wantKey) {136 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",137 numIterated, k, wantKey,138 )139 }140 // Ensure the value is as expected.141 if !bytes.Equal(v, wantKey) {142 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",143 numIterated, v, wantKey,144 )145 }146 numIterated++147 return true148 },149 )150 // Ensure all items were iterated.151 if numIterated != numItems {152 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",153 numIterated, numItems,154 )155 }156 // Delete the keys one-by-one while checking several of the treap functions work as expected.157 for i := 0; i < numItems; i++ {158 key := serializeUint32(uint32(i))159 testTreap.Delete(key)160 // Ensure the treap length is the expected value.161 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {162 t.Fatalf("Len #%d: unexpected length - got %d, want %d",163 i, gotLen, numItems-i-1,164 )165 }166 // Ensure the treap no longer has the key.167 if testTreap.Has(key) {168 t.Fatalf("Has #%d: key %q is in treap", i, key)169 }170 // Get the key that no longer exists from the treap and ensure it is nil.171 if gotVal := testTreap.Get(key); gotVal != nil {172 t.Fatalf("Get #%d: unexpected value - got %x, want nil",173 i, gotVal,174 )175 }176 // Ensure the expected size is reported.177 expectedSize -= nodeFieldsSize + 8178 if gotSize := testTreap.Size(); gotSize != expectedSize {179 t.Fatalf("Size #%d: unexpected byte size - got %d, "+180 "want %d", i, gotSize, expectedSize,181 )182 }183 }184}185// TestMutableReverseSequential ensures that putting keys into a mutable treap in reverse sequential order works as186// expected.187func TestMutableReverseSequential(t *testing.T) {188 t.Parallel()189 // Insert a bunch of sequential keys while checking several of the treap functions work as expected.190 expectedSize := uint64(0)191 numItems := 1000192 testTreap := NewMutable()193 for i := 0; i < numItems; i++ {194 key := serializeUint32(uint32(numItems - i - 1))195 testTreap.Put(key, key)196 // Ensure the treap length is the expected value.197 if gotLen := testTreap.Len(); gotLen != i+1 {198 t.Fatalf("Len #%d: unexpected length - got %d, want %d",199 i, gotLen, i+1,200 )201 }202 // Ensure the treap has the key.203 if !testTreap.Has(key) {204 t.Fatalf("Has #%d: key %q is not in treap", i, key)205 }206 // Get the key from the treap and ensure it is the expected value.207 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {208 t.Fatalf("Get #%d: unexpected value - got %x, want %x",209 i, gotVal, key,210 )211 }212 // Ensure the expected size is reported.213 expectedSize += nodeFieldsSize + 8214 if gotSize := testTreap.Size(); gotSize != expectedSize {215 t.Fatalf("Size #%d: unexpected byte size - got %d, "+216 "want %d", i, gotSize, expectedSize,217 )218 }219 }220 // Ensure the all keys are iterated by ForEach in order.221 var numIterated int222 testTreap.ForEach(func(k, v []byte) bool {223 wantKey := serializeUint32(uint32(numIterated))224 // Ensure the key is as expected.225 if !bytes.Equal(k, wantKey) {226 t.Fatalf("ForEach #%d: unexpected key - got %x, want %x",227 numIterated, k, wantKey,228 )229 }230 // Ensure the value is as expected.231 if !bytes.Equal(v, wantKey) {232 t.Fatalf("ForEach #%d: unexpected value - got %x, want %x",233 numIterated, v, wantKey,234 )235 }236 numIterated++237 return true238 },239 )240 // Ensure all items were iterated.241 if numIterated != numItems {242 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",243 numIterated, numItems,244 )245 }246 // Delete the keys one-by-one while checking several of the treap functions work as expected.247 for i := 0; i < numItems; i++ {248 // Intentionally use the reverse order they were inserted here.249 key := serializeUint32(uint32(i))250 testTreap.Delete(key)251 // Ensure the treap length is the expected value.252 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {253 t.Fatalf("Len #%d: unexpected length - got %d, want %d",254 i, gotLen, numItems-i-1,255 )256 }257 // Ensure the treap no longer has the key.258 if testTreap.Has(key) {259 t.Fatalf("Has #%d: key %q is in treap", i, key)260 }261 // Get the key that no longer exists from the treap and ensure it is nil.262 if gotVal := testTreap.Get(key); gotVal != nil {263 t.Fatalf("Get #%d: unexpected value - got %x, want nil",264 i, gotVal,265 )266 }267 // Ensure the expected size is reported.268 expectedSize -= nodeFieldsSize + 8269 if gotSize := testTreap.Size(); gotSize != expectedSize {270 t.Fatalf("Size #%d: unexpected byte size - got %d, "+271 "want %d", i, gotSize, expectedSize,272 )273 }274 }275}276// TestMutableUnordered ensures that putting keys into a mutable treap in no particular order works as expected.277func TestMutableUnordered(t *testing.T) {278 t.Parallel()279 // Insert a bunch of out-of-order keys while checking several of the treap functions work as expected.280 expectedSize := uint64(0)281 numItems := 1000282 testTreap := NewMutable()283 for i := 0; i < numItems; i++ {284 // Hash the serialized int to generate out-of-order keys.285 hash := sha256.Sum256(serializeUint32(uint32(i)))286 key := hash[:]287 testTreap.Put(key, key)288 // Ensure the treap length is the expected value.289 if gotLen := testTreap.Len(); gotLen != i+1 {290 t.Fatalf("Len #%d: unexpected length - got %d, want %d",291 i, gotLen, i+1,292 )293 }294 // Ensure the treap has the key.295 if !testTreap.Has(key) {296 t.Fatalf("Has #%d: key %q is not in treap", i, key)297 }298 // Get the key from the treap and ensure it is the expected299 // value.300 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, key) {301 t.Fatalf("Get #%d: unexpected value - got %x, want %x",302 i, gotVal, key,303 )304 }305 // Ensure the expected size is reported.306 expectedSize += nodeFieldsSize + uint64(len(key)+len(key))307 if gotSize := testTreap.Size(); gotSize != expectedSize {308 t.Fatalf("Size #%d: unexpected byte size - got %d, "+309 "want %d", i, gotSize, expectedSize,310 )311 }312 }313 // Delete the keys one-by-one while checking several of the treap functions work as expected.314 for i := 0; i < numItems; i++ {315 // Hash the serialized int to generate out-of-order keys.316 hash := sha256.Sum256(serializeUint32(uint32(i)))317 key := hash[:]318 testTreap.Delete(key)319 // Ensure the treap length is the expected value.320 if gotLen := testTreap.Len(); gotLen != numItems-i-1 {321 t.Fatalf("Len #%d: unexpected length - got %d, want %d",322 i, gotLen, numItems-i-1,323 )324 }325 // Ensure the treap no longer has the key.326 if testTreap.Has(key) {327 t.Fatalf("Has #%d: key %q is in treap", i, key)328 }329 // Get the key that no longer exists from the treap and ensure it is nil.330 if gotVal := testTreap.Get(key); gotVal != nil {331 t.Fatalf("Get #%d: unexpected value - got %x, want nil",332 i, gotVal,333 )334 }335 // Ensure the expected size is reported.336 expectedSize -= nodeFieldsSize + 64337 if gotSize := testTreap.Size(); gotSize != expectedSize {338 t.Fatalf("Size #%d: unexpected byte size - got %d, "+339 "want %d", i, gotSize, expectedSize,340 )341 }342 }343}344// TestMutableDuplicatePut ensures that putting a duplicate key into a mutable treap updates the existing value.345func TestMutableDuplicatePut(t *testing.T) {346 t.Parallel()347 key := serializeUint32(0)348 val := []byte("testval")349 // Put the key twice with the second put being the expected final value.350 testTreap := NewMutable()351 testTreap.Put(key, key)352 testTreap.Put(key, val)353 // Ensure the key still exists and is the new value.354 if gotVal := testTreap.Has(key); !gotVal {355 t.Fatalf("Has: unexpected result - got %v, want true", gotVal)356 }357 if gotVal := testTreap.Get(key); !bytes.Equal(gotVal, val) {358 t.Fatalf("Get: unexpected result - got %x, want %x", gotVal, val)359 }360 // Ensure the expected size is reported.361 expectedSize := uint64(nodeFieldsSize + len(key) + len(val))362 if gotSize := testTreap.Size(); gotSize != expectedSize {363 t.Fatalf("Size: unexpected byte size - got %d, want %d",364 gotSize, expectedSize,365 )366 }367}368// TestMutableNilValue ensures that putting a nil value into a mutable treap results in a key being added with an empty369// byte slice.370func TestMutableNilValue(t *testing.T) {371 t.Parallel()372 key := serializeUint32(0)373 // Put the key with a nil value.374 testTreap := NewMutable()375 testTreap.Put(key, nil)376 // Ensure the key exists and is an empty byte slice.377 if gotVal := testTreap.Has(key); !gotVal {378 t.Fatalf("Has: unexpected result - got %v, want true", gotVal)379 }380 if gotVal := testTreap.Get(key); gotVal == nil {381 t.Fatalf("Get: unexpected result - got nil, want empty slice")382 }383 if gotVal := testTreap.Get(key); len(gotVal) != 0 {384 t.Fatalf("Get: unexpected result - got %x, want empty slice",385 gotVal,386 )387 }388}389// TestMutableForEachStopIterator ensures that returning false from the ForEach callback of a mutable treap stops390// iteration early.391func TestMutableForEachStopIterator(t *testing.T) {392 t.Parallel()393 // Insert a few keys.394 numItems := 10395 testTreap := NewMutable()396 for i := 0; i < numItems; i++ {397 key := serializeUint32(uint32(i))398 testTreap.Put(key, key)399 }400 // Ensure ForEach exits early on false return by caller.401 var numIterated int402 testTreap.ForEach(func(k, v []byte) bool {403 numIterated++404 return numIterated != numItems/2405 },406 )407 if numIterated != numItems/2 {408 t.Fatalf("ForEach: unexpected iterate count - got %d, want %d",409 numIterated, numItems/2,410 )411 }412}
Each
Using AI Code Generation
1import (2func main() {3 f, err := excelize.OpenFile("Book1.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 rows, err := f.GetRows("Sheet1")8 if err != nil {9 fmt.Println(err)10 }11 cols, err := f.GetCols("Sheet1")12 if err != nil {13 fmt.Println(err)14 }15 fmt.Println(cols)
Each
Using AI Code Generation
1import (2func main() {3 xlsx, err := excelize.OpenFile("Book1.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 rows := xlsx.GetRows("Sheet1")8 for _, row := range rows {9 for _, colCell := range row {10 fmt.Print(colCell, "\t")11 }12 fmt.Println()13 }14}
Each
Using AI Code Generation
1import (2func main() {3 xlsx := excelize.NewFile()4 index := xlsx.NewSheet("Sheet2")5 xlsx.SetCellValue("Sheet1", "A1", "Hello world.")6 xlsx.SetCellValue("Sheet2", "A2", "Hello world.")7 xlsx.SetActiveSheet(index)8 xlsx.SaveAs("Book1.xlsx")9 rows := xlsx.GetRows("Sheet1")10 for _, row := range rows {11 for _, colCell := range row {12 fmt.Print(colCell, "\t")13 }14 fmt.Println()15 }16}
Each
Using AI Code Generation
1import (2func main() {3 xlFile, err := xlsx.OpenFile(excelFileName)4 if err != nil {5 fmt.Println(err)6 }7 for _, sheet := range xlFile.Sheets {8 fmt.Println(sheet.Name)9 for _, row := range sheet.Rows {10 for _, cell := range row.Cells {11 text := cell.String()12 fmt.Printf("%s\n", text)13 }14 }15 }16}
Each
Using AI Code Generation
1import (2func main() {3 f, err := excelize.OpenFile("Book1.xlsx")4 if err != nil {5 log.Fatal(err)6 }7 rows, err := f.GetRows("Sheet1")8 for _, row := range rows {9 fmt.Println(row)10 }11}
Each
Using AI Code Generation
1import (2func main() {3 f, err := excelize.OpenFile("Book1.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 rows := f.GetRows("Sheet1")8 for _, row := range rows {9 for _, colCell := range row {10 fmt.Print(colCell, "\t")11 }12 fmt.Println()13 }14}
Each
Using AI Code Generation
1import (2func main() {3 f, err := excelize.OpenFile("Book1.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 rows, err := f.GetRows("Sheet1")8 for _, row := range rows {9 for _, colCell := range row {10 fmt.Print(colCell, "\t")11 }12 fmt.Println()13 }14}
Each
Using AI Code Generation
1import (2func main() {3 xlsx, err := excelize.OpenFile("Book1.xlsx")4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 rows := xlsx.GetRows("Sheet1")9 for _, row := range rows {10 for _, colCell := range row {11 fmt.Print(colCell, "\t")12 }13 fmt.Println()14 }15}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!