Julia Package Project
A package is a folder that holds a module, a project file, and tests. This package project builds one tiny example of that shape.
A module groups one function under one name
The module holds a doubling function. Calling it through the module name gives 42.
module ToolKit
double(x) = 2 * x
end
println(ToolKit.double(21))A project file names the package and its version
The block writes the project file and then reads it back. The printed text is the file content.
text = "name = \"ToolKit\"\nuuid = \"a1b2c3d4-e5f6-4789-abcd-ef0123456789\"\nversion = \"0.1.0\"\n"
write("Project.toml", text)
print(read("Project.toml", String))Two tests pass for the doubling function
The test block checks two doubling values. The test summary is discarded, so only the pass count is printed.
module ToolKit
double(x) = 2 * x
end
using Test
ts = redirect_stdout(devnull) do
@testset "doubling" begin
@test ToolKit.double(3) == 6
@test ToolKit.double(0) == 0
end
end
println(ts.n_passed)
println(ToolKit.double(21))Example
module ToolKit
double(x) = 2 * x
end
text = "name = \"ToolKit\"\nuuid = \"a1b2c3d4-e5f6-4789-abcd-ef0123456789\"\nversion = \"0.1.0\"\n"
write("Project.toml", text)
print(read("Project.toml", String))
using Test
ts = redirect_stdout(devnull) do
@testset "doubling" begin
@test ToolKit.double(3) == 6
@test ToolKit.double(0) == 0
end
end
println(ts.n_passed)
println(ToolKit.double(21))Key points
- Package project: a package project holds a module plus its project file plus its tests in one folder.
- Module: a module groups related functions under one name.
- Project file: the project file names the package and its version.
- Test: a test checks that a function returns an expected value.
- Pass count: the pass count says how many tests passed.
In this example, the module defines the doubling function and the block writes the project file and prints it back. The test block then checks two doubling values and prints only the pass count. The last line shows the doubling function in use.
Note
test summaries carry a timing value that changes between runs, so this lesson prints only the pass count.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
Which file names the package and its version in this package project?