package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file unit_parsing_js.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
open Common
open OUnit

(*****************************************************************************)
(* Unit tests *)
(*****************************************************************************)

let unittest =
  "parsing_js" >::: [

    "regression files" >:: (fun () ->
      let dir = Filename.concat Config_pfff.path "/tests/js/parsing" in
      let files = 
        Common2.glob (spf "%s/*.js" dir) @
        Common2.glob (spf "%s/jsx/*.js" dir) @
        Common2.glob (spf "%s/typescript/*.js" dir) @
        []
      in
      files +> List.iter (fun file ->
        try
          let _ = Parse_js.parse_program file in
          ()
        with Parse_js.Parse_error _ ->
          assert_failure (spf "it should correctly parse %s" file)
      )
    );


    "rejecting bad code" >:: (fun () ->
      try 
        Common.save_excursion Flag_parsing.show_parsing_error false (fun()->
         let _ = Parse_js.program_of_string "echo 1+" in
         assert_failure "it should have thrown a Parse_error exception"
        )
      with Parse_js.Parse_error _ -> ()
    );
(*
    "the javascript AST mapper" >:: (fun () ->
      let js_ex = "foo(42, 101);" in
      Common2.with_tmp_file ~str:js_ex ~ext:".js" (fun file ->
        let prog = Parse_js.parse_program file in
        let map_visitor = M.mk_visitor { M.default_visitor with
          M.kexpr = (fun (k, _) x ->
            match x with
            | L (Num (s, tok)) ->
                let i = s_to_i s in
                L (Num (i_to_s (i+1), Ast.fakeInfo()))
            | _ -> k x
          );
        }
        in
        let transformed_ast = map_visitor (Program prog) in

        let integers = 
          V.do_visit_with_ref (fun aref -> { V.default_visitor with
            V.kexpr = (fun (k, _) x ->
              match x with
              | L (Num (s, tok)) -> 
                  Common.push2 (s_to_i s) aref
              | _ -> k x
            );
          }) transformed_ast in
        assert_equal
          ~msg:"it should increment all the integers in the program"
          [43; 102] integers;
      )
    );
*)
  ]
OCaml

Innovation. Community. Security.