feat(cli): support snapshot testing with --update flag

This PR adds an `--update` flag to the `tree-sitter test` command, which adds the ability to replace the _expected_ output in the corpus.txt with the _actual_ output produced by the parser, that is, we can now simply use this `--update` flag to write all the corresponding parser output back to the corpus.txt, and we just need to check the output without typing its actual sexp.

- use the same output format as `tree-sitter parse`, except there won't be any position information printed.
- the corpus.txt won't be touched if there's no difference between the _expected_ output and the _actual_ output in that file.
- if there're differences between _expected_ and _actual_, only the test case that is different will be replaced, the rest test cases will stay as-is. (All the delimiters `===`/`---` will be normalized as 80-column long, though.)
- this flag also works with `--filter` flag.
This commit is contained in:
Ika 2019-09-01 23:52:39 +08:00
parent 4b0489e2f3
commit 1b033fdfa4
6 changed files with 274 additions and 105 deletions

View file

@ -59,8 +59,8 @@ fn test_real_language_corpus_files() {
let language = get_language(language_name);
let corpus_dir = grammars_dir.join(language_name).join("corpus");
let error_corpus_file = error_corpus_dir.join(&format!("{}_errors.txt", language_name));
let main_tests = parse_tests(&corpus_dir).unwrap();
let error_tests = parse_tests(&error_corpus_file).unwrap_or(TestEntry::default());
let main_tests = parse_tests(&corpus_dir, true).unwrap();
let error_tests = parse_tests(&error_corpus_file, true).unwrap_or(TestEntry::default());
let mut tests = flatten_tests(main_tests);
tests.extend(flatten_tests(error_tests));
@ -243,7 +243,7 @@ fn test_feature_corpus_files() {
let corpus_path = test_path.join("corpus.txt");
let c_code = generate_result.unwrap().1;
let language = get_test_language(language_name, &c_code, Some(&test_path));
let test = parse_tests(&corpus_path).unwrap();
let test = parse_tests(&corpus_path, true).unwrap();
let tests = flatten_tests(test);
if !tests.is_empty() {
@ -381,7 +381,7 @@ fn flatten_tests(test: TestEntry) -> Vec<(String, Vec<u8>, String, bool)> {
}
result.push((name, input, output, has_fields));
}
TestEntry::Group { mut name, children } => {
TestEntry::Group { mut name, children, .. } => {
if !prefix.is_empty() {
name.insert_str(0, " - ");
name.insert_str(0, prefix);