Taylor Bockman 7 years ago
parent
commit
3eb729653a
  1. 13
      src/lib.rs
  2. 26
      tests/lib.rs

13
src/lib.rs

@ -4,6 +4,8 @@
mod commands;
use std::io::{BufRead, Write};
#[derive(Debug)]
pub struct Engine<'a, R, W> {
@ -31,11 +33,16 @@ pub struct Engine<'a, R, W> {
/// Additionally this code should have a parser. You call it with the reader supplied and it waits for a command
/// and parses it into a tuple of some kind for the user.
impl<'a, R, W> Engine<'a, R, W> {
impl<'a, R, W> Engine<'a, R, W>
where
R: BufRead,
W: Write,
{
pub fn new(name: &'a str, author: &'a str, reader: R, writer: W) -> Engine<'a, R, W> {
// TODO: This should also take an EngineOptions thing that indicates which options are supported
// so that when called it can send them easily.
// Engine options are all optional and appeared to be fixed by the UCI standard.
Engine {
name: name,
@ -47,8 +54,8 @@ impl<'a, R, W> Engine<'a, R, W> {
/// Sends identification messages to the writer for the GUI to pick up
pub fn identify(&mut self) {
let name_id: String = format!("{} {} {}", commands::ID, commands::NAME, self.name);
let author_id: String = format!("{} {} {}", commands::ID, commands::AUTHOR, self.author);
let name_id: String = format!("{} {} {}\n", commands::ID, commands::NAME, self.name);
let author_id: String = format!("{} {} {}\n", commands::ID, commands::AUTHOR, self.author);
// For these two writes we can panic - there's no possibility of recovery if the engine fails at this stage
write!(&mut self.writer, "{}", name_id).expect("failed to send name identification to writer");

26
tests/lib.rs

@ -4,8 +4,30 @@ use uci::Engine;
#[test]
fn instantiate_new_engine() {
let e: Engine;
e = Engine::new("test_name", "test");
let input = b"UNUSED";
let mut output = Vec::new();
let e = Engine::new("test_name", "test", &input[..], &mut output);
assert_eq!(e.name, "test_name");
assert_eq!(e.author, "test");
}
#[test]
fn send_indentification_data() {
let input = b"UNUSED";
let mut output = Vec::new();
// We need to scope this so that the mutable borrow ends and we can test it safely
{
let mut e = Engine::new("test_name", "test", &input[..], &mut output);
e.identify();
}
println!("HERE!!!!!!!");
println!("{:?}")
// To get this to play nicely the vector needs to be converted to utf-8 string(s) for comparison
//assert_eq!(output, [b"id name test_name\n", b"id author test\n"]);
}

Loading…
Cancel
Save