This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build script generates slow integration tests.
`build.rs` generates a file `tests/test_generated.rs` which contains unit tests to check that the Chawathe+ 96 algorithm can generate a valid edit script for every pair of input files in `tests/`. These tests take a long time to run, and so are marked with `#[ignore]`. They can be run with: `cargo test --no-fail-fast -- --ignored --nocapture` A number of failing cases found with this new test have been added as ordinary test cases. Some code common to the generated integration tests and other files has been factored into `tests/common.rs`.
- Loading branch information
Sarah Mount
committed
Mar 2, 2018
1 parent
7ec9cf0
commit ce44009
Showing
8 changed files
with
266 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
target/ | ||
**/*.rs.bk | ||
Cargo.lock | ||
tests/test_generated.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) 2018 King's College London | ||
// created by the Software Development Team <http://soft-dev.org/> | ||
// | ||
// The Universal Permissive License (UPL), Version 1.0 | ||
// | ||
// Subject to the condition set forth below, permission is hereby granted to any | ||
// person obtaining a copy of this software, associated documentation and/or | ||
// data (collectively the "Software"), free of charge and under any and all | ||
// copyright rights in the Software, and any and all patent rights owned or | ||
// freely licensable by each licensor hereunder covering either (i) the | ||
// unmodified Software as contributed to or provided by such licensor, or (ii) | ||
// the Larger Works (as defined below), to deal in both | ||
// | ||
// (a) the Software, and | ||
// (b) any piece of software and/or hardware listed in the lrgrwrks.txt file | ||
// if one is included with the Software (each a "Larger Work" to which the Software | ||
// is contributed by such licensors), | ||
// | ||
// without restriction, including without limitation the rights to copy, create | ||
// derivative works of, display, perform, and distribute the Software and make, | ||
// use, sell, offer for sale, import, export, have made, and have sold the | ||
// Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
// either these or other terms. | ||
// | ||
// This license is subject to the following condition: The above copyright | ||
// notice and either this complete permission notice or at a minimum a reference | ||
// to the UPL must be included in all copies or substantial portions of the | ||
// Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
//! Write out a file of integration tests which tests every pair of example | ||
//! files in the `tests/` directory. | ||
|
||
#![warn(missing_docs)] | ||
|
||
extern crate glob; | ||
|
||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
use glob::glob; | ||
|
||
/// Directory containing example files for testing and to which the generated | ||
/// tests should be written. | ||
const DIR: &str = "tests/"; | ||
|
||
/// Generate a file of integration tests which run a diff (with both Null and | ||
/// Myers matchers) on every pair of test files in the `tests/` directory. | ||
/// Include diffs of files against themselves. This test takes a long time to | ||
/// run (well over 5 minutes) and so should not be run on CI infrastructure such | ||
/// as Travis. | ||
/// | ||
/// Run with: `cargo test --no-fail-fast -- --ignored --nocapture` | ||
fn main() { | ||
// Glob all example files in the `tests/` directory. | ||
// Filter out files which have been seeded with deliberate lexical or | ||
// syntactic errors. | ||
let test_files: Vec<String> = | ||
glob(&format!("{}*.calc", DIR)).expect("Failed to read glob pattern") | ||
.into_iter() | ||
.chain(glob(&format!("{}*.java", DIR)) | ||
.expect("Failed to read glob pattern") | ||
.into_iter()) | ||
.chain(glob(&format!("{}*.txt", DIR)) | ||
.expect("Failed to read glob pattern") | ||
.into_iter()) | ||
.map(|res| res.unwrap() | ||
.to_str() | ||
.unwrap() | ||
.to_string()) | ||
.filter(|name| !name.contains("_err")) | ||
.collect::<Vec<String>>(); | ||
|
||
let mut outfile = File::create(Path::new(&DIR).join("test_generated.rs")).unwrap(); | ||
write!( | ||
outfile, | ||
r#" | ||
extern crate diffract; | ||
use diffract::myers_matcher::MyersConfig; | ||
use diffract::null_matcher::NullConfig; | ||
mod common; | ||
use common::check_files; | ||
"# | ||
).unwrap(); | ||
|
||
for fname1 in test_files.iter() { | ||
for fname2 in test_files.iter() { | ||
// Remove directory and extension from `fname1`, `fname2`. | ||
// We include the file extension in the test name because some files | ||
// are intended to be parsed by different Yacc files (e.g. | ||
// `Hello.java` and `Hello.txt`). | ||
let name1_ext = fname1[DIR.len()..].split(".").collect::<Vec<&str>>(); | ||
let name2_ext = fname2[DIR.len()..].split(".").collect::<Vec<&str>>(); | ||
let name1 = format!("{}_{}", name1_ext[0], name1_ext[1]).to_lowercase(); | ||
let name2 = format!("{}_{}", name2_ext[0], name2_ext[1]).to_lowercase(); | ||
write!(outfile, r#" | ||
#[test] | ||
#[ignore] | ||
fn test_globbed_{name1}_{name2}_null() {{ | ||
check_files("{fname1}", "{fname2}", Box::new(NullConfig::new())); | ||
}} | ||
#[test] | ||
#[ignore] | ||
fn test_globbed_{name1}_{name2}_myers() {{ | ||
check_files("{fname1}", "{fname2}", Box::new(MyersConfig::new())); | ||
}} | ||
"#, name1=name1, name2=name2, fname1=fname1, fname2=fname2).unwrap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
max_width = 120 | ||
max_width = 90 | ||
indent_style = "Visual" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Hello { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, world!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Test { | ||
public String foo(int i) { | ||
if (i == 0) return "Foo!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2018 King's College London | ||
// created by the Software Development Team <http://soft-dev.org/> | ||
// | ||
// The Universal Permissive License (UPL), Version 1.0 | ||
// | ||
// Subject to the condition set forth below, permission is hereby granted to any | ||
// person obtaining a copy of this software, associated documentation and/or | ||
// data (collectively the "Software"), free of charge and under any and all | ||
// copyright rights in the Software, and any and all patent rights owned or | ||
// freely licensable by each licensor hereunder covering either (i) the | ||
// unmodified Software as contributed to or provided by such licensor, or (ii) | ||
// the Larger Works (as defined below), to deal in both | ||
// | ||
// (a) the Software, and | ||
// (b) any piece of software and/or hardware listed in the lrgrwrks.txt file | ||
// if one is included with the Software (each a "Larger Work" to which the Software | ||
// is contributed by such licensors), | ||
// | ||
// without restriction, including without limitation the rights to copy, create | ||
// derivative works of, display, perform, and distribute the Software and make, | ||
// use, sell, offer for sale, import, export, have made, and have sold the | ||
// Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
// either these or other terms. | ||
// | ||
// This license is subject to the following condition: The above copyright | ||
// notice and either this complete permission notice or at a minimum a reference | ||
// to the UPL must be included in all copies or substantial portions of the | ||
// Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
//! Code common to several integration tests. | ||
#![warn(missing_docs)] | ||
|
||
extern crate diffract; | ||
|
||
use diffract::edit_script::{Chawathe96Config, EditScriptGenerator}; | ||
use diffract::matchers::MatchTrees; | ||
use diffract::parser::{get_lexer, get_parser, parse_file}; | ||
|
||
/// Check that the Chawathe 1996 edit script generator correctly generates an | ||
/// edit script for two files and a given matcher. | ||
pub fn check_files(path1: &str, path2: &str, matcher: Box<MatchTrees<String>>) { | ||
let ast_from = parse_file(path1, &get_lexer(path1), &get_parser(path1)).unwrap(); | ||
let ast_to = parse_file(path2, &get_lexer(path2), &get_parser(path2)).unwrap(); | ||
// Generate mappings between ASTs. | ||
let store = matcher.match_trees(ast_from.clone(), ast_to.clone()); | ||
// Generate an edit script. | ||
let gen_config: Box<EditScriptGenerator<String>> = Box::new(Chawathe96Config::new()); | ||
let edit_script_wrapped = gen_config.generate_script(&store); | ||
assert!(edit_script_wrapped.is_ok(), | ||
"Edit script generator failed to complete."); | ||
// Get root node ids after the edit script has run (note that the root nodes | ||
// may be altered by the edit script). | ||
let root_from = store.from_arena.borrow().root().unwrap(); | ||
let root_to = store.to_arena.borrow().root().unwrap(); | ||
// Test 1: every reachable node in both ASTs should be mapped. The edit | ||
// script generator should have turned the partial matching provided by the | ||
// matcher into a total matching. N.B. deleted nodes in the to AST will be | ||
// in the arena but should not be reachable from the root node. | ||
let mut count_nodes = 0; | ||
let mut count_mapped = 0; | ||
for node in root_from.breadth_first_traversal(&store.from_arena.borrow()) { | ||
count_nodes += 1; | ||
assert!(store.contains_from(&node)); | ||
count_mapped += 1; | ||
} | ||
assert_eq!(count_nodes, count_mapped); | ||
count_nodes = 0; | ||
count_mapped = 0; | ||
for node in root_to.breadth_first_traversal(&store.to_arena.borrow()) { | ||
count_nodes += 1; | ||
assert!(store.contains_to(&node)); | ||
count_mapped += 1; | ||
} | ||
assert_eq!(count_nodes, count_mapped, | ||
"Final mapping not total for files: {} and {}.", | ||
path1, path2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters