compiler: Add a test for mapping offset to/from line/column

... in SourceFile. Let's hope that this will finally make sure that
this works properly!
This commit is contained in:
Tobias Hunger 2023-04-27 17:39:14 +02:00 committed by Tobias Hunger
parent 31147fecd7
commit cf1984f544

View file

@ -520,3 +520,53 @@ impl BuildDiagnostics {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_source_file_offset_line_column_mapping() {
let content = r#"import { LineEdit, Button, Slider, HorizontalBox, VerticalBox } from "std-widgets.slint";
component MainWindow inherits Window {
property <duration> total-time: slider.value * 1s;
callback tick(duration);
VerticalBox {
HorizontalBox {
padding-left: 0;
Text { text: "Elapsed Time:"; }
Rectangle {
Rectangle {
height: 100%;
background: lightblue;
}
}
}
}
}
"#.to_string();
let sf = SourceFileInner::new(PathBuf::from("foo.slint"), content.clone());
let mut line = 1;
let mut column = 1;
for offset in 0..content.len() {
let b = *content.as_bytes().get(offset).unwrap();
assert_eq!(sf.offset(line, column), offset);
assert_eq!(sf.line_column(offset), (line, column));
if b == b'\n' {
line += 1;
column = 1;
} else {
column += 1;
}
}
}
}