about summary refs log blame commit diff stats
path: root/003trace.test.cc
blob: 31c35e2d2e7a1026c11a381be0ee865cdd369f71 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                  
                                        
                                          

 
                                              

                                          

                                                  

 





                                                                          
                                             

                                          
                                            

 
                                              

                                          
                                            

 
                                               
                                            
                                            

 
                                                
                                           


                                            
                                        


                                          
                                                                                 

 
                                  

                                          



                                                  

                                          


                                           
                                                     
                                            


                                                 

                                                          
                                        

                                                                               
                                                           

                                                                                      







                                                       
                             




                                                      
                                




                                                

                                




                                                    


                                




                                                       


                                
 















                                 
void test_trace_check_compares() {
  trace("test layer") << "foo" << end();
  CHECK_TRACE_CONTENTS("test layer: foo");
}

void test_trace_check_ignores_other_layers() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 2") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
  CHECK_TRACE_DOESNT_CONTAIN("test layer 2: foo");
}

void test_trace_check_ignores_leading_whitespace() {
  trace("test layer 1") << " foo" << end();
  CHECK(trace_count("test layer 1", /*too little whitespace*/"foo") == 1);
  CHECK(trace_count("test layer 1", /*too much whitespace*/"  foo") == 1);
}

void test_trace_check_ignores_other_lines() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_check_ignores_other_lines2() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: bar");
}

void test_trace_ignores_trailing_whitespace() {
  trace("test layer 1") << "foo\n" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_ignores_trailing_whitespace2() {
  trace("test layer 1") << "foo " << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_orders_across_layers() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 2") << "bar" << end();
  trace("test layer 1") << "qux" << end();
  CHECK_TRACE_CONTENTS("test layer 1: footest layer 2: bartest layer 1: qux");
}

void test_trace_supports_count() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "foo" << end();
  CHECK_EQ(trace_count("test layer 1", "foo"), 2);
}

void test_trace_supports_count2() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_EQ(trace_count("test layer 1"), 2);
}

void test_trace_count_ignores_trailing_whitespace() {
  trace("test layer 1") << "foo\n" << end();
  CHECK(trace_count("test layer 1", "foo") == 1);
}

// pending: DUMP tests
// pending: readable_contents() adds newline if necessary.
// pending: raise also prints to stderr.
// pending: raise doesn't print to stderr if Hide_errors is set.
// pending: raise doesn't have to be saved if Hide_errors is set, just printed.
// pending: raise prints to stderr if Trace_stream is NULL.
// pending: raise prints to stderr if Trace_stream is NULL even if Hide_errors is set.
// pending: raise << ... die() doesn't die if Hide_errors is set.



// can't check trace because trace methods call 'split'

void test_split_returns_at_least_one_elem() {
  vector<string> result = split("", ",");
  CHECK_EQ(result.size(), 1);
  CHECK_EQ(result.at(0), "");
}

void test_split_returns_entire_input_when_no_delim() {
  vector<string> result = split("abc", ",");
  CHECK_EQ(result.size(), 1);
  CHECK_EQ(result.at(0), "abc");
}

void test_split_works() {
  vector<string> result = split("abc,def", ",");
  CHECK_EQ(result.size(), 2);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
}

void test_split_works2() {
  vector<string> result = split("abc,def,ghi", ",");
  CHECK_EQ(result.size(), 3);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
  CHECK_EQ(result.at(2), "ghi");
}

void test_split_handles_multichar_delim() {
  vector<string> result = split("abc,,def,,ghi", ",,");
  CHECK_EQ(result.size(), 3);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
  CHECK_EQ(result.at(2), "ghi");
}

