def test_cpln_variable(self): content, positions = unmark_text( dedent("""\ @nice-blue: #5B83AD; @light-blue: @<1>nice-blue + #111; #header { @nested-color: white; color: @<2>light-blue; } #footer { color: @<3> } @<4> """)) self.assertCompletionsAre(markup_text(content, pos=positions[1]), [("variable", "light-blue"), ("variable", "nice-blue")]) self.assertCompletionsAre(markup_text(content, pos=positions[2]), [("variable", "light-blue"), ("variable", "nested-color"), ("variable", "nice-blue")]) self.assertCompletionsAre(markup_text(content, pos=positions[3]), [("variable", "light-blue"), ("variable", "nice-blue")]) self.assertCompletionsDoNotInclude( markup_text(content, pos=positions[4]), [("variable", "light-blue"), ("variable", "nice-blue")])
def test_ellipsis_literal(self): content, positions = unmark_text(dedent(""" var = ... "string".<1>e """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "encode")])
def test_complete_property_values_complex2(self): # completions for complex style of propery-values name = "css-complete-property-values" # XXX - Talk to Eric # content, positions = unmark_text(dedent("""\ # <p style=" # background: <1>url('../img/header <2>tab.gif') /* <3>comment: <4>'abc <5>xyz' <6>*/ <7>600px <8>no-r; # " /> #""")) content, positions = unmark_text(dedent("""\ <p style=" background: <1> /* <3>comment: <4>'abc <5>xyz' <6>*/ <7>600px <8>no-r; " /> """)) values = set(CSS_ATTR_DICT['background']) for i in (1, 7, 8): self.assertTriggerMatches(markup_text(content, pos=positions[i]), name=name) # Remove these values, as they are already in the property list v = values.copy() if i == 7 or i == 8: v.discard('url(') self.assertCompletionsInclude( markup_text(content, pos=positions[i]), [("value", x) for x in v]) for i in range(3, 7): self.assertTriggerDoesNotMatch( markup_text(content, pos=positions[i]), name=name)
def test_cpln_variable(self): content, positions = unmark_text( dedent("""\ $font-stack: Helvetica, sans-serif; $primary-color: #333; body { font: 100% $<1>font-stack; color: $<2>primary-color; } #main { $width: 5em; width: $<3>width; } #sidebar { width: $<4>; } """)) for i in xrange(1, 2): self.assertCompletionsAre(markup_text(content, pos=positions[i]), [("variable", "font-stack"), ("variable", "primary-color")]) self.assertCompletionsAre(markup_text(content, pos=positions[3]), [("variable", "font-stack"), ("variable", "primary-color"), ("variable", "width")]) self.assertCompletionsAre(markup_text(content, pos=positions[4]), [("variable", "font-stack"), ("variable", "primary-color")])
def test_iterable_unpacking(self): content, positions = unmark_text(dedent(""" (a, *rest, b) = [1, 2, 3, 4] rest.<1>i """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "insert")])
def test_railsenv_views_basic_html_erb_suffix(self): test_dir = join(self.test_dir, "railsapp01", "app", "views", "layouts") main_filename = "add.html.erb" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ # Contrived: most layouts are implicit members of this class class Zoomoo < ActionView::<1>Base Zoomoo.<2>cache_template_extensions Zoomoo.new.<3>form_for "whatever".<4>pluralize end h = {'zounds' => 1, 'ok' => 2} h.<5>keys """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) targets = [[("class", "Base"), ], [("function", "cache_template_extensions"), ], [("function", "form_for"), ], [("function", "pluralize"), ], [("function", "stringify_keys!"), ], ] for i in range(len(targets)): self.assertCompletionsInclude2(main_buf, main_positions[i + 1], targets[i]) ## Verify we don't get false hits self.assertCompletionsDoNotInclude(markup_text(main_content, pos=main_positions[ i + 1]), targets[i])
def test_railsenv_model_basic(self): test_dir = join(self.test_dir, "railsapp01", "app", "models") main_filename = "cart1.rb" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ class Cart < ActiveRecord::<1>Base Cart.<2>acts_as_list def octopus self.<3>insert_at(0) i = 4 print "you owe #{i.<4>gigabyte} dollars" end end """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) targets = [[("class", "Base"), ], [("function", "acts_as_list"), ], [("function", "insert_at"), ], [("function", "megabyte"), ], ] for i in range(len(targets)): self.assertCompletionsInclude2(main_buf, main_positions[i + 1], targets[i]) ## Verify we don't get false hits self.assertCompletionsDoNotInclude(markup_text(main_content, pos=main_positions[ i + 1]), targets[i])
def test_simple(self): test_dir = join(self.test_dir, "test_defn") foo_es_content, foo_es_positions = unmark_text( dedent("""\ import Bar from 'bar'; Bar.b<1>ar """)) manifest = [ ("bar.es", dedent(""" export default { bar: 42 }; """)), ("foo.es", foo_es_content), ] for file, content in manifest: path = join(test_dir, file) writefile(path, content) buf = self.mgr.buf_from_path(join(test_dir, "foo.es"), lang=self.lang) self.assertDefnMatches2( buf, foo_es_positions[1], ilk="variable", name="bar", line=2, citdl="Number", path=join(test_dir, "bar.es"), )
def test_cpln_variable(self): content, positions = unmark_text(dedent("""\ $font-stack: Helvetica, sans-serif; $primary-color: #333; body { font: 100% $<1>font-stack; color: $<2>primary-color; } #main { $width: 5em; width: $<3>width; } #sidebar { width: $<4>; } """)) for i in range(1, 2): self.assertCompletionsAre(markup_text(content, pos=positions[i]), [("variable", "font-stack"), ("variable", "primary-color")]) self.assertCompletionsAre(markup_text(content, pos=positions[3]), [("variable", "font-stack"), ("variable", "primary-color"), ("variable", "width")]) self.assertCompletionsAre(markup_text(content, pos=positions[4]), [("variable", "font-stack"), ("variable", "primary-color")])
def test_rhtml_calltips(self): test_dir = join(self.test_dir, "railsapp01", "app", "views", "admin") main_filename = "list.rhtml" main_markup = dedent("""\ <h1>Listing titles</h1> <table> <% for title in @titles %> <tr> <td align='left'><%= link_to(<1> 'Show', :action => 'show', :id => title) %> <td align='left'><%= link_to_if <2>'Show', :action => 'show', :id => title) %> <td align='left'><%= <$>link<3> %> </td></tr></table> """) main_content, main_positions = unmark_text(main_markup) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) ''' self.assertCalltipIs2(main_buf, main_positions[1], dedent("""\ (name, options = {}, html_options = nil, *parameters_for_method_reference)""")) ''' self.assertCalltipIs2(main_buf, main_positions[2], dedent("""\ (condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) Creates a link tag of the given name</tt> using a URL created by the set of <tt>options</tt> if <tt>condition is true, in which case only the name is returned. To specialize the default behavior, you can pass a block that accepts the name or the full argument list for link_to_unless (see the examples in link_to_unless).""")) self.assertNoPrecedingTrigger(markup_text(main_content, start_pos=main_positions[ 'start_pos'], pos=main_positions[3]))
def test_open(self): content, positions = unmark_text(dedent(""" f = open("/dev/null", "w") f.<1>w """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "write")])
def test_cpln_variable(self): content, positions = unmark_text(dedent("""\ @nice-blue: #5B83AD; @light-blue: @<1>nice-blue + #111; #header { @nested-color: white; color: @<2>light-blue; } #footer { color: @<3> } @<4> """)) self.assertCompletionsAre(markup_text(content, pos=positions[1]), [("variable", "light-blue"), ("variable", "nice-blue")]) self.assertCompletionsAre(markup_text(content, pos=positions[2]), [("variable", "light-blue"), ("variable", "nested-color"), ("variable", "nice-blue")]) self.assertCompletionsAre(markup_text(content, pos=positions[3]), [("variable", "light-blue"), ("variable", "nice-blue")]) self.assertCompletionsDoNotInclude(markup_text(content, pos=positions[4]), [("variable", "light-blue"), ("variable", "nice-blue")])
def test_no_completions(self): content, positions = unmark_text(dedent("""\ <div id="<1>"> <div class="<2>"> """)) self.assertCompletionsAre(markup_text(content, pos=positions[1]), None) self.assertCompletionsAre(markup_text(content, pos=positions[2]), None)
def test_model_sees_rails2_migrations_1(self): dirs1 = [self.test_dir, "bug75440", "app"] test_model_dir = join(*(dirs1 + ["models"])) book_path = join(test_model_dir, "book.rb") dirs2 = [self.test_dir, "bug75440", "db", "migrate"] migrate_dir = join(*dirs2) migrate_table_create_path = join(migrate_dir, "001_create_books_and_dishes.rb") model_content, model_positions = unmark_text( self.adjust_content( dedent( """\ class Book < ActiveRecord::Base def get_title(a) return self.<1>title end end """ ) ) ) manifest = [(migrate_table_create_path, self.books_and_dishes_migration), (book_path, model_content)] for path, content in manifest: writefile(path, content) model_buf = self.mgr.buf_from_path(book_path) self.assertCompletionsInclude2( model_buf, model_positions[1], [("function", "title"), ("function", "author"), ("function", "publisher"), ("function", "isbn")], ) self.assertCompletionsDoNotInclude2( model_buf, model_positions[1], [("function", "bet"), ("function", "manufacturer"), ("function", "created_at"), ("function", "updated_at")], )
def test_railsenv_model_toplevel_1(self): test_dir = join(self.test_dir, "railsapp01", "app", "models") main_filename = "cart1.rb" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ class Cart < ActiveRecord::<2>Base act<1>s_as_list end """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) pos_targets = [ ("function", "acts_as_list"), ("function", "acts_as_tree"), ("function", "acts_as_nested_set"), ] neg_targets = [ ("function", "add_child"), ] self.assertCompletionsInclude2(main_buf, main_positions[1], pos_targets) self.assertCompletionsDoNotInclude(markup_text(main_content, pos=main_positions[1]), neg_targets)
def test_unicode_in_defn(self): self.doc.buffer, self.positions = unmark_text(dedent(u""" #Ůɳíčóďé<3> my $pCnt = 0;<1> # Some filler text to make sure we do not accidentally find the # previous definition $p<2>Cnt++; """).strip()) self.assertGreater(self.positions[3], len("#Unicode"), "Unicode positions are character positions " "instead of byte positions") spinner = AsyncSpinner(self, callback=partial(setattr, self, "trg")) with spinner: self.buf.defn_trg_from_pos(self.positions[2], spinner) self.assertIsNotNone(self.trg) self.assertEqual(self.trg.form, TRG_FORM_DEFN) handler = UIHandler(spinner) with spinner: spinner.callback = None self.buf.async_eval_at_trg(self.trg, handler, Ci.koICodeIntelBuffer.EVAL_SILENT) self.assertEqual(len(handler.defns), 1) self.assertEqual(handler.defns[0].lang, "Perl") self.assertEqual(handler.defns[0].name, "$pCnt") self.assertEqual([handler.defns[0].line], lines_from_pos(self.doc.buffer, [self.positions[1]])) self.assertEqual(handler.defns[0].ilk, "variable")
def test_calltip(self): spinner = AsyncSpinner(self, callback=partial(setattr, self, "trg")) with spinner: self.buf.preceding_trg_from_pos(self.positions[2], self.positions[3], spinner) self.assertIsNotNone(self.trg) self.assertEqual(self.trg.form, TRG_FORM_CALLTIP) spinner = AsyncSpinner(self) with spinner: handler = UIHandler(spinner) self.buf.async_eval_at_trg(self.trg, handler, Ci.koICodeIntelBuffer.EVAL_SILENT) calltip, calltip_pos = unmark_text(dedent(""" pformat(object, <1>indent=1<2>, width=80, depth=None) Format a Python object into a pretty-printed representation. """).strip()) self.assertEqual(handler.calltip, calltip) with spinner: positions = [] def callback(start, end): positions[:] = [start, end] spinner.callback = callback self.buf.get_calltip_arg_range(self.trg.pos, calltip, self.positions[3], spinner) self.assertEquals(positions, [calltip_pos[1], calltip_pos[2]], "Unexpected calltip range positions; should cover " "second argument")
def test_railsenv_views_basic_realistic(self): test_dir = join(self.test_dir, "railsapp01", "app", "views", "layouts") main_filename = "add.rhtml" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ # Contrived: most layouts are implicit members of this class h = Act<1>ionView::<2>Base for<3>m_for "whatever".<4>pluralize """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) targets = [[ ("namespace", "ActionView"), ("namespace", "ActiveRecord"), ("namespace", "ActionController"), ], [ ("class", "Base"), ], [ ("function", "form_for"), ], [ ("function", "pluralize"), ]] for i in range(len(targets)): self.assertCompletionsInclude2(main_buf, main_positions[i + 1], targets[i]) ## Verify we don't get false hits self.assertCompletionsDoNotInclude( markup_text(main_content, pos=main_positions[i + 1]), targets[i])
def test_railsenv_controller_basic(self): test_dir = join(self.test_dir, "railsapp01", "app", "controllers") main_filename = "admin_controller.rb" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ class ApplicationController < ActionController::<1>Base ApplicationController.<2>after_filter :check_authentication, :except => [:signin] def signin self.<3>render(:layout, "sheep".<4>pluralize) end end """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) targets = [[("class", "Base"), ], [("function", "after_filter"), ], [("function", "render"), ], [("function", "pluralize"), ], ] for i in range(len(targets)): self.assertCompletionsInclude2(main_buf, main_positions[i + 1], targets[i]) ## Verify we don't get false hits self.assertCompletionsDoNotInclude(markup_text(main_content, pos=main_positions[ i + 1]), targets[i])
def test_complete_property_values_complex2(self): # completions for complex style of propery-values name = "css-complete-property-values" # XXX - Talk to Eric #content, positions = unmark_text(dedent("""\ # <p style=" # background: <1>url('../img/header <2>tab.gif') /* <3>comment: <4>'abc <5>xyz' <6>*/ <7>600px <8>no-r; # " /> #""")) content, positions = unmark_text( dedent("""\ <p style=" background: <1> /* <3>comment: <4>'abc <5>xyz' <6>*/ <7>600px <8>no-r; " /> """)) values = set(CSS_ATTR_DICT['background']) for i in (1, 7, 8): self.assertTriggerMatches(markup_text(content, pos=positions[i]), name=name) # Remove these values, as they are already in the property list v = values.copy() if i == 7 or i == 8: v.discard('url(') self.assertCompletionsInclude( markup_text(content, pos=positions[i]), [("value", x) for x in v]) for i in range(3, 7): self.assertTriggerDoesNotMatch(markup_text(content, pos=positions[i]), name=name)
def test_railsenv_views_basic_realistic(self): test_dir = join(self.test_dir, "railsapp01", "app", "views", "layouts") main_filename = "add.rhtml" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ # Contrived: most layouts are implicit members of this class h = Act<1>ionView::<2>Base for<3>m_for "whatever".<4>pluralize """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) targets = [ [("namespace", "ActionView"), ("namespace", "ActiveRecord"), ("namespace", "ActionController"), ], [("class", "Base"), ], [("function", "form_for"), ], [("function", "pluralize"), ] ] for i in range(len(targets)): self.assertCompletionsInclude2(main_buf, main_positions[i + 1], targets[i]) ## Verify we don't get false hits self.assertCompletionsDoNotInclude(markup_text(main_content, pos=main_positions[ i + 1]), targets[i])
def test_railsenv_controller_find_peer(self): test_dir = join(self.test_dir, "railsapp01", "app", "controllers") adminc_filename = "admin_controller%s" % self.ext adminc_content, adminc_positions = \ unmark_text(self.adjust_content(dedent("""\ module AppBogus # to force a choice at point 2 end class AdminController < <1>App<2>licationController aft<3>er_filter :check_authentication, :except => [:signin] AdminController.filter_parameter_logging(<6>'a', 'b') def open exp<4>ires_in 10.seconds self.<5>redirect_to("chumley") end end """))) manifest = [ (join(test_dir, "application.rb"), dedent("""\ class ApplicationController < ActionController::Base def foo end end """)), (adminc_filename, adminc_content), ] for file, content in manifest: path = join(test_dir, file) writefile(path, content) adminc_buf = self.mgr.buf_from_path(join(test_dir, adminc_filename)) targets = [None, # 0 None, # 1 [("class", "ApplicationController"), # 2 ("namespace", "AppBogus"), ], [("function", "after_filter"), # 3 ], [("function", "expires_in"), # 4 ("function", "expires_now") ], [("function", "redirect_to"), # 5 ("function", "session_enabled?"), ], ] # for i in range(2, 1 + len(targets)): for i in (2, 5): self.assertCompletionsInclude2(adminc_buf, adminc_positions[i], targets[i]) self.assertCalltipIs2(adminc_buf, adminc_positions[6], dedent("""\ (*filter_words, &block) {|key, value| ...} Replace sensitive paramater data from the request log. Filters paramaters that have any of the arguments as a substring. Looks in all subhashes of the param hash for keys to filter. If a block is given, each key and value of the paramater hash and all subhashes is passed to it, the value or key can be replaced using String#replace or similar method."""))
def test_open(self): content, positions = unmark_text( dedent(""" f = open("/dev/null", "w") f.<1>w """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "write")])
def test_kw_only_args(self): content, positions = unmark_text(dedent(""" def func(*, kw): return "string" func().<1>s """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "strip")])
def test_ellipsis_literal(self): content, positions = unmark_text( dedent(""" var = ... "string".<1>e """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "encode")])
def test_iterable_unpacking(self): content, positions = unmark_text( dedent(""" (a, *rest, b) = [1, 2, 3, 4] rest.<1>i """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "insert")])
def test_no_completions(self): content, positions = unmark_text( dedent("""\ <div id="<1>"> <div class="<2>"> """)) self.assertCompletionsAre(markup_text(content, pos=positions[1]), None) self.assertCompletionsAre(markup_text(content, pos=positions[2]), None)
def test_kw_only_args(self): content, positions = unmark_text( dedent(""" def func(*, kw): return "string" func().<1>s """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "strip")])
def test_controller_find_peer(self): dirs1 = [self.test_dir, "peers", "app"] test_controller_dir = join(*(dirs1 + ["controllers"])) test_model_dir = join(*(dirs1 + ["models"])) adminc_filename = join(test_controller_dir, "admin_controller.rb") book_path = join(test_model_dir, "book.rb") cart_path = join(test_model_dir, "cart.rb") adminc_content, adminc_positions = \ unmark_text(self.adjust_content(dedent("""\ class ApplicationController < ActionController::Base def foo x = Cart<5>.<1>new x.<2>add_i<6>tem() y = Boo<7>k.<3>new y.<4>re<8>ad() end end """))) manifest = [ (cart_path, dedent("""\ class Cart < ActiveRecord::Base def add_item(a) end end """)), (book_path, dedent("""\ class Book < ActiveRecord::Base def read(a) end end """)), (adminc_filename, adminc_content), ] for path, content in manifest: writefile(path, content) adminc_buf = self.mgr.buf_from_path(adminc_filename) targets = [None, # 0 [("function", "new"), # 1 ], [("function", "add_item"), # 2 ], [("function", "new"), # 3 ], [("function", "read"), # 4 ], ] repl_path = 'models' fixed_cart_path = cart_path.replace('models', repl_path) fixed_book_path = book_path.replace('models', repl_path) self.assertDefnMatches2(adminc_buf, adminc_positions[5], ilk="class", name="Cart", line=1, path=fixed_cart_path) self.assertDefnMatches2(adminc_buf, adminc_positions[6], ilk="function", name="add_item", line=2, path=fixed_cart_path) self.assertDefnMatches2(adminc_buf, adminc_positions[7], ilk="class", name="Book", line=1, path=fixed_book_path) self.assertDefnMatches2(adminc_buf, adminc_positions[8], ilk="function", name="read", line=2, path=fixed_book_path)
def test_string_literals(self): content, positions = unmark_text(dedent(""" literal = "hello" literal.<1>e """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "encode")]) self.assertCompletionsDoNotInclude(markup_text(content, positions[1]), [("function", "decode")])
def test_pseudo_retrigger(self): content, positions = unmark_text(dedent(""" .cursor font-weight: bold .cursor:<1>h<2>ov """)) pseudo_results = [("pseudo-class", s) for s in ("active", "visited", "link", "hover", "first-child")] self.assertCompletionsInclude(markup_text(content, pos=positions[1]), pseudo_results) self.assertNoTrigger(markup_text(content, pos=positions[2]))
def test_string_literals(self): content, positions = unmark_text( dedent(""" literal = "hello" literal.<1>e """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "encode")]) self.assertCompletionsDoNotInclude(markup_text(content, positions[1]), [("function", "decode")])
def assertCITDLExprUnderPosIs(self, markedup_content, citdl_expr, lang=None, prefix_filter=None, implicit=True, trigger_name=None, **fields): """Assert that the CITDL expression at the current position is as expected. This uses buf.citdl_expr_under_pos() -- or, for Perl, buf.citdl_expr_and_prefix_filter_from_trg(). Note: This API is a mess right now. C.f. bug 65776. The "prefix_filter" optional argument can be used for Perl to test the value its relevant function returns. """ if lang is None: lang = self.lang content, data = unmark_text(self.adjust_content(markedup_content)) path = os.path.join("<Unsaved>", "rand%d" % random.randint(0, 100)) buf = self.mgr.buf_from_content(content, lang=lang, path=path) langintel = self.mgr.langintel_from_lang(lang) if trigger_name is None: trigger_name = "fakey-completion-type" if lang == "Perl": trg = Trigger(lang, TRG_FORM_DEFN, trigger_name, data["pos"], implicit=implicit, length=0, **fields) actual_citdl_expr, actual_prefix_filter \ = langintel.citdl_expr_and_prefix_filter_from_trg(buf, trg) else: #actual_citdl_expr = langintel.citdl_expr_under_pos(buf, data["pos"]) trg = Trigger(lang, TRG_FORM_DEFN, trigger_name, data["pos"], implicit=implicit, **fields) actual_citdl_expr = langintel.citdl_expr_from_trg(buf, trg) self.assertEqual( actual_citdl_expr, citdl_expr, "unexpected actual %s CITDL expr under pos:\n" " expected: %r\n" " got: %r\n" " buffer:\n%s" % (lang, citdl_expr, actual_citdl_expr, indent(markedup_content))) if prefix_filter is not None: XXX #TODO: compare prefix_filter to given value
def test_nonlocal(self): content, positions = unmark_text(dedent(""" global_var = "string" def func(): nonlocal global_var global_var = global_var return global_var func().<1>s """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "strip")])
def assertNoPrecedingTrigger(self, markedup_content, lang=None): if lang is None: lang = self.lang path = os.path.join("<Unsaved>", "rand%d" % random.randint(0, 100)) content, data = unmark_text(self.adjust_content(markedup_content)) buf = self.mgr.buf_from_content(content, lang=lang, path=path) trg = buf.preceding_trg_from_pos(data["start_pos"], data["pos"]) if trg is not None: self.fail("unexpectedly found a preceding %s trigger '%s' when " "didn't expect one, buffer:\n%s" % (lang, trg.name, indent(markedup_content)))
def test_xpcom_classes_array_cplns(self): content, positions = unmark_text(dedent("""\ var observerCls = Components.classes["<1>@mozilla.org/observer-service;1"]; observerCls.<2>; """)) self.assertCompletionsInclude(markup_text(content, pos=positions[1]), [("variable", "@mozilla.org/observer-service;1"), ("variable", "@mozilla.org/embedcomp/prompt-service;1")]) self.assertCompletionsInclude(markup_text(content, pos=positions[2]), [("function", "getService"), ("function", "createInstance")])
def assertCITDLExprIs(self, markedup_content, citdl_expr, lang=None, prefix_filter=None, implicit=True, trigger_name=None, **fields): """Assert that the preceding CITDL expression at the current position is as expected. This uses buf.citdl_expr_from_trg() -- or, for Perl, buf.citdl_expr_and_prefix_filter_from_trg(). The "prefix_filter" optional argument can be used for Perl to test the value its relevant function returns. """ if lang is None: lang = self.lang content, data = unmark_text( self.adjust_content(markedup_content)) path = os.path.join("<Unsaved>", "rand%d" % random.randint(0, 100)) buf = self.mgr.buf_from_content(content, lang=lang, path=path) langintel = self.mgr.langintel_from_lang(lang) if trigger_name is None: trigger_name = "fakey-completion-type" if lang == "Perl": # Bit of a hack to fake the trigger length. if content[data["pos"]-1] in ('>', ':'): # '->' or '::' triggers length = 2 else: length = 1 trg = Trigger(lang, TRG_FORM_CPLN, trigger_name, data["pos"], implicit=implicit, length=length, **fields) actual_citdl_expr, actual_prefix_filter \ = langintel.citdl_expr_and_prefix_filter_from_trg(buf, trg) else: trg = Trigger(lang, TRG_FORM_CPLN, trigger_name, data["pos"], implicit=implicit, **fields) actual_citdl_expr = langintel.citdl_expr_from_trg(buf, trg) self.assertEqual(actual_citdl_expr, citdl_expr, "unexpected actual %s CITDL expr preceding trigger:\n" " expected: %r\n" " got: %r\n" " buffer:\n%s" % (lang, citdl_expr, actual_citdl_expr, indent(markedup_content))) if lang == "Perl" and prefix_filter is not None: self.assertEqual(actual_prefix_filter, prefix_filter, "unexpected actual %s variable prefix filter " "preceding trigger:\n" " expected: %r\n" " got: %r\n" " buffer:\n%s" % (lang, prefix_filter, actual_prefix_filter, indent(markedup_content)))
def test_complete_nested(self): if self.lang != 'Less': return # SCSS inherits this test case; ignore content, positions = unmark_text( dedent("""\ .test { .testClass { .<1> } } """)) self.assertCompletionsAre(markup_text(content, pos=positions[1]), [("class", "test"), ("class", "testClass")])
def test_complete_nested(self): if self.lang != 'Less': return # SCSS inherits this test case; ignore content, positions = unmark_text(dedent("""\ .test { .testClass { .<1> } } """)) self.assertCompletionsAre(markup_text(content, pos=positions[1]), [("class", "test"), ("class", "testClass")])
def test_zend_view_hooks(self): test_dir = join(self.test_dir, "test_zend_view_hooks") content, positions = unmark_text( dedent("""\ <?php if($this->values) { ?> <h3>You just submitted the following values:</h3> <ul> <?php foreach ($this->values as $value) :?> <li> <?= $this-><1>escape(<2>$value); ?> </li> <?php endforeach; ?> </ul> <?= $this->form; ?> """)) phpExtraPaths = os.pathsep.join(self._ci_extra_path_dirs_) env = SimplePrefsEnvironment(phpExtraPaths=phpExtraPaths) # Test to make sure the zend hooks are only modifying the files # in the /view/scripts/ directories. We should *not* get the completions # for the above code. path = join(test_dir, "test_zend_view.phtml") writefile(path, content) buf = self.mgr.buf_from_path(path, lang=self.lang, env=env) buf.scan_if_necessary() self.assertCompletionsDoNotInclude2(buf, positions[1], [("function", "escape"), ("function", "render")]) # Test to make sure the zend hooks are only modifying the ".phtml" files # in the /view/scripts/ directories. We should *not* get the completions # for the above code. path = join(test_dir, "test_zend_view.php") writefile(path, content) buf = self.mgr.buf_from_path(path, lang=self.lang, env=env) buf.scan_if_necessary() self.assertCompletionsDoNotInclude2(buf, positions[1], [("function", "escape"), ("function", "render")]) # Now make sure we do get the completions on /view/scripts/ directories. path = join(test_dir, "views", "scripts", "test_zend_view.phtml") writefile(path, content) buf = self.mgr.buf_from_path(path, lang=self.lang, env=env) buf.scan_if_necessary() self.assertCompletionsInclude2(buf, positions[1], [("function", "escape"), ("function", "render")]) # Make sure we do not get completions on the added "(render)" function. self.assertCompletionsDoNotInclude2(buf, positions[1], [ ("function", "(render)"), ])
def assertNoPrecedingTrigger(self, markedup_content, lang=None): if lang is None: lang = self.lang path = os.path.join("<Unsaved>", "rand%d" % random.randint(0, 100)) content, data = unmark_text( self.adjust_content(markedup_content)) buf = self.mgr.buf_from_content(content, lang=lang, path=path) trg = buf.preceding_trg_from_pos(data["start_pos"], data["pos"]) if trg is not None: self.fail("unexpectedly found a preceding %s trigger '%s' when " "didn't expect one, buffer:\n%s" % (lang, trg.name, indent(markedup_content)))
def test_nonlocal(self): content, positions = unmark_text( dedent(""" global_var = "string" def func(): nonlocal global_var global_var = global_var return global_var func().<1>s """)) self.assertCompletionsInclude(markup_text(content, positions[1]), [("function", "strip")])
def test_xpcom_classes_array_cplns(self): content, positions = unmark_text( dedent("""\ var observerCls = Components.classes["<1>@mozilla.org/observer-service;1"]; observerCls.<2>; """)) self.assertCompletionsInclude( markup_text(content, pos=positions[1]), [("variable", "@mozilla.org/observer-service;1"), ("variable", "@mozilla.org/embedcomp/prompt-service;1")]) self.assertCompletionsInclude(markup_text(content, pos=positions[2]), [("function", "getService"), ("function", "createInstance")])
def setUp(self): _BufferTestCaseBase.setUp(self) self.doc.buffer, self.positions = unmark_text(dedent(u""" # Ťĥíš ƒíłé ĥáš Ůɳíčóďé ťéхť ťó ťéšť ƀýťé νš čĥář ƿóšíťíóɳš def silly(): indent = 4<5> import <1>pprint pprint.pformat(<2>obj, indent<3>, width, depth) def other_method(): pass<6> def third_method(): pass<7> """).strip())
def _get_buf_and_data(self, markedup_content, lang, path=None, env=None): if path is None: # Try to ensure no accidental re-use of the same buffer name # across the whole test suite. Also try to keep the buffer # names relatively short (keeps tracebacks cleaner). name = "buf-" + md5(markedup_content).hexdigest()[:16] path = os.path.join("<Unsaved>", name) content, data = unmark_text(self.adjust_content(markedup_content)) #print banner(path) #sys.stdout.write(content) #print banner(None) buf = self.mgr.buf_from_content(content, lang=lang, path=path, env=env) return buf, data
def _doEval(self, markedup_content, lang=None, implicit=True): self.doc.buffer, self.positions = unmark_text(dedent(markedup_content).strip()) spinner = AsyncSpinner(self, callback=partial(setattr, self, "trg")) with spinner: self.buf.trg_from_pos(self.positions["pos"], implicit, spinner) self.assertIsNotNone(self.trg) # don't really care about the rest of the internals of the trigger # as long as it actually works... spinner = AsyncSpinner(self) with spinner: handler = UIHandler(spinner) self.buf.async_eval_at_trg(self.trg, handler, Ci.koICodeIntelBuffer.EVAL_SILENT) return handler
def test_def_decl_trailing_comma(self): """Test having a trailing comma in a function definition; this needs to be invalid in Python 2 but valid in Python 3.""" test_dir = join(self.test_dir, "def_decl_trailing_comma") content, pos = unmark_text(dedent("""\ def foo<1>(arg:int,): arg<2> """)) path = join(test_dir, "def_decl_trailing_comma.py") writefile(path, content) buf = self.mgr.buf_from_path(path, lang=self.lang) lines = lines_from_pos(content, pos) self.assertDefnMatches2(buf, pos[2], line=lines[1], ilk="argument", name="arg", path=path)
def assertScopeLpathIs(self, markedup_content, lpath, lang=None): if lang is None: lang = self.lang path = os.path.join("<Unsaved>", "rand%d" % random.randint(0, 100)) content, data = unmark_text(self.adjust_content(markedup_content)) buf = self.mgr.buf_from_content(content, lang=lang, path=path) buf.scan(skip_scan_time_check=True) actual_blob, actual_lpath = buf.scoperef_from_pos(data["pos"]) self.failUnlessEqual( lpath, actual_lpath, "unexpected %s scope lookup path (lpath) at the given position\n" " expected: %r\n" " got: %r\n" " buffer:\n%s" % (self.lang, lpath, actual_lpath, indent(markedup_content)))
def _doEval(self, markedup_content, lang=None, implicit=True): self.doc.buffer, self.positions = unmark_text( dedent(markedup_content).strip()) spinner = AsyncSpinner(self, callback=partial(setattr, self, "trg")) with spinner: self.buf.trg_from_pos(self.positions["pos"], implicit, spinner) self.assertIsNotNone(self.trg) # don't really care about the rest of the internals of the trigger # as long as it actually works... spinner = AsyncSpinner(self) with spinner: handler = UIHandler(spinner) self.buf.async_eval_at_trg(self.trg, handler, Ci.koICodeIntelBuffer.EVAL_SILENT) return handler
def test_query_interface_2(self): content, positions = unmark_text( dedent("""\ var aFile = Components.classes["@mozilla.org/file/local;1"].createInstance(); if (aFile) { aFile.QueryInterface(Components.interfaces.nsIFile); // Should now know that this supports nsIFile aFile.<1>; } """)) self.assertCompletionsInclude(markup_text(content, pos=positions[1]), [("variable", "followLinks"), ("variable", "path"), ("function", "clone"), ("function", "exists"), ("function", "initWithPath")])
def test_buffer_overrun(self): content, positions = unmark_text( dedent("""\ /* 101 'a's in the next line: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <body> { background: <1>transparent <2>url('../img/header_tab.gif') <3>100% <4>-600px <5>no-repeat; font-family: <6>'Lucida Grande', <7>Verdana, <8>sans-serif; float: <9>left; } """)) name = "css-complete-property-values" self.assertTriggerMatches(markup_text(content, pos=positions[1]), name=name) self.assertCompletionsInclude(markup_text(content, pos=positions[2]), (('value', 'repeat-x'), ))
def test_complete_property_values_complex(self): # completions for complex style of propery-values name = "css-complete-property-values" content, positions = unmark_text( dedent("""\ <p style=" background: <1>transparent <2>url('../img/header_tab.gif') <3>100% <4>-600px <5>no-repeat; font-family: <6>'Lucida Grande', <7>Verdana, <8>sans-serif; float: <9>left; " /> """)) values = set(CSS_ATTR_DICT['background']) for i in range(1, 6): self.assertTriggerMatches(markup_text(content, pos=positions[i]), name=name) # Remove these values, as they are already in the property list v = values.copy() if i == 1: v.difference_update(set(['no-repeat', 'url('])) elif i == 2: v.difference_update(set(['transparent', 'no-repeat'])) elif i == 5: v.difference_update(set(['transparent', 'url('])) else: v.difference_update(set(['transparent', 'url(', 'no-repeat'])) self.assertCompletionsInclude( markup_text(content, pos=positions[i]), [("value", x) for x in v]) values = set(CSS_ATTR_DICT['font-family']) # Remove these values, as they are already in the property list values = values.difference(set(['sans-serif'])) for i in range(6, 9): self.assertTriggerMatches(markup_text(content, pos=positions[i]), name=name) self.assertCompletionsInclude( markup_text(content, pos=positions[i]), [("value", v) for v in values]) values = set(CSS_ATTR_DICT['float']) # Remove these values, as they are already in the property list values = values.difference(set(['left'])) self.assertTriggerMatches(markup_text(content, pos=positions[9]), name=name) self.assertCompletionsInclude(markup_text(content, pos=positions[9]), [("value", v) for v in values])
def test_railsenv_model_toplevel_context(self): test_dir = join(self.test_dir, "railsapp01", "app", "models") main_filename = "cart1.rb" main_content, main_positions = \ unmark_text(self.adjust_content(dedent("""\ class Cart < ActiveRecord::Base val<1> def switch des<2>troy end end """))) main_path = join(test_dir, main_filename) writefile(main_path, main_content) main_buf = self.mgr.buf_from_path(main_path) class_targets = [ ("function", "validate"), ("function", "validate_find_options"), ("function", "validate_on_create"), ("function", "validate_on_update"), ("function", "validates_acceptance_of"), ("function", "validates_associated"), ("function", "validates_confirmation_of"), ("function", "validates_each"), ("function", "validates_exclusion_of"), ("function", "validates_format_of"), ("function", "validates_inclusion_of"), ("function", "validates_length_of"), ("function", "validates_numericality_of"), ("function", "validates_presence_of"), ("function", "validates_size_of"), ("function", "validates_uniqueness_of"), ] inst_targets = [ ("function", "destroy"), ("function", "destroy_all"), ] self.assertCompletionsInclude2(main_buf, main_positions[1], class_targets) self.assertCompletionsInclude2(main_buf, main_positions[2], inst_targets) self.assertCompletionsDoNotInclude( markup_text(main_content, pos=main_positions[1]), inst_targets) self.assertCompletionsDoNotInclude( markup_text(main_content, pos=main_positions[2]), class_targets)
def test_def_decl_trailing_comma(self): """Test having a trailing comma in a function definition; this needs to be invalid in Python 2 but valid in Python 3.""" test_dir = join(self.test_dir, "def_decl_trailing_comma") content, pos = unmark_text( dedent("""\ def foo<1>(arg:int,): arg<2> """)) path = join(test_dir, "def_decl_trailing_comma.py") writefile(path, content) buf = self.mgr.buf_from_path(path, lang=self.lang) lines = lines_from_pos(content, pos) self.assertDefnMatches2(buf, pos[2], line=lines[1], ilk="argument", name="arg", path=path)