Patchwork [7,of,8] rust-hglib: extract test code taking a connection as an argument

login
register
mail settings
Submitter Yuya Nishihara
Date April 1, 2018, 11:14 a.m.
Message ID <e6389a767f7198a74355.1522581263@mimosa>
Download mbox | patch
Permalink /patch/30123/
State Accepted
Headers show

Comments

Yuya Nishihara - April 1, 2018, 11:14 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1522485631 -32400
#      Sat Mar 31 17:40:31 2018 +0900
# Node ID e6389a767f7198a743558d4b60fb13d6f33dd376
# Parent  8b115cac0dbdb6c140a838e9e986c153eb0aa3a0
rust-hglib: extract test code taking a connection as an argument

Patch

diff --git a/rust/hglib/tests/connection.rs b/rust/hglib/tests/connection.rs
--- a/rust/hglib/tests/connection.rs
+++ b/rust/hglib/tests/connection.rs
@@ -37,27 +37,53 @@  fn with_temp_repo<F>(f: F) where F : Fn(
     env::set_current_dir(&*lock).unwrap();
 }
 
+fn raw_command_with<B>(connection: &mut Connection<B>)
+    where B: ConnectionBackend,
+{
+    connection.read_hello().unwrap();
+    let (mut result, mut output) = (-1i32, vec![]);
+    {
+        let run = connection.raw_command(vec![b"summary"]).unwrap();
+        for chunk in run {
+            match chunk {
+                Ok(Chunk::Output(s)) => output.extend(s),
+                Ok(Chunk::Error(_)) => continue,
+                Ok(Chunk::Result(r)) => result = r,
+                Ok(c) => panic!("unexpected chunk: {:?}", c),
+                Err(e) => panic!("failed to read command results: {}", e),
+            }
+        }
+    }
+    assert!(output.starts_with(b"parent:"));
+    assert_eq!(result, 0);
+}
+
+fn raw_command_error_with<B>(connection: &mut Connection<B>)
+    where B: ConnectionBackend,
+{
+    connection.read_hello().unwrap();
+    let (mut result, mut error) = (-1i32, vec![]);
+    {
+        let run = connection.raw_command(vec![b"noexist"]).unwrap();
+        for chunk in run {
+            match chunk {
+                Ok(Chunk::Output(_)) => continue,
+                Ok(Chunk::Error(s)) => error.extend(s),
+                Ok(Chunk::Result(r)) => { result = r },
+                Ok(c) => panic!("unexpected chunk: {:?}", c),
+                Err(e) => panic!("failed to read command results: {}", e),
+            }
+        }
+    }
+    assert_eq!(result, 255);
+    assert!(error.starts_with(b"hg: unknown command 'noexist'"));
+}
+
 #[test]
 fn raw_command() {
     with_temp_repo(|| {
         let mut connection = Connection::new().unwrap();
-        connection.read_hello().unwrap();
-        let (mut result, mut output) = (-1i32, vec![]);
-        {
-            let run = connection.raw_command(vec![b"summary"]).unwrap();
-            for chunk in run {
-                match chunk {
-                    Ok(Chunk::Output(s)) => output.extend(s),
-                    Ok(Chunk::Error(_)) => continue,
-                    Ok(Chunk::Result(r)) => result = r,
-                    Ok(c) => panic!("unexpected chunk: {:?}", c),
-                    Err(e) => panic!("failed to read command results: {}", e),
-                }
-            }
-        }
-        assert!(output.starts_with(b"parent:"));
-        assert_eq!(result, 0);
-
+        raw_command_with(&mut connection);
         connection.close().unwrap();
     });
 }
@@ -66,23 +92,7 @@  fn raw_command() {
 fn raw_command_error() {
     with_temp_repo(|| {
         let mut connection = Connection::new().unwrap();
-        connection.read_hello().unwrap();
-        let (mut result, mut error) = (-1i32, vec![]);
-        {
-            let run = connection.raw_command(vec![b"noexist"]).unwrap();
-            for chunk in run {
-                match chunk {
-                    Ok(Chunk::Output(_)) => continue,
-                    Ok(Chunk::Error(s)) => error.extend(s),
-                    Ok(Chunk::Result(r)) => { result = r },
-                    Ok(c) => panic!("unexpected chunk: {:?}", c),
-                    Err(e) => panic!("failed to read command results: {}", e),
-                }
-            }
-        }
-        assert_eq!(result, 255);
-        assert!(error.starts_with(b"hg: unknown command 'noexist'"));
-
+        raw_command_error_with(&mut connection);
         connection.close().unwrap();
     });
 }