|
1 | | -import { UInt8, UInt64 } from '../src/types' |
| 1 | +import { UInt8, UInt16, UInt32, UInt64 } from '../src/types' |
2 | 2 | import { encode, decode } from '../src' |
3 | 3 |
|
4 | 4 | const binary = |
@@ -195,3 +195,109 @@ it('UInt64 is parsed as base 10 for MPT amounts', () => { |
195 | 195 | expect(typeof decodedToken.MPTAmount).toBe('string') |
196 | 196 | expect(decodedToken.MPTAmount).toBe('100') |
197 | 197 | }) |
| 198 | + |
| 199 | +describe('UInt decimal validation', () => { |
| 200 | + describe('UInt8', () => { |
| 201 | + it('should throw error when passed a decimal number', () => { |
| 202 | + expect(() => UInt8.from(1.5)).toThrow( |
| 203 | + new Error('Cannot construct UInt8 from given value'), |
| 204 | + ) |
| 205 | + }) |
| 206 | + |
| 207 | + it('should throw error when passed a negative decimal', () => { |
| 208 | + expect(() => UInt8.from(-1.5)).toThrow( |
| 209 | + new Error('Cannot construct UInt8 from given value'), |
| 210 | + ) |
| 211 | + }) |
| 212 | + |
| 213 | + it('should throw error when passed a small decimal', () => { |
| 214 | + expect(() => UInt8.from(0.1)).toThrow( |
| 215 | + new Error('Cannot construct UInt8 from given value'), |
| 216 | + ) |
| 217 | + }) |
| 218 | + |
| 219 | + it('should accept valid integer values', () => { |
| 220 | + expect(() => UInt8.from(0)).not.toThrow() |
| 221 | + expect(() => UInt8.from(1)).not.toThrow() |
| 222 | + expect(() => UInt8.from(255)).not.toThrow() |
| 223 | + }) |
| 224 | + }) |
| 225 | + |
| 226 | + describe('UInt16', () => { |
| 227 | + it('should throw error when passed a decimal number', () => { |
| 228 | + expect(() => UInt16.from(100.5)).toThrow( |
| 229 | + new Error('Cannot construct UInt16 from given value'), |
| 230 | + ) |
| 231 | + }) |
| 232 | + |
| 233 | + it('should throw error when passed a negative decimal', () => { |
| 234 | + expect(() => UInt16.from(-100.5)).toThrow( |
| 235 | + new Error('Cannot construct UInt16 from given value'), |
| 236 | + ) |
| 237 | + }) |
| 238 | + |
| 239 | + it('should throw error when passed a small decimal', () => { |
| 240 | + expect(() => UInt16.from(0.001)).toThrow( |
| 241 | + new Error('Cannot construct UInt16 from given value'), |
| 242 | + ) |
| 243 | + }) |
| 244 | + |
| 245 | + it('should accept valid integer values', () => { |
| 246 | + expect(() => UInt16.from(0)).not.toThrow() |
| 247 | + expect(() => UInt16.from(1000)).not.toThrow() |
| 248 | + expect(() => UInt16.from(65535)).not.toThrow() |
| 249 | + }) |
| 250 | + }) |
| 251 | + |
| 252 | + describe('UInt32', () => { |
| 253 | + it('should throw error when passed a decimal number', () => { |
| 254 | + expect(() => UInt32.from(1000.5)).toThrow( |
| 255 | + new Error('Cannot construct UInt32 from given value'), |
| 256 | + ) |
| 257 | + }) |
| 258 | + |
| 259 | + it('should throw error when passed a negative decimal', () => { |
| 260 | + expect(() => UInt32.from(-1000.5)).toThrow( |
| 261 | + new Error('Cannot construct UInt32 from given value'), |
| 262 | + ) |
| 263 | + }) |
| 264 | + |
| 265 | + it('should throw error when passed a small decimal', () => { |
| 266 | + expect(() => UInt32.from(0.0001)).toThrow( |
| 267 | + new Error('Cannot construct UInt32 from given value'), |
| 268 | + ) |
| 269 | + }) |
| 270 | + |
| 271 | + it('should accept valid integer values', () => { |
| 272 | + expect(() => UInt32.from(0)).not.toThrow() |
| 273 | + expect(() => UInt32.from(100000)).not.toThrow() |
| 274 | + expect(() => UInt32.from(4294967295)).not.toThrow() |
| 275 | + }) |
| 276 | + }) |
| 277 | + |
| 278 | + describe('UInt64', () => { |
| 279 | + it('should throw error when passed a decimal number', () => { |
| 280 | + expect(() => UInt64.from(10000.5)).toThrow( |
| 281 | + new Error('Cannot construct UInt64 from given value'), |
| 282 | + ) |
| 283 | + }) |
| 284 | + |
| 285 | + it('should throw error when passed a negative decimal', () => { |
| 286 | + expect(() => UInt64.from(-10000.5)).toThrow( |
| 287 | + new Error('Cannot construct UInt64 from given value'), |
| 288 | + ) |
| 289 | + }) |
| 290 | + |
| 291 | + it('should throw error when passed a small decimal', () => { |
| 292 | + expect(() => UInt64.from(0.00001)).toThrow( |
| 293 | + new Error('Cannot construct UInt64 from given value'), |
| 294 | + ) |
| 295 | + }) |
| 296 | + |
| 297 | + it('should accept valid integer values', () => { |
| 298 | + expect(() => UInt64.from(0)).not.toThrow() |
| 299 | + expect(() => UInt64.from(1000000)).not.toThrow() |
| 300 | + expect(() => UInt64.from(BigInt('9223372036854775807'))).not.toThrow() |
| 301 | + }) |
| 302 | + }) |
| 303 | +}) |
0 commit comments