Skip to content

Commit

Permalink
Snapshot tests for scarb-doc (#1420)
Browse files Browse the repository at this point in the history
Closes #1377 
`UPDATE_EXPECT=1 cargo test` fixed the tests (changes the file content)
  • Loading branch information
piotmag769 committed Jul 8, 2024
1 parent 80cc138 commit 3c1bf2c
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 242 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions extensions/scarb-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ cairo-lang-semantic.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-utils.workspace = true
expect-test.workspace = true
itertools.workspace = true
scarb-metadata = { path = "../../scarb-metadata" }
scarb-ui = { path = "../../utils/scarb-ui" }
serde.workspace = true
serde_json.workspace = true
salsa.workspace = true
smol_str.workspace = true
Expand Down
92 changes: 69 additions & 23 deletions extensions/scarb-doc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#![allow(dead_code)]

use itertools::Itertools;
use serde::Serialize;

use crate::db::ScarbDocDatabase;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::diagnostic_utils::StableLocation;
use cairo_lang_defs::ids::{
Expand All @@ -21,7 +21,9 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_utils::Upcast;

#[derive(Clone, Debug)]
use crate::db::ScarbDocDatabase;

#[derive(Serialize, Clone)]
pub struct Crate {
pub root_module: Module,
}
Expand All @@ -34,8 +36,9 @@ impl Crate {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Module {
#[serde(skip)]
pub module_id: ModuleId,
pub item_data: ItemData,

Expand All @@ -54,7 +57,7 @@ pub struct Module {

impl Module {
pub fn new(db: &ScarbDocDatabase, module_id: ModuleId) -> Self {
// TODO: temporary before crate root module doc fetching works.
// FIXME: compiler doesn't support fetching root crate doc
let item_data = match module_id {
ModuleId::CrateRoot(crate_id) => ItemData {
name: crate_id.name(db).to_string(),
Expand Down Expand Up @@ -153,7 +156,7 @@ impl Module {
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Serialize, Clone)]
pub struct ItemData {
pub name: String,
pub doc: Option<String>,
Expand Down Expand Up @@ -190,9 +193,11 @@ impl ItemData {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Constant {
#[serde(skip)]
pub id: ConstantId,
#[serde(skip)]
pub node: ast::ItemConstantPtr,

pub item_data: ItemData,
Expand All @@ -209,9 +214,11 @@ impl Constant {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct FreeFunction {
#[serde(skip)]
pub id: FreeFunctionId,
#[serde(skip)]
pub node: ast::FunctionWithBodyPtr,

pub item_data: ItemData,
Expand All @@ -232,9 +239,11 @@ impl FreeFunction {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Struct {
#[serde(skip)]
pub id: StructId,
#[serde(skip)]
pub node: ast::ItemStructPtr,

pub members: Vec<Member>,
Expand Down Expand Up @@ -269,9 +278,11 @@ impl Struct {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Member {
#[serde(skip)]
pub id: MemberId,
#[serde(skip)]
pub node: ast::MemberPtr,

pub item_data: ItemData,
Expand Down Expand Up @@ -301,9 +312,11 @@ impl Member {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Enum {
#[serde(skip)]
pub id: EnumId,
#[serde(skip)]
pub node: ast::ItemEnumPtr,

pub variants: Vec<Variant>,
Expand Down Expand Up @@ -335,9 +348,11 @@ impl Enum {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Variant {
#[serde(skip)]
pub id: VariantId,
#[serde(skip)]
pub node: ast::VariantPtr,

pub item_data: ItemData,
Expand Down Expand Up @@ -367,9 +382,11 @@ impl Variant {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct TypeAlias {
#[serde(skip)]
pub id: ModuleTypeAliasId,
#[serde(skip)]
pub node: ast::ItemTypeAliasPtr,

pub item_data: ItemData,
Expand All @@ -390,9 +407,11 @@ impl TypeAlias {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct ImplAlias {
#[serde(skip)]
pub id: ImplAliasId,
#[serde(skip)]
pub node: ast::ItemImplAliasPtr,

pub item_data: ItemData,
Expand All @@ -413,9 +432,11 @@ impl ImplAlias {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Trait {
#[serde(skip)]
pub id: TraitId,
#[serde(skip)]
pub node: ast::ItemTraitPtr,

pub trait_constants: Vec<TraitConstant>,
Expand Down Expand Up @@ -457,9 +478,11 @@ impl Trait {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct TraitConstant {
#[serde(skip)]
pub id: TraitConstantId,
#[serde(skip)]
pub node: ast::TraitItemConstantPtr,

pub item_data: ItemData,
Expand All @@ -471,6 +494,8 @@ impl TraitConstant {
Self {
id,
node,
// FIXME: compiler returns empty string for a signature
// FIXME: incorrect full path
item_data: ItemData::new_without_signature(
db,
id,
Expand All @@ -480,9 +505,11 @@ impl TraitConstant {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct TraitType {
#[serde(skip)]
pub id: TraitTypeId,
#[serde(skip)]
pub node: ast::TraitItemTypePtr,

pub item_data: ItemData,
Expand All @@ -494,6 +521,8 @@ impl TraitType {
Self {
id,
node,
// FIXME: compiler returns empty string for a signature
// FIXME: incorrect full path
item_data: ItemData::new_without_signature(
db,
id,
Expand All @@ -503,9 +532,11 @@ impl TraitType {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct TraitFunction {
#[serde(skip)]
pub id: TraitFunctionId,
#[serde(skip)]
pub node: ast::TraitItemFunctionPtr,

pub item_data: ItemData,
Expand All @@ -517,14 +548,17 @@ impl TraitFunction {
Self {
id,
node,
// FIXME: incorrect full path
item_data: ItemData::new(db, id, LookupItemId::TraitItem(TraitItemId::Function(id))),
}
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct Impl {
#[serde(skip)]
pub id: ImplDefId,
#[serde(skip)]
pub node: ast::ItemImplPtr,

pub impl_types: Vec<ImplType>,
Expand Down Expand Up @@ -566,9 +600,11 @@ impl Impl {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct ImplType {
#[serde(skip)]
pub id: ImplTypeDefId,
#[serde(skip)]
pub node: ast::ItemTypeAliasPtr,

pub item_data: ItemData,
Expand All @@ -580,14 +616,17 @@ impl ImplType {
Self {
id,
node,
// FIXME: incorrect full path
item_data: ItemData::new(db, id, LookupItemId::ImplItem(ImplItemId::Type(id))),
}
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct ImplConstant {
#[serde(skip)]
pub id: ImplConstantDefId,
#[serde(skip)]
pub node: ast::ItemConstantPtr,

pub item_data: ItemData,
Expand All @@ -599,14 +638,17 @@ impl ImplConstant {
Self {
id,
node,
// FIXME: incorrect full path
item_data: ItemData::new(db, id, LookupItemId::ImplItem(ImplItemId::Constant(id))),
}
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct ImplFunction {
#[serde(skip)]
pub id: ImplFunctionId,
#[serde(skip)]
pub node: ast::FunctionWithBodyPtr,

pub item_data: ItemData,
Expand All @@ -623,9 +665,11 @@ impl ImplFunction {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct ExternType {
#[serde(skip)]
pub id: ExternTypeId,
#[serde(skip)]
pub node: ast::ItemExternTypePtr,

pub item_data: ItemData,
Expand All @@ -646,9 +690,11 @@ impl ExternType {
}
}

#[derive(Clone, Debug)]
#[derive(Serialize, Clone)]
pub struct ExternFunction {
#[serde(skip)]
pub id: ExternFunctionId,
#[serde(skip)]
pub node: ast::ItemExternFunctionPtr,

pub item_data: ItemData,
Expand Down
Loading

0 comments on commit 3c1bf2c

Please sign in to comment.