Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
parity
Mirrored projects
ink
Commits
247b1f98
Unverified
Commit
247b1f98
authored
Mar 25, 2020
by
Ricardo Rius
Committed by
GitHub
Mar 25, 2020
Browse files
Add Example Tests (#326)
* Initial tests * Add more tests * Clean tests * Add invalid transfer test * Fix test fmt
parent
cca31543
Pipeline
#84694
passed with stages
in 6 minutes and 39 seconds
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
examples/delegator/accumulator/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
examples/delegator/adder/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
examples/delegator/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
examples/delegator/subber/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
examples/erc20/src/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
@@ -142,4 +142,146 @@ mod erc20 {
*
self
.allowances
.get
(
&
(
*
owner
,
*
spender
))
.unwrap_or
(
&
0
)
}
}
/// Unit tests.
#[cfg(test)]
mod
tests
{
/// Imports all the definitions from the outer scope so we can use them here.
use
super
::
*
;
use
ink_core
::
env
;
/// The default constructor does its job.
#[test]
fn
new_works
()
{
// Constructor works.
let
_erc20
=
Erc20
::
new
(
100
);
// Transfer event triggered during initial contruction.
assert_eq!
(
1
,
env
::
test
::
recorded_events
()
.count
());
}
/// The total supply was applied.
#[test]
fn
total_supply_works
()
{
// Constructor works.
let
erc20
=
Erc20
::
new
(
100
);
// Transfer event triggered during initial contruction.
assert_eq!
(
env
::
test
::
recorded_events
()
.count
(),
1
);
// Get the token total supply.
assert_eq!
(
erc20
.total_supply
(),
100
);
}
/// Get the actual balance of an account.
#[test]
fn
balance_of_works
()
{
// Constructor works
let
erc20
=
Erc20
::
new
(
100
);
// Transfer event triggered during initial contruction
assert_eq!
(
env
::
test
::
recorded_events
()
.count
(),
1
);
let
accounts
=
env
::
test
::
default_accounts
::
<
env
::
DefaultEnvTypes
>
()
.expect
(
"Cannot get accounts"
);
// Alice owns all the tokens on deployment
assert_eq!
(
erc20
.balance_of
(
accounts
.alice
),
100
);
// Bob does not owns tokens
assert_eq!
(
erc20
.balance_of
(
accounts
.bob
),
0
);
}
#[test]
fn
transfer_works
()
{
// Constructor works.
let
mut
erc20
=
Erc20
::
new
(
100
);
// Transfer event triggered during initial contruction.
assert_eq!
(
1
,
env
::
test
::
recorded_events
()
.count
());
let
accounts
=
env
::
test
::
default_accounts
::
<
env
::
DefaultEnvTypes
>
()
.expect
(
"Cannot get accounts"
);
assert_eq!
(
erc20
.balance_of
(
accounts
.bob
),
0
);
// Alice transfers 10 tokens to Bob.
assert_eq!
(
erc20
.transfer
(
accounts
.bob
,
10
),
true
);
// The second Transfer event takes place.
assert_eq!
(
2
,
env
::
test
::
recorded_events
()
.count
());
// Bob owns 10 tokens.
assert_eq!
(
erc20
.balance_of
(
accounts
.bob
),
10
);
}
#[test]
fn
invalid_transfer_should_fail
()
{
// Constructor works.
let
mut
erc20
=
Erc20
::
new
(
100
);
// Transfer event triggered during initial contruction.
assert_eq!
(
env
::
test
::
recorded_events
()
.count
(),
1
);
let
accounts
=
env
::
test
::
default_accounts
::
<
env
::
DefaultEnvTypes
>
()
.expect
(
"Cannot get accounts"
);
assert_eq!
(
erc20
.balance_of
(
accounts
.bob
),
0
);
// Get contract address.
let
callee
=
env
::
account_id
::
<
env
::
DefaultEnvTypes
>
()
.unwrap_or
([
0x0
;
32
]
.into
());
// Create call
let
mut
data
=
env
::
call
::
CallData
::
new
(
env
::
call
::
Selector
::
from_str
(
"balance_of"
));
data
.push_arg
(
&
accounts
.bob
);
// Push the new execution context to set Bob as caller
assert_eq!
(
env
::
test
::
push_execution_context
::
<
env
::
DefaultEnvTypes
>
(
accounts
.bob
,
callee
,
1000000
,
1000000
,
data
),
()
);
// Bob fails to transfers 10 tokens to Eve.
assert_eq!
(
erc20
.transfer
(
accounts
.eve
,
10
),
false
);
// Alice owns all the tokens.
assert_eq!
(
erc20
.balance_of
(
accounts
.alice
),
100
);
assert_eq!
(
erc20
.balance_of
(
accounts
.bob
),
0
);
assert_eq!
(
erc20
.balance_of
(
accounts
.eve
),
0
);
}
#[test]
fn
transfer_from_works
()
{
// Constructor works.
let
mut
erc20
=
Erc20
::
new
(
100
);
// Transfer event triggered during initial contruction.
assert_eq!
(
env
::
test
::
recorded_events
()
.count
(),
1
);
let
accounts
=
env
::
test
::
default_accounts
::
<
env
::
DefaultEnvTypes
>
()
.expect
(
"Cannot get accounts"
);
// Bob fails to transfer tokens owned by Alice.
assert_eq!
(
erc20
.transfer_from
(
accounts
.alice
,
accounts
.eve
,
10
),
false
);
// Alice approves Bob for token transfers on her behalf.
assert_eq!
(
erc20
.approve
(
accounts
.bob
,
10
),
true
);
// The approve event takes place.
assert_eq!
(
env
::
test
::
recorded_events
()
.count
(),
2
);
// Get contract address.
let
callee
=
env
::
account_id
::
<
env
::
DefaultEnvTypes
>
()
.unwrap_or
([
0x0
;
32
]
.into
());
// Create call.
let
mut
data
=
env
::
call
::
CallData
::
new
(
env
::
call
::
Selector
::
from_str
(
"balance_of"
));
data
.push_arg
(
&
accounts
.bob
);
// Push the new execution context to set Bob as caller.
assert_eq!
(
env
::
test
::
push_execution_context
::
<
env
::
DefaultEnvTypes
>
(
accounts
.bob
,
callee
,
1000000
,
1000000
,
data
),
()
);
// Bob transfers tokens from Alice to Eve.
assert_eq!
(
erc20
.transfer_from
(
accounts
.alice
,
accounts
.eve
,
10
),
true
);
// The third event takes place.
assert_eq!
(
env
::
test
::
recorded_events
()
.count
(),
3
);
// Eve owns tokens.
assert_eq!
(
erc20
.balance_of
(
accounts
.eve
),
10
);
}
}
}
examples/flipper/src/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
examples/incrementer/src/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
examples/runtime-storage/src/lib.rs
View file @
247b1f98
// Copyright 201
8
-20
19
Parity Technologies (UK) Ltd.
// Copyright 201
9
-20
20
Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment