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

Commit

Permalink
Merge pull request #739 from gobisa/bytearray_bytes_add_operator
Browse files Browse the repository at this point in the history
Bytearray bytes add operator
  • Loading branch information
freakboy3742 authored May 15, 2018
2 parents 22511ee + b396acb commit cacf83b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 65 deletions.
30 changes: 22 additions & 8 deletions batavia/types/Bytearray.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ Bytearray.prototype.__mod__ = function(other) {
}

Bytearray.prototype.__add__ = function(other) {
var types = require('../types')

if (types.isinstance(other, types.Int)) {
throw new exceptions.TypeError.$pyclass("can't concat bytearray to " + type_name(other))
}

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 @@ -224,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
34 changes: 0 additions & 34 deletions tests/datatypes/test_bytearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +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_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 @@ -319,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

0 comments on commit cacf83b

Please sign in to comment.