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

Copy rustdoc comments over to TS output #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
272 changes: 265 additions & 7 deletions tests/enum.rs

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions tests/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ use tsify::Tsify;

#[test]
fn test_flatten() {
/// Comment for A
#[derive(Tsify)]
struct A {
/// Comment for a
a: i32,
/// Comment for b
b: String,
}

/// Comment for B
#[derive(Tsify)]
struct B {
/// Comment for extra
#[serde(flatten)]
extra: A,
/// Comment for c
c: i32,
}

assert_eq!(
B::DECL,
indoc! {"
/**
* Comment for B
*/
export interface B extends A {
/**
* Comment for c
*/
c: number;
}"
}
Expand All @@ -31,22 +43,31 @@ fn test_flatten() {

#[test]
fn test_flatten_option() {
/// Comment for A
#[derive(Tsify)]
struct A {
/// Comment for a
a: i32,
/// Comment for b
b: String,
}

/// Comment for B
#[derive(Tsify)]
struct B {
/// Comment for extra
#[serde(flatten)]
extra: Option<A>,
/// Comment for c
c: i32,
}

assert_eq!(
B::DECL,
indoc! {"
/**
* Comment for B
*/
export type B = { c: number } & (A | {});"
}
);
Expand Down
62 changes: 59 additions & 3 deletions tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,75 @@ use tsify::Tsify;

#[test]
fn test_generic_struct() {
/// Comment for GenericStruct
#[derive(Tsify)]
pub struct GenericStruct<'a, A, B, C, D> {
/// Comment for a
a: A,
/// Comment for b
b: B,
/// Comment for c
#[serde(skip)]
c: &'a C,
/// Comment for d
d: D,
}

assert_eq!(
GenericStruct::<(), (), (), ()>::DECL,
indoc! {"
/**
* Comment for GenericStruct
*/
export interface GenericStruct<A, B, D> {
/**
* Comment for a
*/
a: A;
/**
* Comment for b
*/
b: B;
/**
* Comment for d
*/
d: D;
}"
}
);

/// Comment for GenericNewtype
#[derive(Tsify)]
pub struct GenericNewtype<T>(T);

assert_eq!(
GenericNewtype::<()>::DECL,
"export type GenericNewtype<T> = T;"
indoc! {"
/**
* Comment for GenericNewtype
*/
export type GenericNewtype<T> = T;"
},
);

/// Comment for GenericTuple
#[derive(Tsify)]
pub struct GenericTuple<'a, A, B, C, D>(A, #[serde(skip)] &'a B, C, D);

assert_eq!(
GenericTuple::<(), (), (), ()>::DECL,
"export type GenericTuple<A, C, D> = [A, C, D];"
indoc! {"
/**
* Comment for GenericTuple
*/
export type GenericTuple<A, C, D> = [A, C, D];"
,}
);
}

#[test]
fn test_generic_enum() {
/// Comment for GenericEnum
#[derive(Tsify)]
pub enum GenericEnum<T, U> {
Unit,
Expand All @@ -54,6 +84,9 @@ fn test_generic_enum() {
}

let expected = indoc! {r#"
/**
* Comment for GenericEnum
*/
export type GenericEnum<T, U> = "Unit" | { NewType: T } | { Seq: [T, U] } | { Map: { x: T; y: U } };"#
};

Expand All @@ -62,23 +95,46 @@ fn test_generic_enum() {

#[test]
fn test_generic_enum_with_namespace() {
/// Comment for GenericEnum
#[derive(Tsify)]
#[tsify(namespace)]
pub enum GenericEnum<T, U> {
/// Comment for Unit
Unit,
/// Comment for NewType
NewType(T),
/// Comment for Seq
Seq(T, U),
/// Comment for Map
Map { x: T, y: U },
}

let expected = indoc! {r#"
/**
* Comment for GenericEnum
*/
declare namespace GenericEnum {
/**
* Comment for Unit
*/
export type Unit = "Unit";
/**
* Comment for NewType
*/
export type NewType<T> = { NewType: T };
/**
* Comment for Seq
*/
export type Seq<T, U> = { Seq: [T, U] };
/**
* Comment for Map
*/
export type Map<T, U> = { Map: { x: T; y: U } };
}


/**
* Comment for GenericEnum
*/
export type GenericEnum<T, U> = "Unit" | { NewType: T } | { Seq: [T, U] } | { Map: { x: T; y: U } };"#
};

Expand Down
63 changes: 63 additions & 0 deletions tests/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,80 @@ use tsify::Tsify;

#[test]
fn test_optional() {
/// Comment for Optional
#[derive(Tsify)]
struct Optional {
/// Comment for a
#[tsify(optional)]
a: Option<i32>,
/// Comment for b
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<String>,
/// Comment for c
#[serde(default)]
c: i32,
/// Comment for d
#[serde(default)]
d: Option<String>,
}

/// Comment for OptionalAll
#[derive(Tsify)]
#[serde(default)]
struct OptionalAll {
/// Comment for a
a: i32,
/// Comment for b
b: i32,
/// Comment for c
c: Option<i32>,
}

if cfg!(feature = "js") {
assert_eq!(
Optional::DECL,
indoc! {"
/**
* Comment for Optional
*/
export interface Optional {
/**
* Comment for a
*/
a?: number;
/**
* Comment for b
*/
b?: string;
/**
* Comment for c
*/
c?: number;
/**
* Comment for d
*/
d?: string | undefined;
}"
}
);
assert_eq!(
OptionalAll::DECL,
indoc! {"
/**
* Comment for OptionalAll
*/
export interface OptionalAll {
/**
* Comment for a
*/
a?: number;
/**
* Comment for b
*/
b?: number;
/**
* Comment for c
*/
c?: number | undefined;
}"
}
Expand All @@ -52,20 +88,47 @@ fn test_optional() {
assert_eq!(
Optional::DECL,
indoc! {"
/**
* Comment for Optional
*/
export interface Optional {
/**
* Comment for a
*/
a?: number;
/**
* Comment for b
*/
b?: string;
/**
* Comment for c
*/
c?: number;
/**
* Comment for d
*/
d?: string | null;
}"
}
);
assert_eq!(
OptionalAll::DECL,
indoc! {"
/**
* Comment for OptionalAll
*/
export interface OptionalAll {
/**
* Comment for a
*/
a?: number;
/**
* Comment for b
*/
b?: number;
/**
* Comment for c
*/
c?: number | null;
}"
}
Expand Down
Loading