Skip to content

Commit

Permalink
Modified and added 5 tests to config_to_desktop_file_str.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwctor committed May 24, 2024
1 parent 90b72d4 commit a2cfb98
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub struct Config {
// ------------------------------------

impl Config {
pub fn new(){
Config{..Config::default()};
pub fn new() -> Config{
Config{..Config::default()}
}
// To test:
// Make sure that program_dir_str has the same dir as program_dir
Expand Down Expand Up @@ -76,30 +76,30 @@ impl Config {
// Method to create a .desktop file from a Config.
pub fn config_to_desktop_file_str(config: &Config) -> String {
// Format follows https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
let title = "
[Desktop Entry]
# This file has been written by stm_add.
# stm_add creates files following the format of:
# https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
";

let required_values = format!("
# -----------------------------------------------------------
# Required Values
# -----------------------------------------------------------
Type = {}
Name = {}
Exec = {}
# URL is for links.
# URL = _insert_link_here_ \n
", "Application", config.file_name , config.file_dir_str);

let optional_values = "
# -----------------------------------------------------------
# Optional Values
# -----------------------------------------------------------
"; // Implement optional values here
let title =
"[Desktop Entry]
# This file has been written by stm_add.
# stm_add creates files following the format of:
# https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys\n
";

let required_values = format!(
"# -----------------------------------------------------------
# Required Values
# -----------------------------------------------------------
Type = {}
Name = {}
Exec = {}
# URL is for links.
# URL = _insert_link_here_ \n
", "Application", config.file_name , config.file_dir_str);

let optional_values =
"# -----------------------------------------------------------
# Optional Values
# -----------------------------------------------------------
"; // Implement optional values here

format!("{}{}{}", title, required_values, optional_values)

Expand Down
85 changes: 85 additions & 0 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,90 @@
#[cfg(test)]
mod unit_tests {
// Tests validity of the desktop file created by Config
#[cfg(test)]
mod config_to_desktop_file {
use stm_add::Config;
#[test]
fn title_is_up_to_standard(){
let config = Config::new();
let desktop_file = Config::config_to_desktop_file_str(&config);

let mut title = desktop_file.lines().take(4);
let line_1 = title.next().unwrap();
let line_2 = title.next().unwrap();
let line_3 = title.next().unwrap();
let line_4 = title.next().unwrap();

assert_eq!(line_1, "[Desktop Entry]");
assert_eq!(line_2, "# This file has been written by stm_add.");
assert_eq!(line_3, "# stm_add creates files following the format of:");
assert_eq!(line_4, "# https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys");


}
// All desktop_files created by stm_add have to have this title:
#[test]
fn required_values_is_up_to_standard(){
let config = Config::new();
let desktop_file = Config::config_to_desktop_file_str(&config);

let mut required_values = desktop_file.lines().skip(5).take(5);

let line_1 = required_values.next().unwrap();
let line_2 = required_values.next().unwrap();
let line_3 = required_values.next().unwrap();
let line_4 = required_values.next().unwrap();
let line_5 = required_values.next().unwrap();

// Checks if lines 1 and 3 are coments
assert_eq!(line_1, "# -----------------------------------------------------------");
assert_eq!(line_3, "# -----------------------------------------------------------");

// Checks if line 2 is the title
assert_eq!(line_2, "# Required Values");

assert_eq!(&line_4[..7], "Type = ");
assert_eq!(&line_5[..7], "Name = ");
}

// Currently the type field only supports application, so this unit test tests that
// behavior.
#[test]
fn type_field_is_application(){
let config = Config::new();
let desktop_file = Config::config_to_desktop_file_str(&config);

let type_field_line = desktop_file.lines().skip(8).next().unwrap();
assert_eq!("Type = Application", type_field_line);
}

#[test]
fn file_name_in_config_is_correct_in_desktop_file_str(){
let config = Config{
file_name: "Test_Name.exe".to_string(),
..Config::default()
};

let desktop_file = Config::config_to_desktop_file_str(&config);

let type_field_line = desktop_file.lines().skip(9).next().unwrap();
assert_eq!("Name = Test_Name.exe", type_field_line);
}

#[test]
fn file_dir_str_in_config_becomes_exec_field(){
let config = Config{
file_dir_str: "./home/generic_user/programs/smt_add".to_string(),
..Config::default()
};

let desktop_file = Config::config_to_desktop_file_str(&config);

let exec_field = desktop_file.lines().skip(10).next().unwrap();
assert_eq!("Exec = ./home/generic_user/programs/smt_add", exec_field);

}
}

// A test template to be deleted eventually
#[test]
Expand Down

0 comments on commit a2cfb98

Please sign in to comment.