nu_command/
example_test.rs1#[cfg(test)]
2use nu_protocol::engine::Command;
3
4#[cfg(test)]
5pub fn test_examples(cmd: impl Command + 'static) {
12 test_examples::test_examples(cmd, &[]);
13}
14
15#[cfg(test)]
16pub fn test_examples_with_commands(cmd: impl Command + 'static, commands: &[&dyn Command]) {
17 test_examples::test_examples(cmd, commands);
18}
19
20#[cfg(test)]
21mod test_examples {
22 use super::super::{
23 Ansi, Date, Enumerate, Filter, First, Flatten, From, Get, Into, IntoDatetime, IntoString,
24 Lines, Math, MathRound, MathSum, ParEach, Path, PathParse, Random, Seq, Sort, SortBy,
25 Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Update, Url, Values,
26 Wrap,
27 };
28 use crate::{Default, Each, To};
29 use nu_cmd_lang::example_support::{
30 check_all_signature_input_output_types_entries_have_examples,
31 check_example_evaluates_to_expected_output,
32 check_example_input_and_output_types_match_command_signature,
33 };
34 use nu_cmd_lang::{Break, Describe, Echo, If, Let, Mut};
35 use nu_protocol::{
36 Type,
37 engine::{Command, EngineState, StateWorkingSet},
38 };
39 use std::collections::HashSet;
40
41 pub fn test_examples(cmd: impl Command + 'static, commands: &[&dyn Command]) {
42 let examples = cmd.examples();
43 let signature = cmd.signature();
44 let mut engine_state = make_engine_state(cmd.clone_box(), commands);
45
46 let cwd = std::env::current_dir().expect("Could not get current working directory.");
47
48 let mut witnessed_type_transformations = HashSet::<(Type, Type)>::new();
49
50 for example in examples {
51 if example.result.is_none() {
52 continue;
53 }
54
55 witnessed_type_transformations.extend(
56 check_example_input_and_output_types_match_command_signature(
57 &example,
58 &cwd,
59 &mut make_engine_state(cmd.clone_box(), commands),
60 &signature.input_output_types,
61 signature.operates_on_cell_paths(),
62 ),
63 );
64 check_example_evaluates_to_expected_output(
65 cmd.name(),
66 &example,
67 cwd.as_path(),
68 &mut engine_state,
69 );
70 }
71
72 check_all_signature_input_output_types_entries_have_examples(
73 signature,
74 witnessed_type_transformations,
75 );
76 }
77
78 fn make_engine_state(cmd: Box<dyn Command>, commands: &[&dyn Command]) -> Box<EngineState> {
79 let mut engine_state = Box::new(EngineState::new());
80
81 let delta = {
82 let mut working_set = StateWorkingSet::new(&engine_state);
85 working_set.add_decl(Box::new(Ansi));
86 working_set.add_decl(Box::new(Break));
87 working_set.add_decl(Box::new(Date));
88 working_set.add_decl(Box::new(Default));
89 working_set.add_decl(Box::new(Describe));
90 working_set.add_decl(Box::new(Each));
91 working_set.add_decl(Box::new(Echo));
92 working_set.add_decl(Box::new(Enumerate));
93 working_set.add_decl(Box::new(Filter));
94 working_set.add_decl(Box::new(First));
95 working_set.add_decl(Box::new(Flatten));
96 working_set.add_decl(Box::new(From));
97 working_set.add_decl(Box::new(Get));
98 working_set.add_decl(Box::new(If));
99 working_set.add_decl(Box::new(Into));
100 working_set.add_decl(Box::new(IntoString));
101 working_set.add_decl(Box::new(IntoDatetime));
102 working_set.add_decl(Box::new(Let));
103 working_set.add_decl(Box::new(Lines));
104 working_set.add_decl(Box::new(Math));
105 working_set.add_decl(Box::new(MathRound));
106 working_set.add_decl(Box::new(MathSum));
107 working_set.add_decl(Box::new(Mut));
108 working_set.add_decl(Box::new(Path));
109 working_set.add_decl(Box::new(PathParse));
110 working_set.add_decl(Box::new(ParEach));
111 working_set.add_decl(Box::new(Random));
112 working_set.add_decl(Box::new(Seq));
113 working_set.add_decl(Box::new(Sort));
114 working_set.add_decl(Box::new(SortBy));
115 working_set.add_decl(Box::new(Split));
116 working_set.add_decl(Box::new(SplitColumn));
117 working_set.add_decl(Box::new(SplitRow));
118 working_set.add_decl(Box::new(Str));
119 working_set.add_decl(Box::new(StrJoin));
120 working_set.add_decl(Box::new(StrLength));
121 working_set.add_decl(Box::new(StrReplace));
122 working_set.add_decl(Box::new(To));
123 working_set.add_decl(Box::new(Url));
124 working_set.add_decl(Box::new(Update));
125 working_set.add_decl(Box::new(Values));
126 working_set.add_decl(Box::new(Wrap));
127
128 for command in commands {
130 working_set.add_decl(command.clone_box());
131 }
132
133 working_set.add_decl(cmd);
135
136 working_set.render()
137 };
138
139 engine_state
140 .merge_delta(delta)
141 .expect("Error merging delta");
142 engine_state
143 }
144}