Skip to content

Commit

Permalink
Actually fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Feb 26, 2022
1 parent 435d870 commit beb109c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
31 changes: 18 additions & 13 deletions crates/flowistry/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ impl Range {
})
}

pub fn from_byte_range(
byte_start: usize,
byte_end: usize,
src: &str,
filename: String,
) -> Self {
let char_start = src[.. byte_start].graphemes(true).count();
let char_end = char_start + src[byte_start .. byte_end].graphemes(true).count();
Range {
byte_start,
byte_end,
char_start,
char_end,
filename,
}
}

pub fn from_span(span: Span, source_map: &SourceMap) -> Result<Self> {
let file = source_map.lookup_source_file(span.lo());
let filename = match &file.name {
Expand All @@ -126,19 +143,7 @@ impl Range {
let byte_start = source_map.lookup_byte_offset(span.lo()).pos.0 as usize;
let byte_end = source_map.lookup_byte_offset(span.hi()).pos.0 as usize;

let prefix = &src[0 .. byte_start];
let contents = &src[byte_start .. byte_end];

let char_start = prefix.graphemes(true).count();
let char_end = char_start + contents.graphemes(true).count();

Ok(Range {
char_start,
char_end,
byte_start,
byte_end,
filename,
})
Ok(Self::from_byte_range(byte_start, byte_end, src, filename))
}

pub fn source_file<'a>(
Expand Down
16 changes: 3 additions & 13 deletions crates/flowistry/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,7 @@ fn parse_range_map(
k,
vs.into_iter()
.map(|(byte_start, byte_end)| {
let char_start = src[.. byte_start].graphemes(true).count();
let char_end =
char_start + src[byte_start .. byte_end].graphemes(true).count();
Range {
byte_start,
byte_end,
char_start,
char_end,
filename: "dummy.rs".to_string(),
}
Range::from_byte_range(byte_start, byte_end, &clean, "dummy.rs".into())
})
.collect::<Vec<_>>(),
)
Expand Down Expand Up @@ -346,12 +337,11 @@ pub fn test_command_output(

match expected {
Some(expected_path) => {
let expected_file = fs::read(expected_path);
let expected_file = fs::read_to_string(expected_path);
match expected_file {
Ok(file) => {
let output = String::from_utf8(file).unwrap();
let (_output_clean, output_ranges) =
parse_range_map(&output, vec![("`[", "]`")]).unwrap();
parse_range_map(&file, vec![("`[", "]`")]).unwrap();

let expected = match output_ranges.get("`[") {
Some(ranges) => ranges.clone().into_iter().collect::<HashSet<_>>(),
Expand Down

0 comments on commit beb109c

Please sign in to comment.