Repository: atom/atom
File: spec/text-editor-spec.js (JavaScript)

Code before:
      editor.setCursorBufferPosition([10, 0])
      editor.toggleLineCommentsInSelection()

      expect(editor.lineTextForBufferRow(10)).toBe('// ')
      editor.moveToBeginningOfLine()
      editor.insertText('  ')
      editor.setSelectedBufferRange([[10, 0], [10, 0]])
      editor.toggleLineCommentsInSelection()
      expect(editor.lineTextForBufferRow(10)).toBe('  ')
    })
  })

  describe('.toggleLineCommentsForBufferRows', () => {
    describe('xml', () => {
      beforeEach(async () => {
        await atom.packages.activatePackage('language-xml')
        editor = await atom.workspace.open('test.xml')
        editor.setText('<!-- test -->')
      })

      it('removes the leading whitespace from the comment end pattern match when uncommenting lines', () => {
        editor.toggleLineCommentsForBufferRows(0, 0)
        expect(editor.lineTextForBufferRow(0)).toBe('test')
      })

      it('does not select the new delimiters'), () => {
        editor.setText('<!-- test -->')

        let delimLength = '<!--'.length
        let selection = editor.addCursorAtBufferPosition([0, delimLength])

        selection.toggleLineComments()
        expect(selection.isEmpty() && selection.getBufferRange().start.column === 0).toBe(true)

        selection.toggleLineComments()
        expect(selection.isEmpty() && selection.getBufferRange().start.column === delimLength).toBe(true)

        selection.setBufferRange([[0, delimLength], [0, delimLength + 1 + 'test'.length]])

        selection.toggleLineComments()
        let range = selection.getBufferRange()
        expect(range.start.column === 0 && range.end.column === 'test'.length).toBe(true)

        selection.toggleLineComments()
        range = selection.getBufferRange()
        expect(range.start.column === delimLength + 1 && range.end.column === delimLength + 1 + 'test'.length).toBe(true)

        editor.setText('    test')
        selection.setBufferRange([[0, 4], [0,4]])
        selection.toggleLineComments()
        expect(selection.isEmpty() && selection.start.column === 4 + delimLength + 1).toBe(true)

Review comment (suggestion):
Hi. Unfortunately, the closing parentheses after the description string in this test means that the block following the test isn't running at all. So the test is green only because it isn't really running. I'll try and get the test passing, but I may kick this back to you if it ends up being too much work.

Code after:
      expect(editor.getCursorBufferPosition()).toEqual([0, 2])
      editor.moveLeft()
      expect(editor.getCursorBufferPosition()).toEqual([0, 0])
    })
  })

  describe('.setIndentationForBufferRow', () => {
    describe('when the editor uses soft tabs but the row has hard tabs', () => {
      it('only replaces whitespace characters', () => {
        editor.setSoftWrapped(true)
        editor.setText('\t1\n\t2')
        editor.setCursorBufferPosition([0, 0])
        editor.setIndentationForBufferRow(0, 2)
        expect(editor.getText()).toBe('    1\n\t2')
      })
    })

    describe('when the indentation level is a non-integer', () => {
      it('does not throw an exception', () => {
        editor.setSoftWrapped(true)
        editor.setText('\t1\n\t2')
        editor.setCursorBufferPosition([0, 0])
        editor.setIndentationForBufferRow(0, 2.1)
        expect(editor.getText()).toBe('    1\n\t2')
      })
    })
  })

  describe("when the editor's grammar has an injection selector", () => {
    beforeEach(async () => {
      atom.config.set('core.useTreeSitterParsers', false)
      await atom.packages.activatePackage('language-text')
      await atom.packages.activatePackage('language-javascript')
    })

    it("includes the grammar's patterns when the selector matches the current scope in other grammars", async () => {
      await atom.packages.activatePackage('language-hyperlink')

      const grammar = atom.grammars.selectGrammar('text.js')
      const { line, tags } = grammar.tokenizeLine('var i; // http://github.com')

      const tokens = atom.grammars.decodeTokens(line, tags)
      expect(tokens[0].value).toBe('var')
      expect(tokens[0].scopes).toEqual(['source.js', 'storage.type.var.js'])
      expect(tokens[6].value).toBe('http://github.com')
      expect(tokens[6].scopes).toEqual([
        'source.js',
        'comment.line.double-slash.js',
        'markup.underline.link.http.hyperlink'
      ])
    })