First program
Create a package, run its program, and add a test.
Create a package with i new, run its program, then change and test it. This assumes i is on your PATH and you have a checkout of the i repository for std.
i new hello made the package `hello` in `hello`
run it with `i run --project hello` The command creates hello/i.toml, .gitignore, src/main.i, and test/main_test.i. The generated manifest uses a local path for the test dependency std. If it does not point to the std directory in your i checkout, edit the std path under [dev-dependencies] in i.toml to that directory. Paths can be absolute or relative to the package directory.
The generated src/main.i is:
//// The package `hello`. It imports the standard service `Io` from `i/io`
//// (§9.7), and `i run` gives it, so the program can print.
(
(Io) = i/io
)
(
greeting
)
/// The greeting for `who`.
greeting = (who : String) -> String { "Hello, " <> who <> "!" }
main = () <- (io : Io) { io.print(greeting("hello")) } Hello, hello! The //// lines document the module. The first parenthesized group imports Io from i/io. The second group exports greeting so another module can use it. The /// line documents that function. greeting takes a String and joins its argument to two string literals. main is the program entry; it needs an Io service and calls its print function with the greeting. Read about values, functions, packages, and services in the tour.
Enter the project and run it:
cd hello
i run Hello, hello! Change the argument in main to "world", leaving the rest of the file as follows:
//// The package `hello`. It imports the standard service `Io` from `i/io`
//// (§9.7), and `i run` gives it, so the program can print.
(
(Io) = i/io
)
(
greeting
)
/// The greeting for `who`.
greeting = (who : String) -> String { "Hello, " <> who <> "!" }
main = () <- (io : Io) { io.print(greeting("world")) } Hello, world! i run Hello, world! The generated test already checks greeting. Add a second test so test/main_test.i contains:
//// The package `hello`. It imports the standard service `Io` from `i/io`
//// (§9.7), and `i run` gives it, so the program can print.
(
(Io) = i/io
)
(
greeting
)
/// The greeting for `who`.
greeting = (who : String) -> String { "Hello, " <> who <> "!" }
main = () <- (io : Io) { io.print(greeting("world")) } (
testlib = (test) = std/test
hello/main
)
test("greets", () {
main.greeting("i") |> ("Hello, i!") { Nil } | (other) { panic(other) }
})
test("greets the world", () {
main.greeting("world") |>
("Hello, world!") { Nil }
| (other) { panic(other) }
}) i test pass hello/main_test.greets
pass hello/main_test.greets the world
2 passed, 0 failed Format the source and check the package:
i fmt
i check Both commands completed with no output in this run. i fmt writes formatted source; i check checks the package without running the program.