Skip to content

Commit 02cc45c

Browse files
authored
test: track node sizes (#497)
1 parent 900edfe commit 02cc45c

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

CONTRIBUTING.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,25 @@ include one or more tests to ensure that Solar does not regress in the future.
184184
There are a few ways to write tests:
185185
- [unit tests][unit-tests]
186186
- [documentation tests][documentation-tests]
187+
- [snapshot tests][snapshot-tests]
187188
- [integration tests][integration-tests]
188189

189-
Unit and documentation tests are used to test individual library functions or modules, whereas
190+
Unit, documentation, and snapshot tests are used to test individual library functions or modules, whereas
190191
integration tests are used to test the compiler binary.
191192

193+
#### Snapshot Tests
194+
195+
Snapshot tests are a subset of unit tests that capture some specific output and compare it to a snapshot, usually defined inline in the test itself.
196+
197+
We use `snapbox` as the snapshot testing framework, which does not require any external binaries to be installed.
198+
199+
You can automatically create or update the snapshots by running tests normally with the `SNAPSHOTS=overwrite` environment variable,
200+
optionally specifying the crate or test name, as you would with `cargo test` normally.
201+
For example:
202+
```bash
203+
SNAPSHOTS=overwrite cargo test -p solar-ast
204+
```
205+
192206
#### Integration Tests
193207

194208
Integration tests are located in the `tests` directory. They are run using the

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ast/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ semver.workspace = true
3535
num-rational.workspace = true
3636
strum.workspace = true
3737

38+
[dev-dependencies]
39+
snapbox.workspace = true
40+
3841
[features]
3942
nightly = ["solar-data-structures/nightly", "solar-interface/nightly"]

crates/ast/src/ast/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,61 @@ mod tests {
185185
assert_no_drop::<Item<'_>>();
186186
assert_no_drop::<SourceUnit<'_>>();
187187
}
188+
189+
// Ensure that we track the size of individual AST nodes.
190+
#[test]
191+
#[cfg_attr(not(target_pointer_width = "64"), ignore = "64-bit only")]
192+
#[cfg_attr(feature = "nightly", ignore = "stable only")]
193+
fn sizes() {
194+
use snapbox::{assert_data_eq, str};
195+
#[track_caller]
196+
fn assert_size<T>(size: impl snapbox::IntoData) {
197+
assert_size_(std::mem::size_of::<T>(), size.into_data());
198+
}
199+
#[track_caller]
200+
fn assert_size_(actual: usize, expected: snapbox::Data) {
201+
assert_data_eq!(actual.to_string(), expected);
202+
}
203+
204+
assert_size::<Span>(str!["8"]);
205+
assert_size::<DocComments<'_>>(str!["16"]);
206+
207+
assert_size::<SourceUnit<'_>>(str!["16"]);
208+
209+
assert_size::<PragmaDirective<'_>>(str!["40"]);
210+
assert_size::<ImportDirective<'_>>(str!["40"]);
211+
assert_size::<UsingDirective<'_>>(str!["64"]);
212+
assert_size::<ItemContract<'_>>(str!["64"]);
213+
assert_size::<ItemFunction<'_>>(str!["184"]);
214+
assert_size::<VariableDefinition<'_>>(str!["88"]);
215+
assert_size::<ItemStruct<'_>>(str!["32"]);
216+
assert_size::<ItemEnum<'_>>(str!["32"]);
217+
assert_size::<ItemUdvt<'_>>(str!["48"]);
218+
assert_size::<ItemError<'_>>(str!["40"]);
219+
assert_size::<ItemEvent<'_>>(str!["40"]);
220+
assert_size::<ItemKind<'_>>(str!["184"]);
221+
assert_size::<Item<'_>>(str!["208"]);
222+
223+
assert_size::<FunctionHeader<'_>>(str!["144"]);
224+
assert_size::<ParameterList<'_>>(str!["24"]);
225+
226+
assert_size::<ElementaryType>(str!["3"]);
227+
assert_size::<TypeKind<'_>>(str!["24"]);
228+
assert_size::<Type<'_>>(str!["32"]);
229+
230+
assert_size::<ExprKind<'_>>(str!["40"]);
231+
assert_size::<Expr<'_>>(str!["48"]);
232+
233+
assert_size::<StmtKind<'_>>(str!["64"]);
234+
assert_size::<Stmt<'_>>(str!["88"]);
235+
assert_size::<Block<'_>>(str!["24"]);
236+
237+
assert_size::<yul::ExprCall<'_>>(str!["32"]);
238+
assert_size::<yul::ExprKind<'_>>(str!["40"]);
239+
assert_size::<yul::Expr<'_>>(str!["48"]);
240+
241+
assert_size::<yul::StmtKind<'_>>(str!["120"]);
242+
assert_size::<yul::Stmt<'_>>(str!["144"]);
243+
assert_size::<yul::Block<'_>>(str!["24"]);
244+
}
188245
}

crates/sema/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ alloy-json-abi.workspace = true
3939
serde.workspace = true
4040
serde_json.workspace = true
4141

42+
[dev-dependencies]
43+
snapbox.workspace = true
44+
4245
[features]
4346
nightly = [
4447
"solar-ast/nightly",

crates/sema/src/hir/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,3 +1472,46 @@ pub struct TypeMapping<'hir> {
14721472
pub value: Type<'hir>,
14731473
pub value_name: Option<Ident>,
14741474
}
1475+
1476+
#[cfg(test)]
1477+
mod tests {
1478+
use super::*;
1479+
1480+
// Ensure that we track the size of individual HIR nodes.
1481+
#[test]
1482+
#[cfg_attr(not(target_pointer_width = "64"), ignore = "64-bit only")]
1483+
#[cfg_attr(feature = "nightly", ignore = "stable only")]
1484+
fn sizes() {
1485+
use snapbox::{assert_data_eq, str};
1486+
#[track_caller]
1487+
fn assert_size<T>(size: impl snapbox::IntoData) {
1488+
assert_size_(std::mem::size_of::<T>(), size.into_data());
1489+
}
1490+
#[track_caller]
1491+
fn assert_size_(actual: usize, expected: snapbox::Data) {
1492+
assert_data_eq!(actual.to_string(), expected);
1493+
}
1494+
1495+
assert_size::<Hir<'_>>(str!["216"]);
1496+
1497+
assert_size::<Item<'_, '_>>(str!["16"]);
1498+
assert_size::<Contract<'_>>(str!["120"]);
1499+
assert_size::<Function<'_>>(str!["136"]);
1500+
assert_size::<Struct<'_>>(str!["48"]);
1501+
assert_size::<Enum<'_>>(str!["48"]);
1502+
assert_size::<Udvt<'_>>(str!["56"]);
1503+
assert_size::<Error<'_>>(str!["48"]);
1504+
assert_size::<Event<'_>>(str!["48"]);
1505+
assert_size::<Variable<'_>>(str!["96"]);
1506+
1507+
assert_size::<TypeKind<'_>>(str!["16"]);
1508+
assert_size::<Type<'_>>(str!["24"]);
1509+
1510+
assert_size::<ExprKind<'_>>(str!["56"]);
1511+
assert_size::<Expr<'_>>(str!["72"]);
1512+
1513+
assert_size::<StmtKind<'_>>(str!["32"]);
1514+
assert_size::<Stmt<'_>>(str!["40"]);
1515+
assert_size::<Block<'_>>(str!["24"]);
1516+
}
1517+
}

0 commit comments

Comments
 (0)