Skip to content

Commit

Permalink
refactor: rename WellKnownCharsets to Charset
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Dec 4, 2024
1 parent 424111a commit 3101eff
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions res/controllers/engine-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ declare namespace engine {
*/
function softStart(deck: number, activate: boolean, factor?: number): void;

enum WellKnownCharsets {
enum Charset {
ASCII, // American Standard Code for Information Interchange (7-Bit)
UTF_8, // Unicode Transformation Format (8-Bit)
UTF_16LE, // UTF-16 for Little-Endian devices (ARM, x86)
Expand Down Expand Up @@ -337,5 +337,5 @@ declare namespace engine {
* @param targetCharset The charset to encode the string into.
* @returns The converted String as an array of bytes. Will return an empty buffer on conversion error or unavailable charset.
*/
function convertCharset(targetCharset: WellKnownCharsets, value: string): ArrayBuffer
function convertCharset(targetCharset: Charset, value: string): ArrayBuffer
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ bool ControllerScriptEngineLegacy::initialize() {

auto engine = m_pJSEngine->newQObject(legacyScriptInterface);
auto meta = m_pJSEngine->newQMetaObject(&ControllerScriptInterfaceLegacy::staticMetaObject);
engine.setProperty("WellKnownCharsets", meta);
engine.setProperty("Charset", meta);
engineGlobalObject.setProperty("engine", m_pJSEngine->newQObject(legacyScriptInterface));

#ifdef MIXXX_USE_QML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,9 @@ void ControllerScriptInterfaceLegacy::softStart(int deck, bool activate, double
}

QByteArray ControllerScriptInterfaceLegacy::convertCharset(
const ControllerScriptInterfaceLegacy::WellKnownCharsets targetCharset,
const ControllerScriptInterfaceLegacy::Charset targetCharset,
const QString& value) {
using enum WellKnownCharsets;
using enum Charset;
switch (targetCharset) {
case ASCII:
return convertCharsetInternal(QStringLiteral("US-ASCII"), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ControllerScriptInterfaceLegacy : public QObject {
// NOTE: these enumerator names are exposed to the JS engine! Removal/Changing of
// any name is likely breaking. Only add more and only remove enumerators if
// they're broken to begin with.
enum class WellKnownCharsets {
enum class Charset {
ASCII,
UTF_8,
UTF_16LE,
Expand All @@ -47,7 +47,7 @@ class ControllerScriptInterfaceLegacy : public QObject {
BOCU_1,
CESU_8
};
Q_ENUM(WellKnownCharsets)
Q_ENUM(Charset)

ControllerScriptInterfaceLegacy(ControllerScriptEngineLegacy* m_pEngine,
const RuntimeLoggingCategory& logger);
Expand Down Expand Up @@ -105,7 +105,7 @@ class ControllerScriptInterfaceLegacy : public QObject {
Q_INVOKABLE void softStart(const int deck, bool activate, double factor = 1.0);

Q_INVOKABLE QByteArray convertCharset(
const ControllerScriptInterfaceLegacy::WellKnownCharsets
const ControllerScriptInterfaceLegacy::Charset
targetCharset,
const QString& value);

Expand Down
18 changes: 9 additions & 9 deletions src/test/controllerscriptenginelegacy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,15 +664,15 @@ TEST_F(ControllerScriptEngineLegacyTest, connectionExecutesWithCorrectThisObject

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetCorrectValueStringCharset) {
const auto result = evaluate(
"engine.convertCharset(engine.WellKnownCharsets.Latin9, 'Hello!')");
"engine.convertCharset(engine.Charset.Latin9, 'Hello!')");

EXPECT_EQ(qjsvalue_cast<QByteArray>(result),
QByteArrayView::fromArray({'\x48', '\x65', '\x6c', '\x6c', '\x6f', '\x21'}));
}

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetUnsupportedChars) {
auto result = qjsvalue_cast<QByteArray>(
evaluate("engine.convertCharset(engine.WellKnownCharsets.Latin9, 'مايأ نامز')"));
evaluate("engine.convertCharset(engine.Charset.Latin9, 'مايأ نامز')"));
char sub = '\x1A'; // ASCII/Latin9 SUB character
EXPECT_EQ(result,
QByteArrayView::fromArray(
Expand All @@ -681,7 +681,7 @@ TEST_F(ControllerScriptEngineLegacyTest, convertCharsetUnsupportedChars) {

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetMultiByteEncoding) {
auto result = qjsvalue_cast<QByteArray>(
evaluate("engine.convertCharset(engine.WellKnownCharsets.UTF_16LE, 'مايأ نامز')"));
evaluate("engine.convertCharset(engine.Charset.UTF_16LE, 'مايأ نامز')"));
EXPECT_EQ(result,
QByteArrayView::fromArray({'\x45',
'\x06',
Expand All @@ -705,9 +705,9 @@ TEST_F(ControllerScriptEngineLegacyTest, convertCharsetMultiByteEncoding) {

#define COMPLICATEDSTRINGLITERAL "Hello, 世界! שלום! こんにちは! 안녕하세요! 😊"

static int convertedCharsetForString(ControllerScriptInterfaceLegacy::WellKnownCharsets charset) {
static int convertedCharsetForString(ControllerScriptInterfaceLegacy::Charset charset) {
// the expected length after conversion of COMPLICATEDSTRINGLITERAL
using enum ControllerScriptInterfaceLegacy::WellKnownCharsets;
using enum ControllerScriptInterfaceLegacy::Charset;
switch (charset) {
case UTF_8:
return 63;
Expand Down Expand Up @@ -749,17 +749,17 @@ static int convertedCharsetForString(ControllerScriptInterfaceLegacy::WellKnownC
return 0;
}

TEST_F(ControllerScriptEngineLegacyTest, convertCharsetAllWellKnownCharsets) {
TEST_F(ControllerScriptEngineLegacyTest, convertCharsetAllCharset) {
QMetaEnum charsetEnumEntry = QMetaEnum::fromType<
ControllerScriptInterfaceLegacy::WellKnownCharsets>();
ControllerScriptInterfaceLegacy::Charset>();

for (int i = 0; i < charsetEnumEntry.keyCount(); ++i) {
QString key = charsetEnumEntry.key(i);
auto enumValue =
static_cast<ControllerScriptInterfaceLegacy::WellKnownCharsets>(
static_cast<ControllerScriptInterfaceLegacy::Charset>(
charsetEnumEntry.value(i));
QString source = QStringLiteral(
"engine.convertCharset(engine.WellKnownCharsets.%1, "
"engine.convertCharset(engine.Charset.%1, "
"'" COMPLICATEDSTRINGLITERAL "')")
.arg(key);
auto result = qjsvalue_cast<QByteArray>(evaluate(source));
Expand Down

0 comments on commit 3101eff

Please sign in to comment.