Getting started, 4 of 4
Projects and the fb command
Make a package with fb new, then run, test, check, format, document, and build it.
What you’ll build
A package named greet: a folder with a project.toml and its code, which greets people by name
and has tests that check it. On the way you’ll use each of fb’s commands for working on a
package. A single .fb file is enough for a small program, as the earlier tutorials showed; a
package is how a program grows to several files, gets tests, and uses other packages.
Make a package
fb new makes a folder with a package in it:
fb new greet
cd greetCreated package greet in greet
The folder holds three files. .gitignore keeps fb’s build/ folder out of Git. project.toml
names the package and gives its version:
[project]
name = "greet"
version = "0.1.0"main.fb is the package’s main file, whose top-level code is the program:
Import std.core
Print("Hello, world!")Hello, world!
Run it
In the package’s folder, fb run needs no file name: it builds the package the current folder
is in, then runs it. fb says what it’s building and running, then the program’s output follows:
fb run Compiling greet v0.1.0 (C:/Users/ada/greet)
Finished debug build in 0.68s
Running greet.exe
Hello, world!Arguments after -- go to the program, as in fb run -- Ada.
Add a function and its tests
A test is a function marked @Test, which takes nothing and returns nothing. Expect, from
std.test, checks a condition and fails the test when it’s false. fb test builds the package
with its tests and runs them, instead of the main file’s top-level code.
[project]
name = "greet"
version = "0.1.0"Import std.core
Import std.test
Function Greeting(name: String): String
If name == "" Then Return "Hello!"
Return "Hello, {name}!"
End Function
Print(Greeting("Ada"))
@Test
Function GreetsByName()
Expect(Greeting("Ada") == "Hello, Ada!")
End Function
@Test
Function GreetsNobody()
Expect(Greeting("") == "Hello!")
End FunctionRunning 2 tests test greet.main.GreetsByName ... ok test greet.main.GreetsNobody ... ok test result: ok. 2 passed; 0 failed; 0 skipped; 0 filtered out; finished in 0.02s
Each test is named by its package, its module, and its function. A failed Expect shows the
condition as written, where it is, and each side of the comparison. fb test --filter Nobody
runs only the tests whose names have Nobody in them.
Check, format, and document
Three more commands work on the whole package:
fb checkfinds every error the compiler would, without building anything, so it’s fast.fb fmtformats the package’s files in place: four-space indents, one space around operators, and lines up to 100 columns wide.fb fmt --checklists the files it would change, for CI.fb docwrites the package’s Public declarations as JSON, each with its///comment, for documentation tools.greethas none, since nothing in it isPublic.
fb check
fb fmtfb check says what it checked. fb fmt prints nothing when every file is formatted already,
and a line for each file it changes:
Checking greet v0.1.0 (C:/Users/ada/greet)
Finished check in 0.58sBuild it to ship
fb build builds the package into build/, as build/greet.exe on Windows and build/greet
on macOS and Linux. A debug build checks for integer overflow and has full debug information.
fb build --release optimizes the program, and is the build to give to others: an executable
that runs on its own.
fb build --release Compiling greet v0.1.0 (C:/Users/ada/greet)
Finished release build in 0.65sWhat you learned
fb newmakes a package: aproject.toml, amain.fb, and a.gitignore.- In a package’s folder, fb’s commands work on that package without a file name.
@Testfunctions and std.test’sExpectmake tests, whichfb testruns.fb checkchecks a package,fb fmtformats it, andfb docdescribes its Public API.fb build --releasebuilds an optimized executable intobuild/.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.