void test_trim() {
  CHECK_EQ(trim(""), "");
  CHECK_EQ(trim(" "), "");
  CHECK_EQ(trim("  "), "");
  CHECK_EQ(trim("a"), "a");
  CHECK_EQ(trim(" a"), "a");
  CHECK_EQ(trim("  a"), "a");
  CHECK_EQ(trim("  ab"), "ab");
  CHECK_EQ(trim("a "), "a");
  CHECK_EQ(trim("a  "), "a");
  CHECK_EQ(trim("ab  "), "ab");
  CHECK_EQ(trim(" a "), "a");
  CHECK_EQ(trim("  a  "), "a");
  CHECK_EQ(trim("  ab  "), "ab");
}
n class="o">='search'): self.fm.env.cf = self.fm.env.pwd.pointed_obj self.close() class OpenConsole(ConsoleWithTab): """ The OpenConsole allows you to execute shell commands: !vim * will run vim and open all files in the directory. %f will be replaced with the basename of the highlighted file %s will be selected with all files in the selection There is a special syntax for more control: !d! mplayer will run mplayer with flags (d means detached) !@ mplayer will open the selected files with mplayer (equivalent to !mplayer %s) those two can be combinated: !d!@mplayer will open the selection with a detached mplayer For a list of other flags than "d", look at the documentation of ranger.applications. """ prompt = '!' def init(self): self.history = self.histories[OPEN_HISTORY] def execute(self): command, flags = self._parse() if command: if _CustomTemplate.delimiter in command: command = self._substitute_metachars(command) self.fm.execute_command(command, flags=flags) self.close() def _get_tab(self): try: position_of_last_space = self.line.rindex(" ") except ValueError: return (program + ' ' for program in self.fm.executables \ if program.startswith(self.line)) if position_of_last_space == len(self.line) - 1: return self.line + '%s ' else: before_word, start_of_word = self.line.rsplit(' ', 1) return (before_word + ' ' + file.basename \ for file in self.fm.env.pwd.files \ if file.basename.startswith(start_of_word)) def _substitute_metachars(self, command): dct = {} if self.fm.env.cf: dct['f'] = shell_escape(self.fm.env.cf.basename) else: dct['f'] = '' dct['s'] = ' '.join(shell_escape(fl.basename) \ for fl in self.fm.env.get_selection()) return _CustomTemplate(command).safe_substitute(dct) def _parse(self): if '!' in self.line: flags, cmd = self.line.split('!', 1) else: flags, cmd = '', self.line add_selection = False if cmd.startswith('@'): cmd = cmd[1:] add_selection = True elif flags.startswith('@'): flags = flags[1:] add_selection = True if add_selection: cmd += ' ' + ' '.join(shell_escape(fl.basename) \ for fl in self.env.get_selection()) return (cmd, flags) class QuickOpenConsole(ConsoleWithTab): """ The QuickOpenConsole allows you to open files with pre-defined programs and modes very quickly. By adding flags to the command, you can specify precisely how the program is run, ie. the d-flag will run it detached from the filemanager. """ prompt = 'open with: ' def init(self): self.history = self.histories[QUICKOPEN_HISTORY] def execute(self): split = self.line.split() app, flags, mode = self._get_app_flags_mode() self.fm.execute_file( files = [self.env.cf], app = app, flags = flags, mode = mode ) self.close() def _get_app_flags_mode(self): """ Extracts the application, flags and mode from a string entered into the "openwith_quick" console. """ # examples: # "mplayer d 1" => ("mplayer", "d", 1) # "aunpack 4" => ("aunpack", "", 4) # "p" => ("", "p", 0) # "" => None app = '' flags = '' mode = 0 split = self.line.split() if len(split) == 0: pass elif len(split) == 1: part = split[0] if self._is_app(part): app = part elif self._is_flags(part): flags = part elif self._is_mode(part): mode = part elif len(split) == 2: part0 = split[0] part1 = split[1] if self._is_app(part0): app = part0 if self._is_flags(part1): flags = part1 elif self._is_mode(part1): mode = part1 elif self._is_flags(part0): flags = part0 if self._is_mode(part1): mode = part1 elif self._is_mode(part0): mode = part0 if self._is_flags(part1): flags = part1 elif len(split) >= 3: part0 = split[0] part1 = split[1] part2 = split[2] if self._is_app(part0): app = part0 if self._is_flags(part1): flags = part1 if self._is_mode(part2): mode = part2 elif self._is_mode(part1): mode = part1 if self._is_flags(part2): flags = part2 elif self._is_flags(part0): flags = part0 if self._is_mode(part1): mode = part1 elif self._is_mode(part0): mode = part0 if self._is_flags(part1): flags = part1 return app, flags, int(mode) def _get_tab(self): if ' ' not in self.line: all_apps = self.fm.apps.all() if all_apps: return (app for app in all_apps if app.startswith(self.line)) return None def _is_app(self, arg): return self.fm.apps.has(arg) def _is_flags(self, arg): from ranger.runner import ALLOWED_FLAGS return all(x in ALLOWED_FLAGS for x in arg) def _is_mode(self, arg): return all(x in '0123456789' for x in arg)