<commit_before>path = require 'path'
ColorBuffer = require '../lib/color-buffer'
jsonFixture = require('./spec-helper').jsonFixture(__dirname, 'fixtures')

describe 'ColorBuffer', ->
  [editor, colorBuffer, pigments, project] = []

  sleep = (ms) ->
    start = new Date
    -> new Date - start >= ms

  editBuffer = (text, options={}) ->
    if options.start?
      if options.end?
        range = [options.start, options.end]
      else
        range = [options.start, options.start]

      editor.setSelectedBufferRange(range)

    editor.insertText(text)
    editor.getBuffer().emitter.emit('did-stop-changing') unless options.noEvent

  beforeEach ->
    atom.config.set 'pigments.delayBeforeScan', 0
    atom.config.set 'pigments.sourceNames', [
      '*.styl'
      '*.less'
    ]

    atom.config.set 'pigments.ignoredNames', ['project/vendor/**']

    waitsForPromise ->
      atom.workspace.open('four-variables.styl').then (o) -> editor = o

    waitsForPromise -> atom.packages.activatePackage('pigments').then (pkg) ->
      pigments = pkg.mainModule
      project = pigments.getProject()

  it 'creates a color buffer for each editor in the workspace', ->
    expect(project.colorBuffersByEditorId[editor.id]).toBeDefined()

  describe 'when an editor without path is opened', ->
    beforeEach ->
      waitsForPromise ->
        atom.workspace.open().then (o) ->
          editor = o
          colorBuffer = project.colorBufferForEditor(editor)

      waitsForPromise -> colorBuffer.variablesAvailable()

    it 'does not fails when updating the colorBuffer', ->
      expect(-> colorBuffer.update()).not.toThrow()

    describe 'when the file is saved and aquires a path', ->
      describe 'that is legible', ->
        beforeEach ->
          spyOn(colorBuffer, 'update').andCallThrough()
          spyOn(editor, 'getPath').andReturn('new-path.styl')
          editor.emitter.emit 'did-change-path', editor.getPath()

          waitsFor -> colorBuffer.update.callCount > 0

        it 'adds the path to the project paths', ->
          expect('new-path.styl' in project.getPaths()).toBeTruthy()

      describe 'that is not legible', ->
        beforeEach ->
          spyOn(colorBuffer, 'update').andCallThrough()
          spyOn(editor, 'getPath').andReturn('new-path.sass')
          editor.emitter.emit 'did-change-path', editor.getPath()

          waitsFor -> colorBuffer.update.callCount > 0

        it 'does not add the path to the project paths', ->
          expect('new-path.styl' in project.getPaths()).toBeFalsy()

      describe 'that is ignored', ->
        beforeEach ->
          spyOn(colorBuffer, 'update').andCallThrough()
          spyOn(editor, 'getPath').andReturn('project/vendor/new-path.styl')
          editor.emitter.emit 'did-change-path', editor.getPath()

          waitsFor -> colorBuffer.update.callCount > 0

        it 'does not add the path to the project paths', ->
          expect('new-path.styl' in project.getPaths()).toBeFalsy()

  describe 'with rapid changes that triggers a rescan', ->
    beforeEach ->
      colorBuffer = project.colorBufferForEditor(editor)
      waitsForPromise -> colorBuffer.variablesAvailable()

      runs -><commit_msg>:green_heart: Disable tasks termination test<commit_after>path = require 'path'
ColorBuffer = require '../lib/color-buffer'
jsonFixture = require('./spec-helper').jsonFixture(__dirname, 'fixtures')

describe 'ColorBuffer', ->
  [editor, colorBuffer, pigments, project] = []

  sleep = (ms) ->
    start = new Date
    -> new Date - start >= ms

  editBuffer = (text, options={}) ->
    if options.start?
      if options.end?
        range = [options.start, options.end]
      else
        range = [options.start, options.start]

      editor.setSelectedBufferRange(range)

    editor.insertText(text)
    editor.getBuffer().emitter.emit('did-stop-changing') unless options.noEvent

  beforeEach ->
    atom.config.set 'pigments.delayBeforeScan', 0
    atom.config.set 'pigments.sourceNames', [
      '*.styl'
      '*.less'
    ]

    atom.config.set 'pigments.ignoredNames', ['project/vendor/**']

    waitsForPromise ->
      atom.workspace.open('four-variables.styl').then (o) -> editor = o

    waitsForPromise -> atom.packages.activatePackage('pigments').then (pkg) ->
      pigments = pkg.mainModule
      project = pigments.getProject()

  it 'creates a color buffer for each editor in the workspace', ->
    expect(project.colorBuffersByEditorId[editor.id]).toBeDefined()

  describe 'when an editor without path is opened', ->
    beforeEach ->
      waitsForPromise ->
        atom.workspace.open().then (o) ->
          editor = o
          colorBuffer = project.colorBufferForEditor(editor)

      waitsForPromise -> colorBuffer.variablesAvailable()

    it 'does not fails when updating the colorBuffer', ->
      expect(-> colorBuffer.update()).not.toThrow()

    describe 'when the file is saved and aquires a path', ->
      describe 'that is legible', ->
        beforeEach ->
          spyOn(colorBuffer, 'update').andCallThrough()
          spyOn(editor, 'getPath').andReturn('new-path.styl')
          editor.emitter.emit 'did-change-path', editor.getPath()

          waitsFor -> colorBuffer.update.callCount > 0

        it 'adds the path to the project paths', ->
          expect('new-path.styl' in project.getPaths()).toBeTruthy()

      describe 'that is not legible', ->
        beforeEach ->
          spyOn(colorBuffer, 'update').andCallThrough()
          spyOn(editor, 'getPath').andReturn('new-path.sass')
          editor.emitter.emit 'did-change-path', editor.getPath()

          waitsFor -> colorBuffer.update.callCount > 0

        it 'does not add the path to the project paths', ->
          expect('new-path.styl' in project.getPaths()).toBeFalsy()

      describe 'that is ignored', ->
        beforeEach ->
          spyOn(colorBuffer, 'update').andCallThrough()
          spyOn(editor, 'getPath').andReturn('project/vendor/new-path.styl')
          editor.emitter.emit 'did-change-path', editor.getPath()

          waitsFor -> colorBuffer.update.callCount > 0

        it 'does not add the path to the project paths', ->
          expect('new-path.styl' in project.getPaths()).toBeFalsy()

  # FIXME Using a 1s sleep seems to do nothing on Travis, it'll need
  # a better solution.
  xdescribe 'with rapid changes that triggers a rescan', ->
    beforeEach ->
      colorBuffer = project.colorBufferForEditor(editor)
      waitsForPromise -> colorBuffer.variablesAvailable()

      runs ->
{'fraction_non_alphanumeric': 0.13187652811735942, 'fraction_numerical': 0.0013753056234718827, 'mean_word_length': 2.7658227848101267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '640905', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1876, 'n_words': 446}