fdlint 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +14 -0
  3. data/README.md +68 -0
  4. data/Rakefile +92 -0
  5. data/bin/fdlint +17 -0
  6. data/lib/base_parser.rb +143 -0
  7. data/lib/cmd_runner.rb +145 -0
  8. data/lib/context.rb +31 -0
  9. data/lib/css/parser.rb +186 -0
  10. data/lib/css/reader.rb +30 -0
  11. data/lib/css/rule/check_compression_rule.rb +48 -0
  12. data/lib/css/rule/checklist.rb +45 -0
  13. data/lib/css/struct.rb +111 -0
  14. data/lib/encoding_error.rb +6 -0
  15. data/lib/file_validator.rb +38 -0
  16. data/lib/helper/code_type.rb +50 -0
  17. data/lib/helper/color_string.rb +44 -0
  18. data/lib/helper/file_reader.rb +22 -0
  19. data/lib/helper/strenc.rb +65 -0
  20. data/lib/html/parser.rb +212 -0
  21. data/lib/html/query.rb +96 -0
  22. data/lib/html/rule/check_tag_rule.rb +80 -0
  23. data/lib/html/struct.rb +291 -0
  24. data/lib/js/expr/expr.rb +66 -0
  25. data/lib/js/expr/left_hand.rb +63 -0
  26. data/lib/js/expr/operate.rb +92 -0
  27. data/lib/js/expr/primary.rb +166 -0
  28. data/lib/js/parser.rb +116 -0
  29. data/lib/js/rule/all.rb +35 -0
  30. data/lib/js/rule/checklist.rb +41 -0
  31. data/lib/js/rule/file_checker.rb +42 -0
  32. data/lib/js/rule/helper.rb +96 -0
  33. data/lib/js/rule/no_global.rb +87 -0
  34. data/lib/js/stat/if.rb +25 -0
  35. data/lib/js/stat/iter.rb +85 -0
  36. data/lib/js/stat/stat.rb +117 -0
  37. data/lib/js/stat/switch.rb +65 -0
  38. data/lib/js/stat/try.rb +28 -0
  39. data/lib/js/stat/var.rb +40 -0
  40. data/lib/js/struct.rb +248 -0
  41. data/lib/log_entry.rb +49 -0
  42. data/lib/node.rb +28 -0
  43. data/lib/parse_error.rb +13 -0
  44. data/lib/parser_visitable.rb +138 -0
  45. data/lib/position_info.rb +46 -0
  46. data/lib/printer/base_printer.rb +24 -0
  47. data/lib/printer/console_printer.rb +66 -0
  48. data/lib/printer/nocolor_printer.rb +27 -0
  49. data/lib/printer/vim_printer.rb +19 -0
  50. data/lib/rule.rb +241 -0
  51. data/lib/rule_helper.rb +14 -0
  52. data/lib/runner.rb +225 -0
  53. data/rules.d/css.rule +127 -0
  54. data/rules.d/html.dtd.rule +22 -0
  55. data/rules.d/html.prop.rule +51 -0
  56. data/rules.d/html.tag.rule +136 -0
  57. data/rules.d/js.file.rule +13 -0
  58. data/rules.d/js.jquery.rule +56 -0
  59. data/rules.d/js.mergefile.rule +71 -0
  60. data/rules.d/js.rule +84 -0
  61. data/test/all_tests.rb +84 -0
  62. data/test/cli/cli_test.rb +70 -0
  63. data/test/cli/log_level_test.rb +51 -0
  64. data/test/cli/output_format_test.rb +47 -0
  65. data/test/cli/type_test.rb +77 -0
  66. data/test/css/mac_line_end_support_test.rb +38 -0
  67. data/test/css/parser_test.rb +276 -0
  68. data/test/css/rule/check_encoding_test.rb +66 -0
  69. data/test/css/rule/check_list_rule_test.rb +167 -0
  70. data/test/css/rule/compression_test.rb +53 -0
  71. data/test/css/rule/file_name_test.rb +76 -0
  72. data/test/fixtures/css/broken.css +4 -0
  73. data/test/fixtures/css/cbu/36.css +52 -0
  74. data/test/fixtures/css/cbu/china_top.css +324 -0
  75. data/test/fixtures/css/cbu/default-merge.css +3 -0
  76. data/test/fixtures/css/cbu/default.css +13 -0
  77. data/test/fixtures/css/cbu/diy-merge.css +25 -0
  78. data/test/fixtures/css/cbu/fns-v1.css +27 -0
  79. data/test/fixtures/css/cbu/index_v0.1.css +12 -0
  80. data/test/fixtures/css/cbu/merge.css +11 -0
  81. data/test/fixtures/css/cbu/min.css +2 -0
  82. data/test/fixtures/css/cbu/my_home_admin.css +126 -0
  83. data/test/fixtures/css/cbu/nav.css +95 -0
  84. data/test/fixtures/css/cbu/pic_list.css +386 -0
  85. data/test/fixtures/css/cbu/quote-edit.css +18 -0
  86. data/test/fixtures/css/cbu/selloffer.shopwindow.css +1 -0
  87. data/test/fixtures/css/cbu/v1.css +9 -0
  88. data/test/fixtures/css/css3.css +30 -0
  89. data/test/fixtures/css/empty-min.css +0 -0
  90. data/test/fixtures/css/empty.css +0 -0
  91. data/test/fixtures/css/font-family.css +4 -0
  92. data/test/fixtures/css/gb-good.css +14 -0
  93. data/test/fixtures/css/gb_using_star.css +4 -0
  94. data/test/fixtures/css/import.css +18 -0
  95. data/test/fixtures/css/mac-line-sep-err-min.css +1 -0
  96. data/test/fixtures/css/mac-line-sep-err.css +1 -0
  97. data/test/fixtures/css/mac-line-sep-good-min.css +1 -0
  98. data/test/fixtures/css/mac-line-sep-good.css +1 -0
  99. data/test/fixtures/css/multi-encoding-in-a-file.css +0 -0
  100. data/test/fixtures/css/simple.css +1 -0
  101. data/test/fixtures/css/using_expr.css +8 -0
  102. data/test/fixtures/css/using_hack.css +21 -0
  103. data/test/fixtures/css/using_id.css +1 -0
  104. data/test/fixtures/css/using_star.css +4 -0
  105. data/test/fixtures/css/utf8_good.css +6 -0
  106. data/test/fixtures/css/utf8_good_declaring_charset.css +7 -0
  107. data/test/fixtures/css/utf8_using_star.css +5 -0
  108. data/test/fixtures/html/1-1.html +120 -0
  109. data/test/fixtures/html/1-2.html +120 -0
  110. data/test/fixtures/html/cms.html +373 -0
  111. data/test/fixtures/html/css_out_of_head.html +9 -0
  112. data/test/fixtures/html/fdev-template.html +22 -0
  113. data/test/fixtures/html/google.com.html +33 -0
  114. data/test/fixtures/html/mixed_log_levels.html +4 -0
  115. data/test/fixtures/html/mixed_types.html +13 -0
  116. data/test/fixtures/html/no_dtd.html +6 -0
  117. data/test/fixtures/html/readme.html +94 -0
  118. data/test/fixtures/html/review.board.html +163 -0
  119. data/test/fixtures/html/syntax_err.html +3 -0
  120. data/test/fixtures/html/train/detail/345/233/276/346/226/207/347/273/223/345/220/210.html +208 -0
  121. data/test/fixtures/html/train/detail/347/232/204Flash.html +212 -0
  122. data/test/fixtures/html/train/detail/347/232/204Vedio.html +212 -0
  123. data/test/fixtures/html/train/index.html +37 -0
  124. data/test/fixtures/html/train/test.html +1 -0
  125. data/test/fixtures/html/train//344/277/256/346/224/271/344/270/200/347/272/247/345/210/206/347/261/273.html +112 -0
  126. data/test/fixtures/html/train//344/277/256/346/224/271/345/255/220/345/210/206/347/261/273.html +108 -0
  127. data/test/fixtures/html/train//344/277/256/346/224/271/350/257/276/347/250/213.html +195 -0
  128. data/test/fixtures/html/train//345/215/232/345/256/242/350/256/276/347/275/256.html +142 -0
  129. data/test/fixtures/html/train//346/265/217/350/247/210/350/256/260/345/275/225.html +191 -0
  130. data/test/fixtures/html/train//346/267/273/345/212/240/344/270/200/347/272/247/345/210/206/347/261/273.html +113 -0
  131. data/test/fixtures/html/train//346/267/273/345/212/240/345/255/220/345/210/206/347/261/273.html +112 -0
  132. data/test/fixtures/html/train//346/267/273/345/212/240/350/257/276/347/250/213.html +195 -0
  133. data/test/fixtures/html/train//347/231/273/345/275/225.html +20 -0
  134. data/test/fixtures/html/train//347/256/241/347/220/206/345/210/206/347/261/273.html +210 -0
  135. data/test/fixtures/html/train//347/256/241/347/220/206/345/217/215/351/246/210.html +222 -0
  136. data/test/fixtures/html/train//347/256/241/347/220/206/350/257/276/347/250/213.html +284 -0
  137. data/test/fixtures/html/train//347/256/241/347/220/206/350/264/246/346/210/267.html +107 -0
  138. data/test/fixtures/html/train//347/275/221/344/270/212/345/237/271/350/256/255home/351/241/265.html +354 -0
  139. data/test/fixtures/html/train//347/275/221/345/225/206/345/237/271/350/256/255list/351/241/265.html +255 -0
  140. data/test/fixtures/html/train//350/256/276/347/275/256/351/246/226/351/241/265/346/216/250/350/215/220.html +168 -0
  141. data/test/fixtures/html/train//350/257/264/346/230/216.txt +3 -0
  142. data/test/fixtures/html/train//351/246/226/351/241/265/345/271/277/345/221/212/350/256/276/347/275/256.html +297 -0
  143. data/test/fixtures/html/unescaped.html +2 -0
  144. data/test/fixtures/html/view.vm +916 -0
  145. data/test/fixtures/js/jquery-1.7.js +9300 -0
  146. data/test/fixtures/js/scope-test.js +22 -0
  147. data/test/helper.rb +41 -0
  148. data/test/html/mixed_type_test.rb +35 -0
  149. data/test/html/parser/parse_comment_test.rb +47 -0
  150. data/test/html/parser/parse_dtd_test.rb +46 -0
  151. data/test/html/parser/parse_script_tag_test.rb +55 -0
  152. data/test/html/parser/parse_with_auto_close_tag_test.rb +41 -0
  153. data/test/html/parser/parse_with_diff_case_test.rb +38 -0
  154. data/test/html/parser/parse_with_emtpy_test.rb +22 -0
  155. data/test/html/parser/parse_with_multi_children_test.rb +27 -0
  156. data/test/html/parser/parse_with_multi_line_test.rb +41 -0
  157. data/test/html/parser/parse_with_prop_test.rb +88 -0
  158. data/test/html/parser/parse_with_script_tag_test.rb +26 -0
  159. data/test/html/parser/parse_with_selfclosing_test.rb +39 -0
  160. data/test/html/parser/parse_with_simple_tag_test.rb +44 -0
  161. data/test/html/parser/parse_with_simple_tree_test.rb +40 -0
  162. data/test/html/parser/parse_with_style_tag_test.rb +22 -0
  163. data/test/html/parser/parse_with_text_test.rb +45 -0
  164. data/test/html/parser_test.rb +52 -0
  165. data/test/html/query_test.rb +52 -0
  166. data/test/html/rule/check_block_level_element_test.rb +52 -0
  167. data/test/html/rule/check_button_test.rb +45 -0
  168. data/test/html/rule/check_class_count_test.rb +36 -0
  169. data/test/html/rule/check_css_in_head_test.rb +53 -0
  170. data/test/html/rule/check_dtd_test.rb +46 -0
  171. data/test/html/rule/check_form_element_name_test.rb +49 -0
  172. data/test/html/rule/check_head_contain_meta_and_title_test.rb +52 -0
  173. data/test/html/rule/check_html_template_test.rb +103 -0
  174. data/test/html/rule/check_hyperlink_with_target_test.rb +40 -0
  175. data/test/html/rule/check_hyperlink_with_title_test.rb +43 -0
  176. data/test/html/rule/check_id_n_class_downcase_test.rb +40 -0
  177. data/test/html/rule/check_img_with_alt_prop_test.rb +33 -0
  178. data/test/html/rule/check_no_import_css_test.rb +36 -0
  179. data/test/html/rule/check_prop_have_value_test.rb +32 -0
  180. data/test/html/rule/check_prop_seperator_test.rb +32 -0
  181. data/test/html/rule/check_style_prop_test.rb +30 -0
  182. data/test/html/rule/check_tag_closed_test.rb +59 -0
  183. data/test/html/rule/check_tag_downcase_test.rb +51 -0
  184. data/test/html/rule/check_unescape_char_test.rb +35 -0
  185. data/test/html/rule/check_unique_import_test.rb +56 -0
  186. data/test/html/rule_test.rb +62 -0
  187. data/test/js/expr/expr.rb +57 -0
  188. data/test/js/expr/left_hand.rb +25 -0
  189. data/test/js/expr/operate.rb +145 -0
  190. data/test/js/expr/primary.rb +89 -0
  191. data/test/js/parser_test.rb +98 -0
  192. data/test/js/rule/alert_check_test.rb +37 -0
  193. data/test/js/rule/all_test.rb +23 -0
  194. data/test/js/rule/base_test.rb +34 -0
  195. data/test/js/rule/file_checker_test.rb +131 -0
  196. data/test/js/rule/jq_check_test.rb +90 -0
  197. data/test/js/rule/nest_try_catch_test.rb +71 -0
  198. data/test/js/rule/new_object_and_new_array_test.rb +38 -0
  199. data/test/js/rule/no_eval_test.rb +34 -0
  200. data/test/js/rule/no_global_test.rb +88 -0
  201. data/test/js/rule/private_method_check_test.rb +58 -0
  202. data/test/js/rule/semicolon_test.rb +63 -0
  203. data/test/js/rule/stat_if_with_brace_test.rb +68 -0
  204. data/test/js/rule/stat_if_with_muti_else_test.rb +68 -0
  205. data/test/js/rule/use_strict_equal_test.rb +44 -0
  206. data/test/js/rule_test.rb +47 -0
  207. data/test/js/stat/if.rb +26 -0
  208. data/test/js/stat/iter.rb +115 -0
  209. data/test/js/stat/stat.rb +91 -0
  210. data/test/js/stat/switch.rb +37 -0
  211. data/test/js/stat/try.rb +32 -0
  212. data/test/js/stat/var.rb +38 -0
  213. data/test/parser_visitable_test.rb +102 -0
  214. data/test/position_info_test.rb +66 -0
  215. data/test/rule_dsl/dsl_basic_test.rb +91 -0
  216. data/test/rule_dsl/importing_test.rb +48 -0
  217. data/test/runner/log_level_test.rb +58 -0
  218. metadata +317 -0
