-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathparser_tests.rs
More file actions
101 lines (84 loc) · 2.9 KB
/
parser_tests.rs
File metadata and controls
101 lines (84 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::{
ffi::{CStr, CString},
mem::MaybeUninit,
path::Path,
};
use ruby_prism_sys::{
pm_comment_t, pm_comment_type_t, pm_diagnostic_t, pm_node_destroy, pm_parse, pm_parser_free, pm_parser_init,
pm_parser_t,
};
fn ruby_file_contents() -> (CString, usize) {
let rust_path = Path::new(env!("CARGO_MANIFEST_DIR"));
let ruby_file_path = rust_path.join("../../lib/prism.rb").canonicalize().unwrap();
let file_contents = std::fs::read_to_string(ruby_file_path).unwrap();
let len = file_contents.len();
(CString::new(file_contents).unwrap(), len)
}
#[test]
fn init_test() {
let (ruby_file_contents, len) = ruby_file_contents();
let source = ruby_file_contents.as_ptr().cast::<u8>();
let mut parser = MaybeUninit::<pm_parser_t>::uninit();
unsafe {
pm_parser_init(parser.as_mut_ptr(), source, len, std::ptr::null());
let parser = parser.assume_init_mut();
pm_parser_free(parser);
}
}
#[test]
fn comments_test() {
let source = CString::new("# Meow!").unwrap();
let mut parser = MaybeUninit::<pm_parser_t>::uninit();
unsafe {
pm_parser_init(
parser.as_mut_ptr(),
source.as_ptr().cast::<u8>(),
source.as_bytes().len(),
std::ptr::null(),
);
let parser = parser.assume_init_mut();
let node = pm_parse(parser);
let comment_list = &parser.comment_list;
let comment = comment_list.head as *const pm_comment_t;
assert_eq!((*comment).type_, pm_comment_type_t::PM_COMMENT_INLINE);
let location = {
let start = (*comment).location.start;
let end = (*comment).location.start + (*comment).location.length;
start..end
};
assert_eq!(location, 0..7);
pm_node_destroy(parser, node);
pm_parser_free(parser);
}
}
#[test]
fn diagnostics_test() {
let source = CString::new("class Foo;").unwrap();
let mut parser = MaybeUninit::<pm_parser_t>::uninit();
unsafe {
pm_parser_init(
parser.as_mut_ptr(),
source.as_ptr().cast::<u8>(),
source.as_bytes().len(),
std::ptr::null(),
);
let parser = parser.assume_init_mut();
let node = pm_parse(parser);
let error_list = &parser.error_list;
assert!(!error_list.head.is_null());
let error = error_list.head as *const pm_diagnostic_t;
let message = CStr::from_ptr((*error).message);
assert_eq!(
message.to_string_lossy(),
"unexpected end-of-input, assuming it is closing the parent top level context"
);
let location = {
let start = (*error).location.start;
let end = (*error).location.start + (*error).location.length;
start..end
};
assert_eq!(location, 10..10);
pm_node_destroy(parser, node);
pm_parser_free(parser);
}
}