<commit_before>import * as tmp from 'tmp';
import * as fs from 'fs';

import { scrubFile } from './ngo';


function repeatSpace(count: number) {
  let space = '';
  for (let i = 0; i < count; i++) {
    space += ' ';
  }
  return space;
}

describe('ngo', () => {
  describe('decorators', () => {
    const clazz = 'var Clazz = (function () { function Clazz() { } return Clazz; }());';

    it('replaces Angular decorators with spaces', () => {
      const decorators = 'Clazz.decorators = [ { type: Injectable } ];';
      const input = `
        import { Injectable } from '@angular/core';
        ${clazz}
        ${decorators}
      `;
      const output = input.replace(decorators, repeatSpace(decorators.length));

      const tmpFile = tmp.fileSync({ postfix: '.js' }).name;
      fs.writeFileSync(tmpFile, input);

      expect(scrubFile(tmpFile, 'spec')).toEqual(output);
    });

    it('doesn\'t replace non Angular decorators', () => {
      const decorators = 'Clazz.decorators = [ { type: Injectable } ];';
      const input = `
        import { Injectable } from 'another-lib';
        ${clazz}
        ${decorators}
      `;

      const tmpFile = tmp.fileSync({ postfix: '.js' }).name;
      fs.writeFileSync(tmpFile, input);

      expect(scrubFile(tmpFile, 'spec')).toEqual(input);
    });

    it('leaves non-Angulars decorators in mixed arrays', () => {
      const angularDecorator = '{ type: Injectable }';
      const decorators = `Clazz.decorators = [ ${angularDecorator}, { type: NotInjectable } ];`;
      const input = `
        import { Injectable } from '@angular/core';
        import { NotInjectable } from 'another-lib';
        ${clazz}
        ${decorators}
      `;
      const output = input.replace(angularDecorator, repeatSpace(angularDecorator.length));

      const tmpFile = tmp.fileSync({ postfix: '.js' }).name;
      fs.writeFileSync(tmpFile, input);

      expect(scrubFile(tmpFile, 'spec')).toEqual(output);
    });
  });
});
<commit_msg>add ctorParameters test<commit_after>import * as tmp from 'tmp';
import * as fs from 'fs';

import { scrubFile } from './ngo';

const spaceReplacer = (match) => match.replace(/[^ \t\r\n]/g, ' ');
const scrubFileContents = (content) => {
  const filePath = tmp.fileSync({ postfix: '.js' }).name;
  fs.writeFileSync(filePath, content);
  return scrubFile(filePath, 'spec');
}

describe('ngo', () => {
  const clazz = 'var Clazz = (function () { function Clazz() { } return Clazz; }());';

  describe('decorators', () => {
    it('replaces Angular decorators with spaces', () => {
      const decorators = 'Clazz.decorators = [ { type: Injectable } ];';
      const input = `
        import { Injectable } from '@angular/core';
        ${clazz}
        ${decorators}
      `;
      const output = input.replace(decorators, spaceReplacer);

      expect(scrubFileContents(input)).toEqual(output);
    });

    it('doesn\'t replace non Angular decorators', () => {
      const decorators = 'Clazz.decorators = [ { type: Injectable } ];';
      const input = `
        import { Injectable } from 'another-lib';
        ${clazz}
        ${decorators}
      `;

      expect(scrubFileContents(input)).toEqual(input);
    });

    it('leaves non-Angulars decorators in mixed arrays', () => {
      const angularDecorator = '{ type: Injectable }';
      const decorators = `Clazz.decorators = [ ${angularDecorator}, { type: NotInjectable } ];`;
      const input = `
        import { Injectable } from '@angular/core';
        import { NotInjectable } from 'another-lib';
        ${clazz}
        ${decorators}
      `;
      const output = input.replace(angularDecorator, spaceReplacer);

      expect(scrubFileContents(input)).toEqual(output);
    });
  });

  describe('ctorParameters', () => {
    it('removes empty constructor parameters', () => {
      const ctorParameters = 'Clazz.ctorParameters = function () { return []; };';
      const input = `
        ${clazz}
        ${ctorParameters}
      `;
      const output = input.replace(ctorParameters, spaceReplacer);

      expect(scrubFileContents(input)).toEqual(output);
    });

    it('removes non-empty constructor parameters', () => {
      const ctorParameters = `Clazz.ctorParameters = function () { return [{type: Injector}]; };`;
      const input = `
        ${clazz}
        ${ctorParameters}
      `;
      const output = input.replace(ctorParameters, spaceReplacer);

      expect(scrubFileContents(input)).toEqual(output);
    });

    it('doesn\'t remove constructor parameters from whitelisted classes', () => {
      const ctorParameters = 'PlatformRef_.ctorParameters = function () { return []; };';
      const input = `
        ${clazz.replace('Clazz', 'PlatformRef_')}
        ${ctorParameters}
      `;

      expect(scrubFileContents(input)).toEqual(input);
    });
  });
});

{'fraction_non_alphanumeric': 0.163151364764268, 'fraction_numerical': 0.00020678246484698098, 'mean_word_length': 2.8976631748589847, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 19, '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': '4844343', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1461, 'n_words': 320}