Skip to content

Commit c176286

Browse files
authored
Merge pull request #15 from quake/quake/unit-test
chore: generate unit test code
2 parents f0936e1 + da22dbe commit c176286

File tree

9 files changed

+236
-6
lines changed

9 files changed

+236
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The files in this directory are used to generate code and will be removed automatically after the project template is generated.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// generated unit test for contract {{project-name}}
3+
#[test]
4+
fn test_{{crate_name}}() {
5+
// deploy contract
6+
let mut context = Context::default();
7+
let contract_bin: Bytes = Loader::default().load_binary("{{project-name}}");
8+
let out_point = context.deploy_cell(contract_bin);
9+
10+
// prepare scripts
11+
let lock_script = context
12+
.build_script(&out_point, Bytes::from(vec![42]))
13+
.expect("script");
14+
15+
// prepare cells
16+
let input_out_point = context.create_cell(
17+
CellOutput::new_builder()
18+
.capacity(1000u64.pack())
19+
.lock(lock_script.clone())
20+
.build(),
21+
Bytes::new(),
22+
);
23+
let input = CellInput::new_builder()
24+
.previous_output(input_out_point)
25+
.build();
26+
let outputs = vec![
27+
CellOutput::new_builder()
28+
.capacity(500u64.pack())
29+
.lock(lock_script.clone())
30+
.build(),
31+
CellOutput::new_builder()
32+
.capacity(500u64.pack())
33+
.lock(lock_script)
34+
.build(),
35+
];
36+
37+
let outputs_data = vec![Bytes::new(); 2];
38+
39+
// build transaction
40+
let tx = TransactionBuilder::default()
41+
.input(input)
42+
.outputs(outputs)
43+
.outputs_data(outputs_data.pack())
44+
.build();
45+
let tx = context.complete_tx(tx);
46+
47+
// run
48+
let cycles = context
49+
.verify_tx(&tx, 10_000_000)
50+
.expect("pass verification");
51+
println!("consume cycles: {}", cycles);
52+
}

contract/.cargo-generate/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The files in this directory are used to generate code and will be removed automatically after the project template is generated.

