| 1 | use lib 't/lib'; |
| 2 | use Test::More; |
| 3 | use TidierTests; |
| 4 | |
| 5 | run_test( <<'RAW', <<'TIDIED', 'Simple class defn', '', ); |
| 6 | class Person{ |
| 7 | field $eyes |
| 8 | } |
| 9 | RAW |
| 10 | class Person { |
| 11 | field $eyes; |
| 12 | } |
| 13 | TIDIED |
| 14 | |
| 15 | run_test( <<'RAW', <<'TIDIED', 'Class defn with signature', '', ); |
| 16 | class Person($){ |
| 17 | field $eyes |
| 18 | } |
| 19 | RAW |
| 20 | class Person ($) { |
| 21 | field $eyes; |
| 22 | } |
| 23 | TIDIED |
| 24 | |
| 25 | run_test( <<'RAW', <<'TIDIED', 'Class defn with attributes', '', ); |
| 26 | class Person :Struct :repr(native){ |
| 27 | field $eyes |
| 28 | } |
| 29 | RAW |
| 30 | class Person :Struct :repr(native) { |
| 31 | field $eyes; |
| 32 | } |
| 33 | TIDIED |
| 34 | |
| 35 | run_test( <<'RAW', <<'TIDIED', 'Class defn with version ', '', ); |
| 36 | class Person 1.2{ |
| 37 | field @eyes = 2; |
| 38 | } |
| 39 | RAW |
| 40 | class Person 1.2 { |
| 41 | field @eyes = 2; |
| 42 | } |
| 43 | TIDIED |
| 44 | |
| 45 | |
| 46 | run_test( <<'RAW', <<'TIDIED', 'Role defn', '', ); |
| 47 | role NamedThing { |
| 48 | field 'balance'; |
| 49 | } |
| 50 | RAW |
| 51 | role NamedThing { |
| 52 | field 'balance'; |
| 53 | } |
| 54 | TIDIED |
| 55 | |
| 56 | run_test( <<'RAW', <<'TIDIED', 'Class with role', '', ); |
| 57 | class Person with NamedThing; |
| 58 | RAW |
| 59 | class Person with NamedThing; |
| 60 | TIDIED |
| 61 | |
| 62 | run_test( <<'RAW', <<'TIDIED', 'Class that extends another', '', ); |
| 63 | class Employee :isa(Person) { |
| 64 | field job_title; |
| 65 | } |
| 66 | RAW |
| 67 | class Employee :isa(Person) { |
| 68 | field job_title; |
| 69 | } |
| 70 | TIDIED |
| 71 | |
| 72 | run_test( <<'RAW', <<'TIDIED', 'Class with field with default value when omitted', '', ); |
| 73 | class Employee{ |
| 74 | field job_title = 'employee'; |
| 75 | } |
| 76 | RAW |
| 77 | class Employee { |
| 78 | field job_title = 'employee'; |
| 79 | } |
| 80 | TIDIED |
| 81 | |
| 82 | run_test( <<'RAW', <<'TIDIED', 'Class with field with default value when empty', '', ); |
| 83 | class Employee{ |
| 84 | field job_title ||= 'employee'; |
| 85 | } |
| 86 | RAW |
| 87 | class Employee { |
| 88 | field job_title ||= 'employee'; |
| 89 | } |
| 90 | TIDIED |
| 91 | |
| 92 | run_test( <<'RAW', <<'TIDIED', 'Class with field with default value when undefined', '', ); |
| 93 | class Employee{ |
| 94 | field job_title //= 'employee'; |
| 95 | } |
| 96 | RAW |
| 97 | class Employee { |
| 98 | field job_title //= 'employee'; |
| 99 | } |
| 100 | TIDIED |
| 101 | |
| 102 | run_test( <<'RAW', <<'TIDIED', 'Class that extends another with ::', '', ); |
| 103 | class Employee :isa(Person::Object) { |
| 104 | field job_title; |
| 105 | } |
| 106 | class Person :does(ROLE) { |
| 107 | field $name; |
| 108 | } |
| 109 | RAW |
| 110 | class Employee :isa(Person::Object) { |
| 111 | field job_title; |
| 112 | } |
| 113 | |
| 114 | class Person :does(ROLE) { |
| 115 | field $name; |
| 116 | } |
| 117 | TIDIED |
| 118 | |
| 119 | run_test( <<'RAW', <<'TIDIED', 'Class defn with method', '', ); |
| 120 | class BankAccount { |
| 121 | field 'balance' = 0; |
| 122 | method deposit (Num $amount){ |
| 123 | $self->inc_balance( $amount ); |
| 124 | } |
| 125 | } |
| 126 | RAW |
| 127 | class BankAccount { |
| 128 | field 'balance' = 0; |
| 129 | |
| 130 | method deposit (Num $amount) { |
| 131 | $self->inc_balance($amount); |
| 132 | } |
| 133 | } |
| 134 | TIDIED |
| 135 | |
| 136 | run_test( <<'RAW', <<'TIDIED', 'Class defn with field, :param, method, signatures', '', ); |
| 137 | class Point { |
| 138 | field $x :param = 0; |
| 139 | field $y :param = 0; |
| 140 | |
| 141 | method move ($dX, $dY) { |
| 142 | $x += $dX; |
| 143 | $y += $dY; |
| 144 | } |
| 145 | method describe () { |
| 146 | print "A point at ($x, $y)\n"; |
| 147 | } |
| 148 | } |
| 149 | RAW |
| 150 | class Point { |
| 151 | field $x :param = 0; |
| 152 | field $y :param = 0; |
| 153 | |
| 154 | method move ($dX, $dY) { |
| 155 | $x += $dX; |
| 156 | $y += $dY; |
| 157 | } |
| 158 | |
| 159 | method describe () { |
| 160 | print "A point at ($x, $y)\n"; |
| 161 | } |
| 162 | } |
| 163 | TIDIED |
| 164 | |
| 165 | |
| 166 | done_testing; |