Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo-repository/exercises/demo2/meta.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"learnocaml_version":"1","kind":"exercise","stars":0,
{"learnocaml_version":"1","kind":"exercise","stars":1,
"title":"Demo of the exercise environment (MD version)"}
14 changes: 12 additions & 2 deletions src/app/dune
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@
learnocaml_data
learnocaml_api
sha
re.pcre
ocplib_i18n)
)

(library
(name learnocaml_app_common_test)
(wrapped false)
(modules Learnocaml_app_common_test)
(libraries learnocaml_app_common)
(inline_tests)
(preprocess (pps ppx_expect ppx_inline_test))
)

(executable
(name learnocaml_index_main)
(modes byte js)
(flags :standard -warn-error -6-9-27-33-39)
(libraries ezjsonm
ace
sha
re.pcre
learnocaml_repository
learnocaml_app_common
learnocaml_toplevel
Expand Down Expand Up @@ -141,5 +152,4 @@
(learnocaml_description_main.bc.js as www/js/learnocaml-description.js)
(learnocaml_partition_view.bc.js as www/js/learnocaml-partition-view.js)
(learnocaml_playground_main.bc.js as www/js/learnocaml-playground.js))
)

)
112 changes: 112 additions & 0 deletions src/app/learnocaml_app_common_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
open Learnocaml_common


let to_test_encode s = Printf.printf "%s " (encode s)
let to_test_decode s = Printf.printf "%s " (decode s)
let to_test_bijection1 s = Printf.printf "%s " (decode (encode s))
let to_test_bijection2 s = Printf.printf "%s " (encode (decode s))

let original_e = ["=test";"test=test";"test=";"==";""]
let coded_e = ["-etest";"test-etest";"test-e";"-e-e";""]

let original_c = [",test";"test,test";"test,";",,";""]
let coded_c = ["-ctest";"test-ctest";"test-c";"-c-c";""]

let original_a = ["&test";"test&test";"test&";"&&";""]
let coded_a = ["-atest";"test-atest";"test-a";"-a-a";""]

let original__ = ["-test";"test-test";"test-";"--";""]
let coded__ = ["--test";"test--test";"test--";"----";""]

let original_all = ["=,&-";"=,-&";"=&,-";"=&-,";"=-,&";"=-&,";
",=&-";",=-&";",&=-";",&-=";",-=&";",-&=";
"&=,-";"&=-,";"&,=-";"&,-=";"&-=,";"&-,=";
"-=,&";"-=&,";"-,=&";"-,&=";"-&=,";"-&,="]
let codedall = ["-e-c-a--";"-e-c---a";"-e-a-c--";"-e-a---c";"-e---c-a";"-e---a-c";
"-c-e-a--";"-c-e---a";"-c-a-e--";"-c-a---e";"-c---e-a";"-c---a-e";
"-a-e-c--";"-a-e---c";"-a-c-e--";"-a-c---e";"-a---e-c";"-a---c-e";
"---e-c-a";"---e-a-c";"---c-a-e";"---c-a-e";"---a-e-c";"---a-c-e"]


let%expect_test "encode -e" =
List.iter to_test_encode original_e;
[%expect {|-etest test-etest test-e -e-e |}]

let%expect_test "encode -c" =
List.iter to_test_encode original_c;
[%expect {|-ctest test-ctest test-c -c-c |}]

let%expect_test "encode -a" =
List.iter to_test_encode original_a;
[%expect {|-atest test-atest test-a -a-a |}]

let%expect_test "encode --" =
List.iter to_test_encode original__;
[%expect {|--test test--test test-- ---- |}]

let%expect_test "encode all" =
List.iter to_test_encode original_all;
[%expect {|-e-c-a-- -e-c---a -e-a-c-- -e-a---c -e---c-a -e---a-c -c-e-a-- -c-e---a -c-a-e-- -c-a---e -c---e-a -c---a-e -a-e-c-- -a-e---c -a-c-e-- -a-c---e -a---e-c -a---c-e ---e-c-a ---e-a-c ---c-a-e ---c-a-e ---a-e-c ---a-c-e |}]


let%expect_test "decode -e" =
List.iter to_test_decode coded_e;
[%expect {|=test test=test test= == |}]

