2014-01-04 01:18:30 +01:00
/*global Dict */
2013-12-04 17:16:08 +01:00
var assert = require ( 'assert' ) ;
2014-01-17 21:08:45 +01:00
var path = require ( 'path' ) ;
var fs = require ( 'fs' ) ;
2013-12-04 17:16:08 +01:00
add _dependencies ( {
_ : 'third/underscore/underscore.js' ,
2014-01-04 01:18:30 +01:00
marked : 'third/marked/lib/marked.js' ,
Dict : 'js/dict.js'
2013-12-04 17:16:08 +01:00
} ) ;
2014-01-16 20:18:55 +01:00
var doc = "" ;
set _global ( 'document' , doc ) ;
2013-12-04 17:16:08 +01:00
set _global ( '$' , function ( obj ) {
if ( typeof obj === 'function' ) {
// Run on-load setup
obj ( ) ;
} else if ( typeof obj === 'string' ) {
2014-01-16 20:18:55 +01:00
// $(document).on usage
2013-12-04 17:16:08 +01:00
// Selector usage
return { on : function ( ) { } } ;
}
} ) ;
2014-01-04 00:21:06 +01:00
set _global ( 'emoji' , {
emojis _by _name : { emoji : 'some/url/here/emoji.png' }
} ) ;
2014-01-06 23:42:52 +01:00
set _global ( 'page_params' , {
realm _filters : [ [ "#(?P<id>[0-9]{2,8})" , "https://trac.zulip.net/ticket/%(id)s" ] ]
} ) ;
2014-01-04 01:18:30 +01:00
set _global ( 'people_by_name_dict' , Dict . from ( { 'Cordelia Lear' : { full _name : 'Cordelia Lear' , email : 'cordelia@zulip.com' } } ) ) ;
2013-12-04 17:16:08 +01:00
var echo = require ( 'js/echo.js' ) ;
2014-01-17 21:08:45 +01:00
var bugdown _data = JSON . parse ( fs . readFileSync ( path . join ( _ _dirname , '../../../fixtures/bugdown-data.json' ) , 'utf8' , 'r' ) ) ;
2013-12-04 17:16:08 +01:00
( function test _bugdown _detection ( ) {
var no _markup = [
"This is a plaintext message" ,
"This is a plaintext: message" ,
"This is a :plaintext message" ,
"This is a :plaintext message: message" ,
"Contains a not an image.jpeg/ok file" ,
"Contains a not an http://www.google.com/ok/image.png/stop file" ,
"No png to be found here, a png" ,
"No user mention **leo**" ,
2014-01-03 22:51:00 +01:00
"No user mention @what there" ,
2014-01-04 00:21:06 +01:00
"We like to code\n~~~\ndef code():\n we = \"like to do\"\n~~~" ,
"This is a\nmultiline :emoji: here\n message" ,
2014-01-04 01:18:30 +01:00
"This is an :emoji: message" ,
"User Mention @**leo**" ,
"User Mention @**leo f**" ,
"User Mention @**leo with some name**"
2013-12-04 17:16:08 +01:00
] ;
var markup = [
"Contains a https://zulip.com/image.png file" ,
"Contains a https://zulip.com/image.jpg file" ,
"https://zulip.com/image.jpg" ,
"also https://zulip.com/image.jpg" ,
"https://zulip.com/image.jpg too" ,
"Contains a zulip.com/foo.jpeg file" ,
"Contains a https://zulip.com/image.png file" ,
"twitter url https://twitter.com/jacobian/status/407886996565016579" ,
"https://twitter.com/jacobian/status/407886996565016579" ,
"then https://twitter.com/jacobian/status/407886996565016579" ,
"twitter url http://twitter.com/jacobian/status/407886996565016579" ,
"youtube url https://www.youtube.com/watch?v=HHZ8iqswiCw&feature=youtu.be&a" ,
"This contains !gravatar(leo@zulip.com)" ,
"And an avatar !avatar(leo@zulip.com) is here"
] ;
no _markup . forEach ( function ( content ) {
assert . equal ( echo . contains _bugdown ( content ) , false ) ;
} ) ;
markup . forEach ( function ( content ) {
assert . equal ( echo . contains _bugdown ( content ) , true ) ;
} ) ;
} ( ) ) ;
2014-01-17 21:08:45 +01:00
( function test _marked _shared ( ) {
var tests = bugdown _data . regular _tests ;
tests . forEach ( function ( test ) {
console . log ( "Doing " + test . name ) ;
var output = echo . apply _markdown ( test . input ) ;
if ( test . bugdown _matches _marked ) {
assert . equal ( test . expected _output , output ) ;
} else {
assert . notEqual ( test . expected _output , output ) ;
}
} ) ;
} ( ) ) ;
2013-12-04 17:16:08 +01:00
( function test _marked ( ) {
var test _cases = [
{ input : 'hello' , expected : '<p>hello</p>' } ,
{ input : 'hello there' , expected : '<p>hello there</p>' } ,
{ input : 'hello **bold** for you' , expected : '<p>hello <strong>bold</strong> for you</p>' } ,
{ input : '__hello__' , expected : '<p>__hello__</p>' } ,
2014-01-21 20:46:43 +01:00
{ input : '\n```\nfenced code\n```\n\nand then after\n' , expected : '<div class="codehilite"><pre>fenced code\n</pre></div>\n\n\n<p>and then after</p>' } ,
2013-12-04 17:16:08 +01:00
{ input : '* a\n* list \n* here' ,
expected : '<ul>\n<li>a</li>\n<li>list </li>\n<li>here</li>\n</ul>' } ,
{ input : 'Some text first\n* a\n* list \n* here\n\nand then after' ,
expected : '<p>Some text first</p>\n<ul>\n<li>a</li>\n<li>list </li>\n<li>here</li>\n</ul>\n<p>and then after</p>' } ,
{ input : '1. an\n2. ordered \n3. list' ,
2014-01-03 22:51:00 +01:00
expected : '<p>1. an</p>\n<p>2. ordered </p>\n<p>3. list</p>' } ,
{ input : '\n~~~quote\nquote this for me\n~~~\nthanks\n' ,
2014-01-21 20:46:43 +01:00
expected : '<blockquote>\n<p>quote this for me</p>\n</blockquote>\n\n\n<p>thanks</p>' } ,
2014-01-04 01:18:30 +01:00
{ input : 'This is a @**Cordelia Lear** mention' ,
expected : '<p>This is a <span class="user-mention" data-user-email="cordelia@zulip.com">@Cordelia Lear</span> mention</p>' } ,
{ input : 'This is an :emoji: message' ,
2014-01-06 23:42:52 +01:00
expected : '<p>This is an <img alt=":emoji:" class="emoji" src="some/url/here/emoji.png" title=":emoji:"> message</p>' } ,
{ input : 'This is a realm filter #1234 with text after it' ,
expected : '<p>This is a realm filter <a href="https://trac.zulip.net/ticket/1234" target="_blank" title="https://trac.zulip.net/ticket/1234">#1234</a> with text after it</p>' }
2013-12-04 17:16:08 +01:00
] ;
test _cases . forEach ( function ( test _case ) {
var input = test _case . input ;
var expected = test _case . expected ;
var output = echo . apply _markdown ( input ) ;
assert . equal ( expected , output ) ;
} ) ;
} ( ) ) ;