code wiki / (root) / nx_dom_click_test.nx

nx_dom_click_test.nx source

↩ module page · 83 lines · 3821 B

1// nx_dom_click_test.nx -- smoke for nx_dom_click.nx. 2// 3// Returns exit code = number of failures (0 = all green). 4 5import "nx_dom_click.nx" 6 7func _cl_strlen(s: *u8) -> i64 { 8 var n: i64 = 0 9 while s[n] != 0 { n = n + 1 } 10 return n 11} 12 13func nx_dom_click_test() -> i64 { 14 var fails: i64 = 0 15 16 // Representative bits-up game-emitted HTML (mirror of nx_checkers_render 17 // output). Includes: 18 // - button data-sq="40" data-action="1" data-arg0="40" 19 // - reset button data-action="2" with no args 20 // - select data-action="3" (opponent picker) 21 // - a cell with no data-action (not clickable -- should yield found=0) 22 let html: *u8 = "<div class=\"chk-shell\"><button type=\"button\" class=\"sq dark\" data-sq=\"40\" data-action=\"1\" data-arg0=\"40\"></button><button type=\"button\" class=\"sq dark\" data-sq=\"33\" data-action=\"1\" data-arg0=\"33\" data-arg1=\"7\"></button><button type=\"button\" class=\"reset-btn\" data-action=\"2\">New game</button><div class=\"sq light\" data-sq=\"1\"></div></div>" as *u8 23 let n: i64 = _cl_strlen(html) 24 25 // T1: click data-sq=40 -> action=1, arg0=40, arg1=0 26 let i1: *NxClickIntent = nx_dom_click_intent(html, n, 27 "data-sq" as *u8, "40" as *u8) 28 if i1.found != 1 { fails = fails + 1 } 29 if i1.action != 1 { fails = fails + 1 } 30 if i1.arg0 != 40 { fails = fails + 1 } 31 if i1.arg1 != 0 { fails = fails + 1 } 32 33 // T2: click data-sq=33 -> action=1, arg0=33, arg1=7 34 let i2: *NxClickIntent = nx_dom_click_intent(html, n, 35 "data-sq" as *u8, "33" as *u8) 36 if i2.found != 1 { fails = fails + 1 } 37 if i2.action != 1 { fails = fails + 1 } 38 if i2.arg0 != 33 { fails = fails + 1 } 39 if i2.arg1 != 7 { fails = fails + 1 } 40 if i2.arg2 != 0 { fails = fails + 1 } 41 if i2.arg3 != 0 { fails = fails + 1 } 42 43 // T3: click reset-btn by class -> action=2, args all 0 44 let i3: *NxClickIntent = nx_dom_click_intent_by_class(html, n, 45 "reset-btn" as *u8) 46 if i3.found != 1 { fails = fails + 1 } 47 if i3.action != 2 { fails = fails + 1 } 48 if i3.arg0 != 0 { fails = fails + 1 } 49 50 // T4: light square has no data-action -> NOT clickable 51 let i4: *NxClickIntent = nx_dom_click_intent(html, n, 52 "data-sq" as *u8, "1" as *u8) 53 if i4.found != 0 { fails = fails + 1 } 54 55 // T5: missing element returns found=0 56 let i5: *NxClickIntent = nx_dom_click_intent(html, n, 57 "data-sq" as *u8, "99" as *u8) 58 if i5.found != 0 { fails = fails + 1 } 59 60 // T6: nx_dom_attr_int_at via direct API -- negative value support 61 // (e.g., data-action=-1 for "set opponent to human" in nx_chk_action). 62 let html2: *u8 = "<select data-action=\"3\"><option value=\"-1\">human</option></select>" as *u8 63 let n2: i64 = _cl_strlen(html2) 64 let r6: *NxDomQueryResult = nx_dom_find_attr_eq(html2, n2, 65 "value" as *u8, "-1" as *u8) 66 if r6.found != 1 { fails = fails + 1 } 67 let v6: i64 = nx_dom_attr_int_at(html2, r6.tag_off, r6.tag_len, 68 "value" as *u8, 0 as i64) 69 if v6 != -1 { fails = fails + 1 } 70 71 // T7: attr_int_at returns default when attr missing 72 let r7: *NxDomQueryResult = nx_dom_find_attr_eq(html, n, 73 "data-sq" as *u8, "40" as *u8) 74 let v7: i64 = nx_dom_attr_int_at(html, r7.tag_off, r7.tag_len, 75 "data-arg3" as *u8, -999 as i64) 76 if v7 != -999 { fails = fails + 1 } 77 78 return fails 79} 80 81func main() -> i64 { 82 return nx_dom_click_test() 83}