Best Go-testdeep code snippet using td.First
parse.go
Source:parse.go
...66 // Loop through each cell67 s.Find("td").Each(func(int int, s *goquery.Selection) {68 switch int {69 case 0:70 p.Year = strings.TrimSpace(s.Find("span").First().Contents().Text())71 case 1:72 p.Land = strings.TrimSpace(s.Find("span").First().Contents().Text())73 case 2:74 p.BuildingImprovement = strings.TrimSpace(s.Find("span").First().Contents().Text())75 case 3:76 p.JustMarketValue = strings.TrimSpace(s.Find("span").First().Contents().Text())77 case 4:78 p.AssessedSOHValue = strings.TrimSpace(s.Find("span").First().Contents().Text())79 case 5:80 p.Tax = strings.TrimSpace(s.Find("span").First().Contents().Text())81 }82 })83 return p84}85//LoadAppendPropertyAssessments used to load and append Assessments to the BCPA parent node calls PropertyAssessmentRecord86func LoadAppendPropertyAssessments(doc *goquery.Document, _bcpa *model.Bcpa) {87 doc.Find("body > table:nth-child(3) > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(1) > table:nth-child(6) > tbody > tr").Each(func(i int, s *goquery.Selection) {88 if i > 1 {89 pa := PropertyAssessmentRecord(s)90 pa.CreatedAt = time.Now()91 _bcpa.PropertyAssessments = append(_bcpa.PropertyAssessments, pa)92 }93 })94}95// ExemptionsTaxableRecord parse exemptions called by LoadAppendExemptionsTaxable96func ExemptionsTaxableRecord(s *goquery.Selection, i int, eta model.ExemptionsTaxableValuesbyTaxingAuthority) model.ExemptionsTaxableValuesbyTaxingAuthority {97 // Loop through each cell98 s.Find("td").Each(func(int int, s *goquery.Selection) {99 switch i {100 case 2:101 switch int {102 case 1:103 eta.County.JustValue = strings.TrimSpace(s.Find("span").First().Contents().Text())104 case 2:105 eta.SchoolBoard.JustValue = strings.TrimSpace(s.Find("span").First().Contents().Text())106 case 3:107 eta.Municipal.JustValue = strings.TrimSpace(s.Find("span").First().Contents().Text())108 case 4:109 eta.Independent.JustValue = strings.TrimSpace(s.Find("span").First().Contents().Text())110 }111 case 3:112 switch int {113 case 1:114 eta.County.Portability = strings.TrimSpace(s.Find("span").First().Contents().Text())115 case 2:116 eta.SchoolBoard.Portability = strings.TrimSpace(s.Find("span").First().Contents().Text())117 case 3:118 eta.Municipal.Portability = strings.TrimSpace(s.Find("span").First().Contents().Text())119 case 4:120 eta.Independent.Portability = strings.TrimSpace(s.Find("span").First().Contents().Text())121 }122 case 4:123 switch int {124 case 1:125 eta.County.AssessedSOH = strings.TrimSpace(s.Find("span").First().Contents().Text())126 case 2:127 eta.SchoolBoard.AssessedSOH = strings.TrimSpace(s.Find("span").First().Contents().Text())128 case 3:129 eta.Municipal.AssessedSOH = strings.TrimSpace(s.Find("span").First().Contents().Text())130 case 4:131 eta.Independent.AssessedSOH = strings.TrimSpace(s.Find("span").First().Contents().Text())132 }133 case 5:134 switch int {135 case 1:136 eta.County.Homestead = strings.TrimSpace(s.Find("span").First().Contents().Text())137 case 2:138 eta.SchoolBoard.Homestead = strings.TrimSpace(s.Find("span").First().Contents().Text())139 case 3:140 eta.Municipal.Homestead = strings.TrimSpace(s.Find("span").First().Contents().Text())141 case 4:142 eta.Independent.Homestead = strings.TrimSpace(s.Find("span").First().Contents().Text())143 }144 case 6:145 switch int {146 case 1:147 eta.County.AddHomestead = strings.TrimSpace(s.Find("span").First().Contents().Text())148 case 2:149 eta.SchoolBoard.AddHomestead = strings.TrimSpace(s.Find("span").First().Contents().Text())150 case 3:151 eta.Municipal.AddHomestead = strings.TrimSpace(s.Find("span").First().Contents().Text())152 case 4:153 eta.Independent.AddHomestead = strings.TrimSpace(s.Find("span").First().Contents().Text())154 }155 case 7:156 switch int {157 case 1:158 eta.County.WidVetDis = strings.TrimSpace(s.Find("span").First().Contents().Text())159 case 2:160 eta.SchoolBoard.WidVetDis = strings.TrimSpace(s.Find("span").First().Contents().Text())161 case 3:162 eta.Municipal.WidVetDis = strings.TrimSpace(s.Find("span").First().Contents().Text())163 case 4:164 eta.Independent.WidVetDis = strings.TrimSpace(s.Find("span").First().Contents().Text())165 }166 case 8:167 switch int {168 case 1:169 eta.County.Senior = strings.TrimSpace(s.Find("span").First().Contents().Text())170 case 2:171 eta.SchoolBoard.Senior = strings.TrimSpace(s.Find("span").First().Contents().Text())172 case 3:173 eta.Municipal.Senior = strings.TrimSpace(s.Find("span").First().Contents().Text())174 case 4:175 eta.Independent.Senior = strings.TrimSpace(s.Find("span").First().Contents().Text())176 }177 case 9:178 switch int {179 case 1:180 eta.County.XemptType = strings.TrimSpace(s.Find("span").First().Contents().Text())181 case 2:182 eta.SchoolBoard.XemptType = strings.TrimSpace(s.Find("span").First().Contents().Text())183 case 3:184 eta.Municipal.XemptType = strings.TrimSpace(s.Find("span").First().Contents().Text())185 case 4:186 eta.Independent.XemptType = strings.TrimSpace(s.Find("span").First().Contents().Text())187 }188 case 10:189 switch int {190 case 1:191 eta.County.Taxable = strings.TrimSpace(s.Find("span").First().Contents().Text())192 case 2:193 eta.SchoolBoard.Taxable = strings.TrimSpace(s.Find("span").First().Contents().Text())194 case 3:195 eta.Municipal.Taxable = strings.TrimSpace(s.Find("span").First().Contents().Text())196 case 4:197 eta.Independent.Taxable = strings.TrimSpace(s.Find("span").First().Contents().Text())198 }199 }200 })201 return eta202}203// LoadAppendExemptionsTaxable Load Taxable and Exemptions Calls ExemptionsTaxableRecord204func LoadAppendExemptionsTaxable(doc *goquery.Document, _bcpa *model.Bcpa) {205 //Preload the object206 eta := model.ExemptionsTaxableValuesbyTaxingAuthority{}207 eta.CreatedAt = time.Now()208 eta.County = model.ExemptionsAndTaxableValue{}209 eta.SchoolBoard = model.ExemptionsAndTaxableValue{}210 eta.Municipal = model.ExemptionsAndTaxableValue{}211 eta.Independent = model.ExemptionsAndTaxableValue{}212 doc.Find("body > table:nth-child(3) > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(1) > table:nth-child(8) > tbody > tr").Each(func(i int, s *goquery.Selection) {213 if i > 1 {214 eta = ExemptionsTaxableRecord(s, i, eta)215 }216 })217 _bcpa.ExemptionsTaxable = eta218}219// SalesRecord Parse Sales hostory table called by LoadSalesHistory220func SalesRecord(s *goquery.Selection) model.Sale {221 sale := model.Sale{}222 s.Find("td").Each(func(int int, s *goquery.Selection) {223 switch int {224 case 0:225 sale.Date = strings.TrimSpace(s.Find("span").First().Contents().Text())226 case 1:227 sale.Type = strings.TrimSpace(s.Find("span").First().Contents().Text())228 case 2:229 sale.Price = strings.TrimSpace(s.Find("span").First().Contents().Text())230 case 3:231 sale.BookPageCIN = strings.TrimSpace(s.Find("span").First().Contents().Text())232 }233 })234 return sale235}236// LoadSalesHistory Load up the sales history table in objects and append to BCPA parent calls SalesRecord237func LoadSalesHistory(doc *goquery.Document, _bcpa *model.Bcpa) {238 doc.Find("body > table:nth-child(3) > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(1) > table:nth-child(10) > tbody > tr > td:nth-child(1) > table:nth-child(1) > tbody > tr").Each(func(i int, s *goquery.Selection) {239 if i > 1 {240 if len(strings.TrimSpace(StripSpaces(s.Find("td:nth-child(1)").Find("span").First().Contents().Text()))) > 0 {241 sale := SalesRecord(s)242 //append the sale to the struct243 _bcpa.SalesHistory = append(_bcpa.SalesHistory, sale)244 }245 }246 })247}248// LandCalculationRecord extract the land calculation record called by LoadLandCalculations249func LandCalculationRecord(s *goquery.Selection) model.LandCalculation {250 lc := model.LandCalculation{}251 s.Find("td").Each(func(int int, s *goquery.Selection) {252 switch int {253 case 0:254 lc.Price = strings.TrimSpace(s.Find("span").First().Contents().Text())255 case 1:256 lc.Factor = strings.TrimSpace(s.Find("span").First().Contents().Text())257 case 2:258 lc.Type = strings.TrimSpace(s.Find("span").First().Contents().Text())259 }260 })261 return lc262}263// LoadLandCalculations load calculations structure calls LandCalculationRecord264func LoadLandCalculations(doc *goquery.Document, _bcpa *model.Bcpa) {265 //Parent node to be attached to BCPA266 lcs := model.LandCalculations{}267 //We need a Card placeholder as we'll need to set the URL for use later268 card := model.RecBuildingCard{}269 //Need to know how many rows are in the table. We only need 3-* and the last 2 or 3 rows270 rowCount := doc.Find("body > table:nth-child(3) > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(1) > table:nth-child(10) > tbody > tr > td:nth-child(2) > table > tbody").Find("tr").Size()271 //Last row of table272 EffActYearBuiltRowIndex := rowCount - 1273 //Unit row or Bldg SF row274 UnitOrBldgRowIndex := rowCount - 2275 //Bldg Row of table276 BldgRowIndex := rowCount - 3277 //Lets loop the Table rows278 doc.Find("body > table:nth-child(3) > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(1) > table:nth-child(10) > tbody > tr > td:nth-child(2) > table > tbody > tr").Each(func(i int, s *goquery.Selection) {279 if i == EffActYearBuiltRowIndex { //Grab the last row of the table280 lcs.EffActYearBuilt = strings.TrimSpace(StripSpaces(s.Find("td").First().Find("a").First().Find("span").First().Contents().Text()))281 } else if i == UnitOrBldgRowIndex || i == BldgRowIndex { //Grab the second to last row and check if we have Unit data or not282 //Check the value of the first td283 if strings.Contains(s.Find("td:nth-child(1)").Find("span").Contents().Text(), "Units") { //This is the unit row so grab the unit data284 lcs.Units = strings.TrimSpace(StripSpaces(s.Find("td:nth-child(2)").Find("span").Contents().Text()))285 } else { //This is the Bldg Row, Grab the building data286 //Defualt value for Units287 lcs.Units = "0"288 //Set the SF total289 lcs.AdjBldgSF = strings.TrimSpace(StripSpaces(s.Find("td:nth-child(2)").Find("span").Contents().Text()))290 var hrefExists bool291 //Grab the Sketch URL292 lcs.SketchURL, hrefExists = s.Find("td:nth-child(1)").Find("a:nth-child(3)").Attr("href")293 if !hrefExists {294 log.Println("No Sketch URL")295 }296 //Get the card URL297 card.CardURL, hrefExists = s.Find("td:nth-child(1)").Find("a:nth-child(2)").Attr("href")298 card.CardURL = url.QueryEscape(card.CardURL)299 if !hrefExists {300 log.Println("No Card URL")301 }302 }303 } else if i > 1 { //These are the data rows as we skip the header rows304 //Make sure we have data in the row before proceeding305 if len(strings.TrimSpace(StripSpaces(s.Find("td:nth-child(1)").Find("span").First().Contents().Text()))) > 0 {306 //Build the record307 LandCalculation := LandCalculationRecord(s)308 //Append the Land Cal309 lcs.Calculations = append(lcs.Calculations, LandCalculation)310 }311 }312 })313 //Add the card info if the URL isn't blank314 if card.CardURL != "" {315 lcs.Cards = append(lcs.Cards, card)316 }317 _bcpa.LandCalculations = lcs318}319// SpecialAssessmentRecord extract data for structure called by LoadSpecialAssessments320func SpecialAssessmentRecord(s *goquery.Selection) model.SpecialAssessment {321 sa := model.SpecialAssessment{}322 s.Find("td").Each(func(int int, s *goquery.Selection) {323 switch int {324 case 0:325 sa.Fire = strings.TrimSpace(s.Find("span").First().Contents().Text())326 case 1:327 sa.Garb = strings.TrimSpace(s.Find("span").First().Contents().Text())328 case 2:329 sa.Light = strings.TrimSpace(s.Find("span").First().Contents().Text())330 case 3:331 sa.Drain = strings.TrimSpace(s.Find("span").First().Contents().Text())332 case 4:333 sa.Impr = strings.TrimSpace(s.Find("span").First().Contents().Text())334 case 5:335 sa.Safe = strings.TrimSpace(s.Find("span").First().Contents().Text())336 case 6:337 sa.Storm = strings.TrimSpace(s.Find("span").First().Contents().Text())338 case 7:339 sa.Clean = strings.TrimSpace(s.Find("span").First().Contents().Text())340 case 8:341 sa.Misc = strings.TrimSpace(s.Find("span").First().Contents().Text())342 }343 })344 return sa345}346// LoadSpecialAssessments parse assessments table calls SpecialAssessmentRecord347func LoadSpecialAssessments(doc *goquery.Document, _bcpa *model.Bcpa) {348 //Lets loop the Table rows349 doc.Find("body > table:nth-child(3) > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(1) > table:nth-child(12) > tbody > tr").Each(func(i int, s *goquery.Selection) {350 if i > 1 {351 specialAssessment := SpecialAssessmentRecord(s)352 //append the sale to the struct353 _bcpa.SpecialAssessments = append(_bcpa.SpecialAssessments, specialAssessment)354 }355 })...
manipulation_test.go
Source:manipulation_test.go
...172 </body>173 </html>`)174 doc.Find("table tr td:first-child").BeforeHtml("<td class='c1'>new1</td><td class='c2'>new2</td>")175 assertLength(t, doc.Find("table td").Nodes, 6)176 assertClass(t, doc.Find("table td").First(), "c1")177 printSel(t, doc.Selection)178}179func TestEmpty(t *testing.T) {180 doc := Doc2Clone()181 s := doc.Find("#main").Empty()182 assertLength(t, doc.Find("#main").Children().Nodes, 0)183 assertLength(t, s.Filter("div").Nodes, 6)184 printSel(t, doc.Selection)185}186func TestPrepend(t *testing.T) {187 doc := Doc2Clone()188 doc.Find("#main").Prepend("#nf6")189 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)190 assertLength(t, doc.Find("#main #nf6:first-child").Nodes, 1)191 printSel(t, doc.Selection)192}193func TestPrependBody(t *testing.T) {194 doc := Doc2Clone()195 doc.Find("body").Prepend("#nf6")196 assertLength(t, doc.Find("#foot #nf6").Nodes, 0)197 assertLength(t, doc.Find("#main #nf6").Nodes, 0)198 assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)199 printSel(t, doc.Selection)200}201func TestPrependSelection(t *testing.T) {202 doc := Doc2Clone()203 doc.Find("#main").PrependSelection(doc.Find("#nf1, #nf2"))204 assertLength(t, doc.Find("#foot #nf1").Nodes, 0)205 assertLength(t, doc.Find("#foot #nf2").Nodes, 0)206 assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)207 assertLength(t, doc.Find("#main #nf2:nth-child(2)").Nodes, 1)208 printSel(t, doc.Selection)209}210func TestPrependSelectionExisting(t *testing.T) {211 doc := Doc2Clone()212 doc.Find("#main").PrependSelection(doc.Find("#n5, #n6"))213 assertClass(t, doc.Find("#main :nth-child(1)"), "five")214 assertClass(t, doc.Find("#main :nth-child(2)"), "six")215 assertClass(t, doc.Find("#main :nth-child(5)"), "three")216 assertClass(t, doc.Find("#main :nth-child(6)"), "four")217 printSel(t, doc.Selection)218}219func TestPrependClone(t *testing.T) {220 doc := Doc2Clone()221 doc.Find("#n1").PrependSelection(doc.Find("#nf1").Clone())222 assertLength(t, doc.Find("#foot #nf1:first-child").Nodes, 1)223 assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)224 printSel(t, doc.Selection)225}226func TestPrependHtml(t *testing.T) {227 doc := Doc2Clone()228 doc.Find("div").PrependHtml("<strong>new node</strong>")229 assertLength(t, doc.Find("strong:first-child").Nodes, 14)230 printSel(t, doc.Selection)231}232func TestPrependHtmlContext(t *testing.T) {233 doc := loadString(t, `234 <html>235 <body>236 <table>237 <tr>238 <td>Before1</td>239 </tr>240 <tr>241 <td>Before2</td>242 </tr>243 </table>244 </body>245 </html>`)246 doc.Find("table tr").PrependHtml("<td class='c1'>new node</td><td class='c2'>other new node</td>")247 assertLength(t, doc.Find("table td").Nodes, 6)248 assertClass(t, doc.Find("table tr td").First(), "c1")249 printSel(t, doc.Selection)250}251func TestRemove(t *testing.T) {252 doc := Doc2Clone()253 doc.Find("#nf1").Remove()254 assertLength(t, doc.Find("#foot #nf1").Nodes, 0)255 printSel(t, doc.Selection)256}257func TestRemoveAll(t *testing.T) {258 doc := Doc2Clone()259 doc.Find("*").Remove()260 assertLength(t, doc.Find("*").Nodes, 0)261 printSel(t, doc.Selection)262}263func TestRemoveRoot(t *testing.T) {264 doc := Doc2Clone()265 doc.Find("html").Remove()266 assertLength(t, doc.Find("html").Nodes, 0)267 printSel(t, doc.Selection)268}269func TestRemoveFiltered(t *testing.T) {270 doc := Doc2Clone()271 nf6 := doc.Find("#nf6")272 s := doc.Find("div").RemoveFiltered("#nf6")273 assertLength(t, doc.Find("#nf6").Nodes, 0)274 assertLength(t, s.Nodes, 1)275 if nf6.Nodes[0] != s.Nodes[0] {276 t.Error("Removed node does not match original")277 }278 printSel(t, doc.Selection)279}280func TestReplaceWith(t *testing.T) {281 doc := Doc2Clone()282 doc.Find("#nf6").ReplaceWith("#main")283 assertLength(t, doc.Find("#foot #main:last-child").Nodes, 1)284 printSel(t, doc.Selection)285 doc.Find("#foot").ReplaceWith("#main")286 assertLength(t, doc.Find("#foot").Nodes, 0)287 assertLength(t, doc.Find("#main").Nodes, 1)288 printSel(t, doc.Selection)289}290func TestReplaceWithHtml(t *testing.T) {291 doc := Doc2Clone()292 doc.Find("#main, #foot").ReplaceWithHtml("<div id=\"replace\"></div>")293 assertLength(t, doc.Find("#replace").Nodes, 2)294 printSel(t, doc.Selection)295}296func TestReplaceWithHtmlContext(t *testing.T) {297 doc := loadString(t, `298 <html>299 <body>300 <table>301 <tr>302 <th>Before1</th>303 </tr>304 <tr>305 <th>Before2</th>306 </tr>307 </table>308 </body>309 </html>`)310 doc.Find("table th").ReplaceWithHtml("<td class='c1'>Test</td><td class='c2'>Replace</td>")311 assertLength(t, doc.Find("table th").Nodes, 0)312 assertLength(t, doc.Find("table tr td").Nodes, 4)313 assertClass(t, doc.Find("table tr td").First(), "c1")314 printSel(t, doc.Selection)315}316func TestSetHtml(t *testing.T) {317 doc := Doc2Clone()318 q := doc.Find("#main, #foot")319 q.SetHtml(`<div id="replace">test</div>`)320 assertLength(t, doc.Find("#replace").Nodes, 2)321 assertLength(t, doc.Find("#main, #foot").Nodes, 2)322 if q.Text() != "testtest" {323 t.Errorf("Expected text to be %v, found %v", "testtest", q.Text())324 }325 printSel(t, doc.Selection)326}327func TestSetHtmlNoMatch(t *testing.T) {328 doc := Doc2Clone()329 q := doc.Find("#notthere")330 q.SetHtml(`<div id="replace">test</div>`)331 assertLength(t, doc.Find("#replace").Nodes, 0)332 printSel(t, doc.Selection)333}334func TestSetHtmlEmpty(t *testing.T) {335 doc := Doc2Clone()336 q := doc.Find("#main")337 q.SetHtml(``)338 assertLength(t, doc.Find("#main").Nodes, 1)339 assertLength(t, doc.Find("#main").Children().Nodes, 0)340 printSel(t, doc.Selection)341}342func TestSetHtmlContext(t *testing.T) {343 doc := loadString(t, `344 <html>345 <body>346 <table>347 <tr>348 <th>Before1</th>349 </tr>350 <tr>351 <th>Before2</th>352 </tr>353 </table>354 </body>355 </html>`)356 doc.Find("table tr").SetHtml("<td class='c1'>Test</td><td class='c2'>Again</td>")357 assertLength(t, doc.Find("table th").Nodes, 0)358 assertLength(t, doc.Find("table td").Nodes, 4)359 assertLength(t, doc.Find("table tr").Nodes, 2)360 printSel(t, doc.Selection)361}362func TestSetText(t *testing.T) {363 doc := Doc2Clone()364 q := doc.Find("#main, #foot")365 repl := "<div id=\"replace\">test</div>"366 q.SetText(repl)367 assertLength(t, doc.Find("#replace").Nodes, 0)368 assertLength(t, doc.Find("#main, #foot").Nodes, 2)369 if q.Text() != (repl + repl) {370 t.Errorf("Expected text to be %v, found %v", (repl + repl), q.Text())371 }372 h, err := q.Html()373 if err != nil {374 t.Errorf("Error: %v", err)375 }376 esc := "<div id="replace">test</div>"377 if h != esc {378 t.Errorf("Expected html to be %v, found %v", esc, h)379 }380 printSel(t, doc.Selection)381}382func TestReplaceWithSelection(t *testing.T) {383 doc := Doc2Clone()384 sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))385 assertSelectionIs(t, sel, "#nf6")386 assertLength(t, doc.Find("#nf6").Nodes, 0)387 assertLength(t, doc.Find("#nf5").Nodes, 1)388 printSel(t, doc.Selection)389}390func TestUnwrap(t *testing.T) {391 doc := Doc2Clone()392 doc.Find("#nf5").Unwrap()393 assertLength(t, doc.Find("#foot").Nodes, 0)394 assertLength(t, doc.Find("body > #nf1").Nodes, 1)395 assertLength(t, doc.Find("body > #nf5").Nodes, 1)396 printSel(t, doc.Selection)397 doc = Doc2Clone()398 doc.Find("#nf5, #n1").Unwrap()399 assertLength(t, doc.Find("#foot").Nodes, 0)400 assertLength(t, doc.Find("#main").Nodes, 0)401 assertLength(t, doc.Find("body > #n1").Nodes, 1)402 assertLength(t, doc.Find("body > #nf5").Nodes, 1)403 printSel(t, doc.Selection)404}405func TestUnwrapBody(t *testing.T) {406 doc := Doc2Clone()407 doc.Find("#main").Unwrap()408 assertLength(t, doc.Find("body").Nodes, 1)409 assertLength(t, doc.Find("body > #main").Nodes, 1)410 printSel(t, doc.Selection)411}412func TestUnwrapHead(t *testing.T) {413 doc := Doc2Clone()414 doc.Find("title").Unwrap()415 assertLength(t, doc.Find("head").Nodes, 0)416 assertLength(t, doc.Find("head > title").Nodes, 0)417 assertLength(t, doc.Find("title").Nodes, 1)418 printSel(t, doc.Selection)419}420func TestUnwrapHtml(t *testing.T) {421 doc := Doc2Clone()422 doc.Find("head").Unwrap()423 assertLength(t, doc.Find("html").Nodes, 0)424 assertLength(t, doc.Find("html head").Nodes, 0)425 assertLength(t, doc.Find("head").Nodes, 1)426 printSel(t, doc.Selection)427}428func TestWrap(t *testing.T) {429 doc := Doc2Clone()430 doc.Find("#nf1").Wrap("#nf2")431 nf1 := doc.Find("#foot #nf2 #nf1")432 assertLength(t, nf1.Nodes, 1)433 nf2 := doc.Find("#nf2")434 assertLength(t, nf2.Nodes, 2)435 printSel(t, doc.Selection)436}437func TestWrapEmpty(t *testing.T) {438 doc := Doc2Clone()439 doc.Find("#nf1").Wrap("#doesnt-exist")440 origHtml, _ := Doc2().Html()441 newHtml, _ := doc.Html()442 if origHtml != newHtml {443 t.Error("Expected the two documents to be identical.")444 }445 printSel(t, doc.Selection)446}447func TestWrapHtml(t *testing.T) {448 doc := Doc2Clone()449 doc.Find(".odd").WrapHtml(wrapHtml)450 nf2 := doc.Find("#ins #nf2")451 assertLength(t, nf2.Nodes, 1)452 printSel(t, doc.Selection)453}454func TestWrapSelection(t *testing.T) {455 doc := Doc2Clone()456 doc.Find("#nf1").WrapSelection(doc.Find("#nf2"))457 nf1 := doc.Find("#foot #nf2 #nf1")458 assertLength(t, nf1.Nodes, 1)459 nf2 := doc.Find("#nf2")460 assertLength(t, nf2.Nodes, 2)461 printSel(t, doc.Selection)462}463func TestWrapAll(t *testing.T) {464 doc := Doc2Clone()465 doc.Find(".odd").WrapAll("#nf1")466 nf1 := doc.Find("#main #nf1")467 assertLength(t, nf1.Nodes, 1)468 sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")469 assertLength(t, sel.Nodes, 1)470 printSel(t, doc.Selection)471}472func TestWrapAllHtml(t *testing.T) {473 doc := Doc2Clone()474 doc.Find(".odd").WrapAllHtml(wrapHtml)475 nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")476 assertLength(t, nf1.Nodes, 1)477 printSel(t, doc.Selection)478}479func TestWrapInnerNoContent(t *testing.T) {480 doc := Doc2Clone()481 doc.Find(".one").WrapInner(".two")482 twos := doc.Find(".two")483 assertLength(t, twos.Nodes, 4)484 assertLength(t, doc.Find(".one .two").Nodes, 2)485 printSel(t, doc.Selection)486}487func TestWrapInnerWithContent(t *testing.T) {488 doc := Doc3Clone()489 doc.Find(".one").WrapInner(".two")490 twos := doc.Find(".two")491 assertLength(t, twos.Nodes, 4)492 assertLength(t, doc.Find(".one .two").Nodes, 2)493 printSel(t, doc.Selection)494}495func TestWrapInnerNoWrapper(t *testing.T) {496 doc := Doc2Clone()497 doc.Find(".one").WrapInner(".not-exist")498 twos := doc.Find(".two")499 assertLength(t, twos.Nodes, 2)500 assertLength(t, doc.Find(".one").Nodes, 2)501 assertLength(t, doc.Find(".one .two").Nodes, 0)502 printSel(t, doc.Selection)503}504func TestWrapInnerHtml(t *testing.T) {505 doc := Doc2Clone()506 doc.Find("#foot").WrapInnerHtml(wrapHtml)507 foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3")508 assertLength(t, foot.Nodes, 1)509 printSel(t, doc.Selection)510}511func TestParsingRespectsVaryingContext(t *testing.T) {512 docA := loadString(t, `513 <html>514 <body>515 <a class="x"></a>516 </body>517 </html>`)518 docTable := loadString(t, `519 <html>520 <body>521 <table class="x"></table>522 </body>523 </html>`)524 docBoth := loadString(t, `525 <html>526 <body>527 <table class="x"></table>528 <a class="x"></a>529 </body>530 </html>`)531 sA := docA.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")532 sTable := docTable.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")533 sBoth := docBoth.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")534 printSel(t, docA.Selection)535 printSel(t, docTable.Selection)536 printSel(t, docBoth.Selection)537 oA, _ := sA.Html()538 oTable, _ := sTable.Html()539 if oA == oTable {540 t.Errorf("Expected inner html of <a> and <table> to not be equal, but got %s and %s", oA, oTable)541 }542 oBothTable, _ := sBoth.First().Html()543 if oBothTable != oTable {544 t.Errorf("Expected inner html of <table> and <table> in doc containing both tags to be equal, but got %s and %s",545 oTable,546 oBothTable)547 }548 oBothA, _ := sBoth.Last().Html()549 if oBothA != oA {550 t.Errorf("Expected inner html of <a> and <a> in doc containing both tags to be equal, but got %s and %s",551 oA,552 oBothA)553 }554}555func TestHtmlWithNonElementNode(t *testing.T) {556 const data = `...
types_test.go
Source:types_test.go
1package btf2import (3 "fmt"4 "testing"5 qt "github.com/frankban/quicktest"6 "github.com/google/go-cmp/cmp"7)8func TestSizeof(t *testing.T) {9 testcases := []struct {10 size int11 typ Type12 }{13 {0, (*Void)(nil)},14 {1, &Int{Size: 1}},15 {4, &Enum{}},16 {0, &Array{Type: &Pointer{Target: (*Void)(nil)}, Nelems: 0}},17 {12, &Array{Type: &Enum{}, Nelems: 3}},18 }19 for _, tc := range testcases {20 name := fmt.Sprint(tc.typ)21 t.Run(name, func(t *testing.T) {22 have, err := Sizeof(tc.typ)23 if err != nil {24 t.Fatal("Can't calculate size:", err)25 }26 if have != tc.size {27 t.Errorf("Expected size %d, got %d", tc.size, have)28 }29 })30 }31}32func TestCopyType(t *testing.T) {33 _, _ = copyType((*Void)(nil), nil)34 in := &Int{Size: 4}35 out, _ := copyType(in, nil)36 in.Size = 837 if size := out.(*Int).Size; size != 4 {38 t.Error("Copy doesn't make a copy, expected size 4, got", size)39 }40 t.Run("cyclical", func(t *testing.T) {41 _, _ = copyType(newCyclicalType(2), nil)42 })43 t.Run("identity", func(t *testing.T) {44 u16 := &Int{Size: 2}45 out, _ := copyType(&Struct{46 Members: []Member{47 {Name: "a", Type: u16},48 {Name: "b", Type: u16},49 },50 }, nil)51 outStruct := out.(*Struct)52 qt.Assert(t, outStruct.Members[0].Type, qt.Equals, outStruct.Members[1].Type)53 })54}55// The following are valid Types.56//57// There currently is no better way to document which58// types implement an interface.59func ExampleType_validTypes() {60 var _ Type = &Void{}61 var _ Type = &Int{}62 var _ Type = &Pointer{}63 var _ Type = &Array{}64 var _ Type = &Struct{}65 var _ Type = &Union{}66 var _ Type = &Enum{}67 var _ Type = &Fwd{}68 var _ Type = &Typedef{}69 var _ Type = &Volatile{}70 var _ Type = &Const{}71 var _ Type = &Restrict{}72 var _ Type = &Func{}73 var _ Type = &FuncProto{}74 var _ Type = &Var{}75 var _ Type = &Datasec{}76}77func TestType(t *testing.T) {78 types := []func() Type{79 func() Type { return &Void{} },80 func() Type { return &Int{Size: 2, Bits: 3} },81 func() Type { return &Pointer{Target: &Void{}} },82 func() Type { return &Array{Type: &Int{}} },83 func() Type {84 return &Struct{85 Members: []Member{{Type: &Void{}}},86 }87 },88 func() Type {89 return &Union{90 Members: []Member{{Type: &Void{}}},91 }92 },93 func() Type { return &Enum{} },94 func() Type { return &Fwd{Name: "thunk"} },95 func() Type { return &Typedef{Type: &Void{}} },96 func() Type { return &Volatile{Type: &Void{}} },97 func() Type { return &Const{Type: &Void{}} },98 func() Type { return &Restrict{Type: &Void{}} },99 func() Type { return &Func{Name: "foo", Type: &Void{}} },100 func() Type {101 return &FuncProto{102 Params: []FuncParam{{Name: "bar", Type: &Void{}}},103 Return: &Void{},104 }105 },106 func() Type { return &Var{Type: &Void{}} },107 func() Type {108 return &Datasec{109 Vars: []VarSecinfo{{Type: &Void{}}},110 }111 },112 }113 compareTypes := cmp.Comparer(func(a, b *Type) bool {114 return a == b115 })116 for _, fn := range types {117 typ := fn()118 t.Run(fmt.Sprintf("%T", typ), func(t *testing.T) {119 t.Logf("%v", typ)120 if typ == typ.copy() {121 t.Error("Copy doesn't copy")122 }123 var first, second typeDeque124 typ.walk(&first)125 typ.walk(&second)126 if diff := cmp.Diff(first.all(), second.all(), compareTypes); diff != "" {127 t.Errorf("Walk mismatch (-want +got):\n%s", diff)128 }129 })130 }131}132func TestTypeDeque(t *testing.T) {133 a, b := new(Type), new(Type)134 t.Run("pop", func(t *testing.T) {135 var td typeDeque136 td.push(a)137 td.push(b)138 if td.pop() != b {139 t.Error("Didn't pop b first")140 }141 if td.pop() != a {142 t.Error("Didn't pop a second")143 }144 if td.pop() != nil {145 t.Error("Didn't pop nil")146 }147 })148 t.Run("shift", func(t *testing.T) {149 var td typeDeque150 td.push(a)151 td.push(b)152 if td.shift() != a {153 t.Error("Didn't shift a second")154 }155 if td.shift() != b {156 t.Error("Didn't shift b first")157 }158 if td.shift() != nil {159 t.Error("Didn't shift nil")160 }161 })162 t.Run("push", func(t *testing.T) {163 var td typeDeque164 td.push(a)165 td.push(b)166 td.shift()167 ts := make([]Type, 12)168 for i := range ts {169 td.push(&ts[i])170 }171 if td.shift() != b {172 t.Error("Didn't shift b first")173 }174 for i := range ts {175 if td.shift() != &ts[i] {176 t.Fatal("Shifted wrong Type at pos", i)177 }178 }179 })180 t.Run("all", func(t *testing.T) {181 var td typeDeque182 td.push(a)183 td.push(b)184 all := td.all()185 if len(all) != 2 {186 t.Fatal("Expected 2 elements, got", len(all))187 }188 if all[0] != a || all[1] != b {189 t.Fatal("Elements don't match")190 }191 })192}193func newCyclicalType(n int) Type {194 ptr := &Pointer{}195 prev := Type(ptr)196 for i := 0; i < n; i++ {197 switch i % 5 {198 case 0:199 prev = &Struct{200 Members: []Member{201 {Type: prev},202 },203 }204 case 1:205 prev = &Const{Type: prev}206 case 2:207 prev = &Volatile{Type: prev}208 case 3:209 prev = &Typedef{Type: prev}210 case 4:211 prev = &Array{Type: prev}212 }213 }214 ptr.Target = prev215 return ptr216}...
First
Using AI Code Generation
1import (2func main() {3 fmt.Println(td.First())4}5import (6func main() {7 fmt.Println(td.Second())8}9import (10func main() {11 fmt.Println(td.Third())12}13import (14func main() {15 fmt.Println(td.Fourth())16}17import (18func main() {19 fmt.Println(td.Fifth())20}21import (22func main() {23 fmt.Println(td.Sixth())24}25import (26func main() {27 fmt.Println(td.Seventh())28}29import (30func main() {31 fmt.Println(td.Eighth())32}33import (34func main() {35 fmt.Println(td.Ninth())36}37import (38func main() {39 fmt.Println(td.Tenth())40}41import (42func main() {43 fmt.Println(td.Eleventh())44}45import (46func main() {47 fmt.Println(td.Twelfth())48}
First
Using AI Code Generation
1func main() {2 td.First()3}4func main() {5 td.Second()6}
First
Using AI Code Generation
1import (2func main() {3 td.First()4}5import (6func main() {7 td.Second()8}9import (10func main() {11 td.Third()12}13import (14func main() {15 td.Fourth()16}17import (18func main() {19 td.Fifth()20}21import (22func main() {23 td.Sixth()24}25import (26func main() {27 td.Seventh()28}29import (30func main() {31 td.Eighth()32}33import (34func main() {35 td.Ninth()36}37import (38func main() {39 td.Tenth()40}41import (42func main() {43 td.Eleventh()44}45import (
First
Using AI Code Generation
1import (2func main() {3 fmt.Println(First(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))4}5func First(a ...int) int {6}
First
Using AI Code Generation
1import (2func main() {3 fmt.Println("First method of td class")4 td.First()5 fmt.Println("Second method of td class")6 td.Second()7}8import (9func main() {10 fmt.Println("First method of td class")11 td.First()12 fmt.Println("Second method of td class")13 td.Second()14}15import (16func main() {17 fmt.Println("First method of td class")18 td.First()19 fmt.Println("Second method of td class")20 td.Second()21}22import (23func main() {24 fmt.Println("First method of td class")25 td.First()26 fmt.Println("Second method of td class")27 td.Second()28}29import (30func main() {31 fmt.Println("First method of td class")32 td.First()33 fmt.Println("Second method of td class")34 td.Second()35}36import (37func main() {38 fmt.Println("First method of td class")39 td.First()
First
Using AI Code Generation
1import (2func main() {3 td := td{}4 td.First()5}6import (7func main() {8 td := td{}9 td.Second()10}11import (12func main() {13 td := td{}14 td.Third()15}16import (17func main() {18 td := td{}19 td.Fourth()20}21import (22func main() {23 td := td{}24 td.Fifth()25}26import (27func main() {28 td := td{}29 td.Sixth()30}31import (32func main() {33 td := td{}34 td.Seventh()35}36import (37func main() {38 td := td{}39 td.Eighth()40}41import (42func main() {43 td := td{}
First
Using AI Code Generation
1import (2func main() {3 t = td.TD{3, 4, 5}4 fmt.Println("Area of Triangle is: ", t.First())5}6import (7func main() {8 t = td.TD{3, 4, 5}9 fmt.Println("Area of Triangle is: ", t.Second())10}11import (12func main() {13 t = td.TD{3, 4, 5}14 fmt.Println("Area of Triangle is: ", t.Third())15}16import (17func main() {18 t = td.TD{3, 4, 5}19 fmt.Println("Area of Triangle is: ", t.Fourth())20}21import (22func main() {23 t = td.TD{3, 4, 5}24 fmt.Println("Area of Triangle is: ", t.Fifth())25}26import (27func main() {28 t = td.TD{3, 4, 5}29 fmt.Println("Area of Triangle is: ", t.Sixth())30}31import (32func main() {33 t = td.TD{3, 4, 5}34 fmt.Println("Area of Triangle is: ", t.Seventh())35}36import (
First
Using AI Code Generation
1import (2func main() {3 td := td.New()4 td.Add("1", "2")5 td.Add("3", "4")6 row := td.First()7 fmt.Printf("%s %s8}9import (10func main() {11 td := td.New()12 td.Add("1", "2")13 td.Add("3", "4")14 row := td.Last()15 fmt.Printf("%s %s16}17import (18func main() {19 td := td.New()20 td.Add("1", "2")21 td.Add("3", "4")22 row := td.Find("1", "3")23 fmt.Printf("%s %s24}25import (26func main() {27 td := td.New()28 td.Add("1", "2")29 td.Add("3", "4")30 td.Add("5", "6")31 rows := td.FindAll("1", "3")32 for _, row := range rows {33 fmt.Printf("%s %s34 }35}36import (37func main() {38 td := td.New()39 td.Add("1", "2
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!!