contract/.cargo-generate/tests.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// generated unit test for contract {{project-name}}
3+
#[test]
4+
fn test_{{crate_name}}() {
5+
// deploy contract
6+
let mut context = Context::default();
7+
let contract_bin: Bytes = Loader::default().load_binary("{{project-name}}");
8+
let out_point = context.deploy_cell(contract_bin);
9+
10+
// prepare scripts
11+
let lock_script = context
12+
.build_script(&out_point, Bytes::from(vec![42]))
13+
.expect("script");
14+
15+
// prepare cells
16+
let input_out_point = context.create_cell(
17+
CellOutput::new_builder()
18+
.capacity(1000u64.pack())
19+
.lock(lock_script.clone())
20+
.build(),
21+
Bytes::new(),
22+
);
23+
let input = CellInput::new_builder()
24+
.previous_output(input_out_point)
25+
.build();
26+
let outputs = vec![
27+
CellOutput::new_builder()
28+
.capacity(500u64.pack())
29+
.lock(lock_script.clone())
30+
.build(),
31+
CellOutput::new_builder()
32+
.capacity(500u64.pack())
33+
.lock(lock_script)
34+
.build(),
35+
];
36+
37+
let outputs_data = vec![Bytes::new(); 2];
38+
39+
// build transaction
40+
let tx = TransactionBuilder::default()
41+
.input(input)
42+
.outputs(outputs)
43+
.outputs_data(outputs_data.pack())
44+
.build();
45+
let tx = context.complete_tx(tx);
46+
47+
// run
48+
let cycles = context
49+
.verify_tx(&tx, 10_000_000)
50+
.expect("pass verification");
51+
println!("consume cycles: {}", cycles);
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The files in this directory are used to generate code and will be removed automatically after the project template is generated.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// generated unit test for contract {{project-name}}
3+
#[test]
4+
fn test_{{crate_name}}() {
5+
// deploy contract
6+
let mut context = Context::default();
7+
let contract_bin: Bytes = Loader::default().load_binary("{{project-name}}");
8+
let out_point = context.deploy_cell(contract_bin);
9+
10+
// prepare scripts
11+
let lock_script = context
12+
.build_script(&out_point, Bytes::from(vec![42]))
13+
.expect("script");
14+
15+
// prepare cells
16+
let input_out_point = context.create_cell(
17+
CellOutput::new_builder()
18+
.capacity(1000u64.pack())
19+
.lock(lock_script.clone())
20+
.build(),
21+
Bytes::new(),
22+
);
23+
let input = CellInput::new_builder()
24+
.previous_output(input_out_point)
25+
.build();
26+
let outputs = vec![
27+
CellOutput::new_builder()
28+
.capacity(500u64.pack())
29+
.lock(lock_script.clone())
30+
.build(),
31+
CellOutput::new_builder()
32+
.capacity(500u64.pack())
33+
.lock(lock_script)
34+
.build(),
35+
];
36+
37+
let outputs_data = vec![Bytes::new(); 2];
38+
39+
// build transaction
40+
let tx = TransactionBuilder::default()
41+
.input(input)
42+
.outputs(outputs)
43+
.outputs_data(outputs_data.pack())
44+
.build();
45+
let tx = context.complete_tx(tx);
46+
47+
// run
48+
let cycles = context
49+
.verify_tx(&tx, 10_000_000)
50+
.expect("pass verification");
51+
println!("consume cycles: {}", cycles);
52+
}
Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
1-
// Include your tests here
2-
// See https://github.com/xxuejie/ckb-native-build-sample/blob/main/tests/src/tests.rs for examples
3-
41
use super::Loader;
2+
use ckb_testtool::ckb_types::{
3+
bytes::Bytes,
4+
core::TransactionBuilder,
5+
packed::*,
6+
prelude::*,
7+
};
8+
use ckb_testtool::context::Context;
59

10+
// Include your tests here
11+
// See https://github.com/xxuejie/ckb-native-build-sample/blob/main/tests/src/tests.rs for more examples
12+
13+
// generated unit test for contract {{project-name}}
614
#[test]
7-
fn test_binary_exists() {
8-
Loader::default().load_binary("{{project-name}}");
15+
fn test_{{crate_name}}() {
16+
// deploy contract
17+
let mut context = Context::default();
18+
let contract_bin: Bytes = Loader::default().load_binary("{{project-name}}");
19+
let out_point = context.deploy_cell(contract_bin);
20+
21+
// prepare scripts
22+
let lock_script = context
23+
.build_script(&out_point, Bytes::from(vec![42]))
24+
.expect("script");
25+
26+
// prepare cells
27+
let input_out_point = context.create_cell(
28+
CellOutput::new_builder()
29+
.capacity(1000u64.pack())
30+
.lock(lock_script.clone())
31+
.build(),
32+
Bytes::new(),
33+
);
34+
let input = CellInput::new_builder()
35+
.previous_output(input_out_point)
36+
.build();
37+
let outputs = vec![
38+
CellOutput::new_builder()
39+
.capacity(500u64.pack())
40+
.lock(lock_script.clone())
41+
.build(),
42+
CellOutput::new_builder()
43+
.capacity(500u64.pack())
44+
.lock(lock_script)
45+
.build(),
46+
];
47+
48+
let outputs_data = vec![Bytes::new(); 2];
49+
50+
// build transaction
51+
let tx = TransactionBuilder::default()
52+
.input(input)
53+
.outputs(outputs)
54+
.outputs_data(outputs_data.pack())
55+
.build();
56+
let tx = context.complete_tx(tx);
57+
58+
// run
59+
let cycles = context
60+
.verify_tx(&tx, 10_000_000)
61+
.expect("pass verification");
62+
println!("consume cycles: {}", cycles);
963
}

workspace/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,20 @@ generate:
9494
cargo generate $(TEMPLATE_TYPE) $(TEMPLATE_REPO) $(TEMPLATE) \
9595
--destination $(DESTINATION); \
9696
GENERATED_DIR=$$(ls -dt $(DESTINATION)/* | head -n 1); \
97+
if [ -f "$$GENERATED_DIR/.cargo-generate/tests.rs" ]; then \
98+
cat $$GENERATED_DIR/.cargo-generate/tests.rs >> tests/src/tests.rs; \
99+
rm -rf $$GENERATED_DIR/.cargo-generate/; \
100+
fi; \
97101
sed "s,@@INSERTION_POINT@@,@@INSERTION_POINT@@\n \"$$GENERATED_DIR\"\,," Cargo.toml > Cargo.toml.new; \
98102
mv Cargo.toml.new Cargo.toml; \
99103
else \
100104
cargo generate $(TEMPLATE_TYPE) $(TEMPLATE_REPO) $(TEMPLATE) \
101105
--destination $(DESTINATION) \
102106
--name $(CRATE); \
107+
if [ -f "$(DESTINATION)/$(CRATE)/.cargo-generate/tests.rs" ]; then \
108+
cat $(DESTINATION)/$(CRATE)/.cargo-generate/tests.rs >> tests/src/tests.rs; \
109+
rm -rf $(DESTINATION)/$(CRATE)/.cargo-generate/; \
110+
fi; \
103111
sed '/@@INSERTION_POINT@@/s/$$/\n "$(DESTINATION)\/$(CRATE)",/' Cargo.toml > Cargo.toml.new; \
104112
mv Cargo.toml.new Cargo.toml; \
105113
fi

workspace/tests/src/tests.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
use crate::Loader;
2+
use ckb_testtool::ckb_types::{
3+
bytes::Bytes,
4+
core::TransactionBuilder,
5+
packed::*,
6+
prelude::*,
7+
};
8+
use ckb_testtool::context::Context;
9+
110
// Include your tests here
2-
// See https://github.com/xxuejie/ckb-native-build-sample/blob/main/tests/src/tests.rs for examples
11+
// See https://github.com/xxuejie/ckb-native-build-sample/blob/main/tests/src/tests.rs for more examples

0 commit comments

Comments
 (0)