Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 0 additions & 94 deletions rust/candid_parser/src/bindings/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion rust/candid_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub IDLInitArgs: IDLInitArgs = {
<decs:SepBy<Def, ";">> <args:ArgTupTyp> => IDLInitArgs { decs, args }
}

// Test file
// Test file. Follows the "specification" in test/README.md

Input: Input = {
Text => Input::Text(<>),
Expand Down
12 changes: 0 additions & 12 deletions rust/candid_parser/tests/test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,3 @@ fn decode_test(resource: &str) {
let ast = test.parse::<Test>().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::<Test>().unwrap();
println!("{}", test_generate(ast));
}
16 changes: 9 additions & 7 deletions tools/didc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
A multi-purpose tool for Candid.

```
didc 0.1.0
didc 0.4.0

USAGE:
didc <SUBCOMMAND>

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
Expand Down
22 changes: 0 additions & 22 deletions tools/didc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
},
/// 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
Expand Down Expand Up @@ -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::<candid_parser::test::Test>(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));
}
Expand Down