浏览代码

Feature: Tests in Core (#378)

* add tests to config

* add docs to config

* add file_system test.

* decreases the thread sleep timer.

* fix proptest case amount.

* remove linux from test for now.
Tensor-Programming 5 年之前
父节点
当前提交
2a1fc0c20c
共有 3 个文件被更改,包括 155 次插入5 次删除
  1. 91 5
      tauri/src/config.rs
  2. 64 0
      tauri/src/file_system.rs
  3. 0 0
      tauri/test/fixture/test.txt

+ 91 - 5
tauri/src/config.rs

@@ -1,6 +1,6 @@
 use std::env;
 
-#[derive(Deserialize, Clone)]
+#[derive(PartialEq, Deserialize, Clone, Debug)]
 #[serde(tag = "window", rename_all = "camelCase")]
 pub struct WindowConfig {
   #[serde(default = "default_width")]
@@ -38,7 +38,7 @@ fn default_window() -> WindowConfig {
   };
 }
 
-#[derive(Deserialize, Clone)]
+#[derive(PartialEq, Deserialize, Clone, Debug)]
 #[serde(tag = "embeddedServer", rename_all = "camelCase")]
 pub struct EmbeddedServerConfig {
   #[serde(default = "default_host")]
@@ -62,7 +62,7 @@ fn default_embedded_server() -> EmbeddedServerConfig {
   }
 }
 
-#[derive(Deserialize, Clone)]
+#[derive(PartialEq, Deserialize, Clone, Debug)]
 #[serde(tag = "tauri", rename_all = "camelCase")]
 pub struct TauriConfig {
   #[serde(default = "default_window")]
@@ -71,7 +71,7 @@ pub struct TauriConfig {
   pub embedded_server: EmbeddedServerConfig,
 }
 
-#[derive(Deserialize, Clone)]
+#[derive(PartialEq, Deserialize, Clone, Debug)]
 #[serde(tag = "build", rename_all = "camelCase")]
 pub struct BuildConfig {
   #[serde(default = "default_dev_path")]
@@ -82,7 +82,7 @@ fn default_dev_path() -> String {
   "".to_string()
 }
 
-#[derive(Deserialize, Clone)]
+#[derive(PartialEq, Deserialize, Clone, Debug)]
 #[serde(rename_all = "camelCase")]
 pub struct Config {
   #[serde(default = "default_tauri")]
@@ -113,3 +113,89 @@ pub fn get() -> crate::Result<Config> {
     ),
   }
 }
+
+#[cfg(test)]
+mod test {
+  use super::*;
+  // generate a test_config based on the test fixture
+  fn create_test_config() -> Config {
+    Config {
+      tauri: TauriConfig {
+        window: WindowConfig {
+          width: 800,
+          height: 600,
+          resizable: true,
+          title: String::from("Tauri App"),
+        },
+        embedded_server: EmbeddedServerConfig {
+          host: String::from("http://127.0.0.1"),
+          port: String::from("random"),
+        },
+      },
+      build: BuildConfig {
+        dev_path: String::from("http://localhost:4000"),
+      },
+    }
+  }
+
+  #[test]
+  // test the get function.  Will only resolve to true if the TAURI_CONFIG variable is set properly to the fixture.
+  fn test_get() {
+    // get test_config
+    let test_config = create_test_config();
+
+    // call get();
+    let config = get();
+
+    // check to see if there is an OK or Err, on Err fail test.
+    match config {
+      // On Ok, check that the config is the same as the test config.
+      Ok(c) => assert_eq!(c, test_config),
+      Err(_) => assert!(false),
+    }
+  }
+
+  #[test]
+  // test all of the default functions
+  fn test_defaults() {
+    // get default tauri config
+    let t_config = default_tauri();
+    // get default build config
+    let b_config = default_build();
+    // get default dev path
+    let d_path = default_dev_path();
+    // get default embedded server
+    let de_server = default_embedded_server();
+    // get default window
+    let d_window = default_window();
+    // get default title
+    let d_title = default_title();
+
+    // create a tauri config.
+    let tauri = TauriConfig {
+      window: WindowConfig {
+        width: 800,
+        height: 600,
+        resizable: true,
+        title: String::from("Tauri App"),
+      },
+      embedded_server: EmbeddedServerConfig {
+        host: String::from("http://127.0.0.1"),
+        port: String::from("random"),
+      },
+    };
+
+    // create a build config
+    let build = BuildConfig {
+      dev_path: String::from(""),
+    };
+
+    // test the configs
+    assert_eq!(t_config, tauri);
+    assert_eq!(b_config, build);
+    assert_eq!(de_server, tauri.embedded_server);
+    assert_eq!(d_path, String::from(""));
+    assert_eq!(d_title, tauri.window.title);
+    assert_eq!(d_window, tauri.window);
+  }
+}

+ 64 - 0
tauri/src/file_system.rs

@@ -108,3 +108,67 @@ pub fn read_binary_file<T: 'static>(
     error,
   );
 }
+
+// test webview functionality.
+#[cfg(test)]
+mod test {
+  use super::*;
+  use web_view::*;
+
+  // create a makeshift webview
+  fn create_test_webview() -> crate::Result<WebView<'static, ()>> {
+    // basic html set into webview
+    let content = r#"<html><head></head><body></body></html>"#;
+
+    Ok(
+      // use webview builder to create simple webview
+      WebViewBuilder::new()
+        .title("test")
+        .size(800, 800)
+        .resizable(true)
+        .debug(true)
+        .user_data(())
+        .invoke_handler(|_wv, _arg| Ok(()))
+        .content(Content::Html(content))
+        .build()?,
+    )
+  }
+
+  #[test]
+  #[cfg(not(target_os = "linux"))]
+  // test the file_write functionality
+  fn test_write_to_file() -> crate::Result<()> {
+    // import read_to_string and write to be able to manipulate the file.
+    use std::fs::{read_to_string, write};
+
+    // create the webview
+    let mut webview = create_test_webview()?;
+
+    // setup the contents and the path.
+    let contents = String::from(r#"Write to the Test file"#);
+    let path = String::from("test/fixture/test.txt");
+
+    // clear the file by writing nothing to it.
+    write(&path, "")?;
+
+    //call write file with the path and contents.
+    write_file(
+      &mut webview,
+      path.clone(),
+      contents.clone(),
+      String::from(""),
+      String::from(""),
+    );
+
+    // sleep the main thread to wait for the promise to execute.
+    std::thread::sleep(std::time::Duration::from_millis(200));
+
+    // read from the file.
+    let data = read_to_string(path)?;
+
+    // check that the file contents is equal to the expected contents.
+    assert_eq!(data, contents);
+
+    Ok(())
+  }
+}

+ 0 - 0
tauri/test/fixture/test.txt