Skip to content

Commit

Permalink
Tests: Automatically normalize line ends (#2934)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jun 13, 2021
1 parent 8e93c5d commit 99f3ddc
Show file tree
Hide file tree
Showing 91 changed files with 1,162 additions and 834 deletions.
3 changes: 0 additions & 3 deletions .gitattributes
@@ -1,4 +1 @@
* text=auto

# Test files should not have their line endings modified by git
/tests/languages/**/*.test binary
2 changes: 1 addition & 1 deletion components/prism-false.js
Expand Up @@ -13,7 +13,7 @@
greedy: true
},
'character-code': {
pattern: /'[\s\S]/,
pattern: /'(?:[^\r]|\r\n?)/,
alias: 'number'
},
'assembler-code': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-false.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions tests/helper/test-case.js
Expand Up @@ -66,7 +66,6 @@ module.exports = {

function updateFile() {
// change the file
const lineEnd = (/\r\n/.test(testCase.code) || !/\n/.test(testCase.code)) ? '\r\n' : '\n';
const separator = '\n\n----------------------------------------------------\n\n';
const pretty = TokenStreamTransformer.prettyprint(tokenStream, '\t');

Expand All @@ -75,7 +74,9 @@ module.exports = {
content += separator + testCase.comment.trim();
}
content += '\n';
content = content.replace(/\r?\n/g, lineEnd);

// convert line ends to the line ends of the file
content = content.replace(/\r\n?|\n/g, testCase.lineEndOnDisk);

fs.writeFileSync(filePath, content, 'utf-8');
}
Expand Down Expand Up @@ -202,14 +203,19 @@ module.exports = {
* @returns {ParsedTestCase}
*
* @typedef ParsedTestCase
* @property {string} lineEndOnDisk The EOL format used by the parsed file.
* @property {string} code
* @property {string} expectedJson
* @property {number} expectedLineOffset
* @property {Array | null} expectedTokenStream
* @property {string} comment
*/
parseTestCaseFile(filePath) {
const testCaseSource = fs.readFileSync(filePath, 'utf8');
let testCaseSource = fs.readFileSync(filePath, 'utf8');
const lineEndOnDisk = (/\r\n?|\n/.exec(testCaseSource) || ['\n'])[0];
// normalize line ends to \r\n
testCaseSource = testCaseSource.replace(/\r\n?|\n/g, '\r\n');

const testCaseParts = testCaseSource.split(/^-{10,}[ \t]*$/m);

if (testCaseParts.length > 3) {
Expand All @@ -221,9 +227,10 @@ module.exports = {
const comment = (testCaseParts[2] || '').trimStart();

const testCase = {
lineEndOnDisk,
code,
expectedJson: expected,
expectedLineOffset: code.split(/\r\n?|\n/g).length,
expectedLineOffset: code.split(/\r\n/g).length,
expectedTokenStream: expected ? JSON.parse(expected) : null,
comment
};
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/agda/comment_feature.test
Expand Up @@ -7,7 +7,7 @@
----------------------------------------------------

[
["comment", "{-\n\tThis is a\n\tmultiline comment\n-}"],
["comment", "{-\r\n\tThis is a\r\n\tmultiline comment\r\n-}"],
["comment", "-- This is a singleline comment"]
]

Expand Down
9 changes: 5 additions & 4 deletions tests/languages/agda/function_feature.test
Expand Up @@ -29,11 +29,11 @@ merge xs@(x ∷ xs₁) ys@(y ∷ ys₁) =
["operator", "→"],
" List A ",
["operator", "→"],
" List A\nmerge xs [] ",
" List A\r\nmerge xs [] ",
["operator", "="],
" xs\nmerge [] ys ",
" xs\r\nmerge [] ys ",
["operator", "="],
" ys\nmerge xs",
" ys\r\nmerge xs",
["punctuation", "@"],
["punctuation", "("],
"x ∷ xs₁",
Expand All @@ -44,7 +44,8 @@ merge xs@(x ∷ xs₁) ys@(y ∷ ys₁) =
"y ∷ ys₁",
["punctuation", ")"],
["operator", "="],
"\n\tif x < y then x ∷ merge xs₁ ys\n\t\t\t\t\t else y ∷ merge xs ys₁"

"\r\n\tif x < y then x ∷ merge xs₁ ys\r\n\t\t\t\t\t else y ∷ merge xs ys₁"
]

----------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion tests/languages/agda/module_feature.test
Expand Up @@ -11,12 +11,14 @@ open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; _∎) renaming (begin_ to star
["punctuation", "."],
"test ",
["keyword", "where"],

["keyword", "import"],
" Relation",
["punctuation", "."],
"Binary",
["punctuation", "."],
"PropositionalEquality as Eq\n",
"PropositionalEquality as Eq\r\n",

["keyword", "open"],
" Eq ",
["keyword", "hiding"],
Expand All @@ -25,6 +27,7 @@ open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; _∎) renaming (begin_ to star
["punctuation", ";"],
" refl",
["punctuation", ")"],

["keyword", "open"],
" Eq",
["punctuation", "."],
Expand Down
11 changes: 8 additions & 3 deletions tests/languages/agda/record_feature.test
Expand Up @@ -18,15 +18,20 @@ open Fin
["operator", ":"],
["keyword", "Set"],
["keyword", "where"],

["keyword", "constructor"],
" _[_]\n\t",
" _[_]\r\n\t",

["keyword", "field"],

["function", "⟦_⟧ "],
["operator", ":"],
" Nat\n\t\t",
" Nat\r\n\t\t",

["function", "proof "],
["operator", ":"],
" suc ⟦_⟧ ≤ n\n",
" suc ⟦_⟧ ≤ n\r\n",

["keyword", "open"],
" Fin"
]
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/al/comment_feature.test
Expand Up @@ -9,7 +9,7 @@
[
["comment", "// comment"],
["comment", "/**/"],
["comment", "/*\n comment\n*/"]
["comment", "/*\r\n comment\r\n*/"]
]

----------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions tests/languages/antlr4/action_feature.test
Expand Up @@ -21,12 +21,14 @@ from LexerAdaptor import LexerAdaptor
["content", " superClass = LexerAdaptor; "],
["punctuation", "}"]
]],

["annotation", "@lexer::header"],
["action", [
["punctuation", "{"],
["content", "\n import { Token } from 'antlr4ts/Token';\n import { CommonToken } from 'antlr4ts/CommonToken';\n import { Python3Parser } from './Python3Parser';\n"],
["content", "\r\n import { Token } from 'antlr4ts/Token';\r\n import { CommonToken } from 'antlr4ts/CommonToken';\r\n import { Python3Parser } from './Python3Parser';\r\n"],
["punctuation", "}"]
]],

["definition", "END"],
["punctuation", ":"],
["string", "'end'"],
Expand All @@ -36,10 +38,11 @@ from LexerAdaptor import LexerAdaptor
["punctuation", "}"]
]],
["punctuation", ";"],

