Skip to content

Commit

Permalink
[Codegen]: Generate TWCoinTypeTests.cppp file
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Nov 23, 2023
1 parent 1cca1b1 commit c177cd0
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 17 deletions.
2 changes: 1 addition & 1 deletion codegen-v2/src/codegen/cpp/coin_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl BlockchainImpl {
TemplateGenerator::new(ENTRY_HEADER_TEMPLATE)
.write_to(entry_header_path.clone())
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

Ok(entry_header_path)
}
Expand Down
14 changes: 14 additions & 0 deletions codegen-v2/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::registry::CoinItem;
use std::env;
use std::path::PathBuf;

Expand All @@ -12,6 +13,7 @@ pub mod coin_entry;
pub mod new_blockchain;
pub mod tw_blockchain;
pub mod tw_coin_type;
pub mod tw_coin_type_tests_generator;

pub fn cpp_source_directory() -> PathBuf {
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
Expand All @@ -25,3 +27,15 @@ pub fn cpp_include_directory() -> PathBuf {
.join("include")
.join("TrustWalletCore")
}

pub fn integration_tests_directory() -> PathBuf {
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..")
.join("tests")
}

pub fn coin_integration_tests_directory(coin: &CoinItem) -> PathBuf {
integration_tests_directory()
.join("chains")
.join(coin.coin_type())
}
6 changes: 5 additions & 1 deletion codegen-v2/src/codegen/cpp/new_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::codegen::cpp::blockchain_registry::BlockchainDispatcher;
use crate::codegen::cpp::coin_entry::BlockchainImpl;
use crate::codegen::cpp::tw_blockchain::TWBlockchain;
use crate::codegen::cpp::tw_coin_type::TWCoinType;
use crate::codegen::cpp::tw_coin_type_tests_generator::TWCoinTypeTestsGenerator;
use crate::registry::CoinItem;
use crate::Result;

Expand All @@ -20,7 +21,10 @@ pub fn new_blockchain(coin: CoinItem) -> Result<()> {
// Add the new blockchain type to the `TWBlockchain` enum.
TWBlockchain::new(coin.clone()).add_blockchain_type_variant()?;
// Add the blockchain entry to the dispatcher `Coin.cpp`.
BlockchainDispatcher::new(coin).add()?;
BlockchainDispatcher::new(coin.clone()).add()?;

// Add integration tests.
TWCoinTypeTestsGenerator::new(coin).generate()?;

Ok(())
}
31 changes: 31 additions & 0 deletions codegen-v2/src/codegen/cpp/templates/TWCoinTypeTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2017-{YEAR} Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

#include "TestUtilities.h"
#include <TrustWalletCore/TWCoinTypeConfiguration.h>
#include <gtest/gtest.h>

TEST(TW{COIN_TYPE}CoinType, TWCoinType) {
const auto coin = TWCoinType{COIN_TYPE};
const auto symbol = WRAPS(TWCoinTypeConfigurationGetSymbol(coin));
const auto id = WRAPS(TWCoinTypeConfigurationGetID(coin));
const auto name = WRAPS(TWCoinTypeConfigurationGetName(coin));
const auto txId = WRAPS(TWStringCreateWithUTF8Bytes("{EXPLORER_SAMPLE_TX}"));
const auto txUrl = WRAPS(TWCoinTypeConfigurationGetTransactionURL(coin, txId.get()));
const auto accId = WRAPS(TWStringCreateWithUTF8Bytes("{EXPLORER_SAMPLE_ACCOUNT}"));
const auto accUrl = WRAPS(TWCoinTypeConfigurationGetAccountURL(coin, accId.get()));

assertStringsEqual(id, "{COIN_ID}");
assertStringsEqual(name, "{COIN_TYPE}");
assertStringsEqual(symbol, "{SYMBOL}");
ASSERT_EQ(TWCoinTypeConfigurationGetDecimals(coin), {DECIMALS});
ASSERT_EQ(TWCoinTypeBlockchain(coin), TWBlockchain{BLOCKCHAIN});
ASSERT_EQ(TWCoinTypeP2pkhPrefix(coin), {P2PKH_PREFIX});
ASSERT_EQ(TWCoinTypeP2shPrefix(coin), {P2SH_PREFIX});
ASSERT_EQ(TWCoinTypeStaticPrefix(coin), {STATIC_PREFIX});
assertStringsEqual(txUrl, "{EXPLORER_URL}{EXPLORER_TX_PATH}{EXPLORER_SAMPLE_TX}");
assertStringsEqual(accUrl, "{EXPLORER_URL}{EXPLORER_ACCOUNT_PATH}{EXPLORER_SAMPLE_ACCOUNT}");
}
48 changes: 48 additions & 0 deletions codegen-v2/src/codegen/cpp/tw_coin_type_tests_generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::codegen::cpp::coin_integration_tests_directory;
use crate::codegen::template_generator::TemplateGenerator;
use crate::registry::CoinItem;
use crate::Result;
use std::fs;
use std::path::PathBuf;

const TW_COIN_TYPE_TESTS_TEMPLATE: &str = include_str!("templates/TWCoinTypeTests.cpp");

pub fn tw_coin_type_tests_path(coin: &CoinItem) -> PathBuf {
coin_integration_tests_directory(coin).join("TWCoinTypeTests.cpp")
}

pub struct TWCoinTypeTestsGenerator {
coin: CoinItem,
}

impl TWCoinTypeTestsGenerator {
pub fn new(coin: CoinItem) -> TWCoinTypeTestsGenerator {
TWCoinTypeTestsGenerator { coin }
}

pub fn generate(self) -> Result<()> {
let coin_tests_dir = coin_integration_tests_directory(&self.coin);
let tw_coin_type_tests_path = coin_tests_dir.join("TWCoinTypeTests.cpp");

if !coin_tests_dir.exists() {
fs::create_dir(coin_tests_dir)?;
}

if tw_coin_type_tests_path.exists() {
return Ok(());
}

TemplateGenerator::new(TW_COIN_TYPE_TESTS_TEMPLATE)
.write_to(tw_coin_type_tests_path)
.with_default_patterns(&self.coin)
.write()?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion codegen-v2/src/codegen/proto/proto_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl ProtoGenerator {
TemplateGenerator::new(PROTO_TEMPLATE)
.write_to(proto_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions codegen-v2/src/codegen/rust/coin_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,32 @@ impl CoinCrate {
TemplateGenerator::new(BLOCKCHAIN_MANIFEST_TEMPLATE)
.write_to(blockchain_toml_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

TemplateGenerator::new(BLOCKCHAIN_LIB_TEMPLATE)
.write_to(blockchain_lib_rs_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

TemplateGenerator::new(BLOCKCHAIN_ENTRY_TEMPLATE)
.write_to(blockchain_entry_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

TemplateGenerator::new(BLOCKCHAIN_COMPILER_TEMPLATE)
.write_to(blockchain_compiler_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

TemplateGenerator::new(BLOCKCHAIN_ADDRESS_TEMPLATE)
.write_to(blockchain_address_rs_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

TemplateGenerator::new(BLOCKCHAIN_SIGNER_TEMPLATE)
.write_to(blockchain_signer_rs_path)
.with_default_patterns(&self.coin)
.replace_all()?;
.write()?;

Ok(blockchain_path)
}
Expand Down
8 changes: 4 additions & 4 deletions codegen-v2/src/codegen/rust/coin_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl CoinIntegrationTests {
TemplateGenerator::new(ADDRESS_TESTS_TEMPLATE)
.write_to(address_tests_path)
.with_default_patterns(&self.coin)
.replace_all()
.write()
}

fn create_compile_tests(&self) -> Result<()> {
Expand All @@ -101,7 +101,7 @@ impl CoinIntegrationTests {
TemplateGenerator::new(COMPILE_TESTS_TEMPLATE)
.write_to(compile_tests_path)
.with_default_patterns(&self.coin)
.replace_all()
.write()
}

fn create_sign_tests(&self) -> Result<()> {
Expand All @@ -113,7 +113,7 @@ impl CoinIntegrationTests {
TemplateGenerator::new(SIGN_TESTS_TEMPLATE)
.write_to(sign_tests_path)
.with_default_patterns(&self.coin)
.replace_all()
.write()
}

fn create_chain_tests_mod_rs(&self) -> Result<()> {
Expand All @@ -122,7 +122,7 @@ impl CoinIntegrationTests {
TemplateGenerator::new(MOD_TESTS_TEMPLATE)
.write_to(blockchain_tests_mod_path)
.with_default_patterns(&self.coin)
.replace_all()
.write()
}

fn list_blockchain_in_chains_mod(&self) -> Result<()> {
Expand Down
18 changes: 15 additions & 3 deletions codegen-v2/src/codegen/template_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{current_year, Error, Result};
use std::fs;
use std::path::PathBuf;

const PATTERNS_CAPACITY: usize = 20;

pub struct TemplateGenerator {
template_content: &'static str,
write_to: Option<PathBuf>,
Expand All @@ -21,8 +23,8 @@ impl TemplateGenerator {
TemplateGenerator {
template_content,
write_to: None,
to_replace: Vec::default(),
replace_with: Vec::default(),
to_replace: Vec::with_capacity(PATTERNS_CAPACITY),
replace_with: Vec::with_capacity(PATTERNS_CAPACITY),
}
}

Expand All @@ -38,6 +40,16 @@ impl TemplateGenerator {
.add_pattern("{TW_CRATE_NAME}", coin.id.to_tw_crate_name())
.add_pattern("{COIN_ID}", coin.id.as_str())
.add_pattern("{COIN_TYPE}", coin.coin_type())
.add_pattern("{SYMBOL}", &coin.symbol)
.add_pattern("{DECIMALS}", coin.decimals)
.add_pattern("{P2PKH_PREFIX}", coin.p2pkh_prefix)
.add_pattern("{P2SH_PREFIX}", coin.p2sh_prefix)
.add_pattern("{STATIC_PREFIX}", coin.static_prefix)
.add_pattern("{EXPLORER_URL}", &coin.explorer.url)
.add_pattern("{EXPLORER_TX_PATH}", &coin.explorer.tx_path)
.add_pattern("{EXPLORER_ACCOUNT_PATH}", &coin.explorer.account_path)
.add_pattern("{EXPLORER_SAMPLE_TX}", &coin.explorer.sample_tx)
.add_pattern("{EXPLORER_SAMPLE_ACCOUNT}", &coin.explorer.sample_account)
}

pub fn add_pattern<K: ToString, V: ToString>(
Expand All @@ -50,7 +62,7 @@ impl TemplateGenerator {
self
}

pub fn replace_all(self) -> Result<()> {
pub fn write(self) -> Result<()> {
let write_to_path = self.write_to.ok_or_else(|| {
Error::io_error_other("Incorrect use of 'TemplateGenerator'".to_string())
})?;
Expand Down
29 changes: 28 additions & 1 deletion codegen-v2/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,40 @@ pub fn registry_json_path() -> PathBuf {
.join("registry.json")
}

#[derive(Clone, Deserialize)]
pub struct CoinExplorer {
pub url: String,
#[serde(rename = "txPath")]
pub tx_path: String,
#[serde(rename = "accountPath")]
pub account_path: String,
#[serde(rename = "sampleTx")]
#[serde(default)]
pub sample_tx: String,
#[serde(rename = "sampleAccount")]
#[serde(default)]
pub sample_account: String,
}

#[derive(Clone, Deserialize)]
pub struct CoinItem {
pub id: CoinId,
pub name: String,
#[serde(rename = "coinId")]
pub coin_id_number: u32,
pub name: String,
pub symbol: String,
pub decimals: u8,
pub blockchain: String,
#[serde(rename = "p2pkhPrefix")]
#[serde(default)]
pub p2pkh_prefix: u8,
#[serde(rename = "p2shPrefix")]
#[serde(default)]
pub p2sh_prefix: u8,
#[serde(rename = "staticPrefix")]
#[serde(default)]
pub static_prefix: u8,
pub explorer: CoinExplorer,
}

impl CoinItem {
Expand Down

0 comments on commit c177cd0

Please sign in to comment.