|
| 1 | +/* |
| 2 | + * Copyright 2023 PARAMETA Corp. |
| 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 com.iconloop.score.test; |
| 18 | + |
| 19 | +import score.Address; |
| 20 | +import score.impl.TypeConverter; |
| 21 | + |
| 22 | +public class Event { |
| 23 | + private final Address contract; |
| 24 | + private final Object[] indexed; |
| 25 | + private final Object[] data; |
| 26 | + |
| 27 | + public static final Object[] EMPTY = new Object[0]; |
| 28 | + |
| 29 | + public Event(Address contract, Object[] indexed, Object[] data) { |
| 30 | + if (contract != null && !contract.isContract()) { |
| 31 | + throw new IllegalArgumentException("invalid contract address"); |
| 32 | + } |
| 33 | + if (indexed == null || indexed.length < 1 || indexed.length > 4) { |
| 34 | + throw new IllegalArgumentException("invalid indexed parameter: length=" + |
| 35 | + ((indexed != null) ? indexed.length : "null")); |
| 36 | + } |
| 37 | + this.contract = contract; |
| 38 | + this.indexed = indexed; |
| 39 | + this.data = data == null ? EMPTY : data; |
| 40 | + for (var obj : this.indexed) { |
| 41 | + if (!TypeConverter.isValidEventValue(obj)) { |
| 42 | + throw new IllegalArgumentException("invalid value type " + obj.getClass()); |
| 43 | + } |
| 44 | + } |
| 45 | + for (var obj : this.data) { |
| 46 | + if (!TypeConverter.isValidEventValue(obj)) { |
| 47 | + throw new IllegalArgumentException("invalid value type " + obj.getClass()); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public int numberOfIndexed() { |
| 53 | + return indexed.length; |
| 54 | + } |
| 55 | + |
| 56 | + public int numberOfData() { |
| 57 | + return data.length; |
| 58 | + } |
| 59 | + |
| 60 | + public Address getContract() { |
| 61 | + return this.contract; |
| 62 | + } |
| 63 | + |
| 64 | + public Object getIndexed(int idx) { |
| 65 | + return this.indexed[idx]; |
| 66 | + } |
| 67 | + public Object getData(int idx) { |
| 68 | + return this.data[idx]; |
| 69 | + } |
| 70 | + |
| 71 | + private boolean match(Event log, boolean ignoreNull) { |
| 72 | + if (this == log) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + if (indexed.length != log.indexed.length |
| 77 | + || data.length != log.data.length |
| 78 | + ) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (contract != null) { |
| 82 | + if (!contract.equals(log.contract)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (!ignoreNull && log.contract != null) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + for (int i=0 ; i<indexed.length ; i++) { |
| 91 | + var v1 = indexed[i]; |
| 92 | + if (v1==null && ignoreNull) continue; |
| 93 | + var v2 = log.indexed[i]; |
| 94 | + var mo = TypeConverter.toBytes(v1); |
| 95 | + var mv = TypeConverter.toBytes(v2); |
| 96 | + if (!java.util.Arrays.equals(mo, mv)) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + for (int i=0 ; i<data.length ; i++) { |
| 101 | + var v1 = data[i]; |
| 102 | + if (v1==null && ignoreNull) continue; |
| 103 | + var v2 = log.data[i]; |
| 104 | + var mo = TypeConverter.toBytes(v1); |
| 105 | + var mv = TypeConverter.toBytes(v2); |
| 106 | + if (!java.util.Arrays.equals(mo, mv)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + public boolean match(Event log) { |
| 114 | + return this.match(log, true); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean equals(Object log) { |
| 119 | + if (log == null || this.getClass() != log.getClass()) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + return this.match((Event)log, false); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return "Event(caller="+contract |
| 128 | + +",indexed="+java.util.Arrays.toString(indexed) |
| 129 | + +",data="+java.util.Arrays.toString(data)+")"; |
| 130 | + } |
| 131 | +} |
0 commit comments