<commit_before>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';

describe('Canonicalizer', function() {
  var Canonicalizer;
  var IDLFile;
  var IDLFileContents;
  var Parser;

  beforeEach(function() {
    Canonicalizer = foam.lookup('org.chromium.webidl.Canonicalizer');
    IDLFile = foam.lookup('org.chromium.webidl.IDLFile');
    IDLFileContents = foam.lookup('org.chromium.webidl.IDLFileContents');
    Parser = foam.lookup('org.chromium.webidl.Parser');
  });

  var parse = function(idl) {
    return Parser.create().parseString(idl).value[0];
  }

  it('should return same number of files since we have different interfaces', function(done) {
    // Setting up files for canonicalization.
    var firstIdl = `interface FirstInterface { };`;
    var secondIdl = `partial interface SecondInterface { };`;

    var firstAst = parse(firstIdl);
    var secondAst = parse(secondIdl);

    // Callback function once Canonicalizer has finished processing.
    var onDone = function(results) {
      expect(results.length).toBe(2);
      done();
    };

    var canonicalizer = Canonicalizer.create({
      onDone: onDone,
      waitTime: 3, // Three seconds.
    });

    canonicalizer.addFragment(firstAst);
    canonicalizer.addFragment(secondAst);
  });

  it('should return canonicalized file for two fragments with same interface', function(done) {
    // Setting up files for canonicalization.
    var firstIdl = `
        [RaisesException=Constructor]
        interface SharedWorker : EventTarget {
        readonly attribute MessagePort incomingPort;
        readonly attribute MessagePort outgoingPort;
      };`;

    var secondIdl = `
      partial interface SharedWorker {
        [CallWith=ScriptState, Measure] readonly attribute DOMHighResTimeStamp workerStart;
      };`;

    var firstAst = parse(firstIdl);
    var secondAst = parse(secondIdl);

    // Callback function once Canonicalizer has finished processing.
    var onDone = function(results) {
      // Expecting one canonicalized file.
      expect(results.length).toBe(1);
      // Expecting the file to reference two sources.
      expect(results[0].sources.length).toBe(2);
      // Expecting the definition to have 2 members and not be partial.
      expect(results[0].definition.members.length).toBe(3);
      expect(results[0].definition.isPartial).toBe(false);
      // Expecting decorators / attributes to be present.
      expect(results[0].attrs.length).toBe(1);
      // Expecting inheritance to be present.
      expect(results[0].definition.inheritsFrom).toBeDefined();
      done();
    };

    var canonicalizer = Canonicalizer.create({
      onDone: onDone,<commit_msg>Adding additional Canonicalizer test to verify objects are cloned<commit_after>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';

describe('Canonicalizer', function() {
  var Canonicalizer;
  var IDLFile;
  var IDLFileContents;
  var Parser;

  beforeEach(function() {
    Canonicalizer = foam.lookup('org.chromium.webidl.Canonicalizer');
    IDLFile = foam.lookup('org.chromium.webidl.IDLFile');
    IDLFileContents = foam.lookup('org.chromium.webidl.IDLFileContents');
    Parser = foam.lookup('org.chromium.webidl.Parser');
  });

  var parse = function(idl) {
    return Parser.create().parseString(idl).value[0];
  }

  it('should return same number of files since we have different interfaces', function(done) {
    // Setting up files for canonicalization.
    var firstIdl = `interface FirstInterface { };`;
    var secondIdl = `partial interface SecondInterface { };`;

    var firstAst = parse(firstIdl);
    var secondAst = parse(secondIdl);

    // Callback function once Canonicalizer has finished processing.
    var onDone = function(results) {
      expect(results.length).toBe(2);
      done();
    };

    var canonicalizer = Canonicalizer.create({
      onDone: onDone,
      waitTime: 3, // Three seconds.
    });

    canonicalizer.addFragment(firstAst);
    canonicalizer.addFragment(secondAst);
  });

  it('should return canonicalized file for two fragments with same interface', function(done) {
    // Setting up files for canonicalization.
    var firstIdl = `
        [RaisesException=Constructor]
        interface SharedWorker : EventTarget {
        readonly attribute MessagePort incomingPort;
        readonly attribute MessagePort outgoingPort;
      };`;

    var secondIdl = `
      partial interface SharedWorker {
        [CallWith=ScriptState, Measure] readonly attribute DOMHighResTimeStamp workerStart;
      };`;

    var firstAst = parse(firstIdl);
    var secondAst = parse(secondIdl);

    var firstClone = firstAst.clone();
    var secondClone = secondAst.clone();

    // Callback function once Canonicalizer has finished processing.
    var onDone = function(results) {
      // Expecting one canonicalized file.
      expect(results.length).toBe(1);
      // Expecting the file to reference two sources.
      expect(results[0].sources.length).toBe(2);
      // Expecting the definition to have 2 members and not be partial.
      expect(results[0].definition.members.length).toBe(3);
      expect(results[0].definition.isPartial).toBe(false);
      // Expecting decorators / attributes to be present.
      expect(results[0].attrs.length).toBe(1);
      // Expecting inheritance to be present.
      expect(results[0].definition.inheritsFrom).toBeDefined();

      // After merging, the original objects should remain untouched.
      expect(firstAst.definition.members.length)
          .toBe(firstClone.definition.members.length);
      expect(secondAst.definition.members.length)
          .toBe(secondClone.definition.members.length);
      expect(firstAst.attrs.length).toEqual(firstClone.attrs.length);
      expect(secondAst.attrs.length).toEqual(secondClone.attrs.length);
      expect(firstAst.definition.isPartial).toBe(false);
      expect(secondAst.definition.isPartial).toBe(true);

      done();
    };

    var canonicalizer = Canonicalizer.create({
      onDone: onDone,
{'fraction_non_alphanumeric': 0.1076849336954785, 'fraction_numerical': 0.005432177664163604, 'mean_word_length': 3.3593314763231197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '947110', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1715, 'n_words': 451}