<commit_before>#!/usr/bin/env coffee

jvm = require './jvm'
{RuntimeState} = require './runtime'
util = require './util'
{disassemble} = require './disassembler'
ClassFile = require './ClassFile'
fs = node?.fs ? require 'fs'
path = node?.path ? require 'path'

"use strict"

root = exports ? window.testing ?= {}

root.find_test_classes = (doppio_dir) ->
  test_dir = path.resolve doppio_dir, 'classes/test'
  for file in fs.readdirSync(test_dir) when path.extname(file) == '.java'
    "classes/test/#{path.basename(file, '.java')}"
  # Note that the lack of return value here implies that the above is actually
  # a list comprehension. This is intended behavior.

root.run_tests = (test_classes, stdout, callback) ->
  doppio_dir = if node? then '/home/doppio/' else path.resolve __dirname, '..'
  # get the tests, if necessary
  if test_classes?.length > 0
    test_classes = (tc.replace(/\.class$/,'') for tc in test_classes)
  else
    test_classes = root.find_test_classes doppio_dir
  # set up the classpath
  jcl_dir = path.resolve doppio_dir, 'vendor/classes'
  jvm.classpath = [doppio_dir, jcl_dir]

  _runner = () ->
    if test_classes.length == 0
      stdout "All tests passed!\n"
      return callback()
    test = test_classes.shift()
    stdout "testing #{test}...\n"
    if (disasm_diff = run_disasm_test(doppio_dir, test))?
      stdout "Failed disasm test #{test}:\n#{disasm_diff}\n"
      return
    run_stdout_test doppio_dir, test, (diff) ->
      if diff?
        stdout "Failed output test #{test}:\n#{diff}\n" 
      else
        _runner()

  _runner()

# remove comments and blank lines, ignore specifics of float/double printing and whitespace
sanitize = (str) ->
  str.replace(/\/\/.*/g, '')
     .replace(/^\s*$[\n\r]+/mg, '')
     .replace(/(float|double)\t.*/g, '$1')
     .replace(/[ \t\r]+/g, ' ')
     .replace(/[ ]\n/g, '\n')
     .replace(/\[ \]/g, '[]')

run_disasm_test = (doppio_dir, test_class) -><commit_msg>cosmetic tweak for test runner<commit_after>#!/usr/bin/env coffee

jvm = require './jvm'
{RuntimeState} = require './runtime'
util = require './util'
{disassemble} = require './disassembler'
ClassFile = require './ClassFile'
fs = node?.fs ? require 'fs'
path = node?.path ? require 'path'

"use strict"

root = exports ? window.testing ?= {}

root.find_test_classes = (doppio_dir) ->
  test_dir = path.resolve doppio_dir, 'classes/test'
  for file in fs.readdirSync(test_dir) when path.extname(file) == '.java'
    "classes/test/#{path.basename(file, '.java')}"
  # Note that the lack of return value here implies that the above is actually
  # a list comprehension. This is intended behavior.

root.run_tests = (test_classes, stdout, callback) ->
  batch_mode = test_classes.length > 1
  doppio_dir = if node? then '/home/doppio/' else path.resolve __dirname, '..'
  # get the tests, if necessary
  if test_classes?.length > 0
    test_classes = (tc.replace(/\.class$/,'') for tc in test_classes)
  else
    test_classes = root.find_test_classes doppio_dir
  # set up the classpath
  jcl_dir = path.resolve doppio_dir, 'vendor/classes'
  jvm.classpath = [doppio_dir, jcl_dir]

  _runner = () ->
    if test_classes.length == 0
      if batch_mode
        stdout "All tests passed!\n"
      else
        stdout "Pass\n"
      return callback()
    test = test_classes.shift()
    stdout "testing #{test}...\n"
    if (disasm_diff = run_disasm_test(doppio_dir, test))?
      stdout "Failed disasm test #{test}:\n#{disasm_diff}\n"
      return
    run_stdout_test doppio_dir, test, (diff) ->
      if diff?
        stdout "Failed output test #{test}:\n#{diff}\n" 
      else
        _runner()

  _runner()

# remove comments and blank lines, ignore specifics of float/double printing and whitespace
sanitize = (str) ->
  str.replace(/\/\/.*/g, '')
     .replace(/^\s*$[\n\r]+/mg, '')
     .replace(/(float|double)\t.*/g, '$1')
     .replace(/[ \t\r]+/g, ' ')
     .replace(/[ ]\n/g, '\n')
     .replace(/\[ \]/g, '[]')

run_disasm_test = (doppio_dir, test_class) ->
{'fraction_non_alphanumeric': 0.1604784450535759, 'fraction_numerical': 0.00174433092449539, 'mean_word_length': 3.1424148606811144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '5587427', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1400, 'n_words': 360}