<commit_before>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.nanos.test',
  name: 'Test',
  extends: 'foam.nanos.script.Script',

  imports: ['testDAO as scriptDAO'],

  javaImports: [
    'bsh.Interpreter',
    'foam.nanos.logger.Logger',
    'foam.nanos.app.AppConfig',
    'foam.nanos.app.Mode',
    'foam.nanos.pm.PM',
    'java.io.ByteArrayOutputStream',
    'java.io.PrintStream',
    'java.util.Date'
  ],

  tableColumns: [
    'id', 'enabled', /*'description',*/ 'server',
    'passed', 'failed', 'lastRun', 'lastDuration',
    /*'status',*/ 'run'
  ],

  searchColumns: ['id', 'description', 'server'],

  documentation: `
    A scriptable Unit Test.
    Tests can be run on either the server in BeanShell or on the client in Javascript.
    Call test(boolean exp, String message) for each test, where 'exp' evaluates to
    true if the test passed and false if it failed.
  `,

  properties: [
    {
      class: 'Long',
      name: 'passed',
      visibility: 'RO',
      tableCellFormatter: function(value) {
        if ( value ) this.start().style({ color: '#0f0' }).add(value).end();
      },
      tableWidth: 85
    },
    {
      class: 'Long',
      name: 'failed',
      visibility: 'RO',
      tableCellFormatter: function(value) {
        if ( value ) this.start().style({ color: '#f00' }).add(value).end();
      },
      tableWidth: 85
    },
    {
      class: 'String',
      name: 'testSuite'
    },
    {
      class: 'String',
      name: 'daoKey',
      value: 'testDAO',
      transient: true,
      visibility: 'HIDDEN',
      documentation: `Name of dao which journal will be used to store script run logs. To set from inheritor
      just change property value`
    }
  ],

  methods: [
    {
      /** Template method used to add additional code in subclasses. */
      name: 'runTest',
      type: 'Void',
      javaThrows: ['Throwable'],
      code: function(x) {
        return eval(this.code);
      },
      args: [
        {
          name: 'x',
          type: 'Context'
        }
      ],
      javaCode: '/* NOOP */'
    },
    {
      name: 'test',
      type: 'Void',
      args: [
        {
          name: 'exp', type: 'Boolean'
        },
        {
          name: 'message', type: 'String'
        }
      ],
      javaCode: `
        if ( exp ) {
          setPassed(getPassed()+1);
        } else {
          setFailed(getFailed()+1);
        }
        print((exp ? "SUCCESS: " : "FAILURE: ") + message);
      `
    },
    {
      name: 'print',
      type: 'Void',
      args: [
        {
          name: 'message', type: 'String'
        }
      ],
      javaCode: `
        setOutput(getOutput() + "\\n" + message);
      `
    },
    {
      name: 'runScript',
      code: function() {
        var ret;
        var startTime = Date.now();

        return new Promise((resolve, reject) => {
          try {
            this.passed = 0;
            this.failed = 0;
            this.output = '';
            var log = function() {
              this.output += Array.from(arguments).join('') + '\n';
            }.bind(this);
            var test = (condition, message) => {
              if ( condition ) {
                this.passed += 1;
              } else {
                this.failed += 1;
              }
              this.output += ( condition ? 'SUCCESS: ' : 'FAILURE: ' ) +
                message + '\n';
            };

            var updateStats = () => {
              var endTime = Date.now();
              var duration = endTime - startTime; // Unit: milliseconds
              this.lastRun = new Date();
              this.lastDuration = duration;
            };

            with ( { log: log, print: log, x: this.__context__, test: test } ) {
              new Promise.resolve(eval(this.code)).then(() => {
                updateStats();
                resolve();
                //          this.scriptDAO.put(this);
              }, (err) => {
                updateStats();
                this.failed += 1;
                reject(err);
              });
            }
          } catch (err) {
            updateStats();
            this.failed += 1;
            reject(err);
          }
        });
      },
      args: [
        {
          name: 'x', type: 'Context'
        }
      ],
      javaCode: `
        // disable tests in production
        if ( ((AppConfig) x.get("appConfig")).getMode() == Mode.PRODUCTION ) {
          return;
        }

        ByteArrayOutputStream baos  = new ByteArrayOutputStream();
        PrintStream           ps    = new PrintStream(baos);
        Interpreter           shell = createInterpreter(x);<commit_msg>Fix promise test<commit_after>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.nanos.test',
  name: 'Test',
  extends: 'foam.nanos.script.Script',

  imports: ['testDAO as scriptDAO'],

  javaImports: [
    'bsh.Interpreter',
    'foam.nanos.logger.Logger',
    'foam.nanos.app.AppConfig',
    'foam.nanos.app.Mode',
    'foam.nanos.pm.PM',
    'java.io.ByteArrayOutputStream',
    'java.io.PrintStream',
    'java.util.Date'
  ],

  tableColumns: [
    'id', 'enabled', /*'description',*/ 'server',
    'passed', 'failed', 'lastRun', 'lastDuration',
    /*'status',*/ 'run'
  ],

  searchColumns: ['id', 'description', 'server'],

  documentation: `
    A scriptable Unit Test.
    Tests can be run on either the server in BeanShell or on the client in Javascript.
    Call test(boolean exp, String message) for each test, where 'exp' evaluates to
    true if the test passed and false if it failed.
  `,

  properties: [
    {
      class: 'Long',
      name: 'passed',
      visibility: 'RO',
      tableCellFormatter: function(value) {
        if ( value ) this.start().style({ color: '#0f0' }).add(value).end();
      },
      tableWidth: 85
    },
    {
      class: 'Long',
      name: 'failed',
      visibility: 'RO',
      tableCellFormatter: function(value) {
        if ( value ) this.start().style({ color: '#f00' }).add(value).end();
      },
      tableWidth: 85
    },
    {
      class: 'String',
      name: 'testSuite'
    },
    {
      class: 'String',
      name: 'daoKey',
      value: 'testDAO',
      transient: true,
      visibility: 'HIDDEN',
      documentation: `Name of dao which journal will be used to store script run logs. To set from inheritor
      just change property value`
    }
  ],

  methods: [
    {
      /** Template method used to add additional code in subclasses. */
      name: 'runTest',
      type: 'Void',
      javaThrows: ['Throwable'],
      code: function(x) {
        return eval(this.code);
      },
      args: [
        {
          name: 'x',
          type: 'Context'
        }
      ],
      javaCode: '/* NOOP */'
    },
    {
      name: 'test',
      type: 'Void',
      args: [
        {
          name: 'exp', type: 'Boolean'
        },
        {
          name: 'message', type: 'String'
        }
      ],
      javaCode: `
        if ( exp ) {
          setPassed(getPassed()+1);
        } else {
          setFailed(getFailed()+1);
        }
        print((exp ? "SUCCESS: " : "FAILURE: ") + message);
      `
    },
    {
      name: 'print',
      type: 'Void',
      args: [
        {
          name: 'message', type: 'String'
        }
      ],
      javaCode: `
        setOutput(getOutput() + "\\n" + message);
      `
    },
    {
      name: 'runScript',
      code: function() {
        var ret;
        var startTime = Date.now();

        return new Promise((resolve, reject) => {
          try {
            this.passed = 0;
            this.failed = 0;
            this.output = '';
            var log = function() {
              this.output += Array.from(arguments).join('') + '\n';
            }.bind(this);
            var test = (condition, message) => {
              if ( condition ) {
                this.passed += 1;
              } else {
                this.failed += 1;
              }
              this.output += ( condition ? 'SUCCESS: ' : 'FAILURE: ' ) +
                message + '\n';
            };

            var updateStats = () => {
              var endTime = Date.now();
              var duration = endTime - startTime; // Unit: milliseconds
              this.lastRun = new Date();
              this.lastDuration = duration;
            };

            with ( { log: log, print: log, x: this.__context__, test: test } ) {
              Promise.resolve(eval(this.code)).then(() => {
                updateStats();
                resolve();
                //          this.scriptDAO.put(this);
              }, (err) => {
                updateStats();
                this.failed += 1;
                reject(err);
              });
            }
          } catch (err) {
            updateStats();
            this.failed += 1;
            reject(err);
          }
        });
      },
      args: [
        {
          name: 'x', type: 'Context'
        }
      ],
      javaCode: `
        // disable tests in production
        if ( ((AppConfig) x.get("appConfig")).getMode() == Mode.PRODUCTION ) {
          return;
        }

        ByteArrayOutputStream baos  = new ByteArrayOutputStream();
        PrintStream           ps    = new PrintStream(baos);
        Interpreter           shell = createInterpreter(x);
{'fraction_non_alphanumeric': 0.14838233744335547, 'fraction_numerical': 0.004636948045104858, 'mean_word_length': 1.5145733969263382, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '1434460', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2789, 'n_words': 662}