mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-01-06 03:22:47 +01:00
Add some tests for snake to camel case conversion.
This commit is contained in:
@@ -13,7 +13,6 @@ readme = "../README.md"
|
||||
description = "A bincode-based transport for tarpc services."
|
||||
|
||||
[dependencies]
|
||||
bincode = "1"
|
||||
futures-preview = { version = "0.3.0-alpha.17", features = ["compat"] }
|
||||
futures_legacy = { version = "0.1", package = "futures" }
|
||||
pin-utils = "0.1.0-alpha.4"
|
||||
|
||||
@@ -19,8 +19,7 @@ serde1 = []
|
||||
travis-ci = { repository = "google/tarpc" }
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.8"
|
||||
syn = { version = "0.15", features = ["full", "extra-traits"] }
|
||||
syn = { version = "0.15", features = ["full"] }
|
||||
quote = "0.6"
|
||||
proc-macro2 = "0.4"
|
||||
|
||||
@@ -28,15 +27,6 @@ proc-macro2 = "0.4"
|
||||
proc-macro = true
|
||||
|
||||
[dev-dependencies]
|
||||
bincode = "1"
|
||||
bincode-transport = { package = "tarpc-bincode-transport", version = "0.7", path = "../bincode-transport" }
|
||||
bytes = { version = "0.4", features = ["serde"] }
|
||||
futures-preview = { version = "0.3.0-alpha.17", features = ["compat"] }
|
||||
humantime = "1.0"
|
||||
env_logger = "0.6"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
tarpc = { path = "../tarpc" }
|
||||
tokio = "0.1"
|
||||
tokio-executor = "0.1"
|
||||
tokio-tcp = "0.1"
|
||||
pin-utils = "0.1.0-alpha.4"
|
||||
|
||||
@@ -6,13 +6,11 @@
|
||||
|
||||
#![recursion_limit = "512"]
|
||||
|
||||
extern crate itertools;
|
||||
extern crate proc_macro;
|
||||
extern crate proc_macro2;
|
||||
extern crate quote;
|
||||
extern crate syn;
|
||||
|
||||
use itertools::Itertools;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
@@ -114,6 +112,13 @@ impl Parse for RpcMethod {
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates:
|
||||
/// - service trait
|
||||
/// - serve fn
|
||||
/// - client stub struct
|
||||
/// - new_stub client factory fn
|
||||
/// - Request and Response enums
|
||||
/// - ResponseFut Future
|
||||
#[proc_macro_attribute]
|
||||
pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
struct EmptyArgs;
|
||||
@@ -133,7 +138,7 @@ pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
|
||||
let camel_case_fn_names: Vec<String> = rpcs
|
||||
.iter()
|
||||
.map(|rpc| convert_str(&rpc.ident.to_string()))
|
||||
.map(|rpc| snake_to_camel(&rpc.ident.to_string()))
|
||||
.collect();
|
||||
let ref outputs: Vec<TokenStream2> = rpcs
|
||||
.iter()
|
||||
@@ -310,33 +315,46 @@ pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
tokens.into()
|
||||
}
|
||||
|
||||
fn convert_str(ident_str: &str) -> String {
|
||||
fn snake_to_camel(ident_str: &str) -> String {
|
||||
let mut camel_ty = String::new();
|
||||
|
||||
// Find the first non-underscore and add it capitalized.
|
||||
let mut chars = ident_str.chars();
|
||||
|
||||
// Find the first non-underscore char, uppercase it, and append it.
|
||||
// Guaranteed to succeed because all idents must have at least one non-underscore char.
|
||||
camel_ty.extend(chars.find(|&c| c != '_').unwrap().to_uppercase());
|
||||
|
||||
// When we find an underscore, we remove it and capitalize the next char. To do this,
|
||||
// we need to ensure the next char is not another underscore.
|
||||
let mut chars = chars.coalesce(|c1, c2| {
|
||||
if c1 == '_' && c2 == '_' {
|
||||
Ok(c1)
|
||||
} else {
|
||||
Err((c1, c2))
|
||||
}
|
||||
});
|
||||
|
||||
let mut last_char_was_underscore = true;
|
||||
while let Some(c) = chars.next() {
|
||||
if c != '_' {
|
||||
camel_ty.push(c);
|
||||
} else if let Some(c) = chars.next() {
|
||||
camel_ty.extend(c.to_uppercase());
|
||||
match c {
|
||||
'_' => last_char_was_underscore = true,
|
||||
c if last_char_was_underscore => {
|
||||
camel_ty.extend(c.to_uppercase());
|
||||
last_char_was_underscore = false;
|
||||
}
|
||||
c => camel_ty.extend(c.to_lowercase()),
|
||||
}
|
||||
}
|
||||
|
||||
camel_ty
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snake_to_camel_basic() {
|
||||
assert_eq!(snake_to_camel("abc_def"), "AbcDef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snake_to_camel_underscore_suffix() {
|
||||
assert_eq!(snake_to_camel("abc_def_"), "AbcDef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snake_to_camel_underscore_prefix() {
|
||||
assert_eq!(snake_to_camel("_abc_def"), "AbcDef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snake_to_camel_underscore_consecutive() {
|
||||
assert_eq!(snake_to_camel("abc__def"), "AbcDef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snake_to_camel_capital_in_middle() {
|
||||
assert_eq!(snake_to_camel("aBc_dEf"), "AbcDef");
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use tarpc::context;
|
||||
fn att_service_trait() {
|
||||
use futures::future::{ready, Ready};
|
||||
|
||||
#[tarpc_plugins::service]
|
||||
#[tarpc::service]
|
||||
trait Foo {
|
||||
async fn two_part(s: String, i: i32) -> (String, i32);
|
||||
async fn bar(s: String) -> String;
|
||||
@@ -33,7 +33,7 @@ fn att_service_trait() {
|
||||
|
||||
#[test]
|
||||
fn syntax() {
|
||||
#[tarpc_plugins::service]
|
||||
#[tarpc::service]
|
||||
trait Syntax {
|
||||
#[deny(warnings)]
|
||||
#[allow(non_snake_case)]
|
||||
|
||||
Reference in New Issue
Block a user