Skip to content

Commit 73d3431

Browse files
committed
Parse testing inputs
1 parent 60d3ca5 commit 73d3431

5 files changed

Lines changed: 99 additions & 32 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crossbeam = "0.8.4"
1111
dashmap = "6.1.0"
1212
expect-test = "1.5.1"
1313
futures = "0.3.31"
14+
indoc = "2.0.6"
1415
itertools = "0.14.0"
1516
lazy-regex = "3.4.1"
1617
lazy_static = "1.5.0"

src/completion.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ mod test {
211211
.await;
212212
}
213213

214-
async fn check_complete_flake(source: &str, _old_lock_file: Option<&str>, expected: Expect) {
214+
async fn check_complete_files(source: &str, expected: Expect) {
215215
let (left, right) = source.split("$0").collect_tuple().unwrap();
216216
let offset = left.len() as u32;
217217

@@ -936,37 +936,6 @@ mod test {
936936
.await;
937937
}
938938

939-
#[test_log::test(tokio::test)]
940-
async fn test_complete_flake_inputs() {
941-
check_complete_flake(
942-
r#"
943-
{
944-
inputs.flake-utils.url = "github:numtide/flake-utils?rev=919d646de7be200f3bf08cb76ae1f09402b6f9b4";
945-
outputs = {flake-utils}: flake-utils.lib.$0
946-
}
947-
"#,
948-
None,
949-
expect![[r#"
950-
[
951-
"allSystems",
952-
"check-utils",
953-
"defaultSystems",
954-
"eachDefaultSystem",
955-
"eachDefaultSystemMap",
956-
"eachSystem",
957-
"eachSystemMap",
958-
"filterPackages",
959-
"flattenTree",
960-
"meld",
961-
"mkApp",
962-
"simpleFlake",
963-
"system",
964-
]
965-
"#]],
966-
)
967-
.await;
968-
}
969-
970939
#[test_log::test(tokio::test)]
971940
async fn test_complete_system() {
972941
check_complete(

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod modules;
1515
mod safe_stringification;
1616
mod schema;
1717
mod syntax;
18+
mod testing;
1819
mod walk_attrs;
1920

2021
use anyhow::Result;

src/testing.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::{
2+
collections::HashMap,
3+
path::{Path, PathBuf},
4+
};
5+
6+
use ropey::Rope;
7+
8+
use crate::syntax::rope_offset_to_position;
9+
10+
#[derive(Debug)]
11+
pub struct Location {
12+
pub path: PathBuf,
13+
pub line: u32,
14+
pub col: u32,
15+
}
16+
17+
#[derive(Debug)]
18+
pub struct TestInput {
19+
pub files: HashMap<PathBuf, String>,
20+
pub location: Option<Location>,
21+
}
22+
23+
fn parse_test_input(input: &str) -> TestInput {
24+
let mut files = HashMap::new();
25+
let mut lines = input.lines().peekable();
26+
let mut current_path = PathBuf::from("/nowhere.nix");
27+
let mut current_content = Vec::new();
28+
let mut location = None;
29+
30+
let mut add_file = |current_path: &Path, current_content: &[String]| {
31+
if !current_content.is_empty() {
32+
let mut content = current_content.join("\n");
33+
if let Some(offset) = content.find("$0") {
34+
content = format!("{}{}", &content[0..offset], &content[offset + 2..]);
35+
let rope = Rope::from_str(&content);
36+
let position = rope_offset_to_position(&rope, offset);
37+
location = Some(Location {
38+
path: current_path.to_path_buf(),
39+
line: position.line,
40+
col: position.character,
41+
})
42+
}
43+
files.insert(current_path.to_path_buf(), content);
44+
}
45+
};
46+
47+
while let Some(line) = lines.next() {
48+
eprintln!("LINE = {:?}", line);
49+
if line.starts_with("## ") {
50+
add_file(&current_path, &current_content);
51+
current_content.clear();
52+
current_path = PathBuf::from(line[2..].trim());
53+
} else {
54+
current_content.push(line.to_string());
55+
}
56+
}
57+
58+
add_file(&current_path, &current_content);
59+
60+
TestInput { files, location }
61+
}
62+
63+
#[cfg(test)]
64+
mod test {
65+
use expect_test::{expect, Expect};
66+
use indoc::indoc;
67+
68+
use super::parse_test_input;
69+
70+
fn check_parse_test_input(input: &str, expected: Expect) {
71+
expected.assert_debug_eq(&parse_test_input(input.trim()));
72+
}
73+
74+
#[test]
75+
fn test_parse_test_input() {
76+
check_parse_test_input(
77+
indoc! {r#"
78+
## /test.nix
79+
aaaaaaaaaaaaaaa
80+
bbbbbb
81+
cccccccc
82+
## /your mother.nix
83+
aaaaaaaaaaaaaaaaaaaazzzzzzzzzzzzz
84+
xyz.$0cyz
85+
"# },
86+
expect![[]],
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)