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

Test asserts plugin #1390

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions scarb/scarblib/cairo_test_asserts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "assert_macros"
version = "0.1.0"

no-core = true

[cairo-plugin]
builtin = true
26 changes: 25 additions & 1 deletion scarb/src/compiler/plugin/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_syntax::node::ast::ModuleItem;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_test_plugin::test_plugin_suite;
use cairo_lang_test_plugin::{test_assert_suite, test_plugin_suite};

use crate::compiler::plugin::{CairoPlugin, CairoPluginInstance};
use crate::core::{PackageId, PackageName, SourceId};
Expand Down Expand Up @@ -105,3 +105,27 @@ impl MacroPlugin for CairoRunPlugin {
self.declared_attributes()
}
}

pub struct BuiltinTestAssertsPlugin;

impl CairoPlugin for BuiltinTestAssertsPlugin {
fn id(&self) -> PackageId {
PackageId::new(
PackageName::TEST_ASSERTS_PLUGIN,
semver::Version::new(0, 1, 0),
SourceId::for_std(),
)
}

fn instantiate(&self) -> Result<Box<dyn CairoPluginInstance>> {
Ok(Box::new(BuiltinTestAssertsPluginInstance))
}
}

struct BuiltinTestAssertsPluginInstance;

impl CairoPluginInstance for BuiltinTestAssertsPluginInstance {
fn plugin_suite(&self) -> PluginSuite {
test_assert_suite()
}
}
2 changes: 2 additions & 0 deletions scarb/src/compiler/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;

use crate::compiler::plugin::builtin::BuiltinTestAssertsPlugin;
use anyhow::{anyhow, bail, Result};
use cairo_lang_semantic::plugin::PluginSuite;
use itertools::Itertools;
Expand Down Expand Up @@ -64,6 +65,7 @@ impl CairoPluginRepository {
repo.add(Box::new(BuiltinStarkNetPlugin)).unwrap();
repo.add(Box::new(BuiltinTestPlugin)).unwrap();
repo.add(Box::new(BuiltinCairoRunPlugin)).unwrap();
repo.add(Box::new(BuiltinTestAssertsPlugin)).unwrap();
repo
}

Expand Down
7 changes: 6 additions & 1 deletion scarb/src/core/package/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

use crate::internal::restricted_names;
use crate::{CAIRO_RUN_PLUGIN_NAME, STARKNET_PLUGIN_NAME, TEST_PLUGIN_NAME};

#[cfg(doc)]
use crate::core::Package;

use crate::{
CAIRO_RUN_PLUGIN_NAME, STARKNET_PLUGIN_NAME, TEST_ASSERTS_PLUGIN_NAME, TEST_PLUGIN_NAME,
};

/// A [`String`]-like type representing [`Package`] name.
///
/// * Instances of this type are validated upon construction to comply with the
Expand All @@ -32,6 +35,8 @@ impl PackageName {
pub const STARKNET: Self = PackageName(SmolStr::new_inline(STARKNET_PLUGIN_NAME));
pub const TEST_PLUGIN: Self = PackageName(SmolStr::new_inline(TEST_PLUGIN_NAME));
pub const CAIRO_RUN_PLUGIN: Self = PackageName(SmolStr::new_inline(CAIRO_RUN_PLUGIN_NAME));
pub const TEST_ASSERTS_PLUGIN: Self =
PackageName(SmolStr::new_inline(TEST_ASSERTS_PLUGIN_NAME));

/// Constructs and validates new [`PackageName`].
///
Expand Down
1 change: 1 addition & 0 deletions scarb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ pub const DEFAULT_README_FILE_NAME: &str = "README.md";
pub const DEFAULT_LICENSE_FILE_NAME: &str = "LICENSE";
pub const STARKNET_PLUGIN_NAME: &str = "starknet";
pub const TEST_PLUGIN_NAME: &str = "cairo_test";
pub const TEST_ASSERTS_PLUGIN_NAME: &str = "assert_macros";
pub const CAIRO_RUN_PLUGIN_NAME: &str = "cairo_run";
6 changes: 6 additions & 0 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ pub fn resolve_workspace_with_opts(
.version_req(version_req.clone())
.source_id(SourceId::for_std())
.build(),
ManifestDependency::builder()
.kind(DepKind::Target(TargetKind::TEST))
.name(PackageName::TEST_ASSERTS_PLUGIN)
.version_req(DependencyVersionReq::exact(&semver::Version::new(0, 1, 0)))
.source_id(SourceId::for_std())
.build(),
],
);
if let Some(custom_source_patches) = ws.config().custom_source_patches() {
Expand Down
Loading