From 916cc42e0bef4db381e82031cda4e78b15a24d76 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 10 Sep 2015 22:13:27 +0300 Subject: [PATCH] Do not use setfenv in tests --- markdown-tests.lua | 102 +++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 54 deletions(-) diff --git a/markdown-tests.lua b/markdown-tests.lua index b234cff..fff1499 100644 --- a/markdown-tests.lua +++ b/markdown-tests.lua @@ -1,6 +1,4 @@ -if not markdown then - require "markdown" -end +local markdown = require "markdown" -- Splits the text into an array of separate lines. local function split(text, sep) @@ -26,23 +24,19 @@ end -- Set up a table to store all the tests. Customize __newindex to record the -- order in which the tests are created so that we can process them in the right -- order. -local TESTS = {} -local TESTS_MT = { - __index = _G, - __newindex = function(t,k,v) - if k:sub(1,1) == "_" then rawset(t,k,v) return end +local tests_mt = { + __newindex = function(t, k, v) + if type(k) == "string" then + table.insert(t, k) + end - if t._indices == nil then t._indices = {} end - table.insert(t._indices, k) - rawset(t,k,v) + rawset(t, k, v) end } - -setmetatable(TESTS, TESTS_MT) -setfenv(1, TESTS) +local tests = setmetatable({}, tests_mt) -- Test header markers -headers = [[ +tests.headers = [[ This is an h1 ============ @@ -89,7 +83,7 @@ This is an h2 ]] -- Test blockquotes -blockquotes = [[ +tests.blockquotes = [[ > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, > consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. > Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus. @@ -163,7 +157,7 @@ id sem consectetuer libero luctus adipiscing. ]] -- Test lists -lists = [[ +tests.lists = [[ * Red * Green * Blue @@ -339,7 +333,7 @@ sit amet, consectetuer adipiscing elit.

]] -- Test code blocks -code_blocks = [[ +tests.code_blocks = [[ This is a normal paragraph: This is a code block. @@ -380,7 +374,7 @@ And _underlines_. ]] -- Test rules -rules = [[ +tests.rules = [[ * * * *** @@ -403,7 +397,7 @@ rules = [[ ]] -- Test links -links = [[ +tests.links = [[ This is [an example](http://example.com/ "Title") inline link. ~

This is an example inline link.

@@ -491,7 +485,7 @@ or MSN.

]] -- Test emphasis -emphasis = [[ +tests.emphasis = [[ Test *single asterisks* test. ~

Test single asterisks test.

@@ -522,7 +516,7 @@ Test with *two* different *words*. ]] -- Test code -code = [[ +tests.code = [[ Use the `printf()` function. ~

Use the printf() function.

@@ -554,7 +548,7 @@ wrapped with an ]] -- Test images -images = [[ +tests.images = [[ ![Alt text](/path/to/img.jpg) ![Alt text](/path/to/img.jpg "Optional title") @@ -571,7 +565,7 @@ images = [[ ]] -- Test autolinking -autolinks = [[ +tests.autolinks = [[ An auto link . ~

An auto link http://www.google.com/.

@@ -588,14 +582,14 @@ Mail links and . ]] -- Test escape codes -escapes = [[ +tests.escapes = [[ \*literal asterisks\* ~

*literal asterisks*

]] -- Test hard linebreaks -linebreaks = [[ +tests.linebreaks = [[ Poetry in motion @@ -606,7 +600,7 @@ motion

]] -- Test amps and angles conversion -amps = [[ +tests.amps = [[ © ~

©

@@ -621,7 +615,7 @@ AT&T ]] -- Test raw blocks -raw = [[ +tests.raw = [[
This should not be disturbed by Markdown.
@@ -633,7 +627,7 @@ raw = [[ -- Markdown test suit -markdown_amps_and_angles = [[ +tests.markdown_amps_and_angles = [[ AT&T has an ampersand in their name. AT&T is another way to write it. @@ -674,7 +668,7 @@ Here's an inline [link]().

Here's an inline link.

]] -markdown_auto_links = [[ +tests.markdown_auto_links = [[ Link: . With an ampersand: @@ -709,7 +703,7 @@ Auto-links should not occur here: `` ]] -markdown_backslash_escapes = [[ +tests.markdown_backslash_escapes = [[ These should all get escaped: Backslash: \\ @@ -919,7 +913,7 @@ Minus: \-

Minus: \-

]] -markdown_blockquotes_with_codeblocks = [[ +tests.markdown_blockquotes_with_codeblocks = [[ > Example: > > sub status { @@ -949,7 +943,7 @@ markdown_blockquotes_with_codeblocks = [[ ]] -markdown_hard_wrapped_paragraphs_with_list_like_lines = [[ +tests.markdown_hard_wrapped_paragraphs_with_list_like_lines = [[ In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item. Because a hard-wrapped line in the @@ -969,7 +963,7 @@ list item.

* criminey.

]] -markdown_horizontal_rules = [[ +tests.markdown_horizontal_rules = [[ Dashes: --- @@ -1113,7 +1107,7 @@ _ _ _ -- currently not supported by regular markdown, so we ignore it too --[[ -markdown_inline_html_advanced = +tests.markdown_inline_html_advanced = Simple block on one line:
foo
@@ -1145,7 +1139,7 @@ foo ]] -markdown_inline_html_simple = [[ +tests.markdown_inline_html_simple = [[ Here's a simple block:
@@ -1289,7 +1283,7 @@ Blah
]] -markdown_inline_html_comments = [[ +tests.markdown_inline_html_comments = [[ Paragraph one. @@ -1319,7 +1313,7 @@ The end.

The end.

]] -markdown_links_inline_style = [[ +tests.markdown_links_inline_style = [[ Just a [URL](/url/). [URL and title](/url/ "title"). @@ -1341,7 +1335,7 @@ Just a [URL](/url/).

Empty.

]] -markdown_links_reference_style = [=[ +tests.markdown_links_reference_style = [=[ Foo [bar] [1]. Foo [bar][1]. @@ -1394,7 +1388,7 @@ Indented [four][] times. ]=] -markdown_literal_quotes_in_text = [=[ +tests.markdown_literal_quotes_in_text = [=[ Foo [bar][]. Foo [bar](/url/ "Title with "quotes" inside"). @@ -1407,7 +1401,7 @@ Foo [bar](/url/ "Title with "quotes" inside").

Foo bar.

]=] -markdown_basics = [[ +tests.markdown_basics = [[ Markdown: Basics ================ @@ -2031,7 +2025,7 @@ you've got to put paragraph tags in your blockquotes:</p> ]] -markdown_syntax = [[ +tests.markdown_syntax = [[ Markdown: Syntax ================ @@ -3864,7 +3858,7 @@ _ underscore ]] -markdown_nested_blockquotes = [[ +tests.markdown_nested_blockquotes = [[ > foo > > > bar @@ -3882,7 +3876,7 @@ markdown_nested_blockquotes = [[ ]] -markdown_ordered_and_unordered_lists = [[ +tests.markdown_ordered_and_unordered_lists = [[ ## Unordered Asterisks tight: @@ -4145,7 +4139,7 @@ back.

]] -markdown_strong_and_em_together = [[ +tests.markdown_strong_and_em_together = [[ ***This is strong and em.*** So is ***this*** word. @@ -4163,7 +4157,7 @@ So is ___this___ word.

So is this word.

]] -markdown_tabs = [[ +tests.markdown_tabs = [[ + this is a list item indented with tabs @@ -4213,7 +4207,7 @@ indented with spaces

]] -markdown_tidyness = [[ +tests.markdown_tidyness = [[ > A list within a blockquote: > > * asterisk 1 @@ -4230,7 +4224,7 @@ markdown_tidyness = [[ ]] -bug_from_paul_chiusano_gmail_com = [[ +tests.bug_from_paul_chiusano_gmail_com = [[ [context-free grammar][CFG] [notated][BNF] [unrestricted grammar][] @@ -4248,7 +4242,7 @@ bug_from_paul_chiusano_gmail_com = [[

foo()

]] -links = [[ +tests.links = [[ [the_dog](the_dog.html) ~

the_dog

@@ -4278,7 +4272,7 @@ _Kalle_

Kalle

]] -bugs = [==[ +tests.bugs = [==[ [the dog](the%20dog.html) ~

the dog

@@ -4400,7 +4394,7 @@ This is [an example] [id] reference-style link. -- Unhandled test: _M*A*S*H_ -setfenv(1, _G) +local quiet_mode -- Test running function local function run_tests() @@ -4460,9 +4454,9 @@ local function run_tests() return failed end - total_failed = 0 - for _,k in ipairs(TESTS._indices) do - total_failed = total_failed + run_test(k,TESTS[k]) + local total_failed = 0 + for _,k in ipairs(tests) do + total_failed = total_failed + run_test(k,tests[k]) end if total_failed > 0 then os.exit(-1)