Skip to content

Commit

Permalink
Merge pull request #11 from josueBarretogit/make-tests
Browse files Browse the repository at this point in the history
added test (for real)
  • Loading branch information
josueBarretogit authored Jun 5, 2024
2 parents c29d431 + 11df8e2 commit f9ea0bb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use strum::{Display, EnumIter, FromRepr};

use crate::view::widgets::{CrateItemList, FeatureItemList, ItemListStatus};

pub mod backend;

#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Crates {
Expand Down
1 change: 0 additions & 1 deletion src/backend/backend.rs

This file was deleted.

107 changes: 102 additions & 5 deletions src/content_parser.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::backend::{Categories, CategoriesWithSubCategories, Crates, Table, TableEntry};
use crate::scraper::scraper::Group;

pub mod content_parser;
pub mod jsoncontentparser;

pub trait ContentParser {
fn get_general_crates(&self) -> Table;
fn get_general_crates(&self) -> Table;

fn get_crates(&self, category: &Categories) -> Table;
fn get_crates(&self, category: &Categories) -> Table;

fn get_crates_with_sub(&self, category: &CategoriesWithSubCategories) -> Table;
fn get_crates_with_sub(&self, category: &CategoriesWithSubCategories) -> Table;
}

impl From<&Group> for Table {
Expand All @@ -27,7 +27,7 @@ impl From<&Group> for Table {
.notes
.clone()
.unwrap_or("No description".to_string()),
features: None
features: None,
});
}

Expand Down Expand Up @@ -67,3 +67,100 @@ impl From<&Group> for Table {
Table { entries }
}
}

#[cfg(test)]
mod test {
use self::jsoncontentparser::JsonContentParser;

use super::*;


async fn setup_json_content_parser() -> JsonContentParser {
JsonContentParser::parse_content().await
}

fn test_section(entries: &[TableEntry], name_first_recommended_crate: &str) {
assert!(!entries.is_empty());

let first_entry = entries.first().unwrap();
assert!(!first_entry.crates.is_empty());

let first_recommended_crate = first_entry.crates.first().unwrap();

assert_eq!(first_recommended_crate.name, name_first_recommended_crate);
}

#[tokio::test]
async fn general_crates_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section = json_content_parser.get_general_crates();
test_section(&section.entries, "rand");
}

#[tokio::test]
async fn common_crates_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section = json_content_parser.get_crates_with_sub(&CategoriesWithSubCategories::Common);
test_section(&section.entries, "anyhow");
}

#[tokio::test]
async fn math_section_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section = json_content_parser.get_crates(&Categories::Math);
test_section(&section.entries, "num-traits");
}

#[tokio::test]
async fn ffi_section_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section = json_content_parser.get_crates(&Categories::FFI);
test_section(&section.entries, "bindgen");
}

#[tokio::test]
async fn cryptography_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section = json_content_parser.get_crates(&Categories::Cryptography);
test_section(&section.entries, "argon2");
}

#[tokio::test]
async fn concurrency_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section =
json_content_parser.get_crates_with_sub(&CategoriesWithSubCategories::Concurrency);
test_section(&section.entries, "parking_lot");
}

#[tokio::test]
async fn networking_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section =
json_content_parser.get_crates_with_sub(&CategoriesWithSubCategories::Networking);
test_section(&section.entries, "tokio");
}

#[tokio::test]
async fn databases_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section =
json_content_parser.get_crates_with_sub(&CategoriesWithSubCategories::Databases);
test_section(&section.entries, "sqlx");
}

#[tokio::test]
async fn cli_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section = json_content_parser.get_crates_with_sub(&CategoriesWithSubCategories::Clis);
test_section(&section.entries, "clap");
}

#[tokio::test]
async fn graphics_has_expected_data() {
let json_content_parser = setup_json_content_parser().await;
let section =
json_content_parser.get_crates_with_sub(&CategoriesWithSubCategories::Graphics);
test_section(&section.entries, "gtk4");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl JsonContentParser {
let mut clis_crates = Table::default();
let mut graphics_crates = Table::default();

page_content.crate_groups.iter().for_each(|group| {
for group in &page_content.crate_groups {
match group.name.trim().to_lowercase().as_str() {
"common" => {
//extract general table
Expand Down Expand Up @@ -72,7 +72,7 @@ impl JsonContentParser {
"graphics" => graphics_crates = group.into(),
_ => {}
}
});
}

Self {
content: page_content,
Expand All @@ -88,27 +88,22 @@ impl JsonContentParser {
general_crates,
}
}



}


impl ContentParser for JsonContentParser {

fn get_general_crates(&self) -> Table {
impl ContentParser for JsonContentParser {
fn get_general_crates(&self) -> Table {
self.general_crates.clone()
}

fn get_crates(&self, category: &Categories) -> Table {
fn get_crates(&self, category: &Categories) -> Table {
match category {
Categories::FFI => self.ffi_crates.clone(),
Categories::Math => self.math_crates.clone(),
Categories::Cryptography => self.cryptography_crates.clone(),
}
}

fn get_crates_with_sub(&self, category: &CategoriesWithSubCategories) -> Table {
fn get_crates_with_sub(&self, category: &CategoriesWithSubCategories) -> Table {
match category {
CategoriesWithSubCategories::Clis => self.clis_crates.clone(),
CategoriesWithSubCategories::Common => self.common_crates.clone(),
Expand All @@ -118,5 +113,4 @@ impl ContentParser for JsonContentParser {
CategoriesWithSubCategories::Concurrency => self.concurrency_crates.clone(),
}
}

}
2 changes: 1 addition & 1 deletion src/tui/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crossterm::event::{KeyCode, KeyEventKind};
use ratatui::{backend::CrosstermBackend, Terminal};
use tokio::sync::mpsc::{self, UnboundedSender};

use crate::content_parser::content_parser::JsonContentParser;
use crate::content_parser::jsoncontentparser::JsonContentParser;
use crate::dependency_builder::CrateToAdd;
use crate::utils::select_crate_if_features_are_selected;
use crate::view::widgets::{CategoriesTabs, CrateItemList, FeatureItemList, ItemListStatus};
Expand Down

0 comments on commit f9ea0bb

Please sign in to comment.