Debian Codegraph @

src/test_utils.rs

raw symbols SCIP
1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
9 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#;
10
11 use dagrust-analyzer cargo esl01-dag 0.3.0 crate/::namedagrust-analyzer cargo esl01-dag 0.3.0 namedag/::MemNameDagrust-analyzer cargo esl01-dag 0.3.0 namedag/mem_namedag/MemNameDag#;
12 use dagrust-analyzer cargo esl01-dag 0.3.0 crate/::namesetrust-analyzer cargo esl01-dag 0.3.0 nameset/::SyncNameSetQueryrust-analyzer cargo esl01-dag 0.3.0 nameset/SyncNameSetQuery#;
13 use dagrust-analyzer cargo esl01-dag 0.3.0 crate/::opsrust-analyzer cargo esl01-dag 0.3.0 ops/::ImportAsciirust-analyzer cargo esl01-dag 0.3.0 ops/ImportAscii#;
14 use dagrust-analyzer cargo esl01-dag 0.3.0 crate/::DagAlgorithmrust-analyzer cargo esl01-dag 0.3.0 ops/DagAlgorithm#;
15 use dagrust-analyzer cargo esl01-dag 0.3.0 crate/::VertexNamerust-analyzer cargo esl01-dag-types 0.3.0 id/VertexName#;
16 use nonblockingrust-analyzer cargo esl01-nonblocking 0.3.0 crate/::non_blocking_resultrust-analyzer cargo esl01-nonblocking 0.3.0 non_blocking_result().;
17 use unicode_widthrust-analyzer cargo unicode-width 0.1.14 crate/::UnicodeWidthStrrust-analyzer cargo unicode-width 0.1.14 UnicodeWidthStr#;
18
19 use superrust-analyzer cargo esl01-renderdag 0.3.0 crate/::renderrust-analyzer cargo esl01-renderdag 0.3.0 render/::Ancestorrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#;
20 use superrust-analyzer cargo esl01-renderdag 0.3.0 crate/::renderrust-analyzer cargo esl01-renderdag 0.3.0 render/::Rendererrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#;
21 use superrust-analyzer cargo esl01-renderdag 0.3.0 crate/::test_fixturesrust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/::TestFixturerust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#;
22
23 pub(craterust-analyzer cargo esl01-renderdag 0.3.0 crate/) fn render_stringrust-analyzer cargo esl01-renderdag 0.3.0 test_utils/render_string().(
24 fixture: &TestFixturerust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#,
25 renderer: &mut dyn Rendererrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#<VertexNamerust-analyzer cargo esl01-dag-types 0.3.0 id/VertexName#, Outputrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#Output# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
26 ) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
27 render_string_with_orderrust-analyzer cargo esl01-renderdag 0.3.0 test_utils/render_string_with_order().(fixture, renderer, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
28 }
29
30 pub(craterust-analyzer cargo esl01-renderdag 0.3.0 crate/) fn render_string_with_orderrust-analyzer cargo esl01-renderdag 0.3.0 test_utils/render_string_with_order().(
31 fixture: &TestFixturerust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#,
32 renderer: &mut dyn Rendererrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#<VertexNamerust-analyzer cargo esl01-dag-types 0.3.0 id/VertexName#, Outputrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#Output# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
33 order: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&[&str]>,
34 ) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
35 let TestFixturerust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture# {
36 dagrust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#dag.: ascii,
37 messagesrust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#messages.,
38 headsrust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#heads.,
39 reserverust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#reserve.,
40 ancestorsrust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#ancestors.,
41 missingrust-analyzer cargo esl01-renderdag 0.3.0 test_fixtures/TestFixture#missing.,
42 } = fixture;
43 let mut dag = MemNameDagrust-analyzer cargo esl01-dag 0.3.0 namedag/mem_namedag/MemNameDag#::newrust-analyzer cargo esl01-dag 0.3.0 namedag/mem_namedag/impl#[`AbstractNameDag<IdDag<InProcessStore>, MemIdMap, MemNameDagPath, MemNameDagState>`]new().();
44 dag.import_ascii_with_headsrust-analyzer cargo esl01-dag 0.3.0 ops/impl#[T][ImportAscii]import_ascii_with_heads().(ascii, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(heads)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
45 // str -> VertexName
46 let v = |s: &str| VertexNamerust-analyzer cargo esl01-dag-types 0.3.0 id/VertexName#::copy_fromrust-analyzer cargo esl01-dag-types 0.3.0 id/impl#[VertexName]copy_from().(s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
47
48 let ancestors: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_> = ancestors
49 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
50 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(desc, anc)| (v(desc), v(anc)))
51 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
52 let missing: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_> = missing.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| v(s)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
53
54 reserve
55 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
56 .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().()
57 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| v(s))
58 .for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|s| renderer.reserverust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#reserve().(s));
59
60 let messages: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _> = messages.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
61
62 let iter: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = match order {
63 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => non_blocking_resultrust-analyzer cargo esl01-nonblocking 0.3.0 non_blocking_result().(dag.allrust-analyzer cargo esl01-dag 0.3.0 namedag/impl#[`AbstractNameDag<IdDag<IS>, M, P, S>`][DagAlgorithm]all().())
64 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
65 .iterrust-analyzer cargo esl01-dag 0.3.0 nameset/impl#[NameSet][SyncNameSetQuery]iter().()
66 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
67 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| v.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
68 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
69 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(order) => order.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|name| v(name)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
70 };
71
72 let mut out = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
73 for node in iter {
74 if missing.containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(&node) {
75 continue;
76 }
77 let parents = non_blocking_resultrust-analyzer cargo esl01-nonblocking 0.3.0 non_blocking_result().(dag.parent_namesrust-analyzer cargo esl01-dag 0.3.0 namedag/impl#[`AbstractNameDag<IdDag<IS>, M, P, S>`][DagAlgorithm]parent_names().(node.clonerust-analyzer cargo esl01-dag-types 0.3.0 id/impl#[VertexName][Clone]clone().()))
78 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
79 .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()
80 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|parent| {
81 if missing.containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(&parent) {
82 Ancestorrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#::Anonymousrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#Anonymous#
83 } else if ancestors.containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(&(node.clonerust-analyzer cargo esl01-dag-types 0.3.0 id/impl#[VertexName][Clone]clone().(), parent.clonerust-analyzer cargo esl01-dag-types 0.3.0 id/impl#[VertexName][Clone]clone().())) {
84 Ancestorrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#::Ancestorrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#Ancestor#(parent)
85 } else {
86 Ancestorrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#::Parentrust-analyzer cargo esl01-renderdag 0.3.0 render/Ancestor#Parent#(parent)
87 }
88 })
89 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
90 let name = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(node.as_refrust-analyzer cargo esl01-dag-types 0.3.0 id/impl#[VertexName][`AsRef<[u8]>`]as_ref().().to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
91 let message = match messages.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(name.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()) {
92 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(message) => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}\n{}", name, message),
93 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => name.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(),
94 };
95 let width = renderer.widthrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#width().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&node), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&parents));
96 let row = renderer.next_rowrust-analyzer cargo esl01-renderdag 0.3.0 render/Renderer#next_row().(node, parents, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("o"), message);
97 let row_indent = row
98 .linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().()
99 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|line| line.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(&name).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|offset| &line[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#offset]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().))
100 .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().()
101 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("name should be in the output");
102 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
103 row_indent.widthrust-analyzer cargo unicode-width 0.1.14 impl#[str][UnicodeWidthStr]width().() as u64,
104 width,
105 "indent '{}' for row for {} is the wrong width",
106 row_indent,
107 name
108 );
109
110 out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&row);
111 }
112
113 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
114 "\n{}",
115 out.trim_endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end().()
116 .linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().()
117 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|l| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(" {}", l))
118 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>()
119 .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("\n")
120 )
121 }