|
6 | 6 | containIncludeCommand, |
7 | 7 | splitLabelToCommands, |
8 | 8 | strip, |
| 9 | + parseValue, |
9 | 10 | parseVariablesFromLabel |
10 | 11 | } from "../src/parser"; |
11 | 12 |
|
@@ -45,7 +46,45 @@ describe("parse", function() { |
45 | 46 | assert(containIncludeCommand(commands)); |
46 | 47 | }); |
47 | 48 | }); |
48 | | - context("parseVariablesFromLabel ", function() { |
| 49 | + describe("parseValue", function() { |
| 50 | + it("should unescape string parameter", function() { |
| 51 | + const result = parseValue( |
| 52 | + '"\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/' + |
| 53 | + "\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~" + |
| 54 | + '&<>AA"', |
| 55 | + "string", |
| 56 | + "" |
| 57 | + ); |
| 58 | + assert.strictEqual(result, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~&<>AA"); |
| 59 | + }); |
| 60 | + it("should backslash unescape commonmark defined characters only", function() { |
| 61 | + const result = parseValue('"\\r\\n\\[\\]"', "string", ""); |
| 62 | + assert.strictEqual(result, "\\r\\n[]"); // \r and \n should not be unescaped. |
| 63 | + }); |
| 64 | + it("should validate markers", function() { |
| 65 | + let result = parseValue('" marker0 , marker1 "', "string", "marker"); |
| 66 | + assert.strictEqual(result, " marker0 , marker1 "); |
| 67 | + |
| 68 | + result = parseValue('"~invalid~"', "string", "marker"); |
| 69 | + assert.strictEqual(result, undefined); |
| 70 | + }); |
| 71 | + it("should parse boolean values", function() { |
| 72 | + let result = parseValue("true", "boolean", ""); |
| 73 | + assert.strictEqual(result, true); |
| 74 | + result = parseValue('"true"', "boolean", ""); |
| 75 | + assert.strictEqual(result, true); |
| 76 | + result = parseValue("'true'", "boolean", ""); |
| 77 | + assert.strictEqual(result, true); |
| 78 | + |
| 79 | + result = parseValue("false", "boolean", ""); |
| 80 | + assert.strictEqual(result, false); |
| 81 | + result = parseValue('"false"', "boolean", ""); |
| 82 | + assert.strictEqual(result, false); |
| 83 | + result = parseValue("'false'", "boolean", ""); |
| 84 | + assert.strictEqual(result, false); |
| 85 | + }); |
| 86 | + }); |
| 87 | + describe("parseVariablesFromLabel ", function() { |
49 | 88 | it("should retrieve edit boolean", function() { |
50 | 89 | const resmap = parseVariablesFromLabel(kvmap, "include,edit:true"); |
51 | 90 | const results = resmap; |
@@ -113,6 +152,36 @@ describe("parse", function() { |
113 | 152 | const results = resmap; |
114 | 153 | assert.equal(results.marker, ""); |
115 | 154 | }); |
| 155 | + it("should handle characters for string parameter", function() { |
| 156 | + const resmap = parseVariablesFromLabel( |
| 157 | + kvmap, |
| 158 | + 'import,title="test+with-special*string"' |
| 159 | + ); |
| 160 | + const results = resmap; |
| 161 | + assert.strictEqual(results.title, "test+with-special*string"); |
| 162 | + }); |
| 163 | + it("should unescape string parameter", function() { |
| 164 | + const resmap = parseVariablesFromLabel( |
| 165 | + kvmap, |
| 166 | + 'import,title="\\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/' + |
| 167 | + "\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~" + |
| 168 | + '&<>AA"' |
| 169 | + ); |
| 170 | + const results = resmap; |
| 171 | + assert.strictEqual(results.title, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~&<>AA"); |
| 172 | + }); |
| 173 | + it("should backslash unescape commonmark defined characters only", function() { |
| 174 | + const resmap = parseVariablesFromLabel(kvmap, 'import,title="\\r\\n\\[\\]"'); |
| 175 | + const results = resmap; |
| 176 | + assert.strictEqual(results.title, "\\r\\n[]"); // \r and \n should not be unescaped. |
| 177 | + }); |
| 178 | + it("should not parse string argument into another key-value pair", function() { |
| 179 | + assert.strictEqual(kvmap.edit, false); |
| 180 | + const resmap = parseVariablesFromLabel(kvmap, 'import,title="edit:true"'); |
| 181 | + const results = resmap; |
| 182 | + assert.strictEqual(results.edit, false); |
| 183 | + assert.strictEqual(results.title, "edit:true"); |
| 184 | + }); |
116 | 185 | }); |
117 | 186 | // inspired from https://github.com/rails/rails/blob/master/activesupport/test/core_ext/string_ext_test.rb |
118 | 187 | describe("strip", function() { |
|
0 commit comments