["annotation", "@header"],
["action", [
["punctuation", "{"],
["content", "\nfrom LexerAdaptor import LexerAdaptor\n"],
["content", "\r\nfrom LexerAdaptor import LexerAdaptor\r\n"],
["punctuation", "}"]
]]
]
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/antlr4/comment_feature.test
Expand Up @@ -7,7 +7,7 @@

[
["comment", "// comment"],
["comment", "/*\n comment\n*/"]
["comment", "/*\r\n comment\r\n*/"]
]

----------------------------------------------------
Expand Down
23 changes: 17 additions & 6 deletions tests/languages/bbcode/tag_feature.test
Expand Up @@ -29,22 +29,25 @@
]],
["punctuation", "]"]
]],

["tag", [
["tag", [
["punctuation", "["],
"*"
]],
["punctuation", "]"]
]],
"Entry A\n ",
"Entry A\r\n ",

["tag", [
["tag", [
["punctuation", "["],
"*"
]],
["punctuation", "]"]
]],
"Entry B\n",
"Entry B\r\n",

["tag", [
["tag", [
["punctuation", "[/"],
Expand All @@ -64,22 +67,25 @@
]],
["punctuation", "]"]
]],

["tag", [
["tag", [
["punctuation", "["],
"*"
]],
["punctuation", "]"]
]],
"Entry 1\n ",
"Entry 1\r\n ",

