#![allow(unused)]
use typescript_definitions::{TypeScriptify, TypeScriptifyTrait, TypescriptDefinition};
use serde_derive::*;
use serde::Serialize;
use quote::quote;
use proc_macro2::TokenStream;
use std::borrow::Cow;
mod patch;
use patch::{patch, patcht};
use wasm_bindgen::prelude::*;
fn type_scriptify_fields() {
#[derive(Serialize, TypeScriptify)]
struct S {
a: i32,
b: f64,
c: String,
d: Vec<String>,
}
}
fn type_scriptify_generic_fields() {
#[derive(Serialize, TypeScriptify)]
struct S<'a, T> {
a: i32,
b: f64,
c: String,
#[serde(rename = "X")]
d: Vec<String>,
e: &'a T,
}
}
fn type_scriptify_flatten() {
#[derive(Serialize, TypeScriptify)]
struct DDD {
e: i32,
f: f64,
}
#[derive(Serialize, TypeScriptify)]
struct SSS {
a: i32,
b: f64,
c: DDD,
}
}
#[test]
fn as_byte_string() {
use serde_json;
#[derive(Serialize, TypeScriptify)]
struct S {
#[serde(serialize_with="typescript_definitions::as_byte_string")]
image : Vec<u8>
}
let s = S { image: vec![1,2,3,4,5]};
assert_eq!(serde_json::to_string(&s).unwrap(), "{\"image\":\"\\\\x01\\\\x02\\\\x03\\\\x04\\\\x05\"}");
}
#[test]
fn untagged_enum() {
use serde_json;
#[derive(Serialize, TypeScriptify)]
#[serde(untagged)]
enum Untagged {
V1 { id: i32, attr : String} ,
V2 { id: i32, attr2: Vec<String> }
}
assert_eq!(Untagged::type_script_ify().replace("\n ",""), patcht(quote!{
export type Untagged = {id: number, attr: string} | {id: number, attr2 : string[] };
}));
}
#[test]
fn external_enum() {
use serde_json;
#[derive(Serialize, TypeScriptify)]
enum External {
V1 { id: i32, attr : String} ,
V2 { id: i32, attr2: Vec<String> }
}
assert_eq!(External::type_script_ify().replace("\n ",""), patcht(quote!{
export type External = { V1: {id: number, attr: string} } | { V2: {id: number, attr2 : string[] }};
}));
}