Exemple #1
0
 def test_as_string(self):
     expected = space.string('Array')
     self.assertTrue(expected.equal(self.array.as_string()).is_true())
     not_expected = space.string('')
     self.assertFalse(not_expected.equal(self.array.as_string()).is_true())
     self.array.set(space.int(0), space.string('Array'))
     self.assertTrue(expected.equal(self.array.as_string()).is_true())
Exemple #2
0
def _print_variable(w_variable, indent_level=""):
    if w_variable.get_type() == PHPTypes.w_string:
        return w_variable
    elif (w_variable.get_type() == PHPTypes.w_int
          or w_variable.get_type() == PHPTypes.w_float
          or w_variable.get_type() == PHPTypes.w_bool
          or w_variable.get_type() == PHPTypes.w_null):
        return w_variable.as_string()
    elif w_variable.get_type() == PHPTypes.w_array:
        assert isinstance(w_variable, W_Array)
        indentation = "    "
        array_variables_indent_level = indent_level + indentation
        next_indent_level = array_variables_indent_level + indentation
        output = "Array\n%s(\n" % indent_level
        # temporary
        iterator = w_variable.storage.iter()
        for i in range(w_variable.len()):
            key, w_value = iterator.nextitem()
            child_output = _print_variable(w_value, next_indent_level).str_w()
            output += "%s[%s] => %s\n" % (array_variables_indent_level,
                                          key, child_output)
        output += "%s)\n" % indent_level
        return space.string(output)

    return space.string("NOT IMPLEMENTED")