["tag", [
["tag", [
["punctuation", "["],
"*"
]],
["punctuation", "]"]
]],
"Entry 2\n",
"Entry 2\r\n",

["tag", [
["tag", [
["punctuation", "[/"],
Expand Down Expand Up @@ -110,7 +116,8 @@
]],
["punctuation", "]"]
]],
" or\n",
" or\r\n",

["tag", [
["tag", [
["punctuation", "["],
Expand All @@ -131,7 +138,8 @@
]],
["punctuation", "]"]
]],
" or\n",
" or\r\n",

["tag", [
["tag", [
["punctuation", "["],
Expand All @@ -151,6 +159,7 @@
]],
["punctuation", "]"]
]],

["tag", [
["tag", [
["punctuation", "["],
Expand All @@ -166,6 +175,7 @@
]],
["punctuation", "]"]
]],

["tag", [
["tag", [
["punctuation", "["],
Expand Down Expand Up @@ -211,6 +221,7 @@
]],
["punctuation", "]"]
]],

["tag", [
["tag", [
["punctuation", "["],
Expand Down
10 changes: 3 additions & 7 deletions tests/languages/brightscript/property_feature.test
Expand Up @@ -9,15 +9,11 @@ a = { foo: 5, bar: 6, "foo bar": 7 }

[
["punctuation", "{"],
["property", "foo"],
["operator", ":"],
["number", "4"],
["property", "\"bar\""],
["operator", ":"],
["number", "5"],
["property", "foo"], ["operator", ":"], ["number", "4"],
["property", "\"bar\""], ["operator", ":"], ["number", "5"],
["punctuation", "}"],

"\n\na ",
"\r\n\r\na ",
["operator", "="],
["punctuation", "{"],
["property", "foo"],
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/clojure/string_feature.test
Expand Up @@ -8,7 +8,7 @@ string"
[
["string", "\"\""],
["string", "\"Fo\\\"obar\""],
["string", "\"multi-line\nstring\""]
["string", "\"multi-line\r\nstring\""]
]

----------------------------------------------------
Expand Down
34 changes: 22 additions & 12 deletions tests/languages/cmake/string_feature.test
Expand Up @@ -10,29 +10,39 @@ string"

[
["string", ["\"This is a string\""]],
["string", ["\"This is \nmulti\nline\nstring\""]],
["string", ["\"This is \r\nmulti\r\nline\r\nstring\""]],
["string", [
"\"",
["interpolation", [
["punctuation", "${"], ["variable", "VAR"], ["punctuation", "}"]]
],
["punctuation", "${"],
["variable", "VAR"],
["punctuation", "}"]
]],
"with",
["interpolation", [
["punctuation", "${"], ["variable", "BAR"], ["punctuation", "}"]]
], "\""]
],
["punctuation", "${"],
["variable", "BAR"],
["punctuation", "}"]
]],
"\""
]],
["string", [
"\"",
["interpolation", [
["punctuation", "${"], ["variable", "FOO"], ["punctuation", "}"]]
],
["punctuation", "${"],
["variable", "FOO"],
["punctuation", "}"]
]],
" with ",
["interpolation", [
["punctuation", "${"], ["variable", "BAR"], ["punctuation", "}"]]
], "\""]
]
["punctuation", "${"],
["variable", "BAR"],
["punctuation", "}"]
]],
"\""
]]
]

----------------------------------------------------

Checks for strings.
Checks for strings.
4 changes: 2 additions & 2 deletions tests/languages/cobol/class-name_feature.test
Expand Up @@ -69,7 +69,7 @@ greeting pic x(12) value is "Hello World".
]],
["punctuation", "."],

"\n\ngreeting ",
"\r\n\r\ngreeting ",
["keyword", "pic"],
["class-name", [
"x",
Expand All @@ -81,4 +81,4 @@ greeting pic x(12) value is "Hello World".
["keyword", "is"],
["string", "\"Hello World\""],
["punctuation", "."]
]
]

0 comments on commit 99f3ddc

Please sign in to comment.