Skip to content

Commit

Permalink
add unit test for issue1356 (project-chip#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulr34 authored Jul 31, 2024
1 parent 69b3522 commit 4fd8efd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src-electron/generator/helper-endpointconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,7 @@ async function collectAttributes(db, sessionId, endpointTypes, options) {
attributeDefaultValue === null &&
a.isNullable
) {
// We don't want to make longTypeDefaultValue know about our null
// string representation.
if (types.isOneBytePrefixedString(a.type)) {
def = '0xFF,'
} else if (types.isTwoBytePrefixedString(a.type)) {
def = '0xFF, 0xFF,'
} else {
throw new Error(`Unknown string type: ${type}`)
}
def = types.nullStringDefaultValue(a.type)
} else {
def = types.longTypeDefaultValue(
defaultSize,
Expand Down
26 changes: 26 additions & 0 deletions src-electron/util/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,31 @@ function isTwoBytePrefixedString(type) {
return type == 'long_char_string' || type == 'long_octet_string'
}

/**
* Generates a default value for a null string based on its type.
* This function is designed to abstract away the specific null representation
* of strings from the longTypeDefaultValue function, ensuring that the latter
* does not need to be aware of these details.
*
* @param {string} type - The type of the string, which determines its null representation.
* @returns {string} The default value for a null string of the specified type.
* @throws {Error} Throws an error if the string type is unknown.
*/

function nullStringDefaultValue(type) {
// We don't want to make longTypeDefaultValue know about our null
// string representation.
let def
if (isOneBytePrefixedString(type)) {
def = '0xFF,'
} else if (isTwoBytePrefixedString(type)) {
def = '0xFF, 0xFF,'
} else {
throw new Error(`Unknown string type: ${type}`)
}
return def
}

/**
* Given a zcl device type returns its sign, size and zcl data type info stored
* in the database table.
Expand Down Expand Up @@ -420,3 +445,4 @@ exports.convertFloatToBigEndian = convertFloatToBigEndian
exports.getSignAndSizeOfZclType = getSignAndSizeOfZclType
exports.intToHexString = intToHexString
exports.hexStringToInt = hexStringToInt
exports.nullStringDefaultValue = nullStringDefaultValue
15 changes: 15 additions & 0 deletions test/types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const types = require('../src-electron/util/types')
const env = require('../src-electron/util/env')

beforeAll(() => {
env.setDevelopmentEnv()
})

test('ZCL types nullable strings', () => {
let r
r = types.nullStringDefaultValue('char_string')
expect(r).toContain('0xFF,')

r = types.nullStringDefaultValue('long_char_string')
expect(r).toContain('0xFF, 0xFF,')
})

0 comments on commit 4fd8efd

Please sign in to comment.