Skip to content

Commit 6ef43bb

Browse files
committed
Add simple test cases for Context
1 parent 300d0e7 commit 6ef43bb

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

unittest/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ java {
1111

1212
dependencies {
1313
compileOnly 'org.bouncycastle:bcprov-jdk15on:1.60'
14+
15+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
16+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
17+
testRuntimeOnly 'org.bouncycastle:bcprov-jdk15on:1.60'
18+
}
19+
20+
test {
21+
useJUnitPlatform()
1422
}
1523

1624
task sourcesJar(type: Jar, dependsOn: classes) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2021 ICONLOOP Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package score;
18+
19+
import com.iconloop.score.test.Account;
20+
import com.iconloop.score.test.Score;
21+
import com.iconloop.score.test.ServiceManager;
22+
import com.iconloop.score.test.TestBase;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import score.impl.Crypto;
26+
27+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
class ContextTest extends TestBase {
31+
private static final ServiceManager sm = getServiceManager();
32+
private static final Account owner = sm.createAccount();
33+
private static Score helloScore;
34+
35+
@BeforeEach
36+
void setUp() throws Exception {
37+
helloScore = sm.deploy(owner, HelloWorld.class, "Alice");
38+
}
39+
40+
@Test
41+
void getAddress() {
42+
assertEquals(helloScore.getAddress(), helloScore.call("getAddress"));
43+
}
44+
45+
@Test
46+
void getOwner() {
47+
assertEquals(owner.getAddress(), helloScore.call("getOwner"));
48+
}
49+
50+
@Test
51+
void hash() {
52+
byte[] data = "Hello world".getBytes();
53+
assertArrayEquals(Crypto.hash("sha3-256", data),
54+
(byte[]) helloScore.call("computeHash", "sha3-256", data));
55+
}
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2021 ICONLOOP Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package score;
18+
19+
import score.annotation.External;
20+
21+
public class HelloWorld {
22+
private final String name;
23+
24+
public HelloWorld(String name) {
25+
this.name = name;
26+
}
27+
28+
@External(readonly=true)
29+
public String name() {
30+
return name;
31+
}
32+
33+
@External(readonly=true)
34+
public Address getAddress() {
35+
return Context.getAddress();
36+
}
37+
38+
@External(readonly=true)
39+
public Address getOwner() {
40+
return Context.getOwner();
41+
}
42+
43+
@External(readonly=true)
44+
public byte[] computeHash(String algorithm, byte[] data) {
45+
if ("sha3-256".equals(algorithm)) {
46+
return Context.hash(algorithm, data);
47+
}
48+
return null;
49+
}
50+
}

0 commit comments

Comments
 (0)