Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Git] Add of some jasmine tests #355

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 76 additions & 63 deletions components/prism-git.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
Prism.languages.git = {
/*
* A simple one line comment like in a git status command
* For instance:
* $ git status
* # On branch infinite-scroll
* # Your branch and 'origin/sharedBranches/frontendTeam/infinite-scroll' have diverged,
* # and have 1 and 2 different commits each, respectively.
* nothing to commit (working directory clean)
*/
'comment': /^#.*$/m,

/*
* a string (double and simple quote)
*/
'string': /("|')(\\?.)*?\1/gm,

/*
* a git command. It starts with a random prompt finishing by a $, then "git" then some other parameters
* For instance:
* $ git add file.txt
*/
'command': {
pattern: /^.*\$ git .*$/m,
(function() {
// Exact copy of string token from prism-bash.js
var string_regexp = {
//allow multiline string
pattern: /("|')(\\?[\s\S])*?\1/g,
inside: {
/*
* A git command can contain a parameter starting by a single or a double dash followed by a string
* For instance:
* $ git diff --cached
* $ git log -p
*/
'parameter': /\s(--|-)\w+/m
//'property' class reused for bash variables
'property': /\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^\}]+\})/g
}
},
};

Prism.languages.git = {
/*
* A simple one line comment like in a git status command
* For instance:
* $ git status
* # On branch infinite-scroll
* # Your branch and 'origin/sharedBranches/frontendTeam/infinite-scroll' have diverged,
* # and have 1 and 2 different commits each, respectively.
* nothing to commit (working directory clean)
*/
comment: /^#.*$/m,

/*
* a git command. It starts with a random prompt finishing by a $, then "git" then some other parameters
* For instance:
* $ git add file.txt
*/
command: {
pattern: /^.*\$ git .*$/m,
inside: {
/*
* A git command can contain a parameter starting by a single or a double dash followed by a string
* For instance:
* $ git diff --cached
* $ git log -p
*/
'string': string_regexp,
'parameter': /\s(--|-)\w+/m
}
},

/*
* Coordinates displayed in a git diff command
* For instance:
* $ git diff
* diff --git file.txt file.txt
* index 6214953..1d54a52 100644
* --- file.txt
* +++ file.txt
* @@ -1 +1,2 @@
* -Here's my tetx file
* +Here's my text file
* +And this is the second line
*/
coord: /^@@.*@@$/m,

/*
* Coordinates displayed in a git diff command
* For instance:
* $ git diff
* diff --git file.txt file.txt
* index 6214953..1d54a52 100644
* --- file.txt
* +++ file.txt
* @@ -1 +1,2 @@
* -Here's my tetx file
* +Here's my text file
* +And this is the second line
*/
'coord': /^@@.*@@$/m,
/*
* Regexp to match the changed lines in a git diff output. Check the example above.
*/
'deleted': /^-(?!-).+$/m,
'inserted': /^\+(?!\+).+$/m,

/*
* Regexp to match the changed lines in a git diff output. Check the example above.
*/
'deleted': /^-(?!-).+$/m,
'inserted': /^\+(?!\+).+$/m,
/*
* a string (double and simple quote)
*/
string: string_regexp,

/*
* Match a "commit [SHA1]" line in a git log output.
* For instance:
* $ git log
* commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09
* Author: lgiraudel
* Date: Mon Feb 17 11:18:34 2014 +0100
*
* Add of a new line
*/
'commit_sha1': /^commit \w{40}$/m
};
/*
* Match a "commit [SHA1]" line in a git log output.
* For instance:
* $ git log
* commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09
* Author: lgiraudel
* Date: Mon Feb 17 11:18:34 2014 +0100
*
* Add of a new line
*/
'commit_sha1': /^commit \w{40}$/m
};
}());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be nicer to pass the global Prism variable here?

And add it as an argument for the anonymous function?
This way, you would have a fixed and static dependency on Prism.

2 changes: 1 addition & 1 deletion components/prism-git.min.js

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

20 changes: 14 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
header = require('gulp-header'),
concat = require('gulp-concat'),
var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
header = require('gulp-header'),
concat = require('gulp-concat'),
jasmine = require('gulp-jasmine'),

paths = {
components: ['components/**/*.js', '!components/**/*.min.js'],
Expand All @@ -14,7 +15,8 @@ var gulp = require('gulp'),
'components/prism-javascript.js',
'plugins/file-highlight/prism-file-highlight.js'
],
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js']
plugins: ['plugins/**/*.js', '!plugins/**/*.min.js'],
tests: ['prism.js', 'components/**/*.js', '!components/**/*.min.js', 'spec/**/*.js']
};

