|
| 1 | +/// Test module for struct and enum transaction arguments in CLI. |
| 2 | +/// |
| 3 | +/// This module contains test types and entry functions for testing the CLI's |
| 4 | +/// ability to parse and pass struct/enum arguments to Move entry functions. |
| 5 | +module struct_enum_tests::struct_enum_tests { |
| 6 | + use std::option::{Self, Option}; |
| 7 | + use std::string::String; |
| 8 | + use std::string::Self; |
| 9 | + |
| 10 | + // Test structs for struct transaction arguments |
| 11 | + |
| 12 | + /// A simple public struct with copy ability for testing struct arguments |
| 13 | + public struct Point has copy, drop { |
| 14 | + x: u64, |
| 15 | + y: u64, |
| 16 | + } |
| 17 | + |
| 18 | + /// A public struct with nested struct fields |
| 19 | + public struct Rectangle has copy, drop { |
| 20 | + top_left: Point, |
| 21 | + bottom_right: Point, |
| 22 | + } |
| 23 | + |
| 24 | + /// A public struct with vector field |
| 25 | + public struct Data has copy, drop { |
| 26 | + values: vector<u64>, |
| 27 | + name: String, |
| 28 | + } |
| 29 | + |
| 30 | + // Test enums for enum transaction arguments |
| 31 | + |
| 32 | + /// A public enum for testing enum arguments - simple variants |
| 33 | + public enum Color has copy, drop { |
| 34 | + Red, |
| 35 | + Green, |
| 36 | + Blue, |
| 37 | + RGB { r: u8, g: u8, b: u8 }, |
| 38 | + } |
| 39 | + |
| 40 | + /// A public enum with struct-containing variants |
| 41 | + public enum Shape has copy, drop { |
| 42 | + Circle { center: Point, radius: u64 }, |
| 43 | + Rectangle { rect: Rectangle }, |
| 44 | + Point { point: Point }, |
| 45 | + } |
| 46 | + |
| 47 | + // Test entry functions for struct transaction arguments |
| 48 | + |
| 49 | + /// Test entry function that takes a Point argument |
| 50 | + public entry fun test_struct_point(_account: &signer, p: Point) { |
| 51 | + // Verify the point values are valid |
| 52 | + assert!(p.x > 0 && p.y > 0, 100); |
| 53 | + } |
| 54 | + |
| 55 | + /// Test entry function that takes a Rectangle argument |
| 56 | + public entry fun test_struct_rectangle(_account: &signer, rect: Rectangle) { |
| 57 | + // Verify rectangle dimensions are valid |
| 58 | + assert!(rect.bottom_right.x >= rect.top_left.x, 101); |
| 59 | + assert!(rect.bottom_right.y >= rect.top_left.y, 102); |
| 60 | + } |
| 61 | + |
| 62 | + /// Test entry function that takes Option::Some |
| 63 | + public entry fun test_option_some(_account: &signer, opt: Option<u64>) { |
| 64 | + // Verify it's Some and has expected value |
| 65 | + assert!(option::is_some(&opt), 103); |
| 66 | + let value = option::destroy_some(opt); |
| 67 | + assert!(value == 100, 104); |
| 68 | + } |
| 69 | + |
| 70 | + /// Test entry function that takes Option::None |
| 71 | + public entry fun test_option_none(_account: &signer, opt: Option<u64>) { |
| 72 | + // Verify it's None |
| 73 | + assert!(option::is_none(&opt), 105); |
| 74 | + option::destroy_none(opt); |
| 75 | + } |
| 76 | + |
| 77 | + /// Test entry function that takes Option<Point> |
| 78 | + public entry fun test_option_point(_account: &signer, opt: Option<Point>) { |
| 79 | + // Verify it's Some and contains a valid point |
| 80 | + assert!(option::is_some(&opt), 106); |
| 81 | + let point = option::destroy_some(opt); |
| 82 | + assert!(point.x == 50 && point.y == 75, 107); |
| 83 | + } |
| 84 | + |
| 85 | + /// Test entry function with mixed primitive and struct arguments |
| 86 | + public entry fun test_mixed_args(_account: &signer, num: u64, p: Point, flag: bool) { |
| 87 | + assert!(num == 42, 108); |
| 88 | + assert!(p.x == 10 && p.y == 20, 109); |
| 89 | + assert!(flag == true, 110); |
| 90 | + } |
| 91 | + |
| 92 | + /// Test entry function with type arguments and struct arguments |
| 93 | + public entry fun test_generic_with_struct<T: drop, U: drop>( |
| 94 | + _account: &signer, |
| 95 | + p: Point, |
| 96 | + value: u64 |
| 97 | + ) { |
| 98 | + assert!(p.x == 15 && p.y == 25, 111); |
| 99 | + assert!(value == 999, 112); |
| 100 | + } |
| 101 | + |
| 102 | + /// Test entry function with Data struct containing vector |
| 103 | + public entry fun test_data_struct(_account: &signer, data: Data) { |
| 104 | + use std::vector; |
| 105 | + |
| 106 | + // Verify vector contents |
| 107 | + assert!(vector::length(&data.values) == 5, 113); |
| 108 | + assert!(*vector::borrow(&data.values, 0) == 1, 114); |
| 109 | + assert!(*vector::borrow(&data.values, 4) == 5, 115); |
| 110 | + |
| 111 | + // Verify name |
| 112 | + assert!(string::bytes(&data.name) == &b"test_data", 116); |
| 113 | + } |
| 114 | + |
| 115 | + // Test entry functions for enum transaction arguments |
| 116 | + |
| 117 | + /// Test entry function that takes a simple enum variant (no fields) |
| 118 | + public entry fun test_enum_color_simple(_account: &signer, color: Color) { |
| 119 | + // Verify it's the Red variant |
| 120 | + assert!(color is Color::Red, 120); |
| 121 | + } |
| 122 | + |
| 123 | + /// Test entry function that takes an enum variant with fields |
| 124 | + public entry fun test_enum_color_rgb(_account: &signer, color: Color) { |
| 125 | + // Verify it's the RGB variant with expected values |
| 126 | + assert!(color is Color::RGB, 121); |
| 127 | + match (color) { |
| 128 | + Color::RGB { r, g, b } => { |
| 129 | + assert!(r == 255, 122); |
| 130 | + assert!(g == 128, 123); |
| 131 | + assert!(b == 0, 124); |
| 132 | + }, |
| 133 | + _ => abort 125, |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// Test entry function that takes a Shape enum with Point variant |
| 138 | + public entry fun test_enum_shape_point(_account: &signer, shape: Shape) { |
| 139 | + // Verify it's the Point variant |
| 140 | + assert!(shape is Shape::Point, 126); |
| 141 | + match (shape) { |
| 142 | + Shape::Point { point } => { |
| 143 | + assert!(point.x == 100, 127); |
| 144 | + assert!(point.y == 200, 128); |
| 145 | + }, |
| 146 | + _ => abort 129, |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// Test entry function that takes a Shape enum with Circle variant |
| 151 | + public entry fun test_enum_shape_circle(_account: &signer, shape: Shape) { |
| 152 | + // Verify it's the Circle variant |
| 153 | + assert!(shape is Shape::Circle, 130); |
| 154 | + match (shape) { |
| 155 | + Shape::Circle { center, radius } => { |
| 156 | + assert!(center.x == 50, 131); |
| 157 | + assert!(center.y == 50, 132); |
| 158 | + assert!(radius == 25, 133); |
| 159 | + }, |
| 160 | + _ => abort 134, |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /// Test entry function with mixed primitive and enum arguments |
| 165 | + public entry fun test_mixed_with_enum(_account: &signer, num: u64, color: Color, p: Point) { |
| 166 | + assert!(num == 999, 135); |
| 167 | + assert!(color is Color::Green, 136); |
| 168 | + assert!(p.x == 10 && p.y == 20, 137); |
| 169 | + } |
| 170 | + |
| 171 | + /// Test entry function with Option<Color> enum |
| 172 | + public entry fun test_option_enum(_account: &signer, opt: Option<Color>) { |
| 173 | + // Verify it's Some and contains Green variant |
| 174 | + assert!(option::is_some(&opt), 138); |
| 175 | + let color = option::destroy_some(opt); |
| 176 | + assert!(color is Color::Green, 139); |
| 177 | + } |
| 178 | +} |
0 commit comments