Skip to content

Commit

Permalink
Setup tests
Browse files Browse the repository at this point in the history
Issue #47
  • Loading branch information
AngheloAlf committed Feb 24, 2024
1 parent 70ea23d commit 33cfa2c
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 3 deletions.
229 changes: 229 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use slinky::{Document, LinkerWriter};

fn main() {
// TODO: don't use expect?
let document =
Document::read_file(Path::new("test_case.yaml")).expect("Error while parsing input file");
let document = Document::read_file(Path::new("tests/input_files/test_case.yaml"))
.expect("Error while parsing input file");

// println!("settings {:#?}", document.settings);

Expand All @@ -20,6 +20,6 @@ fn main() {
writer.end_sections();

writer
.save_linker_script(Path::new("test_case.ld"))
.save_linker_script(Path::new("tests/linker_scripts/test_case.ld"))
.expect("Error writing the linker script");
}
3 changes: 3 additions & 0 deletions slinky/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ edition = "2021"
serde = {version="1.0.197", features = ["derive"]}
serde_yaml = "0.9.32"
thiserror = "1.0.57"

[dev-dependencies]
rstest = "0.18.2"
50 changes: 50 additions & 0 deletions slinky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,53 @@ pub use segment::Segment;
pub use document::Document;

pub use linker_writer::LinkerWriter;

#[cfg(test)]
mod tests {
use std::fs;
use std::path::{Path, PathBuf};

use rstest::rstest;

use crate::Document;
use crate::LinkerWriter;

// Is there a better way to do this?
// Converts "tests/input_files/test_case.yaml" to "tests/`folder_name`/test_case.`extension`"
fn get_target_path(original: &Path, folder_name: &str, extension: &str) -> PathBuf {
let ancestors: Vec<_> = original.ancestors().collect();
let mut test_path = ancestors[2].to_path_buf();

let extension_temp = original.with_extension(extension);
let ld_name = extension_temp
.file_name()
.unwrap()
.to_os_string()
.into_string()
.unwrap();

test_path.extend(Path::new(folder_name));
test_path.extend(Path::new(&ld_name));

test_path
}

#[rstest]
fn test_simple_linker_script_generation(#[files("../tests/input_files/*.yaml")] path: PathBuf) {
let ld_path = get_target_path(&path, "linker_scripts", "ld");

let document = Document::read_file(&path).expect("unable to read original file");

let mut writer = LinkerWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
}
writer.end_sections();

let expected_ld_contents =
fs::read_to_string(ld_path).expect("unable to read expected ld file");

assert_eq!(expected_ld_contents, writer.export_as_string());
}
}
11 changes: 11 additions & 0 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ impl<'a> LinkerWriter<'a> {

Ok(())
}

#[must_use]
pub fn export_as_string(&self) -> String {
let mut ret = String::new();

for line in &self.buffer {
ret += &format!("{}\n", line);
}

ret
}
}

// internal functions
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 33cfa2c

Please sign in to comment.