Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Bytearray bytes add operator #739

Merged
merged 9 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions batavia/types/Bytearray.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,15 @@ Bytearray.prototype.__mod__ = function(other) {
}

Bytearray.prototype.__add__ = function(other) {
var types = require('../types')
if (types.isinstance(other, types.Bool)) {
let Buffer = require('buffer').Buffer
let types = require('../types')
if (types.isinstance(other, types.Bytearray)) {
let combined_bytes = new types.Bytes(Buffer.concat([this.val.val, other.val.val]))
return new Bytearray(combined_bytes)
} else if (types.isinstance(other, types.Bytes)) {
let combined_bytes = new types.Bytes(Buffer.concat([this.val.val, other.val]))
return new Bytearray(combined_bytes)
} else {
throw new exceptions.TypeError.$pyclass("can't concat bytearray to " + type_name(other))
}
}
Expand Down Expand Up @@ -219,7 +226,19 @@ Bytearray.prototype.__itruediv__ = function(other) {
}

Bytearray.prototype.__iadd__ = function(other) {
throw new exceptions.NotImplementedError.$pyclass('Bytearray.__iadd__ has not been implemented')
let Buffer = require('buffer').Buffer
let types = require('../types')
if (types.isinstance(other, types.Bytearray)) {
let combined_bytes = new types.Bytes(Buffer.concat([this.val.val, other.val.val]))
this.val = combined_bytes
return this
} else if (types.isinstance(other, types.Bytes)) {
let combined_bytes = new types.Bytes(Buffer.concat([this.val.val, other.val]))
this.val = combined_bytes
return this
} else {
throw new exceptions.TypeError.$pyclass("can't concat bytearray to " + type_name(other))
}
}

Bytearray.prototype.__isub__ = function(other) {
Expand Down
6 changes: 4 additions & 2 deletions batavia/types/Bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ Bytes.prototype.__add__ = function(other) {
byteBuffer.write(this.valueOf().toString() + other.valueOf().toString())
return new Bytes(byteBuffer)
} else if (types.isinstance(other, [types.Bytearray])) {
throw new exceptions.NotImplementedError.$pyclass('Bytes.__add__ has not been implemented')
let byteBuffer = Buffer.alloc(this.valueOf().length + other.valueOf().valueOf().length)
byteBuffer.write(this.valueOf().toString() + other.valueOf().valueOf().toString())
return new Bytes(byteBuffer)
} else if (types.isinstance(other, [
types.Bool,
types.Dict,
Expand Down Expand Up @@ -377,7 +379,7 @@ Bytes.prototype.__itruediv__ = function(other) {
}

Bytes.prototype.__iadd__ = function(other) {
throw new exceptions.NotImplementedError.$pyclass('Bytes.__iadd__ has not been implemented')
return this['__add__'](other)
}

Bytes.prototype.__isub__ = function(other) {
Expand Down
35 changes: 0 additions & 35 deletions tests/datatypes/test_bytearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ class BinaryBytearrayOperationTests(BinaryOperationTestCase, TranspileTestCase):
data_type = 'bytearray'

not_implemented = [
'test_add_bytearray',
'test_add_bytes',
'test_add_class',
'test_add_complex',
'test_add_dict',
'test_add_float',
'test_add_frozenset',
'test_add_int',
'test_add_list',
'test_add_None',
'test_add_NotImplemented',
'test_add_range',
'test_add_set',
'test_add_slice',
'test_add_str',
'test_add_tuple',

'test_and_bool',
'test_and_bytearray',
'test_and_bytes',
Expand Down Expand Up @@ -320,24 +303,6 @@ class InplaceBytearrayOperationTests(InplaceOperationTestCase, TranspileTestCase
data_type = 'bytearray'

not_implemented = [
'test_add_bool',
'test_add_bytearray',
'test_add_bytes',
'test_add_class',
'test_add_complex',
'test_add_dict',
'test_add_float',
'test_add_frozenset',
'test_add_int',
'test_add_list',
'test_add_None',
'test_add_NotImplemented',
'test_add_range',
'test_add_set',
'test_add_slice',
'test_add_str',
'test_add_tuple',

'test_and_bool',
'test_and_bytearray',
'test_and_bytes',
Expand Down
21 changes: 0 additions & 21 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ class BinaryBytesOperationTests(BinaryOperationTestCase, TranspileTestCase):
}

not_implemented = [

'test_add_bytearray',

'test_eq_bytearray',

'test_ge_bytearray',
Expand All @@ -144,24 +141,6 @@ class InplaceBytesOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'bytes'

not_implemented = [
'test_add_bool',
'test_add_bytearray',
'test_add_bytes',
'test_add_class',
'test_add_complex',
'test_add_dict',
'test_add_float',
'test_add_frozenset',
'test_add_int',
'test_add_list',
'test_add_None',
'test_add_NotImplemented',
'test_add_range',
'test_add_set',
'test_add_slice',
'test_add_str',
'test_add_tuple',

'test_lshift_bool',
'test_lshift_bytearray',
'test_lshift_bytes',
Expand Down