diff --git a/larkjs/lark.js b/larkjs/lark.js index e498966..e7f7f40 100644 --- a/larkjs/lark.js +++ b/larkjs/lark.js @@ -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) { @@ -3981,4 +3981,5 @@ module.exports = { Indenter, PythonIndenter, get_parser, + isupper, }; diff --git a/test/test.js b/test/test.js index 4ffbe5f..c79b2d5 100644 --- a/test/test.js +++ b/test/test.js @@ -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); \ No newline at end of file +run_test_class(TestTrees); +run_test_class(TestUtils); diff --git a/test/test_utils.js b/test/test_utils.js new file mode 100644 index 0000000..5664aab --- /dev/null +++ b/test/test_utils.js @@ -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 };