PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/web_js/js_of_ocaml/tests/tests.ml

http://github.com/aryx/fork-ocsigen
OCaml | 245 lines | 183 code | 38 blank | 24 comment | 10 complexity | d8372ef31f6d7e5e3db3e49a15e387f8 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1, MIT, WTFPL
  1. (* Js_of_ocaml example
  2. * http://www.ocsigen.org/js_of_ocaml/
  3. * Copyright (C) 2010 Jérôme Vouillon
  4. * Laboratoire PPS - CNRS Université Paris Diderot
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *)
  20. let success_count = ref 0
  21. let test_count = ref 0
  22. let log_success () = incr success_count; incr test_count
  23. let log_failure s =
  24. incr test_count;
  25. Firebug.console##log_2 (Js.string "\tFAILURE: ", Js.string s)
  26. let log_start s =
  27. Firebug.console##log_2 (Js.string "START: ", Js.string s)
  28. let log_stop s =
  29. Firebug.console##log_2 (Js.string "STOP: ", Js.string s)
  30. let raw_log x =
  31. Firebug.console##log_2 (Js.string "\t\t", x)
  32. let log s = raw_log (Js.string s)
  33. (* TEST Url *)
  34. let () = log_start "Url test suite"
  35. let url_string_url u = Url.url_of_string (Url.string_of_url u)
  36. let () = match Url.Current.get () with
  37. | None -> log_failure "can't parse current url"
  38. | Some u -> match url_string_url u with
  39. | None -> log_failure "can't parse pretty-printed url"
  40. | Some v ->
  41. if u = v then
  42. log_success ()
  43. else
  44. log_failure "no fixpoint"
  45. let () =
  46. let t1 = Url.urlencode "/toto+ blah&tutu" in
  47. let t2 = Url.urlencode ~with_plus:false "/toto+ blah&tutu" in
  48. if t1 = "/toto%2B%20blah%26tutu" && t2 = "/toto+%20blah%26tutu" then
  49. log_success ()
  50. else
  51. log_failure "escaping error"
  52. let () = log_stop "Url test suite"
  53. (* Tests Regexp *)
  54. let () = log_start "Regexp test suite"
  55. let () =
  56. let re1 = Regexp.regexp "ab?" in
  57. let re2 = Regexp.regexp "\\." in
  58. let re3 = Regexp.regexp_string "(.)\\(.)" in
  59. let s1 = "totobtutua" in
  60. let s2 = "rr.ee.ab.a.b.bb.a.ee." in
  61. begin match Regexp.string_match re1 s1 0 with
  62. | None -> log_failure "Can't match 1 1"
  63. | Some r ->
  64. let x = Regexp.matched_string r in
  65. if x = "a" then
  66. log_success ()
  67. else
  68. log_failure ("Wrong match 1 1: " ^ x)
  69. end;
  70. begin match Regexp.string_match re1 s2 0 with
  71. | None -> log_failure "Can't match 1 2"
  72. | Some r ->
  73. let x = Regexp.matched_string r in
  74. if x = "ab" then
  75. log_success ()
  76. else
  77. log_failure ("Wrong match 1 2: " ^ x)
  78. end;
  79. begin
  80. let l = Regexp.split re2 s2 in
  81. if l = ["rr";"ee";"ab";"a";"b";"bb";"a";"ee";""] then
  82. log_success ()
  83. else
  84. log_failure "Wrong split 2 2"
  85. end ;
  86. begin
  87. let x = Regexp.global_replace re2 s2 "" in
  88. if x = "rreeababbbaee" then
  89. log_success ()
  90. else
  91. log_failure ("Wrong replacement 2 2: " ^ x)
  92. end ;
  93. begin match Regexp.string_match re3 "(.)\\(.)" 0 with
  94. | None -> log_failure "Quote 3 3"
  95. | Some x -> log_success ()
  96. end
  97. let () = log_stop "Regexp test suite"
  98. (* Tests Colors *)
  99. let () = log_start "CSS.Colors test suite"
  100. let () =
  101. let cols = [
  102. CSS.Color.RGB(120, 3, 56);
  103. CSS.Color.RGBA(120, 3, 56,1.);
  104. CSS.Color.RGB_percent(10, 3,60);
  105. CSS.Color.RGBA_percent(100,53,60,0.45);
  106. CSS.Color.HSL(120,75,56);
  107. CSS.Color.HSLA(180, 3,56,0.2);
  108. CSS.Color.RGB (CSS.Color.rgb_of_name CSS.Color.Dodgerblue);
  109. CSS.Color.RGB (CSS.Color.rgb_of_name CSS.Color.Pink);
  110. CSS.Color.Name CSS.Color.Hotpink;
  111. CSS.Color.Name CSS.Color.Cornsilk;
  112. ]
  113. in
  114. List.iter
  115. (fun c ->
  116. try
  117. let js = CSS.Color.js c in
  118. let ml = CSS.Color.ml js in
  119. if c = ml then
  120. log_success ()
  121. else
  122. log_failure (Printf.sprintf "%s %s"
  123. (CSS.Color.string_of_t c)
  124. (CSS.Color.string_of_t ml)
  125. )
  126. with
  127. | Invalid_argument s -> log_failure s
  128. | Failure s -> log_failure s
  129. )
  130. cols
  131. let () = log_stop "CSS.Colors test suite"
  132. (* CSS.Length testing *)
  133. let () = log_start "CSS.Length test suite"
  134. let () =
  135. let ls =
  136. [ CSS.Length.Em 0.1
  137. ; CSS.Length.Ex 0.12
  138. ; CSS.Length.Px 5.4
  139. ; CSS.Length.Gd 0.
  140. ; CSS.Length.Rem 10.0
  141. ; CSS.Length.Vw 0.10
  142. ; CSS.Length.Vh 0.1
  143. ; CSS.Length.Vm 0.5
  144. ; CSS.Length.Ch 20.6
  145. ; CSS.Length.Zero
  146. ; CSS.Length.Mm 100.
  147. ; CSS.Length.Cm 10.1
  148. ; CSS.Length.In 0.1
  149. ; CSS.Length.Pt 10.2
  150. ; CSS.Length.Pc 103.1
  151. ]
  152. in
  153. List.iter
  154. (fun c ->
  155. try
  156. let js = CSS.Length.js c in
  157. let ml = CSS.Length.ml js in
  158. if c = ml then
  159. log_success ()
  160. else
  161. log_failure (Printf.sprintf "%s %s"
  162. (CSS.Length.string_of_t c)
  163. (CSS.Length.string_of_t ml)
  164. )
  165. with
  166. | Invalid_argument s -> log_failure s
  167. | Failure s -> log_failure s
  168. )
  169. ls
  170. let () = log_stop "CSS.Length test suite"
  171. (*CSS.Angle test suite*)
  172. let () = log_start "CSS.Angle test suite"
  173. let () =
  174. let a =
  175. [ CSS.Angle.Rad 0.1
  176. ; CSS.Angle.Turns 0.12
  177. ; CSS.Angle.Deg 5.4
  178. ; CSS.Angle.Turns 0.
  179. ; CSS.Angle.Grad 10.0
  180. ; CSS.Angle.Grad 0.10
  181. ]
  182. in
  183. List.iter
  184. (fun c ->
  185. try
  186. let js = CSS.Angle.js c in
  187. let ml = CSS.Angle.ml js in
  188. if c = ml then
  189. log_success ()
  190. else
  191. log_failure (Printf.sprintf "%s %s"
  192. (CSS.Angle.string_of_t c)
  193. (CSS.Angle.string_of_t ml)
  194. )
  195. with
  196. | Invalid_argument s -> log_failure s
  197. | Failure s -> log_failure s
  198. )
  199. a
  200. let () = log_stop "CSS.Angle test suite"
  201. let () =
  202. Firebug.console##log(
  203. Js.string (
  204. Printf.sprintf "Test results: %d sucesses out of %d tests"
  205. !success_count !test_count
  206. )
  207. )