let%expect_test "decode -c" =
List.iter to_test_decode coded_c;
[%expect {|,test test,test test, ,, |}]

let%expect_test "decode -a" =
List.iter to_test_decode coded_a;
[%expect {|&test test&test test& && |}]

let%expect_test "decode --" =
List.iter to_test_decode coded_e;
[%expect {|-test test-test test- -- |}]

let%expect_test "decode all" =
List.iter to_test_decode codedall;
[%expect {|=,&- =,-& =&,- =&-, =-,& =-&, ,=&- ,=-& ,&=- ,&-= ,-=& ,-&= &=,- &=-, &,=- &,-= &-=, &-,= -=,& -=&, -,=& -,&= -&=, -&,= |}]


let%expect_test "bijection 1 -e" =
List.iter to_test_bijection1 original_e;
[%expect {|=test test=test test= == |}]

let%expect_test "bijection 1 -c" =
List.iter to_test_bijection1 original_c;
[%expect {|,test test,test test, ,, |}]

let%expect_test "bijection 1 -a" =
List.iter to_test_bijection1 original_a;
[%expect {|&test test&test test& && |}]

let%expect_test "bijection 1 --" =
List.iter to_test_bijection1 original__;
[%expect {|-test test-test test- -- |}]

let%expect_test "bijection 1 all" =
List.iter to_test_bijection1 original_all;
[%expect {|=,&- =,-& =&,- =&-, =-,& =-&, ,=&- ,=-& ,&=- ,&-= ,-=& ,-&= &=,- &=-, &,=- &,-= &-=, &-,= -=,& -=&, -,=& -,&= -&=, -&,= |}]


let%expect_test "bijection 2 -e" =
List.iter to_test_bijection2 coded_e;
[%expect {|-etest test-etest test-e -e-e |}]

let%expect_test "bijection 2 -c" =
List.iter to_test_bijection2 coded_c;
[%expect {|-ctest test-ctest test-c -c-c |}]

let%expect_test "bijection 2 -a" =
List.iter to_test_bijection2 coded_a;
[%expect {|-atest test-atest test-a -a-a |}]

let%expect_test "bijection 2 --" =
List.iter to_test_bijection2 coded__;
[%expect {|--test test--test test-- ---- |}]

let%expect_test "bijection 2 all" =
List.iter to_test_bijection2 codedall;
[%expect {|-e-c-a-- -e-c---a -e-a-c-- -e-a---c -e---c-a -e---a-c -c-e-a-- -c-e---a -c-a-e-- -c-a---e -c---e-a -c---a-e -a-e-c-- -a-e---c -a-c-e-- -a-c---e -a---e-c -a---c-e ---e-c-a ---e-a-c ---c-a-e ---c-a-e ---a-e-c ---a-c-e |}]
12 changes: 12 additions & 0 deletions src/app/learnocaml_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ open Js_utils
open Lwt.Infix
open Learnocaml_data
open Learnocaml_config
open Re.Pcre

module H = Tyxml_js.Html

let encode str =
Re.Pcre.substitute ~rex:(Re.Pcre.regexp ",") ~subst:(fun _ -> "-c") (
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "&") ~subst:(fun _ -> "-a") (
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "=") ~subst:(fun _ -> "-e") (
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-") ~subst:(fun _ -> "--") str)))
let decode str =
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "--") ~subst:(fun _ -> "-") (
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-e") ~subst:(fun _ -> "=") (
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-a") ~subst:(fun _ -> "&") (
Re.Pcre.substitute ~rex:(Re.Pcre.regexp "-c") ~subst:(fun _ -> ",") str)))

let find_div_or_append_to_body id =
match Manip.by_id id with
| Some div -> div
Expand Down
4 changes: 4 additions & 0 deletions src/app/learnocaml_common.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ open Js_of_ocaml
open Js_of_ocaml_tyxml
open Learnocaml_data

val encode : string -> string

val decode : string -> string

val find_div_or_append_to_body : string -> [> Html_types.div ] Tyxml_js.Html.elt

val find_component : string -> 'a Tyxml_js.Html.elt
Expand Down
Loading
Loading