Skip to content
This repository
branch: nqp-p6
Fetching contributors…

Cannot retrieve contributors at this time

file 368 lines (272 sloc) 7.419 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#!./parrot
# Copyright (C) 2001-2010, Parrot Foundation.

=head1 NAME

t/pmc/capture.t - Test the Capture PMC

=head1 SYNOPSIS

% parrot t/pmc/capture.t

=head1 DESCRIPTION

Tests the C<Capture> PMC. Checks integer and key access using
a variety of keys and values.

=cut

.sub 'test' :main
    .include 'test_more.pir'

    test_new_capture()
    empty_capture_tests()
    basic_capture_tests()
    test_defined_delete_exists()
    test_hash_list()
    test_get_integer()
    test_get_number()
    test_keyed_int_delegation()
    test_list_delegation()
    test_set_capture()

    test_freeze_thaw()

    "done_testing"()
.end

.sub 'test_new_capture'
    .local pmc capt

    capt = new ['Capture']
    ok(1, 'new Capture')
.end

.sub 'empty_capture_tests'
    .local pmc capt

    capt = new ['Capture']

    $I0 = elements capt
    is($I0, 0, 'elements on empty Capture')

    $N0 = capt[0]
    is($N0, 0.0, 'get_number_keyed_int on empty Capture')
    $I0 = capt[0]
    is($I0, 0, 'get_integer_keyed_int on empty Capture')
    $S0 = capt[0]
    is($S0, '', 'get_string_keyed_int on empty Capture')

    $N0 = capt['nothing']
    is($N0, 0.0, 'get_number_keyed on empty Capture')

    $S0 = capt
    $S1 = 'get_string on empty Capture - ' . $S0
    substring($S0, 'Capture[', $S1)
.end

.sub 'basic_capture_tests'
    .local pmc capt

    capt = new ['Capture']

    capt[0] = 0
    capt[1] = 1.5
    capt[2] = 'two'
    $P0 = new ['Integer']
    $P0 = 3
    capt[3] = $P0

    push capt, 4
    push capt, 5.5
    push capt, 'six'
    $P0 = new ['Integer']
    $P0 = 7
    push capt, $P0

    unshift capt, 8
    unshift capt, 9.5
    unshift capt, 'ten'
    $P0 = new ['Integer']
    $P0 = 11
    unshift capt, $P0

    capt['alpha'] = 12
    capt['beta'] = 13.5
    capt['gamma'] = 'fourteen'
    $P0 = new ['Integer']
    $P0 = 15
    capt['delta'] = $P0

    $I0 = elements capt
    is($I0, 12, 'elements')

    $I0 = capt[11]
    is($I0, 7, 'get_integer_keyed_int')

    $P0 = capt[10]
    is($P0, 'six', 'get_pmc_keyed_int')

    $N0 = capt[9]
    is($N0, 5.5, 'get_number_keyed_int')

    $S0 = capt[8]
    is($S0, '4', 'get_string_keyed_int')

    $I0 = pop capt
    is($I0, 7, 'pop an integer')

    $P0 = pop capt
    is($P0, 'six', 'pop a PMC')

    $N0 = pop capt
    is($N0, 5.5, 'pop a number')

    $S0 = pop capt
    is($S0, '4', 'pop a string')

    $I0 = elements capt
    is($I0, 8, 'elements after pop')

    $I0 = shift capt
    is($I0, 11, 'shift an integer')

    $P0 = shift capt
    is($P0, 'ten', 'shift a PMC')

    $N0 = shift capt
    is($N0, 9.5, 'shift a number')

    $S0 = shift capt
    is($S0, '8', 'shift a string')

    $I0 = elements capt
    is($I0, 4, 'elements after shift')

    $P0 = pop capt
    is($P0, 3, 'pop 1 out of 4')

    $P0 = pop capt
    is($P0, 'two', 'pop 2 out of 4')

    $P0 = pop capt
    is($P0, 1.5, 'pop 3 out of 4')

    $P0 = pop capt
    is($P0, 0, 'pop 4 out of 4')

    $I0 = capt['delta']
    is($I0, 15, 'get_integer_keyed_str')

    $P0 = capt['gamma']
    is($P0, 'fourteen', 'get_pmc_keyed_str')

    $N0 = capt['beta']
    is($N0, 13.5, 'get_number_keyed_str')

    $S0 = capt['alpha']
    is($S0, '12', 'get_string_keyed_str')
.end

