Skip to content

Commit

Permalink
Update isupper
Browse files Browse the repository at this point in the history
  • Loading branch information
thekevinscott committed Jan 25, 2025
1 parent 29ef81e commit 2c0c669
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
3 changes: 2 additions & 1 deletion larkjs/lark.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ function list_repeat(list, count) {
}

function isupper(a) {
return /^[A-Z_$]*$/.test(a);
return /^[^a-z]*[A-Z][^a-z]*$/.test(a);
}

function rsplit(s, delimiter, limit) {
Expand Down Expand Up @@ -3981,4 +3981,5 @@ module.exports = {
Indenter,
PythonIndenter,
get_parser,
isupper,
};
22 changes: 13 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const {TestTrees} = require("./test_trees.js");
const {TestUtils} = require("./test_utils.js");

function run_test_class(cls) {
describe(cls.constructor.name, function() {
describe(cls.constructor.name, function() {

let test = new cls();
test.setUp();
let test_names = Object.getOwnPropertyNames(cls.prototype).filter((prop) => prop.startsWith("test_"))
for (const name of test_names) {
it(name, () => {test[name]()})
}
});
let test = new cls();
if (test.setUp) {
test.setUp();
}
let test_names = Object.getOwnPropertyNames(cls.prototype).filter((prop) => prop.startsWith("test_"))
for (const name of test_names) {
it(name, () => {test[name]()})
}
});
}

run_test_class(TestTrees);
run_test_class(TestTrees);
run_test_class(TestUtils);
49 changes: 49 additions & 0 deletions test/test_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const _ = require("lodash");
const lark = require("../larkjs/lark.js");
const assert = require('assert');

const {
isupper,
} = lark;

class TestCase {
assertEqual(a, b) {
assert(_.isEqual(a, b), "Not equal:", a, b);
}
}

class TestUtils extends TestCase {
test_is_upper_ignore_0() {
this.assertEqual(isupper('__IGNORE_0'), true);
}

test_is_upper_hello() {
this.assertEqual(isupper('HELLO'), true);
}

test_is_upper_foo_bar() {
this.assertEqual(isupper('FOO_BAR'), true);
}

test_is_upper_foo_bar_baz() {
this.assertEqual(isupper('FOO-BAR BAZ'), true);
}

test_is_upper_hello_lowercase() {
this.assertEqual(isupper('Hello'), false);
}

test_is_upper_hell_o_uppercase() {
this.assertEqual(isupper('HellO'), false);
}

test_is_upper_single_digit() {
this.assertEqual(isupper('0'), false);
}

test_is_upper_numbers() {
this.assertEqual(isupper('123'), false);
}
}

module.exports = { TestUtils };

0 comments on commit 2c0c669

Please sign in to comment.