Skip to content

Commit

Permalink
Clean up syntest.rs example
Browse files Browse the repository at this point in the history
The error handling in test_file was rearranged to use
the try! macro and ok_or combinator. This decreases
indentation and makes the code easier to read.

It is recommended to view this diff without whitespace
to make it clearer what actually changed.
  • Loading branch information
trishume committed Mar 12, 2017
1 parent f9da736 commit 3e95235
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions examples/syntest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn process_assertions(assertion: &AssertionRange, test_against_line_scopes: &Vec
results
}

/// If `parse_test_lines` is `false` then lines that only contain assertions are not parsed
fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool) -> Result<SyntaxTestFileResult, SyntaxTestHeaderError> {
let f = File::open(path).unwrap();
let mut reader = BufReader::new(f);
Expand All @@ -129,19 +130,19 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool) -> Result<Synt
line = line.replace("\r", &"");

// parse the syntax test header in the first line of the file
match SYNTAX_TEST_HEADER_PATTERN.captures(&line.to_string()) {
Some(captures) => {
let header_line = line.clone();
let search_result = SYNTAX_TEST_HEADER_PATTERN.captures(&header_line);
let captures = try!(search_result.ok_or(SyntaxTestHeaderError::MalformedHeader));

let testtoken_start = captures.name("testtoken_start").unwrap().as_str();
let testtoken_end = captures.name("testtoken_end").map_or(None, |c|Some(c.as_str()));
let syntax_file = captures.name("syntax_file").unwrap().as_str();

// find the relevant syntax definition to parse the file with - case is important!
println!("The test file references syntax definition file: {}", syntax_file);
let syntax = match ss.find_syntax_by_path(syntax_file) {
Some(syntax) => syntax,
None => return Err(SyntaxTestHeaderError::SyntaxDefinitionNotFound)
};
let syntax = try!(ss.find_syntax_by_path(syntax_file).ok_or(SyntaxTestHeaderError::SyntaxDefinitionNotFound));

// iterate over the lines of the file, testing them
let mut state = ParseState::new(syntax);
let mut stack = ScopeStack::new();

Expand All @@ -153,14 +154,14 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool) -> Result<Synt
let mut assertion_failures: usize = 0;
let mut total_assertions: usize = 0;

loop {
loop { // over lines of file, starting with the header line
let mut line_only_has_assertion = false;
let mut line_has_assertion = false;
if let Some(assertion) = get_line_assertion_details(testtoken_start, testtoken_end, &line) {
let result = process_assertions(&assertion, &scopes_on_line_being_tested);
total_assertions += &assertion.end_char - &assertion.begin_char;
for failure in result.iter().filter(|r|!r.success) {
let chars: Vec<char> = previous_non_assertion_line.chars().skip(failure.column_begin).take(failure.column_end - failure.column_begin).collect();
let chars = &previous_non_assertion_line[failure.column_begin..failure.column_end];
println!(" Assertion selector {:?} \
from line {:?} failed against line {:?}, column range {:?}-{:?} \
(with text {:?}) \
Expand All @@ -187,7 +188,8 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool) -> Result<Synt
stack.apply(op);
if s.is_empty() { // in this case we don't care about blank tokens
continue;
} else if !line_has_assertion {
}
if !line_has_assertion {
// if the line has no assertions on it, remember the scopes on the line so we can test against them later
let len = s.chars().count();
scopes_on_line_being_tested.push(
Expand All @@ -210,15 +212,10 @@ fn test_file(ss: &SyntaxSet, path: &Path, parse_test_lines: bool) -> Result<Synt
}
line = line.replace("\r", &"");
}
Ok(
if assertion_failures > 0 {
SyntaxTestFileResult::FailedAssertions(assertion_failures, total_assertions)
Ok(SyntaxTestFileResult::FailedAssertions(assertion_failures, total_assertions))
} else {
SyntaxTestFileResult::Success(total_assertions)
}
)
},
None => Err(SyntaxTestHeaderError::MalformedHeader)
Ok(SyntaxTestFileResult::Success(total_assertions))
}
}

Expand Down

0 comments on commit 3e95235

Please sign in to comment.