.sub 'test_defined_delete_exists'
    .local pmc capt
    capt = new ['Capture']

    $I0 = defined capt[2]
    nok($I0, 'defined_i initially false')

    $I1 = exists capt[2]
    nok($I1, 'exists_i initially false')

    $I0 = defined capt['alpha']
    nok($I0, 'defined_s initially false')

    $I1 = exists capt['alpha']
    nok($I1, 'exists_s initially false')

    capt[2] = 1
    capt['alpha'] = 1
    $P0 = new ['Undef']
    capt['beta'] = $P0

    $I0 = defined capt[2]
    ok($I0, 'defined_i true after set')

    $I1 = exists capt[2]
    ok($I1, 'exists_i true after set')

    $I0 = defined capt['alpha']
    ok($I0, 'defined_s true after set')

    $I1 = exists capt['alpha']
    ok($I1, 'exists_s true after set')

    $I0 = defined capt[1]
    nok($I0, 'defined_i - no intermediate element created')

    $I1 = exists capt[1]
    nok($I1, 'exists_i - no intermediate element created')

    $I0 = defined capt['beta']
    nok($I0, 'defined_s checks for Undef values...')

    $I1 = exists capt['beta']
    ok($I1, 'but exists_s does not care')

    delete capt[2]
    delete capt['alpha']

    $I0 = defined capt[2]
    nok($I0, 'defined_i false after delete')
    $I1 = exists capt[2]
    nok($I1, 'exists_i false after delete')

    $I0 = defined capt['alpha']
    nok($I0, 'defined_s false after delete')
    $I1 = exists capt['alpha']
    nok($I1, 'exists_s false after delete')
.end

.sub 'test_hash_list'
    .local pmc capt

    capt = new ['Capture']

    $P0 = capt.'list'()
    $P1 = capt.'hash'()

    isa_ok($P0, 'ResizablePMCArray', "capt.'list'")
    isa_ok($P1, 'Hash', "capt.'hash'")
.end

.sub 'test_get_integer'
    .local pmc capt

    capt = new ['Capture']
    push_eh test_get_integer_catch
    $I0 = capt

    nok(1, 'get_integer not implemented')
    .return ()

  test_get_integer_catch:
    pop_eh
    .local pmc exception
    .local string message
    .get_results (exception)

    message = exception['message']
    message = substr message, 0, 29
    is(message, 'get_integer() not implemented', 'get_integer not implemented')
    .return ()
.end

.sub 'test_get_number'
    .local pmc capt

    capt = new ['Capture']
    push_eh test_get_number_catch
    $N0 = capt

    nok(1, 'get_number not implemented')
    .return ()

  test_get_number_catch:
    .local pmc exception
    .local string message
    .get_results (exception)

    message = exception['message']
    message = substr message, 0, 28
    is(message, 'get_number() not implemented', 'get_number not implemented')
    .return ()
.end


.sub 'test_keyed_int_delegation'
    $P99 = subclass ['Capture'], ['Match']
    $P1 = new ['Match']
    $P1[1] = 1
    $I1 = elements $P1
    is($I1, 2, 'elements - delegated to parent class')

    $P99 = subclass ['Match'], ['Exp']
    $P2 = new ['Exp']
    $P2[1] = 1
    $I2 = elements $P2
    is($I2, 2, 'elements - delegated twice')
.end

.sub 'test_list_delegation'
    $P0 = subclass ['Capture'], ['Match2']
    addattribute $P0, '$.abc'
    addattribute $P0, '$.xyz'
    $P1 = new ['Match2']
    $P1[1] = 1

    $P2 = new ['String']
    setattribute $P1, '$.abc', $P2
    $P2 = new ['String']
    setattribute $P1, '$.xyz', $P2

    $P2 = $P1.'list'()
    $P2 = 0
    $I0 = elements $P2
    is($I0, 0, 'list method delegation')
.end

.sub 'test_set_capture'
    .local pmc capt
    .local pmc capt2
    $P1 = new ['String']

    capt = new ['Capture']
    capt2 = new ['Capture']

    capt[0] = 1337

    setref capt2, capt

    $I0 = capt2[0]
    is($I0, 1337, "Set PMC values correct")

    $P0 = new ['Role']
    push_eh set_badpmc
        setref capt, $P0
    pop_eh
    goto finally
    set_badpmc:
        .get_results($P1)
        ok(1, "Bad set_pmc handled properly")
    finally:
.end

.sub 'test_freeze_thaw'
    $P0 = new ["Capture"]
    $P0["foo"] = "foo"
    $P0[0] = "bar"

    # Sanity check
    $S0 = $P0["foo"]
    "is"($S0, "foo")
    $S0 = $P0[0]
    "is"($S0, "bar")

    $S1 = freeze $P0
    $P0 = thaw $S1

    $S0 = $P0["foo"]
    "is"($S0, "foo", "Hash thawed")
    $S0 = $P0[0]
    "is"($S0, "bar", "Array thawed")
.end

# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
Something went wrong with that request. Please try again.