package pfff

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

Source file unit_analyze_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
open Common

module E = Entity_code

open OUnit

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)

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

(*---------------------------------------------------------------------------*)
(* Annotations *)
(*---------------------------------------------------------------------------*)
let annotation_unittest =
  "annotation_js" >::: [

    "commonjs annotations" >:: (fun () ->
      let file_content = "
/**
 * @providesModule my-module
 */
function foo() {}
/**
 * @providesLegacy other-module
 */
function bar() {}
" in
      Common2.with_tmp_file ~str:file_content ~ext:"js" (fun tmpfile ->
        let (ast_and_tokens, _stat) = Parse_js.parse tmpfile in
        let annots = 
          Annotation_js.annotations_of_program_with_comments ast_and_tokens
          +> List.map fst
        in
        assert_equal 
          ~msg:"it should extract @providesModule annotations"
          [Annotation_js.ProvidesModule "my-module";
           Annotation_js.ProvidesLegacy "other-module";
          ]
          annots
      )
    );
  ]

(*---------------------------------------------------------------------------*)
(* Tags *)
(*---------------------------------------------------------------------------*)

let tags_unittest =
 "tags_js" >::: [
   "commonjs tags support" >:: (fun () ->
     let file_content = "
/**
 * @providesModule my-module
 */
function foo() {}
" in
     Common2.with_tmp_file ~str:file_content ~ext:"js" (fun tmpfile ->
       let tags = Tags_js.tags_of_files_or_dirs ~verbose:false [tmpfile] in
       (match tags +> List.map snd +> List.flatten with
       | [{ Tags_file.tagname = "my-module"; kind = E.Module; _}] -> ()
       | _ -> assert_failure "it should extract module names from comments"
       );

       let tmpfile = Common.new_temp_file "unit" "tags" in
       Tags_file.generate_vi_tags_file tmpfile tags;
       let content = Common.read_file tmpfile in
       if content =~ "my-module\t.*\t/\\(.*\\)/;.*"
       then
         let str = Common.matched1 content in
         assert_equal
           ~msg:"it should correctly escape slash in vi tags file"
           "\\/**"
           str
       else assert_failure "it should generate the write vi tags file"
     ));
 ]

(*---------------------------------------------------------------------------*)
(* Final suite *)
(*---------------------------------------------------------------------------*)

let unittest =
  "analyze_js" >::: [
    annotation_unittest;
    tags_unittest;
  ]
OCaml

Innovation. Community. Security.