This repository was archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.rs
More file actions
101 lines (86 loc) · 3.62 KB
/
unit_tests.rs
File metadata and controls
101 lines (86 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#[cfg(test)]
mod hello_tests {
use scrypto::math::Decimal;
use sqrt::blueprint::{AdminBadge, Blueprint};
use sqrt::method::{Arg, Method};
use sqrt::method_args;
use sqrt::package::Package;
use sqrt::test_environment::TestEnvironment;
// To define a Blueprint, we need to implement the Blueprint Trait for some object.
// We therefore define an empty struct for which we will implement the Blueprint Trait.
struct HelloBp {}
impl Blueprint for HelloBp {
// A new "Hello" blueprint is instantiated from the "instantiate_hello" method
fn instantiation_name(&self) -> &str {
"instantiate_hello"
}
// The name of the blueprint is indeed "Hello"
fn name(&self) -> &str {
"Hello"
}
// The "Hello" blueprint does not use an admin badge
fn has_admin_badge(&self) -> AdminBadge {
AdminBadge::None
}
}
// To test methods of a blueprint, we need to implement the Method Trait. The best way of doing so
// is defining an enum which variants are the blueprint's methods and then implement the Method Trait
// for it.
// In this case, the blueprint "Hello" has only one method with no argument so we create an enum
// with only one variant which has no arguments
enum HelloMethods {
FreeToken,
}
// We can now implement the Method trait for our enum
impl Method for HelloMethods {
fn name(&self) -> &str {
match self {
HelloMethods::FreeToken => "free_token",
}
}
// We have no method that needs an argument so we always return `method_args![]` with no args
fn args(&self) -> Option<Vec<Arg>> {
method_args![]
}
// None of our methods requires the Component's admin badge to be called, so we always return false
fn needs_admin_badge(&self) -> bool {
false
}
// None of our methods require a custom name
fn custom_manifest_name(&self) -> Option<&str> {
None
}
}
#[test]
fn test_publish() {
let mut test_env = TestEnvironment::new();
let hello_blueprint = Box::new(HelloBp {});
let mut hello_package = Package::new("tests/hello_token/package/");
hello_package.add_blueprint("hello", hello_blueprint);
test_env.publish_package("hello", hello_package);
}
#[test]
fn test_instantiate() {
let mut test_env = TestEnvironment::new();
let hello_blueprint = Box::new(HelloBp {});
let mut hello_package = Package::new("tests/hello_token/package/");
hello_package.add_blueprint("hello", hello_blueprint);
test_env.publish_package("hello", hello_package);
test_env.new_component("hello_comp", "hello", vec![]);
// We check that the new tokens have indeed been recognized by the TestEnvironment
test_env.get_resource("HelloToken");
test_env.get_resource("test");
}
#[test]
fn test_free_token() {
let mut test_env = TestEnvironment::new();
let hello_blueprint = Box::new(HelloBp {});
let mut hello_package = Package::new("tests/hello_token/package/");
hello_package.add_blueprint("hello", hello_blueprint);
test_env.publish_package("hello", hello_package);
test_env.new_component("hello_comp", "hello", vec![]);
test_env.call_method(HelloMethods::FreeToken).run();
// We check that we indeed received 1 HelloToken after having called the FreeToken function
assert_eq!(test_env.amount_owned_by_current("HelloToken"), Decimal::ONE);
}
}