aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-15 21:17:00 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-15 21:17:00 +0000
commit60f721b94ae70533a4a6ee9ffa7d24e5a19abff5 (patch)
treea057cd3a3a2f6a7ed0c650cc8b5914777164acde
parent15e40bf3d028b383afbf7251910a121807e0d1b1 (diff)
Add unit tests for Context
-rw-r--r--src/executor.rs66
-rw-r--r--src/main.rs4
2 files changed, 55 insertions, 15 deletions
diff --git a/src/executor.rs b/src/executor.rs
index d9bfaee..189df6f 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -1,20 +1,26 @@
use crate::barbfile::{BarbFile, Header};
use std::collections::HashMap;
-use std::env;
use ureq;
-struct Context {
+pub struct Context {
vars: HashMap<String, String>,
}
impl Context {
- pub fn new() -> Context {
- let mut vars = HashMap::new();
- vars.extend(env::vars());
-
+ pub fn new<I>(vars: I) -> Context
+ where
+ I: Iterator<Item = (String, String)>,
+ {
+ let mut toto = HashMap::new();
+ toto.extend(vars);
+
+ Context { vars: toto }
+ }
+
+ pub fn empty() -> Context {
Context {
- vars
+ vars: HashMap::new()
}
}
@@ -41,25 +47,32 @@ impl Context {
}
pub struct Executor {
- context: Context
+ context: Context,
}
impl Executor {
- pub fn new() -> Executor {
+ pub fn new(context: Context) -> Executor {
Executor {
- context: Context::new()
+ context
}
}
pub fn execute(&mut self, bfile: BarbFile) -> Result<ureq::Response, String> {
let mut req = ureq::request(
bfile.method_as_string().as_str(),
- self.context.substitute(&bfile.url()).as_str()
+ self.context.substitute(&bfile.url()).as_str(),
);
for header in bfile.headers() {
- req = req.set(header.name(), self.context.substitute(header.value()).as_str());
- println!("{} {}", header.name(), self.context.substitute(header.value()));
+ req = req.set(
+ header.name(),
+ self.context.substitute(header.value()).as_str(),
+ );
+ println!(
+ "{} {}",
+ header.name(),
+ self.context.substitute(header.value())
+ );
}
match bfile.method().takes_body() {
@@ -74,3 +87,30 @@ impl Executor {
}
// TODO: tests
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_context_key_str() {
+ let ctx = Context::empty();
+ assert_eq!(ctx.key_str(&String::from("foo")), String::from("{foo}"));
+ }
+
+ #[test]
+ fn test_context_substitute() {
+ let vars: Vec<(String, String)> = vec![
+ (String::from("foo"), String::from("bar")),
+ (String::from("bar"), String::from("baz")),
+ ];
+ let ctx = Context::new(vars.into_iter());
+ assert_eq!(
+ ctx.substitute(&String::from("blah blah {foo} blah")),
+ String::from("blah blah bar blah")
+ );
+ assert_eq!(
+ ctx.substitute(&String::from("blah {foo} {bar} blah")),
+ String::from("blah bar baz blah")
+ );
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 67c6541..db517d1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,7 +4,7 @@ mod executor;
use jsonformat::{format_json, Indentation};
use barbfile::BarbFile;
-use executor::Executor;
+use executor::{Context, Executor};
use clap::Parser;
@@ -47,7 +47,7 @@ impl Args {
fn main() {
let args = Args::parse();
- let mut executor = Executor::new();
+ let mut executor = Executor::new(Context::new(env::vars()));
let bfile = BarbFile::from_str(
fs::read_to_string("test.barb")
.expect("Failed to read file")