Skip to content

Commit 38ffadd

Browse files
authored
tests for functions in Arg, Stdin, Locale (#368)
* test untested env functions * use Path.display * tests for functions in Arg, Stdin, Locale * cleanup * fix locale
1 parent 4248f19 commit 38ffadd

File tree

11 files changed

+97
-37
lines changed

11 files changed

+97
-37
lines changed

ci/all_tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ rm -rf dirExampleD
101101
for roc_file in $EXAMPLES_DIR*.roc; do
102102
base_file=$(basename "$roc_file")
103103

104-
# countdown, echo, form, piping... all require user input or special setup
105-
ignore_list=("stdin-basic.roc" "piping.roc" "command-line-args.roc" "http.roc" "env-var.roc" "bytes-stdin-stdout.roc" "error-handling.roc" "tcp-client.roc")
104+
# countdown, echo, form... all require user input or special setup
105+
ignore_list=("stdin-basic.roc" "stdin-pipe.roc" "command-line-args.roc" "http.roc" "env-var.roc" "bytes-stdin-stdout.roc" "error-handling.roc" "tcp-client.roc")
106106

107107
# check if base_file matches something from ignore_list
108108
for file in "${ignore_list[@]}"; do

ci/expect_scripts/command-line-args.exp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ source ./ci/expect_scripts/shared-code.exp
1111

1212

1313
expect "received argument: foo\r\n" {
14-
expect eof {
15-
check_exit_and_segfault
14+
expect "Unix argument, bytes: \\\[102, 111, 111\\\]\r\n" {
15+
expect "back to Arg: \"foo\"\r\n" {
16+
expect eof {
17+
check_exit_and_segfault
18+
}
19+
}
1620
}
1721
}
1822

ci/expect_scripts/locale.exp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/expect
2+
3+
# uncomment line below for debugging
4+
# exp_internal 1
5+
6+
set timeout 7
7+
8+
source ./ci/expect_scripts/shared-code.exp
9+
10+
spawn $env(EXAMPLES_DIR)locale
11+
12+
expect -re {The most preferred locale for this system or application: [a-zA-Z\-]+\r\n} {
13+
expect -re {All available locales for this system or application: \[.*\]\r\n} {
14+
expect eof {
15+
check_exit_and_segfault
16+
}
17+
}
18+
}
19+
20+
puts stderr "\nExpect script failed: output was different from expected value."
21+
exit 1
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ set timeout 7
77

88
source ./ci/expect_scripts/shared-code.exp
99

10-
# Spawn the command to pipe in the data to the script B
11-
spawn bash -c "echo -e \"test\n123\" | $env(EXAMPLES_DIR)/piping"
10+
# -n to make sure Stdin.read_to_end! works without newline in input
1211

13-
# Expect the output
14-
expect -exact "I read 2 lines from stdin.\r\n" {
12+
spawn bash -c "echo -n \"hey\" | $env(EXAMPLES_DIR)/stdin-pipe"
13+
14+
expect "This is what you piped in: \"hey\"\r\n" {
1515
expect eof {
1616
check_exit_and_segfault
1717
}

examples/.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dup-bytes
2020
out.txt
2121
path
2222
print
23-
piping
23+
stdin-pipe
2424
record-builder
2525
result
2626
stdin
@@ -31,4 +31,6 @@ sqlite-test
3131
task-list
3232
tcp-client
3333
time
34-
bytes-stdin-stdout
34+
bytes-stdin-stdout
35+
locale
36+
piping

examples/command-line-args.roc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,26 @@ import pf.Arg exposing [Arg]
1212
main! : List Arg => Result {} _
1313
main! = |raw_args|
1414

15-
args = List.map(raw_args, Arg.display)
16-
1715
# get the second argument, the first is the executable's path
18-
when List.get(args, 1) |> Result.map_err(|_| ZeroArgsGiven) is
16+
when List.get(raw_args, 1) |> Result.map_err(|_| ZeroArgsGiven) is
1917
Err(ZeroArgsGiven) ->
2018
Err(Exit(1, "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`"))
2119

2220
Ok(first_arg) ->
23-
Stdout.line!("received argument: ${first_arg}")
21+
Stdout.line!("received argument: ${Arg.display(first_arg)}")?
22+
23+
# # OPTIONAL TIP:
24+
25+
# If you ever need to pass an arg to a Roc package, it will probably expect an argument with the type `[Unix (List U8), Windows (List U16)]`.
26+
# You can convert an Arg to that with `Arg.to_os_raw`.
27+
#
28+
# Roc packages like to be platform agnostic so that everyone can use them, that's why they avoid platform-specific types like `pf.Arg`.
29+
when Arg.to_os_raw(first_arg) is
30+
Unix(bytes) ->
31+
Stdout.line!("Unix argument, bytes: ${Inspect.to_str(bytes)}")?
32+
33+
Windows(u16s) ->
34+
Stdout.line!("Windows argument, u16s: ${Inspect.to_str(u16s)}")?
35+
36+
# You can go back with Arg.from_os_raw:
37+
Stdout.line!("back to Arg: ${Inspect.to_str(Arg.from_os_raw(Arg.to_os_raw(first_arg)))}")

examples/locale.roc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
app [main!] { pf: platform "../platform/main.roc" }
2+
3+
import pf.Stdout
4+
import pf.Arg exposing [Arg]
5+
import pf.Locale
6+
7+
# Getting the preferred locale and all available locales
8+
9+
# To run this example: check the README.md in this folder
10+
11+
main! : List Arg => Result {} _
12+
main! = |_args|
13+
14+
locale_str = Locale.get!({})?
15+
Stdout.line!("The most preferred locale for this system or application: ${locale_str}")?
16+
17+
all_locales = Locale.all!({})
18+
Stdout.line!("All available locales for this system or application: ${Inspect.to_str(all_locales)}")

examples/piping.roc

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/stdin-pipe.roc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
app [main!] { pf: platform "../platform/main.roc" }
2+
3+
import pf.Stdin
4+
import pf.Stdout
5+
import pf.Arg exposing [Arg]
6+
7+
# To run this example: check the README.md in this folder
8+
9+
# Reading piped text from stdin, for example: `echo "hey" | roc ./examples/stdin-pipe.roc`
10+
11+
main! : List Arg => Result {} _
12+
main! = |_args|
13+
14+
# Data is only sent with Stdin.line! if the user presses Enter,
15+
# so you'll need to use read_to_end! to read data that was piped in without a newline.
16+
piped_in = Stdin.read_to_end!({})?
17+
piped_in_str = Str.from_utf8(piped_in)?
18+
19+
Stdout.line!("This is what you piped in: \"${piped_in_str}\"")

platform/Arg.roc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module [
55
from_os_raw,
66
]
77

8-
## An OS-aware representation of a command-line argument.
8+
## An OS-aware (see below) representation of a command-line argument.
99
##
10-
## Though we tend to think of args as Unicode strings, most operating systems
11-
## represent command-line arguments as lists of bytes that aren't necessarily
10+
## Though we tend to think of args as Unicode strings, **most operating systems
11+
## represent command-line arguments as lists of bytes** that aren't necessarily
1212
## UTF-8 encoded. Windows doesn't even use bytes, but U16s.
1313
##
1414
## Most of the time, you will pass these to packages and they will handle the

0 commit comments

Comments
 (0)