# 1.3 Hello, project

> Chapter 1.3: create an Alice project, understand alice.toml, and use alice run, test and fmt. Starts the tea shop project.

Canonical: https://alice-lang.org/documentation/learn/first-project/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

A single file is fine for an experiment. Real programs grow: they get several files, tests and libraries from other people. A **project** keeps all of that together, so that one command builds it the same way on every machine.

## Create a project

The book's running project is a small program for the Mad Hatter's tea shop. It starts by computing the price of an order. Create it:

```sh
alice new tea-shop
cd tea-shop
alice run
```

`alice new` writes a project that already runs. You see its demo output:

```text
Hello, Alice!
sum of squares: 385
```

## What is in a project

```text
tea-shop/
  alice.toml        # the manifest: name, version, kind, dependencies
  src/main.al       # the program
  test/             # tests, one file per topic
  AGENTS.md         # notes for AI coding agents (chapter 22)
  CLAUDE.md
  .gitignore
```

The **manifest** `alice.toml` describes the package:

```toml
[package]
name = "tea-shop"
version = "0.1.0"
kind = "exe"

[dependencies]
```

`kind = "exe"` means this package is a program you can run. Other kinds are `lib` for a library, `tool` for a .NET tool, `game` and `wasm`. You will meet them in [chapter 18](https://alice-lang.org/documentation/learn/packaging.md).

Builds create `bin/`, `obj/` and `.alice/`. They are generated, and `.gitignore` already keeps them out of version control. Commit `alice.toml`, `src/`, `test/` and, once it appears, `alice.lock`.

## The first feature: an order total

The shop needs to know what an order costs. Prices are in cents, as whole numbers. Money in floating point picks up rounding errors, and integer cents do not.

Replace `src/main.al` with:

```alice project="src/main.al"
def total_cents(price : Int32, quantity : Int32) : Int32
  price * quantity
end

puts total_cents(250, 3)
```

```text title="Output"
750
```

Three cups at 2.50 cost 7.50, which is 750 cents. Run it with `alice run`. In a project you do not name the file: Alice finds `src/main.al` through the manifest.

## Test it

A program you cannot test is a program you are afraid to change. Alice has a test runner built in. Every method whose name starts with `test_`, in any file under `test/`, is a test.

Create `test/total_test.al`:

```alice project="test/total_test.al"
def test_total_cents
  assert_equal 750, total_cents(250, 3)
  assert_equal 0, total_cents(250, 0)
end
```

The test can call `total_cents` because tests are compiled together with the sources in `src/`. Top-level statements in `src/main.al`, like the `puts`, do not run during tests.

```sh
alice test
```

```text
test/total_test.al
  ✓ test total cents
```

`assert_equal expected, actual` fails the test when the two differ. `assert condition, "message"` fails when the condition is false.

## Keep it tidy

```sh
alice fmt           # rewrite every .al file in the project's standard layout
alice fmt --check   # change nothing; fail if a file needs formatting (for CI)
```

Formatting is not a matter of taste in Alice: there is one layout, so diffs show only real changes.

## A release build

```sh
alice build --release
```

The debug build that `alice run` and `alice test` use compiles faster. The release build is optimized. [Chapter 18](https://alice-lang.org/documentation/learn/packaging.md) shows how to make a single native executable.

## Add a library

```sh
alice add Newtonsoft.Json --version '13.*'
```

The dependency appears in `alice.toml`. The first build writes the exact version it resolved to `alice.lock`, so every machine builds with the same library. `alice update` refreshes those versions on purpose. Any of the hundreds of thousands of packages on NuGet works: [chapter 14](https://alice-lang.org/documentation/learn/dotnet.md) shows how to use them.

Under `.alice/` you will find a generated `tea-shop.alproj`. It connects the package to MSBuild, the .NET build system. Do not edit it: Alice rewrites it from `alice.toml` on every command.

:::caution\[Common mistakes]

- **Editing `.alice/tea-shop.alproj`.** Changes vanish on the next command. Edit `alice.toml` instead.
- **Expecting top-level code to run in tests.** Only `test_` methods run. Put what you want to test in methods.
- **Naming a test without `test_`.** `def total_works` is an ordinary method, so the runner skips it.
  :::

## Exercises

1. Add a method `with_tip(cents : Int32) : Int32` to `src/main.al` that adds 10 percent, rounded down. Print `with_tip(750)`.
2. Write a test for `with_tip`.
3. What does `alice fmt --check` do when every file is formatted?

<details>
<summary>Answers</summary>

1. Integer division rounds down for positive numbers, so `cents * 110 / 100` works:

   ```alice
   def total_cents(price : Int32, quantity : Int32) : Int32
     price * quantity
   end

   def with_tip(cents : Int32) : Int32
     cents * 110 / 100
   end

   puts with_tip(total_cents(250, 3))
   ```

   ```text title="Output"
   825
   ```

2. Add a file `test/tip_test.al`:

   ```alice skip="project test: compiled with src/ by alice test"
   def test_with_tip
     assert_equal 825, with_tip(750)
     assert_equal 0, with_tip(0)
   end
   ```

3. It changes nothing, reports `0 changed`, and exits with status 0. A file that needs formatting makes it exit with status 1, which is what a CI job checks.

</details>

## Summary

- `alice new NAME` creates a project: `alice.toml`, `src/main.al` and `test/`.
- `alice run`, `alice test`, `alice fmt` and `alice build` work on the whole project.
- Tests are `test_` methods under `test/`. They see every method in `src/`.
- `alice add PACKAGE` adds a NuGet dependency; `alice.lock` pins the versions.
- The tea shop project starts here. Each later chapter adds a feature to it.

That is chapter 1. In [chapter 2](https://alice-lang.org/documentation/learn/guessing-game.md) you write a whole game.

## Continue reading

- Previous: [1.2 Hello, Alice!](https://alice-lang.org/documentation/learn/first-program.md)
- Next: [2. A guessing game](https://alice-lang.org/documentation/learn/guessing-game.md)
