442 lines
11 KiB
Rust
442 lines
11 KiB
Rust
#![allow(dead_code)]
|
|
|
|
use std::{concat, fs};
|
|
|
|
use ts_rs::{Config, TS};
|
|
|
|
/// Doc comment.
|
|
/// Supports new lines.
|
|
///
|
|
/// Testing
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
struct A {
|
|
/// Doc of field
|
|
///
|
|
/// Testing
|
|
name: String,
|
|
}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
/// Doc comment.
|
|
/// Supports new lines.
|
|
///
|
|
/// Testing
|
|
struct B {
|
|
/// Doc of field
|
|
///
|
|
/// Testing
|
|
name: String,
|
|
}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
/// Doc comment.
|
|
/// Supports new lines.
|
|
///
|
|
/// Testing
|
|
struct C {}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
/// Doc comment.
|
|
/// Supports new lines.
|
|
///
|
|
/// Testing
|
|
struct D;
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
/// Doc comment.
|
|
/// Supports new lines.
|
|
///
|
|
/// Testing
|
|
enum E {}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
/// Doc comment.
|
|
/// Supports new lines.
|
|
///
|
|
/// Testing
|
|
enum F {
|
|
/// Doc of variant
|
|
///
|
|
/// Testing
|
|
VarA,
|
|
/// Doc of variant
|
|
///
|
|
/// Testing
|
|
VarB(),
|
|
/// Doc of variant
|
|
///
|
|
/// Testing
|
|
VarC {
|
|
/// Doc of field of variant
|
|
///
|
|
/// Testing
|
|
variant_field: i32,
|
|
},
|
|
}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
struct G {
|
|
/// Docs
|
|
some_other_field: i32,
|
|
|
|
/// Some documentation that should probably NOT be exported
|
|
#[ts(flatten)]
|
|
f: F,
|
|
}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
/**
|
|
* Block doc comment
|
|
*
|
|
* works
|
|
*/
|
|
struct H {
|
|
foo: i32,
|
|
}
|
|
|
|
#[derive(TS)]
|
|
#[ts(export_to = "docs/")]
|
|
#[doc = concat!("line ", line!())]
|
|
struct I {
|
|
#[doc = concat!("column ", column!())]
|
|
a: i32,
|
|
|
|
#[doc = concat!("path ", module_path!())]
|
|
b: i32,
|
|
}
|
|
|
|
#[test]
|
|
fn export_a() {
|
|
let cfg = Config::from_env();
|
|
A::export(&cfg).unwrap();
|
|
|
|
let expected_content = if cfg!(feature = "format") {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type A = {\n",
|
|
" /**\n",
|
|
" * Doc of field\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
" name: string;\n",
|
|
"};\n",
|
|
)
|
|
} else {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type A = { \n",
|
|
"/**\n",
|
|
" * Doc of field\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"name: string, };",
|
|
"\n",
|
|
)
|
|
};
|
|
|
|
let actual_content = super::read_file::<A>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_b() {
|
|
let cfg = Config::from_env();
|
|
B::export(&cfg).unwrap();
|
|
|
|
let expected_content = if cfg!(feature = "format") {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type B = {\n",
|
|
" /**\n",
|
|
" * Doc of field\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
" name: string;\n",
|
|
"};\n",
|
|
)
|
|
} else {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type B = { \n",
|
|
"/**\n",
|
|
" * Doc of field\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"name: string, };",
|
|
"\n",
|
|
)
|
|
};
|
|
|
|
let actual_content = super::read_file::<B>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_c() {
|
|
let cfg = Config::from_env();
|
|
C::export(&cfg).unwrap();
|
|
|
|
let expected_content = concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type C = Record<symbol, never>;\n",
|
|
);
|
|
|
|
let actual_content = super::read_file::<C>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_d() {
|
|
let cfg = Config::from_env();
|
|
D::export(&cfg).unwrap();
|
|
|
|
let expected_content = concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type D = null;\n",
|
|
);
|
|
|
|
let actual_content = super::read_file::<D>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_e() {
|
|
let cfg = Config::from_env();
|
|
E::export(&cfg).unwrap();
|
|
|
|
let expected_content = concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type E = never;\n",
|
|
);
|
|
|
|
let actual_content = super::read_file::<E>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_f() {
|
|
let cfg = Config::from_env();
|
|
F::export(&cfg).unwrap();
|
|
|
|
let expected_content = if cfg!(feature = "format") {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type F = \"VarA\" | { \"VarB\": never[] } | {\n",
|
|
" \"VarC\": {\n",
|
|
" /**\n",
|
|
" * Doc of field of variant\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
" variant_field: number;\n",
|
|
" };\n",
|
|
"};\n",
|
|
)
|
|
} else {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Doc comment.\n",
|
|
" * Supports new lines.\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"export type F = \"VarA\" | { \"VarB\": never[] } | { \"VarC\": { \n",
|
|
"/**\n",
|
|
" * Doc of field of variant\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"variant_field: number, } };",
|
|
"\n",
|
|
)
|
|
};
|
|
|
|
let actual_content = super::read_file::<F>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_g() {
|
|
let cfg = Config::from_env();
|
|
G::export(&cfg).unwrap();
|
|
|
|
let expected_content = if cfg!(feature = "format") {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"export type G =\n",
|
|
" & {\n",
|
|
" /**\n",
|
|
" * Docs\n",
|
|
" */\n",
|
|
" some_other_field: number;\n",
|
|
" }\n",
|
|
" & (\"VarA\" | { \"VarB\": never[] } | {\n",
|
|
" \"VarC\": {\n",
|
|
" /**\n",
|
|
" * Doc of field of variant\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
" variant_field: number;\n",
|
|
" };\n",
|
|
" });\n",
|
|
)
|
|
} else {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
|
|
"\n",
|
|
"export type G = { \n",
|
|
"/**\n",
|
|
" * Docs\n",
|
|
" */\n",
|
|
"some_other_field: number, } & (\"VarA\" | { \"VarB\": never[] } | { \"VarC\": { \n",
|
|
"/**\n",
|
|
" * Doc of field of variant\n",
|
|
" *\n",
|
|
" * Testing\n",
|
|
" */\n",
|
|
"variant_field: number, } });",
|
|
"\n",
|
|
)
|
|
};
|
|
|
|
let actual_content = super::read_file::<G>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_h() {
|
|
let cfg = Config::from_env();
|
|
H::export(&cfg).unwrap();
|
|
|
|
let expected_content = if cfg!(feature = "format") {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Block doc comment\n",
|
|
" *\n",
|
|
" * works\n",
|
|
" */\n",
|
|
"export type H = { foo: number };\n",
|
|
)
|
|
} else {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * Block doc comment\n",
|
|
" *\n",
|
|
" * works\n",
|
|
" */\n",
|
|
"export type H = { foo: number, };\n",
|
|
)
|
|
};
|
|
|
|
let actual_content = super::read_file::<H>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|
|
|
|
#[test]
|
|
fn export_i() {
|
|
let cfg = Config::from_env();
|
|
I::export(&cfg).unwrap();
|
|
|
|
let expected_content = if cfg!(feature = "format") {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" * line 107\n",
|
|
" */\n",
|
|
"export type I = {\n",
|
|
" /**\n",
|
|
" * column 32\n",
|
|
" */\n",
|
|
" a: number;\n",
|
|
" /**\n",
|
|
" * path integration::docs\n",
|
|
" */\n",
|
|
" b: number;\n",
|
|
"};\n",
|
|
)
|
|
} else {
|
|
concat!(
|
|
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
|
|
"/**\n",
|
|
" *line 107\n",
|
|
" */\n",
|
|
"export type I = { \n",
|
|
"/**\n",
|
|
" *column 32\n",
|
|
" */\n",
|
|
"a: number, \n",
|
|
"/**\n",
|
|
" *path integration::docs\n",
|
|
" */\n",
|
|
"b: number, };\n",
|
|
)
|
|
};
|
|
|
|
let actual_content = super::read_file::<I>(&cfg);
|
|
assert_eq!(actual_content, expected_content);
|
|
}
|