diff --git a/CHANGELOG.md b/CHANGELOG.md index e901a0513..c52086976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,11 @@ * Keeps argument names for Rust functions. +### didc + +* Breaking changes: + + The `didc test` subcommand has been removed. + ## 2025-05-15 ### Candid 0.10.14 diff --git a/rust/candid_parser/src/bindings/javascript.rs b/rust/candid_parser/src/bindings/javascript.rs index c0db60de6..823d0bb01 100644 --- a/rust/candid_parser/src/bindings/javascript.rs +++ b/rust/candid_parser/src/bindings/javascript.rs @@ -365,97 +365,3 @@ pub mod value { enclose("[", body, "]") } } - -pub mod test { - use super::value; - use crate::test::{HostAssert, HostTest, Test}; - use candid::pretty::utils::*; - use candid::TypeEnv; - use pretty::RcDoc; - - fn pp_hex(bytes: &[u8]) -> RcDoc { - str("Buffer.from('") - .append(RcDoc::as_string(hex::encode(bytes))) - .append("', 'hex')") - } - fn pp_encode<'a>(args: &'a candid::IDLArgs, tys: &'a [candid::types::Type]) -> RcDoc<'a> { - let vals = value::pp_args(args); - let tys = super::pp_rets(tys); - let items = [tys, vals]; - let params = concat(items.iter().cloned(), ","); - str("IDL.encode").append(enclose("(", params, ")")) - } - - fn pp_decode<'a>(bytes: &'a [u8], tys: &'a [candid::types::Type]) -> RcDoc<'a> { - let hex = pp_hex(bytes); - let tys = super::pp_rets(tys); - let items = [tys, hex]; - let params = concat(items.iter().cloned(), ","); - str("IDL.decode").append(enclose("(", params, ")")) - } - - pub fn test_generate(test: Test) -> String { - let header = r#"// AUTO-GENERATED. DO NOT EDIT. -// tslint:disable -import * as IDL from './idl'; -import { Buffer } from 'buffer/'; -import { Principal } from './principal'; -"#; - let mut res = header.to_string(); - let mut env = TypeEnv::new(); - crate::check_prog( - &mut env, - &crate::IDLProg { - decs: test.defs, - actor: None, - }, - ) - .unwrap(); - res += &super::compile(&env, &None); - for (i, assert) in test.asserts.iter().enumerate() { - let mut types = Vec::new(); - for ty in assert.typ.iter() { - types.push(crate::typing::ast_to_type(&env, ty).unwrap()); - } - let host = HostTest::from_assert(assert, &env, &types); - let mut expects = Vec::new(); - for cmd in host.asserts.iter() { - use HostAssert::*; - let test_func = match cmd { - Encode(args, tys, _, _) | NotEncode(args, tys) => { - let items = [super::pp_rets(tys), pp_encode(args, tys)]; - let params = concat(items.iter().cloned(), ","); - str("IDL.decode").append(enclose("(", params, ")")) - } - Decode(bytes, tys, _, _) | NotDecode(bytes, tys) => pp_decode(bytes, tys), - }; - let (test_func, predicate) = match cmd { - Encode(_, _, true, _) | Decode(_, _, true, _) => (test_func, str(".toEqual")), - Encode(_, _, false, _) | Decode(_, _, false, _) => { - (test_func, str(".not.toEqual")) - } - NotEncode(_, _) | NotDecode(_, _) => { - (str("() => ").append(test_func), str(".toThrow")) - } - }; - let expected = match cmd { - Encode(_, tys, _, bytes) => pp_decode(bytes, tys), - Decode(_, _, _, vals) => value::pp_args(vals), - NotEncode(_, _) | NotDecode(_, _) => RcDoc::nil(), - }; - let expect = enclose("expect(", test_func, ")") - .append(predicate) - .append(enclose("(", expected, ");")); - expects.push(expect); - } - let body = RcDoc::intersperse(expects.iter().cloned(), RcDoc::hardline()); - let body = str("test('") - .append(format!("{} {}", i + 1, host.desc)) - .append("', () => ") - .append(enclose_space("{", body, "});")) - .append(RcDoc::hardline()); - res += &body.pretty(LINE_WIDTH).to_string(); - } - res - } -} diff --git a/rust/candid_parser/src/grammar.lalrpop b/rust/candid_parser/src/grammar.lalrpop index 3a3f3bb05..28ec9df65 100644 --- a/rust/candid_parser/src/grammar.lalrpop +++ b/rust/candid_parser/src/grammar.lalrpop @@ -282,7 +282,7 @@ pub IDLInitArgs: IDLInitArgs = { > => IDLInitArgs { decs, args } } -// Test file +// Test file. Follows the "specification" in test/README.md Input: Input = { Text => Input::Text(<>), diff --git a/rust/candid_parser/tests/test_suite.rs b/rust/candid_parser/tests/test_suite.rs index 83a07133a..730514ed4 100644 --- a/rust/candid_parser/tests/test_suite.rs +++ b/rust/candid_parser/tests/test_suite.rs @@ -10,15 +10,3 @@ fn decode_test(resource: &str) { let ast = test.parse::().unwrap(); check(ast).unwrap(); } - -#[test_generator::test_resources("test/*.test.did")] -fn js_test(resource: &str) { - use candid_parser::bindings::javascript::test::test_generate; - let path = std::env::current_dir() - .unwrap() - .join("../../") - .join(resource); - let test = std::fs::read_to_string(path).unwrap(); - let ast = test.parse::().unwrap(); - println!("{}", test_generate(ast)); -} diff --git a/tools/didc/README.md b/tools/didc/README.md index 0140f272d..9c5d83e50 100644 --- a/tools/didc/README.md +++ b/tools/didc/README.md @@ -3,18 +3,20 @@ A multi-purpose tool for Candid. ``` -didc 0.1.0 +didc 0.4.0 USAGE: didc SUBCOMMANDS: - check Type check Candid file - bind Binding for different languages - test Generate test suites for different languages - encode Encode Candid value - decode Decode Candid binary data - random Generate random Candid values + check Type check Candid file + bind Generate binding for different languages + hash Compute the hash of a field name + encode Encode Candid value + decode Decode Candid binary data + assist Generate textual Candid values based on a terminal dialog + random Generate random Candid values + subtype Check for subtyping ``` ## TOML config diff --git a/tools/didc/src/main.rs b/tools/didc/src/main.rs index 26e0836ab..f44f90efe 100644 --- a/tools/didc/src/main.rs +++ b/tools/didc/src/main.rs @@ -41,14 +41,6 @@ enum Command { /// Specifies a subset of methods to generate bindings. Allowed format: "-m foo,bar", "-m foo bar", "-m foo -m bar" methods: Vec, }, - /// Generate test suites for different languages - Test { - /// Specifies .test.did file for test suites generation - input: PathBuf, - #[clap(short, long, value_parser = ["js", "did"], default_value = "js")] - /// Specifies target language - target: String, - }, /// Compute the hash of a field name Hash { input: String }, /// Encode Candid value @@ -262,20 +254,6 @@ fn main() -> Result<()> { }; println!("{content}"); } - Command::Test { input, target } => { - let test = std::fs::read_to_string(&input) - .map_err(|_| Error::msg(format!("could not read file {}", input.display())))?; - let ast = pretty_parse::(input.to_str().unwrap(), &test)?; - let content = match target.as_str() { - "js" => candid_parser::bindings::javascript::test::test_generate(ast), - "did" => { - candid_parser::test::check(ast)?; - "".to_string() - } - _ => unreachable!(), - }; - println!("{content}"); - } Command::Hash { input } => { println!("{}", candid_parser::idl_hash(&input)); }