Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small corrections for Chapter 8 #128

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions Chapters/03-unittests.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ the specific group of unit tests that you are writing inside this specific `test
In the example below, we are testing if the sum of two objects (`a` and `b`)
is equal to 4. The `expect()` function from the Zig Standard Library
is a function that receives a logical test as input. If this logical test
results in `true`, then, the test passes. But if it results
in `false`, then, the test fails.
results in `true`, then the test passes. But if it results
in `false`, then the test fails.

You can write any Zig code you want inside a `test` block.
Part of this code might be some necessary commands to setup your testing
Expand All @@ -51,15 +51,15 @@ test "testing simple sum" {
}
```

You can have multiple `test` blocks written on the same Zig module.
You can have multiple `test` blocks written in the same Zig module.
Also, you can mix `test` blocks with your source code, with no problems
or consequences. If you mix `test` blocks with your normal source code,
when you execute the `build`, `build-exe`, `build-obj` or `build-lib` commands from the
`zig` compiler that we exposed at @sec-compile-code, these `test` blocks are automatically
when you execute the `build`, `build-exe`, `build-obj`, or `build-lib` commands from the
`zig` compiler that we exposed in @sec-compile-code, these `test` blocks are automatically
ignored by the compiler.

In other words, the `zig` compiler builds and execute your unit tests only
when you ask it to. By default, the compiler always ignore `test`
when you ask it to. By default, the compiler always ignores `test`
blocks written in your Zig modules. The compiler normally checks only if
there are any syntax errors in these `test` blocks.

Expand All @@ -75,7 +75,7 @@ Each programmer might have a different opinion on this.
Some of them might prefer to keep unit tests separate from the actual
source code of their application. If that is your case, you can
simply create a separate `tests` folder in your project, and
start writing Zig modules that contains only unit tests (as you would normally do
start writing Zig modules that contain only unit tests (as you would normally do
on a Python project with `pytest`, for example), and everything will work fine.
It boils down to which is your preference here.

Expand Down Expand Up @@ -110,7 +110,7 @@ memory leaks and double-frees. The `defer` keyword
is especially helpful in this regard.

When developing your source code, you, the programmer, is responsible for making
sure that your code do not produce such problems. However,
sure that your code does not produce such problems. However,
you can also use a special type of an allocator object in Zig
that is capable of automatically detecting such problems for you.
This is the `std.testing.allocator` object.
Expand All @@ -129,8 +129,8 @@ write unit tests for these functions, where you provide the
`std.testing.allocator` object as input to these functions.

Look at the example below, where I'm defining a function that clearly causes
a memory leak. Because we allocate memory, but, at the same time,
we do not free this allocated memory at any point. So, when the function
a memory leak. Because we allocate memory, but do not free it at any
point, a memory leak occurs. So, when the function
returns, we lose the reference to the `buffer` object, which contains
the allocated memory, and, as a result, we can no longer free this memory.

Expand Down Expand Up @@ -200,7 +200,7 @@ Notice that, inside the function `alloc_error()` we are allocating
in the `test` block, we are using the `FixedBufferAllocator()`
allocator object, which is limited to 10 bytes of space, because
the object `buffer`, which we provided to the allocator object,
have only 10 bytes of space.
has only 10 bytes of space.

That is why, the `alloc_error()` function raises an `OutOfMemory` error
on this case.
Expand Down Expand Up @@ -304,10 +304,9 @@ are equal or not. Just provide the two string objects that you want to compare,
as inputs to the function.

If the function finds any existing differences between the two strings,
then, the function will raise an error, and also, print an error message
that shows the exact difference between the two string objects provided,
as the example below demonstrates:

it will raise an error and print a message showing the exact
difference between the provided string objects, as demonstrated in the
example below.

```{zig}
#| eval: false
Expand Down