From 669eabe892a0e1cb658f550021c2d29f0655729c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 5 Sep 2018 20:22:52 +0300 Subject: even less hacks --- crates/libanalysis/tests/tests.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/libanalysis/tests/tests.rs b/crates/libanalysis/tests/tests.rs index 1f210f579..ebc8037b3 100644 --- a/crates/libanalysis/tests/tests.rs +++ b/crates/libanalysis/tests/tests.rs @@ -4,40 +4,38 @@ extern crate test_utils; use std::{ collections::HashMap, - path::{Path}, }; -use relative_path::{RelativePath, RelativePathBuf}; +use relative_path::{RelativePath}; use libanalysis::{AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId}; use test_utils::assert_eq_dbg; struct FileMap(&'static [(u32, &'static str)]); impl FileMap { - fn path(&self, id: FileId) -> &'static Path { - let s = self.0.iter() - .find(|it| it.0 == id.0) + fn iter<'a>(&'a self) -> impl Iterator + 'a { + self.0.iter().map(|&(id, path)| { + assert!(path.starts_with('/')); + (FileId(id), RelativePath::new(&path[1..])) + }) + } + + fn path(&self, id: FileId) -> &RelativePath { + self.iter() + .find(|&(it, _)| it == id) .unwrap() - .1; - Path::new(s) + .1 } } impl FileResolver for FileMap { fn file_stem(&self, id: FileId) -> String { - self.path(id).file_stem().unwrap().to_str().unwrap().to_string() + self.path(id).file_stem().unwrap().to_string() } fn resolve(&self, id: FileId, rel: &RelativePath) -> Option { - let path = rel.to_path(self.path(id)); - let path = path.strip_prefix("/").unwrap(); - let path = RelativePathBuf::from_path(&path).unwrap().normalize(); - let &(id, _) = self.0.iter() - .find(|it| { - let p = Path::new(it.1).strip_prefix("/").unwrap(); - let p = RelativePathBuf::from_path(p).unwrap(); - path == p - })?; - Some(FileId(id)) + let path = self.path(id).join(rel).normalize(); + let id = self.iter().find(|&(_, p)| path == p)?.0; + Some(id) } } -- cgit v1.2.3