diff options
| author | yukang <moorekang@gmail.com> | 2022-10-19 11:46:26 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2022-10-19 21:08:00 +0800 |
| commit | eb8aa9759dc99b604145f94e5296b7add60e0a48 (patch) | |
| tree | f4bcf4c6f5de2d125e70f2dae32a351a561d6bc9 /compiler/rustc_span/src/source_map/tests.rs | |
| parent | 0af255a5aa448efe6ab7e3252e85081128beaa9e (diff) | |
| download | rust-eb8aa9759dc99b604145f94e5296b7add60e0a48.tar.gz rust-eb8aa9759dc99b604145f94e5296b7add60e0a48.zip | |
Add testcase for next_point, fix more trivial issues in find_width_of_character_at_span
Diffstat (limited to 'compiler/rustc_span/src/source_map/tests.rs')
| -rw-r--r-- | compiler/rustc_span/src/source_map/tests.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/compiler/rustc_span/src/source_map/tests.rs b/compiler/rustc_span/src/source_map/tests.rs index 3058ec45a64..1fd81018fa0 100644 --- a/compiler/rustc_span/src/source_map/tests.rs +++ b/compiler/rustc_span/src/source_map/tests.rs @@ -479,3 +479,48 @@ fn path_prefix_remapping_expand_to_absolute() { RealFileName::Remapped { local_path: None, virtual_name: path("XYZ/src/main.rs") } ); } + +#[test] +fn test_next_point() { + let sm = SourceMap::new(FilePathMapping::empty()); + sm.new_source_file(PathBuf::from("example.rs").into(), "a…b".to_string()); + + // Dummy spans don't advance. + let span = DUMMY_SP; + let span = sm.next_point(span); + assert_eq!(span.lo().0, 0); + assert_eq!(span.hi().0, 0); + + // Span advance respect multi-byte character + let span = Span::with_root_ctxt(BytePos(0), BytePos(1)); + assert_eq!(sm.span_to_snippet(span), Ok("a".to_string())); + let span = sm.next_point(span); + assert_eq!(sm.span_to_snippet(span), Ok("…".to_string())); + assert_eq!(span.lo().0, 1); + assert_eq!(span.hi().0, 4); + + // An empty span pointing just before a multi-byte character should + // advance to contain the multi-byte character. + let span = Span::with_root_ctxt(BytePos(1), BytePos(1)); + let span = sm.next_point(span); + assert_eq!(span.lo().0, 1); + assert_eq!(span.hi().0, 4); + + let span = Span::with_root_ctxt(BytePos(1), BytePos(4)); + let span = sm.next_point(span); + assert_eq!(span.lo().0, 4); + assert_eq!(span.hi().0, 5); + + // A non-empty span at the last byte should advance to create an empty + // span pointing at the end of the file. + let span = Span::with_root_ctxt(BytePos(4), BytePos(5)); + let span = sm.next_point(span); + assert_eq!(span.lo().0, 5); + assert_eq!(span.hi().0, 5); + + // Empty span pointing just past the last byte. + let span = Span::with_root_ctxt(BytePos(5), BytePos(5)); + let span = sm.next_point(span); + assert_eq!(span.lo().0, 5); + assert_eq!(span.hi().0, 5); +} |
