Skip to content

Commit

Permalink
test: add more unit tests (#161)
Browse files Browse the repository at this point in the history
* test: remove unused code

* test: main

* test: helpers functions

* test: space between test methods

* test: fix space between test
  • Loading branch information
AlexD10S authored May 13, 2024
1 parent 85353ae commit bb681b2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
22 changes: 17 additions & 5 deletions crates/pop-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,21 @@ fn init() -> Result<Option<Telemetry>> {
Ok(maybe_tel)
}

#[test]
fn verify_cli() {
// https://docs.rs/clap/latest/clap/_derive/_tutorial/chapter_4/index.html
use clap::CommandFactory;
Cli::command().debug_assert()
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn verify_cli() {
// https://docs.rs/clap/latest/clap/_derive/_tutorial/chapter_4/index.html
use clap::CommandFactory;
Cli::command().debug_assert()
}

#[test]
fn test_cache() -> Result<(), Box<dyn std::error::Error>> {
let path = cache()?;
assert_eq!(path.file_name().unwrap().to_str().unwrap().to_string(), "pop");
Ok(())
}
}
17 changes: 0 additions & 17 deletions crates/pop-parachains/src/generator/parachain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// SPDX-License-Identifier: GPL-3.0
use std::path::Path;

use askama::Template;

use crate::utils::helpers::write_to_file;

#[derive(Template)]
#[template(path = "base/chain_spec.templ", escape = "none")]
pub(crate) struct ChainSpec {
Expand All @@ -18,16 +14,3 @@ pub(crate) struct ChainSpec {
pub(crate) struct Network {
pub(crate) node: String,
}

// todo : generate directory structure
// todo : This is only for development
#[allow(unused)]
pub fn generate() {
let cs = ChainSpec {
token_symbol: "DOT".to_owned(),
decimals: 10,
initial_endowment: "1u64 << 15".to_owned(),
};
let rendered = cs.render().unwrap();
let _ = write_to_file(Path::new("src/x.rs"), &rendered);
}
31 changes: 27 additions & 4 deletions crates/pop-parachains/src/utils/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,37 @@ pub struct Release {
#[cfg(test)]
mod tests {
use super::*;
const BASE_PARACHAIN: &str = "https://github.com/r0gue-io/base-parachain";
const POLKADOT_SDK: &str = "https://github.com/paritytech/polkadot-sdk";

#[test]
fn test_parse_org() -> Result<(), Box<dyn std::error::Error>> {
let url = Url::parse(BASE_PARACHAIN)?;
let org = GitHub::org(&url)?;
assert_eq!(org, "r0gue-io");
Ok(())
}

#[test]
fn test_parse_name() -> Result<(), Box<dyn std::error::Error>> {
let url = Url::parse(BASE_PARACHAIN)?;
let name = GitHub::name(&url)?;
assert_eq!(name, "base-parachain");
Ok(())
}

#[test]
fn test_release_url() -> Result<(), Box<dyn std::error::Error>> {
let repo = Url::parse(POLKADOT_SDK)?;
let url = GitHub::release(&repo, &format!("polkadot-v1.9.0"), "polkadot");
assert_eq!(url, format!("{}/releases/download/polkadot-v1.9.0/polkadot", POLKADOT_SDK));
Ok(())
}

#[test]
fn test_convert_to_ssh_url() {
assert_eq!(
GitHub::convert_to_ssh_url(
&Url::parse("https://github.com/r0gue-io/base-parachain")
.expect("valid repository url")
),
GitHub::convert_to_ssh_url(&Url::parse(BASE_PARACHAIN).expect("valid repository url")),
"[email protected]:r0gue-io/base-parachain.git"
);
assert_eq!(
Expand Down
27 changes: 27 additions & 0 deletions crates/pop-parachains/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ pub(crate) fn write_to_file(path: &Path, contents: &str) -> Result<(), Error> {
#[cfg(test)]
mod tests {
use super::*;
use crate::generator::parachain::ChainSpec;
use askama::Template;
use tempfile::tempdir;

#[test]
fn test_write_to_file() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = tempdir()?;
let chainspec = ChainSpec {
token_symbol: "DOT".to_string(),
decimals: 6,
initial_endowment: "1000000".to_string(),
};
let file_path = temp_dir.path().join("file.rs");
let _ = fs::write(&file_path, "");
write_to_file(&file_path, chainspec.render().expect("infallible").as_ref())?;
let generated_file_content =
fs::read_to_string(temp_dir.path().join("file.rs")).expect("Failed to read file");

assert!(generated_file_content
.contains("properties.insert(\"tokenSymbol\".into(), \"DOT\".into());"));
assert!(generated_file_content
.contains("properties.insert(\"tokenDecimals\".into(), 6.into());"));
assert!(generated_file_content.contains("1000000"));

Ok(())
}

#[test]
fn test_is_initial_endowment_valid() {
Expand All @@ -88,6 +114,7 @@ mod tests {
assert_eq!(is_initial_endowment_valid("wrong"), false);
assert_eq!(is_initial_endowment_valid(" "), false);
}

#[test]
fn test_left_shift() {
// Values from https://stackoverflow.com/questions/56392875/how-can-i-initialize-a-users-balance-in-a-substrate-blockchain
Expand Down

0 comments on commit bb681b2

Please sign in to comment.