|
| 1 | +import { parse, PHPTypes } from './parse'; |
| 2 | + |
| 3 | +describe( 'Parser', () => { |
| 4 | + |
| 5 | + test( 'CustomObject Empty', () => { |
| 6 | + const [ token, increment ] = parse( 'C:9:"ClassName":0:{}' ); |
| 7 | + expect( token ).toEqual( new PHPTypes.PHPCustomObject( '', 'ClassName' ) ); |
| 8 | + expect( increment ).toBe( 20 ); |
| 9 | + } ); |
| 10 | + |
| 11 | + test( 'CustomObject Simple', () => { |
| 12 | + const [ token, increment ] = parse( 'C:9:"ClassName":15:{serialized data}' ); |
| 13 | + expect( token ).toEqual( new PHPTypes.PHPCustomObject( 'serialized data', 'ClassName' ) ); |
| 14 | + expect( increment ).toBe( 36 ); |
| 15 | + } ); |
| 16 | + |
| 17 | + test( 'Null', () => { |
| 18 | + const [ token, increment ] = parse( 'N;' ); |
| 19 | + expect( token ).toEqual( new PHPTypes.PHPNull() ); |
| 20 | + expect( increment ).toBe( 2 ); |
| 21 | + } ); |
| 22 | + |
| 23 | + test( 'Object Empty', () => { |
| 24 | + const [ token, increment ] = parse( 'O:8:"stdClass":0:{}' ); |
| 25 | + expect( token ).toEqual( new PHPTypes.PHPObject( new Map(), 'stdClass' ) ); |
| 26 | + expect( increment ).toBe( 19 ); |
| 27 | + } ); |
| 28 | + |
| 29 | + test( 'Object Simple', () => { |
| 30 | + const [ token, increment ] = parse( 'O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}' ); |
| 31 | + expect( token ).toEqual( new PHPTypes.PHPObject( |
| 32 | + new Map( [ |
| 33 | + [ new PHPTypes.PHPString( 'foo' ), new PHPTypes.PHPString( 'bar' ) ] |
| 34 | + ] ), |
| 35 | + 'stdClass' |
| 36 | + ) ); |
| 37 | + expect( increment ).toBe( 39 ); |
| 38 | + } ); |
| 39 | + |
| 40 | + test( 'Object Nested', () => { |
| 41 | + const [ token, increment ] = parse( 'O:8:"stdClass":2:{s:1:"0";O:8:"stdClass":1:{s:1:"0";s:3:"foo";}s:1:"1";O:8:"stdClass":1:{s:1:"0";s:3:"bar";}}' ); |
| 42 | + expect( token ).toEqual( new PHPTypes.PHPObject( |
| 43 | + new Map( [ |
| 44 | + [ new PHPTypes.PHPString( '0' ), new PHPTypes.PHPObject( |
| 45 | + new Map( [ [ new PHPTypes.PHPString( '0' ), new PHPTypes.PHPString( 'foo' ) ] ] ), |
| 46 | + 'stdClass' |
| 47 | + ) ], |
| 48 | + [ new PHPTypes.PHPString( '1' ), new PHPTypes.PHPObject( |
| 49 | + new Map( [ [ new PHPTypes.PHPString( '0' ), new PHPTypes.PHPString( 'bar' ) ] ] ), |
| 50 | + 'stdClass' |
| 51 | + ) ], |
| 52 | + ] ), |
| 53 | + 'stdClass' |
| 54 | + ) ); |
| 55 | + expect( increment ).toBe( 109 ); |
| 56 | + } ); |
| 57 | + |
| 58 | + test( 'Reference', () => { |
| 59 | + const [ token, increment ] = parse( 'R:1;' ); |
| 60 | + expect( token ).toEqual( new PHPTypes.PHPReference( 1 ) ); |
| 61 | + expect( increment ).toBe( 4 ); |
| 62 | + } ); |
| 63 | + |
| 64 | + test( 'String Empty', () => { |
| 65 | + const [ token, increment ] = parse( 's:0:"";' ); |
| 66 | + expect( token ).toEqual( new PHPTypes.PHPString( '' ) ); |
| 67 | + expect( increment ).toBe( 7 ); |
| 68 | + } ); |
| 69 | + |
| 70 | + test( 'String Complicated', () => { |
| 71 | + const [ token, increment ] = parse( 's:103:"This text - "s:38:"She exclaimed "Hello?" into the phone.";" - is an example of a serialized PHP value.";' ); |
| 72 | + expect( token ).toEqual( new PHPTypes.PHPString( 'This text - "s:38:"She exclaimed "Hello?" into the phone.";" - is an example of a serialized PHP value.' ) ); |
| 73 | + expect( increment ).toBe( 112 ); |
| 74 | + } ); |
| 75 | + |
| 76 | + test( 'String Emoji', () => { |
| 77 | + const [ token, increment ] = parse( 's:4:"🐊";' ); |
| 78 | + expect( token ).toEqual( new PHPTypes.PHPString( '🐊' ) ); |
| 79 | + expect( increment ).toBe( 9 ); |
| 80 | + } ); |
| 81 | + |
| 82 | + test( 'Array Empty', () => { |
| 83 | + const [ token, increment ] = parse( 'a:0:{}' ); |
| 84 | + expect( token ).toEqual( new PHPTypes.PHPArray( new Map( [] ) ) ); |
| 85 | + expect( increment ).toBe( 6 ); |
| 86 | + } ); |
| 87 | + |
| 88 | + test( 'Array Simple', () => { |
| 89 | + const [ token, increment ] = parse( 'a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}' ); |
| 90 | + expect( token ).toEqual( new PHPTypes.PHPArray( new Map( [ |
| 91 | + [ new PHPTypes.PHPInteger( 0 ), new PHPTypes.PHPString( 'foo' ) ], |
| 92 | + [ new PHPTypes.PHPInteger( 1 ), new PHPTypes.PHPString( 'bar' ) ], |
| 93 | + ] ) ) ); |
| 94 | + expect( increment ).toBe( 34 ); |
| 95 | + } ); |
| 96 | + |
| 97 | + test( 'Array Nested', () => { |
| 98 | + const [ token, increment ] = parse( 'a:2:{i:0;a:1:{i:0;s:3:"foo";}i:1;a:1:{i:0;s:3:"bar";}}' ); |
| 99 | + expect( token ).toEqual( new PHPTypes.PHPArray( new Map( [ |
| 100 | + [ new PHPTypes.PHPInteger( 0 ), new PHPTypes.PHPArray( new Map( [ |
| 101 | + [ new PHPTypes.PHPInteger( 0 ), new PHPTypes.PHPString( 'foo' ) ], |
| 102 | + ] ) ) ], |
| 103 | + [ new PHPTypes.PHPInteger( 1 ), new PHPTypes.PHPArray( new Map( [ |
| 104 | + [ new PHPTypes.PHPInteger( 0 ), new PHPTypes.PHPString( 'bar' ) ], |
| 105 | + ] ) ) ], |
| 106 | + ] ) ) ); |
| 107 | + expect( increment ).toBe( 54 ); |
| 108 | + } ); |
| 109 | + |
| 110 | + test( 'Boolean False', () => { |
| 111 | + const [ token, increment ] = parse( 'b:0;' ); |
| 112 | + expect( token ).toEqual( new PHPTypes.PHPBoolean( false ) ); |
| 113 | + expect( increment ).toBe( 4 ); |
| 114 | + } ); |
| 115 | + |
| 116 | + test( 'Boolean True', () => { |
| 117 | + const [ token, increment ] = parse( 'b:1;' ); |
| 118 | + expect( token ).toEqual( new PHPTypes.PHPBoolean( true ) ); |
| 119 | + expect( increment ).toBe( 4 ); |
| 120 | + } ); |
| 121 | + |
| 122 | + test( 'Float Positive', () => { |
| 123 | + const [ token, increment ] = parse( 'd:12.34;' ); |
| 124 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 125 | + expect( token.value ).toBeCloseTo( 12.34 ); |
| 126 | + expect( increment ).toBe( 8 ); |
| 127 | + } ); |
| 128 | + |
| 129 | + test( 'Float Negative', () => { |
| 130 | + const [ token, increment ] = parse( 'd:-12.34;' ); |
| 131 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 132 | + expect( token.value ).toBeCloseTo( -12.34 ); |
| 133 | + expect( increment ).toBe( 9 ); |
| 134 | + } ); |
| 135 | + |
| 136 | + test( 'Float Scientific Notation Positive', () => { |
| 137 | + const [ token, increment ] = parse( 'd:1.1111111111111112E+69;' ); |
| 138 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 139 | + expect( token.value ).toBeCloseTo( 1.1111111111111112e+69 ); |
| 140 | + expect( increment ).toBe( 25 ); |
| 141 | + } ); |
| 142 | + |
| 143 | + test( 'Float Scientific Notation Negative', () => { |
| 144 | + const [ token, increment ] = parse( 'd:-1.1111111111111112E+69;' ); |
| 145 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 146 | + expect( token.value ).toBeCloseTo( -1.1111111111111112e+69 ); |
| 147 | + expect( increment ).toBe( 26 ); |
| 148 | + } ); |
| 149 | + |
| 150 | + test( 'Float Infinity Positive', () => { |
| 151 | + const [ token, increment ] = parse( 'd:INF;' ); |
| 152 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 153 | + expect( token.value ).toBe( Infinity ); |
| 154 | + expect( increment ).toBe( 6 ); |
| 155 | + } ); |
| 156 | + |
| 157 | + test( 'Float Infinity Negative', () => { |
| 158 | + const [ token, increment ] = parse( 'd:-INF;' ); |
| 159 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 160 | + expect( token.value ).toBe( -Infinity ); |
| 161 | + expect( increment ).toBe( 7 ); |
| 162 | + } ); |
| 163 | + |
| 164 | + test( 'Float NaN', () => { |
| 165 | + const [ token, increment ] = parse( 'd:NAN;' ); |
| 166 | + expect( token ).toBeInstanceOf( PHPTypes.PHPFloat ); |
| 167 | + expect( token.value ).toBeNaN(); |
| 168 | + expect( increment ).toBe( 6 ); |
| 169 | + } ); |
| 170 | + |
| 171 | + test( 'Integer Positive', () => { |
| 172 | + const [ token, increment ] = parse( 'i:1234;' ); |
| 173 | + expect( token.value ).toBe( 1234 ); |
| 174 | + expect( increment ).toBe( 7 ); |
| 175 | + } ); |
| 176 | + |
| 177 | + test( 'Integer Negative', () => { |
| 178 | + const [ token, increment ] = parse( 'i:-1234;' ); |
| 179 | + expect( token.value ).toBe( -1234 ); |
| 180 | + expect( increment ).toBe( 8 ); |
| 181 | + } ); |
| 182 | + |
| 183 | +} ); |
0 commit comments