@@ -0,0 +1,115 @@
1
+ module XRayTest
2
+ module JS
3
+ module Stat
4
+
5
+ module Iter
6
+
7
+ def test_parse_stat_dowhile
8
+ js = '
9
+ do {
10
+ i++;
11
+ } while (x === 0 || x >= 1)
12
+ '
13
+
14
+ stat = parse_js :parse_stat_dowhile, js
15
+ assert_equal 'dowhile', stat.type
16
+
17
+ assert_equal '(block,[(expression,(++,i,),)],)', stat.body.text
18
+ assert_equal '(||,(===,x,0),(>=,x,1))', stat.condition.text
19
+ end
20
+
21
+ def test_parse_stat_while
22
+ js = '
23
+ while (i++ < 100) {
24
+ alert("hello");
25
+ i++;
26
+ }
27
+ '
28
+
29
+ stat = parse_js :parse_stat_while, js
30
+
31
+ assert_equal 'while', stat.type
32
+ assert_equal '(<,(++,i,),100)', stat.condition.text
33
+ assert_equal '(block,[(expression,((,alert,["hello"]),),(expression,(++,i,),)],)',
34
+ stat.body.text
35
+ end
36
+
37
+ def test_parse_stat_for
38
+ test_parse_stat_fordefault
39
+ test_parse_stat_forvar
40
+ test_parse_stat_forvarin
41
+ test_parse_stat_forin
42
+ end
43
+
44
+ private
45
+
46
+ def test_parse_stat_fordefault
47
+ js = '
48
+ for (i = 0; i < 10; i++) {
49
+ hello(i);
50
+ }
51
+ '
52
+
53
+ stat = parse_js :parse_stat_for, js
54
+ assert_equal 'for', stat.type
55
+
56
+ con = stat.condition
57
+ assert_equal 'fordefault', con.type
58
+ assert_equal '(=,i,0)', con.first.text
59
+ assert_equal '(<,i,10)', con.second.text
60
+ assert_equal '(++,i,)', con.third.text
61
+ end
62
+
63
+ def test_parse_stat_forvar
64
+ js = '
65
+ for (var i = 1; i < 10; i++) {
66
+ hello(i)
67
+ }
68
+ '
69
+
70
+ stat = parse_js :parse_stat_for, js
71
+ con = stat.condition
72
+
73
+ assert_equal 'forvar', con.type
74
+ assert_equal '[(var=,i,1)]', con.first.text
75
+ assert_equal '(<,i,10)', con.second.text
76
+ assert_equal '(++,i,)', con.third.text
77
+ end
78
+
79
+ def test_parse_stat_forvarin
80
+ js = '
81
+ for (var k in o) {
82
+ alert(o[k]);
83
+ }
84
+ '
85
+
86
+ stat = parse_js :parse_stat_for, js
87
+ con = stat.condition
88
+
89
+ assert_equal 'forvarin', con.type
90
+ assert_equal '[(var=,k,)]', con.first.text
91
+ assert_equal 'o', con.second.text
92
+ assert_equal nil, con.third
93
+ end
94
+
95
+ def test_parse_stat_forin
96
+ js = '
97
+ for (k in o) {
98
+ alert(o[k]);
99
+ }
100
+ '
101
+
102
+ stat = parse_js :parse_stat_for, js
103
+ con = stat.condition
104
+
105
+ assert_equal 'forin', con.type
106
+ assert_equal 'k', con.first.text
107
+ assert_equal 'o', con.second.text
108
+ assert_equal nil, con.third
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,91 @@
1
+ require_relative 'var'
2
+ require_relative 'if'
3
+ require_relative 'switch'
4
+ require_relative 'iter'
5
+ require_relative 'try'
6
+
7
+ module XRayTest
8
+ module JS
9
+ module Stat
10
+
11
+ module Stat
12
+
13
+ include Var, If, Switch, Iter, Try
14
+
15
+ def test_parse_statement
16
+ js = ''
17
+ stat = parse_js :parse_statement, js
18
+ end
19
+
20
+ def test_parse_stat_block
21
+ js = '
22
+ {
23
+ a = 1;
24
+ b = 2;
25
+ c++;
26
+ i = i / 1;
27
+ ;
28
+ i++;
29
+ }
30
+ '
31
+
32
+ stat = parse_js :parse_stat_block, js
33
+ assert_equal 6, stat.elements.length
34
+
35
+ assert_equal 'empty', stat.elements[4].type
36
+ end
37
+
38
+ def test_parse_stat_continue
39
+ js = 'continue ; '
40
+ stat = parse_js :parse_stat_continue, js
41
+
42
+ assert_equal 'continue', stat.type
43
+ assert_equal true, stat.end_with_semicolon?
44
+ assert_equal nil, stat.left
45
+
46
+ js = "continue\n"
47
+ stat = parse_js :parse_stat_continue, js
48
+ assert_equal false, stat.end_with_semicolon?
49
+ assert_equal nil, stat.left
50
+
51
+ js = 'continue abcde;'
52
+ stat = parse_js :parse_stat_continue, js
53
+ assert_equal true, stat.end_with_semicolon?
54
+ assert_equal 'abcde', stat.left.text
55
+ end
56
+
57
+ def test_parse_break
58
+ js = 'break'
59
+ stat = parse_js :parse_stat_break, js
60
+ assert_equal 'break', stat.type
61
+ end
62
+
63
+ def test_parse_stat_return
64
+ js = 'return a + 1;'
65
+ stat = parse_js :parse_stat_return, js
66
+
67
+ assert_equal 'return', stat.type
68
+ assert_equal '(+,a,1)', stat.left.text
69
+
70
+ js = 'return;'
71
+ stat = parse_js :parse_stat_return, js
72
+ assert_equal nil, stat.left
73
+
74
+ js = "return"
75
+ stat = parse_js :parse_stat_return, js
76
+ assert_equal false, stat.end_with_semicolon?
77
+ end
78
+
79
+ def test_parse_throw
80
+ js = 'throw "assert false";'
81
+ stat = parse_js :parse_stat_throw, js
82
+
83
+ assert_equal 'throw', stat.type
84
+ assert_equal '"assert false"', stat.left.text
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,37 @@
1
+ module XRayTest
2
+ module JS
3
+ module Stat
4
+
5
+ module Switch
6
+ def test_parse_stat_switch
7
+ js = '
8
+ switch (a) {
9
+ case 1:
10
+ i++;
11
+ break;
12
+ case 2:
13
+ i--;
14
+ break;
15
+ default:
16
+ break;
17
+ case 3:
18
+ a++;
19
+ break;
20
+ }
21
+ '
22
+
23
+ stat = parse_js :parse_stat_switch, js
24
+
25
+ assert_equal 'switch', stat.type
26
+ assert_equal 'a', stat.expression.text
27
+
28
+ block = stat.case_block
29
+ assert_equal '[(caseclause,1,[(expression,(++,i,),),(break,,)]),(caseclause,2,[(expression,(--,i,),),(break,,)])]', block.case_clauses.text
30
+ assert_equal '(defaultclause,[(break,,)],)', block.default_clause.text
31
+ assert_equal '[(caseclause,3,[(expression,(++,a,),),(break,,)])]', block.bottom_case_clauses.text
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ module XRayTest
2
+ module JS
3
+ module Stat
4
+
5
+ module Try
6
+ def test_stat_try
7
+ js = '
8
+ try {
9
+ a = 1 + 2;
10
+ } catch (e) {
11
+ console.debug("something error");
12
+ } finally {
13
+ hello();
14
+ }
15
+ '
16
+
17
+ stat = parse_js :parse_stat_try, js
18
+
19
+ assert_equal 'try', stat.type
20
+ assert_equal '(block,[(expression,(=,a,(+,1,2)),)],)', stat.try_part.text
21
+
22
+ assert_equal 'catch', stat.catch_part.type
23
+ assert_equal 'e', stat.catch_part.left.text
24
+ assert_equal '(block,[(expression,((,(.,console,debug),["something error"]),)],)', stat.catch_part.right.text
25
+
26
+ assert_equal '(block,[(expression,((,hello,[]),)],)', stat.finally_part.text
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ module XRayTest
2
+ module JS
3
+ module Stat
4
+
5
+ module Var
6
+ def test_parse_stat_var
7
+ js = 'var a = 1, b, c = 1 + a;'
8
+ expr = parse_js :parse_stat_var, js
9
+
10
+ assert_equal 'var', expr.type
11
+
12
+ decls = expr.declarations
13
+
14
+ assert_equal 3, decls.length
15
+ assert_equal '[(var=,a,1),(var=,b,),(var=,c,(+,1,a))]', decls.text
16
+ assert_equal true, expr.end_with_semicolon?
17
+
18
+ js = "var a = c++, b = c, c = 123\n a = 1 + 1"
19
+ expr = parse_js :parse_stat_var, js
20
+ assert_equal false, expr.end_with_semicolon?
21
+
22
+ js = 'var a = c, b = a--'
23
+ expr = parse_js :parse_stat_var, js
24
+ assert_equal false, expr.end_with_semicolon?
25
+
26
+ js = <<EOF
27
+ var rBackslash = /\\\\/g,
28
+ rReturn = /\\r\\n/g,
29
+ rNonWord = /\\W/;
30
+ EOF
31
+ expr = parse_js :parse_stat_var, js
32
+ assert_equal '(var,[(var=,rBackslash,/\\\\/g),(var=,rReturn,/\\r\\n/g),(var=,rNonWord,/\\W/)],)', expr.text
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,102 @@
1
+ require_relative 'helper'
2
+
3
+ require 'node'
4
+ require 'parser_visitable'
5
+
6
+
7
+ module XRayTest
8
+
9
+ class ParserVisitableTest < Test::Unit::TestCase
10
+
11
+ Node = XRay::Node
12
+ ParserVisitable = XRay::ParserVisitable
13
+
14
+ class MockParser
15
+
16
+ include ParserVisitable
17
+
18
+ def parse_node
19
+ puts 'parse node'
20
+
21
+ parse_node_a
22
+ parse_node_b
23
+
24
+ Node.new 'node'
25
+ end
26
+
27
+ def parse_node_a
28
+ puts 'parse node a'
29
+ Node.new 'node a'
30
+ end
31
+
32
+ def parse_node_b
33
+ puts 'parse node b'
34
+
35
+ parse_node_c
36
+
37
+ Node.new 'node b'
38
+ end
39
+
40
+ def parse_node_c
41
+ puts 'parse node c'
42
+ Node.new 'node c'
43
+ end
44
+
45
+ end
46
+
47
+ class SimpleVisitor
48
+
49
+ def visit_node(node)
50
+ puts "visit node: #{node}"
51
+ ['visit node', :info]
52
+ end
53
+
54
+ def visit_node_a(node)
55
+ puts "visit node a: #{node}"
56
+ ['visit node a', :warn]
57
+ end
58
+
59
+ def visit_node_b(node)
60
+ puts "visit node b: #{node}"
61
+ [
62
+ ['visit node b 1', :warn],
63
+ ['visit node b 2', :info]
64
+ ]
65
+ end
66
+
67
+ def visit_node_c(node)
68
+ puts "visit node c: #{node}"
69
+ ['visit node c', :warn]
70
+ end
71
+ end
72
+
73
+ class SimpleObserver
74
+ attr_reader :times
75
+
76
+ def update(result, parser)
77
+ puts "observer: #{result}"
78
+ @times ||= 0
79
+ @times += 1
80
+ end
81
+ end
82
+
83
+
84
+ def test_default
85
+ parser = MockParser.new
86
+
87
+ visitor = SimpleVisitor.new
88
+ parser.add_visitor SimpleVisitor.new
89
+
90
+ observer = SimpleObserver.new
91
+ parser.add_observer observer
92
+
93
+ parser.parse_node
94
+
95
+ results = parser.results
96
+ assert_equal 5, results.length
97
+ assert_equal 5, observer.times
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'helper'
4
+
5
+ require 'position_info'
6
+
7
+ module XRayTest
8
+
9
+ class PositionInfoTest < Test::Unit::TestCase
10
+
11
+ PositionInfo = XRay::PositionInfo
12
+
13
+ Position = XRay::Position
14
+
15
+ @@test_paragraph = %(this is line one
16
+ this is line two this is line two
17
+ this is line three
18
+ this is ine 4
19
+ 中文字行数
20
+ 打算怎么办
21
+ )
22
+
23
+ def test_position_of_first_line
24
+ text, info, lines = prepair @@test_paragraph
25
+ len = lines[0].length
26
+ assert_equal Position.new(0, 1, 1), info.locate(0), '第一行第一个字符'
27
+ assert_equal Position.new(6, 1, 7), info.locate(6), '第一行第7个字符'
28
+ assert_equal Position.new(len - 1, 1, len), info.locate(len - 1), '第一行最后一个字符'
29
+ end
30
+
31
+ def test_position_of_middle_line
32
+ text, info, lines = prepair @@test_paragraph
33
+ len = lines[0].length
34
+ assert_equal Position.new(len, 2, 1), info.locate(len), '第2行的第一个字符'
35
+ assert_equal Position.new(len + 1, 2, 2), info.locate(len + 1), '第2行的第2个字符'
36
+ assert_equal Position.new(len + 5, 2, 6), info.locate(len + 5), '第2行的第6个字符'
37
+ end
38
+
39
+ def test_position_of_4th_line
40
+ text, info, lines = prepair @@test_paragraph
41
+ len = 0
42
+ (0..2).each {|n| len += lines[n].length }
43
+ assert_equal Position.new(len, 4, 1), info.locate(len), '第4行的第一个字符'
44
+ assert_equal Position.new(len + 1, 4, 2), info.locate(len + 1), '第4行的第2个字符'
45
+ assert_equal Position.new(len + 5, 4, 6), info.locate(len + 5), '第4行的第6个字符'
46
+ end
47
+
48
+ def test_position_all_line
49
+ text, info, lines = prepair @@test_paragraph
50
+ pos = 0
51
+ lines.each_with_index do |line, row|
52
+ 0.upto(line.length - 1) do |col|
53
+ assert_equal Position.new(pos, row + 1, col + 1), info.locate(pos)
54
+ pos += 1
55
+ end
56
+ end
57
+ end
58
+
59
+ def prepair(text)
60
+ info = PositionInfo.new text
61
+ lines = text.split("\n").map { |l| "#{l}\n" }
62
+ [text, info, lines]
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../helper'
4
+
5
+ module XRayTest
6
+
7
+ module Rule
8
+
9
+ class DSLBasicTest < Test::Unit::TestCase
10
+
11
+ include ::XRay::Rule
12
+
13
+ def setup
14
+ @rule = Proc.new do |file|
15
+ @called = true
16
+ ["it's just a test", :warn]
17
+ end
18
+ @called = false
19
+ clear_all_rules
20
+ common
21
+ end
22
+
23
+ def test_add_file_checker
24
+ check_file &@rule
25
+ assert_contains file_rules, "available in common rules"
26
+ assert_contains css_rules, "common rules available in css rules too"
27
+ assert_contains css_file_rules, "common rules available in css file rules too"
28
+ assert_not_contains only_css_file_rules, "not available in css rules only"
29
+ end
30
+
31
+ def test_add_css_file_checker
32
+ css {
33
+ check_file &@rule
34
+ }
35
+ assert_not_contains file_rules, "not available in common rules"
36
+ assert_contains css_file_rules, "available in css rules"
37
+ assert_contains only_css_file_rules, "available in css only rules"
38
+ end
39
+
40
+ def test_check
41
+ check_file &@rule
42
+ assert_equal check_file("test"), [["it's just a test", :warn]]
43
+ assert @called
44
+ end
45
+
46
+ def test_named_checker
47
+ msg = ['we suggest use "-" instead', :info]
48
+ html {
49
+ check_file_with_underline do |file|
50
+ msg if file =~ /_/
51
+ end
52
+ }
53
+
54
+ results = check_file_with_underline 'a_b.html'
55
+ assert_equal msg, results
56
+ end
57
+
58
+ def test_normal_plus_named_checker
59
+ case_warn = ['must use downcase filename', :error]
60
+ check_file { |file|
61
+ case_warn if file =~ /[A-Z]/
62
+ }
63
+
64
+ msg = ['we suggest use "-" instead', :info]
65
+ html {
66
+ check_file_with_underline do |file|
67
+ msg if file =~ /_/
68
+ end
69
+ }
70
+
71
+ results = check_html_file 'a_B.html'
72
+ assert_equal [case_warn, msg], results
73
+
74
+ end
75
+
76
+ def assert_contains( rules, *msg )
77
+ assert has_rule?(rules), *msg
78
+ end
79
+
80
+ def assert_not_contains( rules, *msg)
81
+ assert !has_rule?(rules), *msg
82
+ end
83
+
84
+ def has_rule?( rules )
85
+ rules.any? {|r| r[:block] == @rule }
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../helper'
4
+
5
+ module XRayTest
6
+
7
+ module Rule
8
+
9
+ class ImportingTest < Test::Unit::TestCase
10
+
11
+ include ::XRay::Rule
12
+
13
+ def setup
14
+ clear_all_rules
15
+ end
16
+
17
+ def test_import_with_symbol
18
+ import :css
19
+ assert css_rules.size > 0
20
+ end
21
+
22
+ def test_import_with_symbol_twice
23
+ assert_equal 0, css_rules.size
24
+
25
+ import :css
26
+ size = css_rules.size
27
+ assert size > 0
28
+
29
+ import :css
30
+ assert_equal size, css_rules.size
31
+ end
32
+
33
+ def test_import_all_twice
34
+ assert_equal 0, css_rules.size
35
+
36
+ import_all
37
+ size = css_rules.size
38
+ assert size > 0
39
+
40
+ import_all
41
+ assert_equal size, css_rules.size
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+ require 'test/unit'
3
+ require_relative '../helper'
4
+
5
+ module XRayTest
6
+
7
+ module Runner
8
+
9
+ class LogLevelTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @runner = XRay::Runner.new
13
+ @html = fixture('html/mixed_log_levels.html')
14
+ end
15
+
16
+ def test_output_every_thing_by_default
17
+ res = @runner.check_html @html
18
+ assert_contain( res, :fatal, :error, :warn )
19
+ end
20
+
21
+ def test_output_fatals_only
22
+ @runner.log_level = :fatal
23
+ res = @runner.check_html( @html )
24
+ assert_contain( res, :fatal )
25
+ assert_not_contain( res, :error, :warn )
26
+ end
27
+
28
+ def test_output_fatals_and_errors
29
+ @runner.log_level = :error
30
+ res = @runner.check_html( @html )
31
+ assert_contain( res, :fatal, :error )
32
+ assert_not_contain( res, :warn )
33
+ end
34
+
35
+ def test_output_fatals_errors_and_warnnings
36
+ @runner.log_level = :warn
37
+ res = @runner.check_html( @html )
38
+ assert_contain( res, :fatal, :error, :warn )
39
+ end
40
+
41
+ private
42
+ def assert_contain( results, *types )
43
+ types.each do |type|
44
+ assert( results.any? { |r| r.level == type } )
45
+ end
46
+ end
47
+
48
+ def assert_not_contain( results, *types )
49
+ types.each do |type|
50
+ assert( ! (results.any? { |r| r.level == type } ) )
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ end