<commit_before>(**
 * Copyright (c) 2013-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the "flow" directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 *)

open Hh_json
open Utils_js

module Ast = Spider_monkey_ast
module C = Tty

module RunHardcodedTests : sig
  val main : unit -> unit
end = struct

  let should_color =
    Sys.os_type <> "Win32" && Unix.isatty Unix.stdout && Sys.getenv "TERM" <> "dumb"

  let print to_print =
    if should_color
    then
      C.cprint to_print
    else
      let strings = List.map snd to_print in
      List.iter (Printf.printf "%s") strings

  let parse_args () =
    let dump_ast = ref false in
    let filter = ref None in
    let json_errors = ref false in
    let test_file = ref None in
    let parse_test_file x =
      if !test_file <> None then raise (Arg.Bad "Too many arguments");
      test_file := Some x
    in
    let spec = Arg.align [
      "--dumpAst", Arg.Set dump_ast,
        " Dumps the esprima & flow ASTs before each test";
      "--filter", Arg.String (fun x -> filter := Some x),
        " Only run tests that match the regex";
      "--jsonErrors", Arg.Set json_errors,
        " Output errors in json format";
    ] in
    let usage = spf "usage: %s [OPTIONS] TEST_FILE\n\nSupported options\n"
      (Filename.basename Sys.executable_name) in
    Arg.parse spec parse_test_file usage;
    let opt_test_file = match !test_file with
    | Some x -> x
    | _ ->
      prerr_endline "ERROR: Path to test file is required\n";
      Arg.usage spec usage;
      exit 1
    in
    (!dump_ast, !filter, !json_errors, opt_test_file)

  let rec parse_spec_value = Ast.Expression.(function
    | (_, Object { Object.properties }) ->
      JSON_Object (List.map parse_spec properties)
    | (_, Array { Array.elements }) ->
      JSON_Object (List.mapi (fun i elem ->
        let child = match elem with
        | Some (Expression e) -> parse_spec_value e
        | Some (Spread _)
        | None -> failwith "spread is not supported"
        in
        string_of_int i, child
      ) elements)
    | (_, Literal { Ast.Literal.value = Ast.Literal.String name; _ }) ->
        JSON_String name
    | (_, Literal { Ast.Literal.value = Ast.Literal.Number num; _ }) ->
        JSON_Number (string_of_float_trunc num)
    | (_, Literal { Ast.Literal.value = Ast.Literal.Boolean value; _ }) ->
        JSON_Bool value
    | (_, Literal { Ast.Literal.value = Ast.Literal.Null; _ }) ->
        JSON_Null
    | (_, Unary { Unary.operator = Unary.Minus;
                  prefix = true;
                  argument = (_, Literal {
                    Ast.Literal.value = Ast.Literal.Number num; _
                  });
                }) -><commit_msg>fix assertion that an array is empty<commit_after>(**
 * Copyright (c) 2013-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the "flow" directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 *)

open Hh_json
open Utils_js

module Ast = Spider_monkey_ast
module C = Tty

module RunHardcodedTests : sig
  val main : unit -> unit
end = struct

  let should_color =
    Sys.os_type <> "Win32" && Unix.isatty Unix.stdout && Sys.getenv "TERM" <> "dumb"

  let print to_print =
    if should_color
    then
      C.cprint to_print
    else
      let strings = List.map snd to_print in
      List.iter (Printf.printf "%s") strings

  let parse_args () =
    let dump_ast = ref false in
    let filter = ref None in
    let json_errors = ref false in
    let test_file = ref None in
    let parse_test_file x =
      if !test_file <> None then raise (Arg.Bad "Too many arguments");
      test_file := Some x
    in
    let spec = Arg.align [
      "--dumpAst", Arg.Set dump_ast,
        " Dumps the esprima & flow ASTs before each test";
      "--filter", Arg.String (fun x -> filter := Some x),
        " Only run tests that match the regex";
      "--jsonErrors", Arg.Set json_errors,
        " Output errors in json format";
    ] in
    let usage = spf "usage: %s [OPTIONS] TEST_FILE\n\nSupported options\n"
      (Filename.basename Sys.executable_name) in
    Arg.parse spec parse_test_file usage;
    let opt_test_file = match !test_file with
    | Some x -> x
    | _ ->
      prerr_endline "ERROR: Path to test file is required\n";
      Arg.usage spec usage;
      exit 1
    in
    (!dump_ast, !filter, !json_errors, opt_test_file)

  let rec parse_spec_value = Ast.Expression.(function
    | (_, Object { Object.properties }) ->
      JSON_Object (List.map parse_spec properties)
    | (_, Array { Array.elements }) ->
      let props = List.mapi (fun i elem ->
        let child = match elem with
        | Some (Expression e) -> parse_spec_value e
        | Some (Spread _)
        | None -> failwith "spread is not supported"
        in
        string_of_int i, child
      ) elements in
      JSON_Object (("length", int_ (List.length elements))::props)
    | (_, Literal { Ast.Literal.value = Ast.Literal.String name; _ }) ->
        JSON_String name
    | (_, Literal { Ast.Literal.value = Ast.Literal.Number num; _ }) ->
        JSON_Number (string_of_float_trunc num)
    | (_, Literal { Ast.Literal.value = Ast.Literal.Boolean value; _ }) ->
        JSON_Bool value
    | (_, Literal { Ast.Literal.value = Ast.Literal.Null; _ }) ->
        JSON_Null
    | (_, Unary { Unary.operator = Unary.Minus;
                  prefix = true;
                  argument = (_, Literal {
                    Ast.Literal.value = Ast.Literal.Number num; _
                  });
                }) ->
{'fraction_non_alphanumeric': 0.10296385129347267, 'fraction_numerical': 0.0023984923762206613, 'mean_word_length': 2.283464566929134, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4724912', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1763, 'n_words': 581}