Exemple #3
0
 def test_array_creation_one_key(self):
     raw = [space.int(1), space.int(1),
            space.string("1"), space.int(2),
            space.float(1.5), space.int(3),
            space.bool(True), space.int(4)]
     actual = space.array(raw)
     expected = {space.string('1'): space.int(4)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #4
0
 def test_array_creation_int_branch(self):
     raw = [space.int(3), space.int(1),
            space.float(-1.2), space.int(2),
            space.bool(False), space.int(2),
            space.bool(True), space.int(3)]
     actual = space.array(raw)
     expected = {space.string('3'): space.int(1),
                 space.string('-1'): space.int(2),
                 space.string('0'): space.int(2), space.string('1'): space.int(3)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #5
0
    def inc(self):
        if not self.is_true():
            return space.string('1')
        try:
            return self.as_number_strict().inc()
        except NotConvertibleToNumber:
            pass
        self._make_mutable()
        index = self.strlen() - 1
        symbol = ''
        while index >= 0:
            item = self.strategy.getitem(self, index)
            symbol_index = ord(item[0])
            if SYMBOL_A <= symbol_index <= SYMBOL_z or SYMBOL_0 <= symbol_index <= SYMBOL_9:
                if symbol_index == SYMBOL_9:
                    symbol = '0'
                    self.strategy.setitem(self, index, symbol)
                elif symbol_index == SYMBOL_Z:
                    symbol = 'A'
                    self.strategy.setitem(self, index, symbol)
                elif symbol_index == SYMBOL_z:
                    symbol = 'a'
                    self.strategy.setitem(self, index, symbol)
                else:
                    symbol_index += 1
                    self.strategy.setitem(self, index, chr(symbol_index))
                    return self
            else:
                return self
            index -= 1
        if index < 0 and symbol:
            self.strategy.append(self, symbol)

        return self
Exemple #6
0
 def test_get_set_combination(self):
     array = space.array()
     with self.assertRaises(KeyError):
         array.get(space.int(5))
     array.set(space.undefined(), space.int(1))
     value = array.get(space.int(0)).deref()
     self.assertTrue(value.equal(space.int(1)).is_true())
     with self.assertRaises(KeyError):
         array.get(space.string("7"))
     array.set(space.undefined(), space.int(2))
     value = array.get(space.int(1)).deref()
     self.assertTrue(value.equal(space.int(2)).is_true())
Exemple #7
0
 def test_set(self):
     array = space.array([space.int(5), space.int(1),
                          space.int(12), space.int(2)])
     array.set(space.int(2), space.int(3))
     value = array.get(space.int(2)).deref()
     self.assertTrue(value.equal(space.int(3)).is_true())
     array.set(space.string("4"), space.int(4))
     value = array.get(space.int(4)).deref()
     self.assertTrue(value.equal(space.int(4)).is_true())
     # no index test
     array.set(space.undefined(), space.int(13))
     value = array.get(space.int(13)).deref()
     self.assertTrue(value.equal(space.int(13)).is_true())
     array.set(space.int(15), space.int(15))
     array.set(space.undefined(), space.int(16))
     value = array.get(space.int(16)).deref()
     self.assertTrue(value.equal(space.int(16)).is_true())
     # now string
     array.set(space.string("20"), space.int(20))
     array.set(space.undefined(), space.int(21))
     value = array.get(space.int(21)).deref()
     self.assertTrue(value.equal(space.int(21)).is_true())
Exemple #8
0
def gettype(context, params):
    php_type = params[0].deref().get_type()
    type_name = 'unknown type'
    if php_type == PHPTypes.w_int:
        type_name = 'integer'
    elif php_type == PHPTypes.w_float:
        type_name = 'double'
    elif php_type == PHPTypes.w_string:
        type_name = 'string'
    elif php_type == PHPTypes.w_bool:
        type_name = 'boolean'
    elif php_type == PHPTypes.w_null:
        type_name = 'NULL'
    elif php_type == PHPTypes.w_array:
        type_name = 'array'
    elif php_type == PHPTypes.w_object:
        type_name = 'object'
    elif php_type == PHPTypes.w_resource:
        type_name = 'resource'

    return space.string(type_name)
Exemple #9
0
 def test_get_type_conversion_string(self):
     array = space.array([space.int(0), space.int(1),
                          space.int(1), space.int(2),
                          space.int(-1), space.int(3)])
     value = array.get(space.string("1")).deref()
     self.assertTrue(value.equal(space.int(2)).is_true())
     value = array.get(space.string("-1")).deref()
     self.assertTrue(value.equal(space.int(3)).is_true())
     with self.assertRaises(KeyError):
         value = array.get(space.string("01"))
     with self.assertRaises(KeyError):
         value = array.get(space.string("-0"))
     value = array.get(space.string("0")).deref()
     self.assertTrue(value.equal(space.int(1)).is_true())
     with self.assertRaises(KeyError):
         value = array.get(space.string("12Tst"))
Exemple #10
0
 def LOAD_NAME(self, index):
     name = self.bytecode.names[index]
     self.frame.stack.append(space.string(name))
Exemple #11
0
 def get_compiled_value(self):
     return space.string(self.const_value)
Exemple #12
0
 def test_array_creation_with_no_keys(self):
     raw = [space.undefined(), space.int(1),
            space.undefined(), space.int(2),
            space.float(6.0), space.int(3),
            space.undefined(), space.int(4),
            space.int(3), space.int(5),
            space.undefined(), space.int(6)]
     actual = space.array(raw)
     expected = {space.string('0'): space.int(1), space.string('1'): space.int(2),
                 space.string('6'): space.int(3), space.string('7'): space.int(4),
                 space.string('3'): space.int(5), space.string('8'): space.int(6)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
     raw = [space.string("6"), space.int(3),
            space.string("3"), space.int(5),
            space.undefined(), space.int(6)]
     actual = space.array(raw)
     expected = {space.string('6'): space.int(3),
                 space.string('3'): space.int(5),
                 space.string('7'): space.int(6)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #13
0
 def test_array_creation_null_string_branch(self):
     raw = [space.string("test"), space.int(1),
            space.string("08"), space.int(2),
            space.string("99"), space.int(3),
            space.null(), space.int(4),
            space.string("-5"), space.int(5),
            space.string("-09"), space.int(7)]
     actual = space.array(raw)
     expected = {space.string("test"): space.int(1),
                 space.string("08"): space.int(2),
                 space.string('99'): space.int(3), space.string(""): space.int(4),
                 space.string('-5'): space.int(5),
                 space.string("-09"): space.int(7)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #14
0
 def as_string(self):
     return space.string(str(self.value))
Exemple #15
0
 def as_string(self):
     return space.string('Array')
Exemple #16
0
 def as_string(self):
     return space.string("")
Exemple #17
0
 def as_string(self):
     if self.value:
         return space.string('1')
     return space.string('')