chiark / gitweb /
wdt: Refactor UsualSetup
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 14 Feb 2021 19:19:41 +0000 (19:19 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 14 Feb 2021 19:28:40 +0000 (19:28 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
wdriver.rs
wdriver/wdt-simple.rs

index 68edf4687d5d1e01f4e044d75677ab6fac370d19..44753ea9a4c6ae4f6153e715f12137f4144a90bd 100644 (file)
@@ -1369,3 +1369,29 @@ impl Setup {
       .collect::<Result<Vec<Window>,AE>>()?
   }
 }
+
+pub struct UsualSetup {
+  pub su: Setup,
+  pub inst: Instance,
+  pub alice: Window,
+  pub bob: Window,
+  pub spec: otter::spec::GameSpec,
+}
+
+impl UsualSetup {
+  #[throws(AE)]
+  pub fn new() -> UsualSetup {
+    let (mut su, inst) = setup(module_path!()).always_context("setup")?;
+    let [alice, bob] : [Window; 2] =
+      su.setup_static_users(&inst)?.try_into().unwrap();
+    let spec = su.ds.game_spec_data()?;
+    UsualSetup { su, inst, alice, bob, spec }
+  }
+}
+
+#[throws(AE)]
+pub fn as_usual<F: FnOnce(UsualSetup) -> Result<(), AE>>(f: F) {
+  let usual = UsualSetup::new()?;
+  f(usual)?;
+  info!("ok");
+}
index d500409beed5284f5fbfee9e3b3abf5777e284aa..707ea4a8c3fb91f9e2db3cdfa24a2f14f50691c1 100644 (file)
@@ -279,27 +279,21 @@ impl Ctx {
 }
 
 #[throws(AE)]
-fn main(){
-  {
-    let (mut su, inst) = setup(module_path!()).always_context("setup")?;
-    let [alice, bob] : [Window; 2] =
-      su.setup_static_users(&inst)?.try_into().unwrap();
-    let spec = su.ds.game_spec_data()?;
-    debug!("ok {:?} {:?}", alice, bob);
+fn tests(UsualSetup { su, alice, bob, spec, ..}: UsualSetup) {
+  let mut c = Ctx { su, alice, bob, spec };
 
-    let mut c = Ctx { su, alice, bob, spec };
+  test!(c, "drag", c.drag()?);
 
-    test!(c, "drag", c.drag()?);
+  test!(c, "drag-rotate-unselect", {
+    let pc = c.rotate().always_context("rotate")?;
+    c.drag_off(pc).always_context("drag off")?;
+    c.unselect(pc).always_context("unselect")?;
+  });
 
-    test!(c, "drag-rotate-unselect", {
-      let pc = c.rotate().always_context("rotate")?;
-      c.drag_off(pc).always_context("drag off")?;
-      c.unselect(pc).always_context("unselect")?;
-    });
+  test!(c, "conflict", c.conflict()?);
 
-    test!(c, "conflict", c.conflict()?);
-
-    debug!("finishing");
-  }
-  info!("ok");
+  debug!("finishing");
 }
+
+#[throws(AE)]
+fn main() { as_usual(tests)? }