Skip to content

Commit d39e28e

Browse files
authored
[toml] Implement a TOML parser as package tomlmojo and use it to re-factor tests (#63)
This pull request includes several changes to improve the testing and packaging of the `decimojo` and `tomlmojo` libraries. The most important changes involve implementing a TOML parser in package `tomlmojo`, restructuring the test cases to load from a TOML file, adding new test cases, and updating the packaging commands. Improvements to testing: * [`tests/biguint/test_biguint_arithmetics.mojo`](diffhunk://#diff-855e834af415ace9cb8340506c824c86a99bed282917f2d90075705170195827R7-R52): Refactored the test functions (`test_add`, `test_subtract`, `test_multiply`, `test_extreme_cases`) to load test cases from a TOML file and added structured `TestCase` for better test management. [[1]](diffhunk://#diff-855e834af415ace9cb8340506c824c86a99bed282917f2d90075705170195827R7-R52) [[2]](diffhunk://#diff-855e834af415ace9cb8340506c824c86a99bed282917f2d90075705170195827L81-L132) [[3]](diffhunk://#diff-855e834af415ace9cb8340506c824c86a99bed282917f2d90075705170195827L142-R108) [[4]](diffhunk://#diff-855e834af415ace9cb8340506c824c86a99bed282917f2d90075705170195827L185-R163) * [`src/decimojo/tests.mojo`](diffhunk://#diff-2111591ff6c3243ac0d5ada070d08238c8c1b15e419da7ecc99fd680917252c4R1-R55): Added a new file to implement `TestCase` struct and functions for managing test cases. * [`tests/biguint/test_data/biguint_arithmetics.toml`](diffhunk://#diff-54c73fb8f7a46321f65b4c3080f874518363de06347f935a232028de46ebd7feR1-R160): Added a new TOML file to hold test data for BigUInt arithmetic operations, including addition, subtraction, multiplication, and extreme cases. Packaging updates: * [`mojoproject.toml`](diffhunk://#diff-1b0ef62120bccf4c05a76e60e13fe686f33eb92d5897b3f05e0c3f8d737fc5c0L20-R26): Updated the packaging commands to include separate packaging for `decimojo` and `tomlmojo` libraries and updated the clean command to remove additional files. Licensing and documentation: * [`src/decimojo/__init__.mojo`](diffhunk://#diff-6499d0e8dbd5799013a6c0d8a5539d86b3d7d1d32ee6983f110d14ca0b4d06ffL2-L4): Removed redundant comments and updated the file header. * [`src/tomlmojo/__init__.mojo`](diffhunk://#diff-d640b9adf5ccc6ab3c2585fae60a6598e1f6d6a9130949bc48e73ab7eaaae77fR1-R21): Added a new file with licensing information and initial import statements for TOML parsing.
1 parent fba2fa8 commit d39e28e

File tree

12 files changed

+1593
-550
lines changed

12 files changed

+1593
-550
lines changed

.github/workflows/run_tests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ jobs:
5252
- name: Build package
5353
run: |
5454
magic run mojo package src/decimojo
55+
magic run mojo package src/tomlmojo
5556
cp decimojo.mojopkg tests/
5657
cp decimojo.mojopkg benches/
58+
mv tomlmojo.mojopkg tests/
5759
5860
- name: Run tests
5961
run: |

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The core types are:
1515

1616
The library is expanding to include `BigDecimal` types that support arbitrary precision[^arbitrary], allowing for calculations with unlimited digits and decimal places. These extensions are currently under active development.
1717

18+
This repository includes [TOMLMojo](https://github.com/forfudan/decimojo/tree/main/src/tomlmojo), a lightweight TOML parser in pure Mojo. It parses configuration files and test data, supporting basic types, arrays, and nested tables. While created for DeciMojo's testing framework, it offers general-purpose structured data parsing with a clean, simple API.
19+
1820
## Installation
1921

2022
DeciMojo is available in the [modular-community](https://repo.prefix.dev/modular-community) package repository. You can install it using any of these methods:

mojoproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ max = ">=25.2"
1717
format = "magic run mojo format ./"
1818

1919
# compile the package
20-
package = "magic run format && magic run mojo package src/decimojo && cp decimojo.mojopkg tests/ && cp decimojo.mojopkg benches/ && rm decimojo.mojopkg"
20+
package_decimojo = "magic run mojo package src/decimojo && cp decimojo.mojopkg tests/ && cp decimojo.mojopkg benches/ && rm decimojo.mojopkg"
21+
package_tomlmojo = "magic run mojo package src/tomlmojo && mv tomlmojo.mojopkg tests/"
22+
package = "magic run format && magic run package_decimojo && magic run package_tomlmojo"
2123
p = "clear && magic run package"
2224

2325
# clean the package files in tests folder
24-
clean = "rm tests/decimojo.mojopkg && rm benches/decimojo.mojopkg"
26+
clean = "rm tests/decimojo.mojopkg && rm benches/decimojo.mojopkg && rm tests/tomlmojo.mojopkg"
27+
c = "clear && magic run clean"
2528

2629
# tests (use the mojo testing tool)
2730
test = "magic run package && magic run mojo test tests --filter"

src/decimojo/__init__.mojo

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
3-
# https://github.com/forfudan/decimojo
4-
#
52
# Copyright 2025 Yuhao Zhu
63
#
74
# Licensed under the Apache License, Version 2.0 (the "License");

src/decimojo/tests.mojo

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ===----------------------------------------------------------------------=== #
2+
# Copyright 2025 Yuhao Zhu
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ===----------------------------------------------------------------------=== #
16+
17+
"""
18+
Implement structs and functions for tests.
19+
"""
20+
21+
22+
struct TestCase:
23+
"""Structure to hold test case data.
24+
25+
Attributes:
26+
a: The first input value as numeric string.
27+
b: The second input value as numeric string.
28+
expected: The expected output value as numeric string.
29+
description: A description of the test case.
30+
"""
31+
32+
var a: String
33+
var b: String
34+
var expected: String
35+
var description: String
36+
37+
fn __init__(
38+
out self, a: String, b: String, expected: String, description: String
39+
):
40+
self.a = a
41+
self.b = b
42+
self.expected = expected
43+
self.description = description + "\nx1 = " + self.a + "\nx2 = " + self.b
44+
45+
fn __copyinit__(out self, other: Self):
46+
self.a = other.a
47+
self.b = other.b
48+
self.expected = other.expected
49+
self.description = other.description
50+
51+
fn __moveinit__(out self, owned other: Self):
52+
self.a = other.a^
53+
self.b = other.b^
54+
self.expected = other.expected^
55+
self.description = other.description^

src/tomlmojo/__init__.mojo

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# ===----------------------------------------------------------------------=== #
2+
# Copyright 2025 Yuhao Zhu
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ===----------------------------------------------------------------------=== #
16+
17+
"""
18+
A simple TOML parser for Mojo.
19+
"""
20+
21+
from .parser import parse_file, TOMLValueType

0 commit comments

Comments
 (0)