Skip to content

Commit

Permalink
misc: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Jun 19, 2020
1 parent 7db6f58 commit 0b391ed
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
8 changes: 4 additions & 4 deletions crates/mun_project/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ impl Manifest {

/// Returns the name of the package
pub fn name(&self) -> &str {
&self.package_id.name
&self.package_id.name()
}

/// Returns the version of the package
pub fn version(&self) -> &semver::Version {
&self.package_id.version
&self.package_id.version()
}

/// Returns the metadata information of the package
Expand All @@ -67,7 +67,7 @@ impl PackageId {

impl fmt::Display for PackageId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} v{}", self.name, self.version)
write!(f, "{} v{}", self.name(), self.version())
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ mod tests {
manifest.version(),
&semver::Version::from_str("0.2.0").unwrap()
);
assert_eq!(manifest.metadata.authors, vec!["Mun Team"]);
assert_eq!(manifest.metadata().authors, vec!["Mun Team"]);
assert_eq!(format!("{}", manifest.package_id()), "test v0.2.0");
}
}
7 changes: 2 additions & 5 deletions crates/mun_project/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ impl Package {
pub fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
let path = path.as_ref();
let manifest = Manifest::from_file(path)?;
Ok(Self {
manifest,
manifest_path: path.to_path_buf(),
})
Ok(Self::new(manifest, path))
}

/// Returns the manifest
Expand Down Expand Up @@ -57,7 +54,7 @@ impl Package {

/// Returns the version of the package
pub fn version(&self) -> &Version {
self.manifest().version()
self.package_id().version()
}

/// Returns the source directory of the package, or None if no such directory exists.
Expand Down
30 changes: 30 additions & 0 deletions crates/mun_project/tests/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use mun_project::{Manifest, Package};
use semver::Version;
use std::path::Path;
use std::str::FromStr;

#[test]
fn manifest_from_file() {
let manifest_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/resources/mun.toml");
let manifest = Manifest::from_file(&manifest_path).expect("could not load manifest");
assert_eq!(manifest.metadata().authors, vec!["Mun Team"]);
assert_eq!(manifest.version(), &Version::from_str("0.2.0").unwrap());
assert_eq!(manifest.name(), "test");
}

#[test]
fn package_from_file() {
let manifest_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/resources/mun.toml");
let package = Package::from_file(&manifest_path).expect("could not load package");
assert_eq!(package.name(), "test");
assert_eq!(package.version(), &Version::from_str("0.2.0").unwrap());
assert_eq!(package.manifest().metadata().authors, vec!["Mun Team"]);
assert_eq!(package.manifest_path(), &manifest_path);
assert_eq!(&package.root(), &manifest_path.parent().unwrap());
assert_eq!(format!("{}", &package), "test v0.2.0");

let source_dir = package
.source_directory()
.expect("could not locate source directory");
assert_eq!(source_dir, manifest_path.parent().unwrap().join("src"));
}
4 changes: 4 additions & 0 deletions crates/mun_project/tests/resources/mun.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name="test"
version="0.2.0"
authors = ["Mun Team"]
2 changes: 2 additions & 0 deletions crates/mun_project/tests/resources/src/main.mun
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub fn main() {
}

0 comments on commit 0b391ed

Please sign in to comment.