gulp.task('components', function() {
Expand Down Expand Up @@ -43,6 +45,12 @@ gulp.task('plugins', function() {
gulp.task('watch', function() {
gulp.watch(paths.components, ['components', 'build']);
gulp.watch(paths.plugins, ['plugins', 'build']);
gulp.watch(paths.tests, ['test']);
});

gulp.task('test', function() {
return gulp.src('spec/**/*.js')
.pipe(jasmine({verbose: false, includeStackTrace: false}));
});

gulp.task('default', ['components', 'plugins', 'build']);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"gulp-concat": "^2.3.4",
"gulp-header": "^1.0.5",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1"
"gulp-uglify": "^0.3.1",
"gulp-jasmine": "~1.0.0"
}
}
101 changes: 101 additions & 0 deletions spec/components/prism-git.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Prism = require('../../prism');
require('../../components/prism-git');

describe('Prism-Git tests', function() {
it('parses git commands', function() {
var input = '$ git config --global core.editor "~/Sublime\ Text\ 3/sublime_text -w"';

var res = Prism.highlight(input, Prism.languages.git);

expect(res).toBe('<span class="token command" >$ git config<span class="token parameter" > --global</span> core.editor <span class="token string" >"~/Sublime Text 3/sublime_text -w"</span></span>');
});


it('parses git comments', function() {
var input =
"$ git status\n" +
"# On branch infinite-scroll\n" +
"# Your branch and 'origin/sharedBranches/frontendTeam/infinite-scroll' have diverged,\n" +
"# and have 1 and 2 different commits each, respectively.\n" +
"nothing to commit (working directory clean)";

var res = Prism.highlight(input, Prism.languages.git);

expect(res).toBe(
'<span class="token command" >$ git status</span>\n' +
'<span class="token comment" spellcheck="true"># On branch infinite-scroll</span>\n' +
'<span class="token comment" spellcheck="true"># Your branch and \'origin/sharedBranches/frontendTeam/infinite-scroll\' have diverged,</span>\n' +
'<span class="token comment" spellcheck="true"># and have 1 and 2 different commits each, respectively.</span>\n' +
'nothing to commit (working directory clean)');
});

it('parses diff', function() {
var input =
"$ git diff\n" +
"diff --git file.txt file.txt\n" +
"index 6214953..1d54a52 100644\n" +
"--- file.txt\n" +
"+++ file.txt\n" +
"@@ -1 +1,2 @@\n" +
"-Here's my tetx file\n" +
"+Here's my text file\n" +
"+And this is the second line";

var res = Prism.highlight(input, Prism.languages.git);

expect(res).toBe(
'<span class="token command" >$ git diff</span>\n' +
'diff --git file.txt file.txt\n' +
'index 6214953..1d54a52 100644\n' +
'--- file.txt\n' +
'+++ file.txt\n' +
'<span class="token coord" >@@ -1 +1,2 @@</span>\n' +
'<span class="token deleted" >-Here\'s my tetx file</span>\n' +
'<span class="token inserted" >+Here\'s my text file</span>\n' +
'<span class="token inserted" >+And this is the second line</span>');
});

it('parses a commit SHA1 (40 char)', function() {
var input =
"$ git log\n" +
"commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09\n" +
"Author: lgiraudel\n" +
"Date: Mon Feb 17 11:18:34 2014 +0100\n" +
"\n" +
" Add of a new line\n" +
"\n" +
"commit 87edc4ad8c71b95f6e46f736eb98b742859abd95\n" +
"Author: lgiraudel\n" +
"Date: Mon Feb 17 11:18:15 2014 +0100\n" +
"\n" +
" Typo fix\n" +
"\n" +
"commit 3102416a90c431400d2e2a14e707fb7fd6d9e06d\n" +
"Author: lgiraudel\n" +
"Date: Mon Feb 17 10:58:11 2014 +0100\n" +
"\n" +
" Initial commit";

var res = Prism.highlight(input, Prism.languages.git);

expect(res).toBe(
'<span class="token command" >$ git log</span>\n' +
'<span class="token commit_sha1" >commit a11a14ef7e26f2ca62d4b35eac455ce636d0dc09</span>\n' +
'Author: lgiraudel\n' +
'Date: Mon Feb 17 11:18:34 2014 +0100\n' +
'\n' +
' Add of a new line\n' +
'\n' +
'<span class="token commit_sha1" >commit 87edc4ad8c71b95f6e46f736eb98b742859abd95</span>\n' +
'Author: lgiraudel\n' +
'Date: Mon Feb 17 11:18:15 2014 +0100\n' +
'\n' +
' Typo fix\n' +
'\n' +
'<span class="token commit_sha1" >commit 3102416a90c431400d2e2a14e707fb7fd6d9e06d</span>\n' +
'Author: lgiraudel\n' +
'Date: Mon Feb 17 10:58:11 2014 +0100\n' +
'\n' +
' Initial commit');
});
});