Skip to content

Commit

Permalink
test(cursor): initial test
Browse files Browse the repository at this point in the history
Implementation has to follow next
  • Loading branch information
Byron committed Apr 16, 2015
1 parent bf37e51 commit c9c3ad0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/rust/cli/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,32 @@ use std::io;
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use std::string::ToString;
use std::io::{Write, Read, stdout};

use std::default::Default;

const FIELD_SEP: &'static str = ".";

#[derive(Clone, Default)]
pub struct FieldCursor(Vec<String>);

impl ToString for FieldCursor {
fn to_string(&self) -> String {
String::new()
}
}

impl FieldCursor {
pub fn set(&mut self, value: &str) -> Result<(), CLIError> {
Ok(())
}

pub fn num_fields(&self) -> usize {
self.0.len()
}
}

pub fn parse_kv_arg<'a>(kv: &'a str, err: &mut InvalidOptionsError)
-> (&'a str, Option<&'a str>) {
let mut add_err = || err.issues.push(CLIError::InvalidKeyValueSyntax(kv.to_string()));
Expand Down
40 changes: 40 additions & 0 deletions src/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,44 @@ bar\r\n\
assert_eq!(r.0.first, 2);
assert_eq!(r.0.last, 42);
}
}

#[cfg(test)]
mod test_cli {
use super::cli::cmn::*;

use std::default::Default;

#[test]
fn cursor() {
let mut c: FieldCursor = Default::default();

assert_eq!(c.to_string(), "");
assert_eq!(c.num_fields(), 0);
assert!(c.set(".").is_ok());
assert!(c.set("..").is_err());
assert_eq!(c.num_fields(), 0);

assert!(c.set("foo").is_ok());
assert_eq!(c.to_string(), "foo");
assert_eq!(c.num_fields(), 1);
assert!(c.set("..").is_ok());
assert_eq!(c.num_fields(), 0);
assert_eq!(c.to_string(), "");

assert!(c.set("foo.").is_err());

assert!(c.set("foo.bar").is_ok());
assert_eq!(c.num_fields(), 2);
assert_eq!(c.to_string(), "foo.bar");
assert!(c.set("sub.level").is_ok());
assert_eq!(c.num_fields(), 4);
assert_eq!(c.to_string(), "foo.bar.sub.level");
assert!(c.set("...other").is_ok());
assert_eq!(c.to_string(), "foo.bar.other");
assert_eq!(c.num_fields(), 3);
assert!(c.set(".one.two.three...beer").is_ok());
assert_eq!(c.num_fields(), 2);
assert_eq!(c.to_string(), "one.beer");
}
}

0 comments on commit c9c3ad0

Please sign in to comment.