Skip to content

Commit

Permalink
Migrate added test cases of String
Browse files Browse the repository at this point in the history
  • Loading branch information
zeptometer committed Mar 2, 2024
1 parent 0e3ff5d commit bd6fbeb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 51 deletions.
35 changes: 0 additions & 35 deletions __test__/satysrc/generic.saty
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,6 @@

open Fn in

let () = Debug.log `==== Ref ====` in
let r = Ref.make 1 in
let () = r |> Ref.get |> String.of-int |> Debug.log in
let () = r |> Ref.set 2 in
let () = r |> Ref.get |> String.of-int |> Debug.log in
let r1 = Ref.make 1 in
let r2 = Ref.make 2 in
let () = Ref.swap r1 r2 in
let () = Debug.log (String.of-int (Ref.get r1) ^ `:` ^ String.of-int (Ref.get r2)) in % 2:1
let r = Ref.make 1 in
let () = r |> Ref.set-temporarily 2 (fun _ -> Debug.log (String.of-int (Ref.get r))) in % 2
let () = Debug.log (String.of-int (Ref.get r)) in % 1

let () = Debug.log `==== String ====` in
let () = Debug.log (String.concat [`a`; `b`; `c`;]) in
let () = Debug.log (String.concat ?:(`;`) [`a`; `b`; `c`;]) in
let () = Debug.log (String.of-bool (String.is-empty ` `)) in % true
let _ = String.split-by (Char.make `;`) `spam;ham;eggs;` |> List.map Debug.log in
let _ = String.to-list `abc` |> List.map (fun c -> (c |> String.of-char |> Debug.log)) in
let () = String.(`a` < `b`) |> Bool.to-string |> Debug.log in % true
let () = String.(`ab` < `ba`) |> Bool.to-string |> Debug.log in % true
let () = String.(`aab` < `aba`) |> Bool.to-string |> Debug.log in % true
let () = String.(`bbb` < `bbba`) |> Bool.to-string |> Debug.log in % true
let () = String.contains `abc` `1abcdef` |> Bool.to-string |> Debug.log in % true
let () = String.contains `abc` `abdef` |> Bool.to-string |> Debug.log in % false
let () = String.contains `abc` `ef` |> Bool.to-string |> Debug.log in % false
let () = String.trim #` abc `# |> Debug.log in % |abc|
let () = String.trim #` `# |> Debug.log in % ||
let () = String.trim-start #` abc `# |> Debug.log in % |abc |
let () = String.trim-end #` abc `# |> Debug.log in % | abc|
let () = String.uppercase-ascii `Grüße, Jürgen` |> Debug.log in % GRüßE, JüRGEN
let () = String.lowercase-ascii `GRÜßE, JÜRGEN` |> Debug.log in % grÜße, jÜrgen
let () = String.capitalize-ascii `tiTle` |> Debug.log in % TiTle
let () = String.uncapitalize-ascii `TiTle` |> Debug.log in % tiTle

let () = Debug.log `==== List ====` in
let lst = [1;3;2;4;5] in
let () = lst |> List.take 0 |> List.map String.of-int |> List.fold-left (^) ` ` |> Debug.log in % ` `
Expand Down
57 changes: 41 additions & 16 deletions test/string.test.satyg
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@
@import: ../src/string

let string-test-cases = open Test in
let streq = Eq.equal String.eq in
let strleq = Eq.equal (List.eq String.eq) in
describe `String module` [
it `check equality of strings` (fun () -> (
Eq.equal String.eq `abc` `abc`
&& not Eq.equal String.eq `abc` `abd`
streq `abc` `abc`
&& not streq `abc` `abd`
|> Expect.is-true
));
it `concatenate strings` (fun () -> (
String.concat [`a`; `b`; `c`]
|> string-same `abc`
|> streq `abc`
|> Expect.is-true
));
it `concatenate strings with delimiter` (fun () -> (
String.concat ?:(`;`) [`a`; `b`; `c`]
|> streq `a;b;c`
|> Expect.is-true
));
it `check emptiness of string` (fun () -> (
Expand All @@ -26,33 +33,51 @@ let string-test-cases = open Test in
));
it `split string with a given delimiter` (fun () -> (
let delimiter = Char.make `;` in
`spam;ham;eggs;`
|> String.split-by delimiter
|> Eq.equal (List.eq String.eq) [`spam`; `ham`; `eggs`; ` `]
String.split-by delimiter `spam;ham;eggs;`
|> strleq [`spam`; `ham`; `eggs`; ` `]
|> Expect.is-true
));
it `convert a string to a list of characters` (fun () -> (
let expected = [
String.to-list `abc`
|> Eq.equal (List.eq Char.eq) [
Char.make `a`;
Char.make `b`;
Char.make `c`;
] in
String.to-list `abc`
|> Eq.equal (List.eq Char.eq) expected
]
|> Expect.is-true
));
it `compare strings with lexicographical order` (fun () -> (
let subject = Ord.compare String.ord in
let strcmp = Ord.compare String.ord in
[
subject `a` `a`;
subject `a` `b`;
subject `ab` `ba`;
subject `aab` `aba`;
subject `bbb` `bbba`;
strcmp `a` `a`;
strcmp `a` `b`;
strcmp `ab` `ba`;
strcmp `aab` `aba`;
strcmp `bbb` `bbba`;
] |> Eq.equal (List.eq Ordering.eq) [
Eq; Lt; Lt; Lt; Lt;
] |> Expect.is-true
));
it `check containment` (fun () -> (
String.contains `abc` `1abcdef`
&& not String.contains `abc` `abdef`
&& not String.contains `abc` `ef`
|> Expect.is-true
));
it `trim heading and trailing spaces` (fun () -> (
streq (String.trim #` abc `#) `abc`
&& streq (String.trim #` `#) ` `
&& streq (String.trim-start #` abc `#) #`abc `#
&& streq (String.trim-end #` abc `#) #` abc`#
|> Expect.is-true
));
it `change case of ascii` (fun () ->
streq (String.uppercase-ascii `Grüße, Jürgen`) `GRüßE, JüRGEN`
&& streq (String.lowercase-ascii `GRÜßE, JÜRGEN`) `grÜße, jÜrgen`
&& streq (String.capitalize-ascii `tiTle`) `TiTle`
&& streq (String.uncapitalize-ascii `TiTle`) `tiTle`
|> Expect.is-true
)
% TODO
% * sub
% * of-bool
Expand Down

0 comments on commit bd6fbeb

Please sign in to comment.