<commit_before><!-- Copyright 2016 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>QUnit Example</title>
  <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.1.css">
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="https://code.jquery.com/qunit/qunit-2.0.1.js"></script>
  <script src="../js/main.js"></script>
  <script>
    /*jshint esversion: 6*/

    var badResponse = new Response('test', {
      status: 404,
      statusText: 'File not found'
    });
    var failure = new Promise(function(resolve) {
      resolve(badResponse);
    });

    var goodResponse = new Response('test', {
      status: 200,
      statusText: 'success'
    });
    var success = new Promise(function(resolve, reject) {
      resolve(goodResponse);
    });

    QUnit.test('validateResponse', function(assert) {
      var done = assert.async(2);
      success
      .then(app.validateResponse)
      .then((response) => {
        assert.equal(response.statusText, goodResponse.statusText,
          'returns the response object');
        done();
      })
      // A good response throws an error
      .catch(() => {
        assert.ok(false,
          'returns the response object');
        done();
      });

      failure
      .then(app.validateResponse)
      // A bad response doesn't throw an error
      .then((response) => {
        assert.ok(false, 'bad responses throw errors');
        done();
      })
      .catch((error) => {
        assert.ok(~error.message.indexOf('File not found'),
          'bad responses throw error that contains response.statusText');
        done();
      });

    });

    QUnit.test('readResponseAsJSON', function(assert) {
      var done = assert.async();

      fetch('../examples/animals.json')
      .then(app.readResponseAsJSON)
      .then((actual) => {
        if (actual) {
          var expected = {
            'animals': {
              'cat': 'kitten',
              'dog': 'puppy',
              'kangaroo': 'joey'
            },
            'type': 'animal'
          };
          assert.equal(actual.animals.cat, expected.animals.cat,
            'JSON is returned');
          done();
        } else {
          assert.ok(false, 'an object is returned');
          done();
        }<commit_msg>test(fetch): make unit test more robust<commit_after><!-- Copyright 2016 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>QUnit Example</title>
  <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.1.css">
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="https://code.jquery.com/qunit/qunit-2.0.1.js"></script>
  <script src="../js/main.js"></script>
  <script>
    /*jshint esversion: 6*/

    var badResponse = new Response('test', {
      status: 404,
      statusText: 'File not found'
    });
    var failure = new Promise(function(resolve) {
      resolve(badResponse);
    });

    var goodResponse = new Response('test', {
      status: 200,
      statusText: 'success'
    });
    var success = new Promise(function(resolve, reject) {
      resolve(goodResponse);
    });

    QUnit.test('validateResponse', function(assert) {
      var done = assert.async(2);
      success
      .then(app.validateResponse)
      .then((response) => {
        assert.equal(response.statusText, goodResponse.statusText,
          'returns the response object');
        done();
      })
      // A good response throws an error
      .catch(() => {
        assert.ok(false,
          'returns the response object');
        done();
      });

      failure
      .then(app.validateResponse)
      // A bad response doesn't throw an error
      .then((response) => {
        assert.ok(false, 'bad responses throw errors');
        done();
      })
      .catch((error) => {
        assert.ok(error.message && error.message.indexOf('File not found') != -1,
          'bad responses throw error that contains response.statusText');
        done();
      });

    });

    QUnit.test('readResponseAsJSON', function(assert) {
      var done = assert.async();

      fetch('../examples/animals.json')
      .then(app.readResponseAsJSON)
      .then((actual) => {
        if (actual) {
          var expected = {
            'animals': {
              'cat': 'kitten',
              'dog': 'puppy',
              'kangaroo': 'joey'
            },
            'type': 'animal'
          };
          assert.equal(actual.animals.cat, expected.animals.cat,
            'JSON is returned');
          done();
        } else {
          assert.ok(false, 'an object is returned');
          done();
        }
{'fraction_non_alphanumeric': 0.14110846650799047, 'fraction_numerical': 0.007990479428765726, 'mean_word_length': 2.3810344827586207, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 53, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '655395', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1666, 'n_words': 469}