ruby-bbcode 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ require 'test_helper'
2
+
3
+ class RubyBbcodeValidityTest < Minitest::Test
4
+ def test_multiple_errors
5
+ input = '[b]Bold not closed, [li]Illegal list item[/li]'
6
+ errors = input.bbcode_check_validity
7
+ assert_equal 2, errors.length
8
+ assert_includes errors, "[b] not closed"
9
+ assert_includes errors, "[li] can only be used in [ul] and [ol], so using it in a [b] tag is not allowed"
10
+ end
11
+
12
+ def test_illegal_items
13
+ assert_equal ['[li] can only be used in [ul] and [ol]'],
14
+ '[li]Illegal item[/li]'.bbcode_check_validity
15
+ assert_equal ['[li] can only be used in [ul] and [ol], so using it in a [b] tag is not allowed'],
16
+ '[b][li]Illegal item[/li][/b]'.bbcode_check_validity
17
+ end
18
+
19
+ def test_illegal_list_contents
20
+ assert_equal ['[ul] can only contain [li] and [*] tags, so "Illegal list" is not allowed'],
21
+ '[ul]Illegal list[/ul]'.bbcode_check_validity
22
+ assert_equal ['[ul] can only contain [li] and [*] tags, so [b] is not allowed'],
23
+ '[ul][b]Illegal list[/b][/ul]'.bbcode_check_validity
24
+ end
25
+
26
+ def test_illegal_list_contents_text_between_list_items
27
+ assert_equal ['[ul] can only contain [li] and [*] tags, so "Illegal text" is not allowed'],
28
+ '[ul][li]item[/li]Illegal text[/ul]'.bbcode_check_validity
29
+ assert_equal ['[ul] can only contain [li] and [*] tags, so "Illegal text" is not allowed'],
30
+ '[ul][li]item[/li]Illegal text[li]item[/li][/ul]'.bbcode_check_validity
31
+ end
32
+
33
+ def test_unordered_list_omit_closing
34
+ errors = '[ul][li]item 1[li]item 2[/ul]'.bbcode_check_validity
35
+ assert_equal 5, errors.length
36
+
37
+ assert_includes errors, '[li] can only be used in [ul] and [ol], so using it in a [li] tag is not allowed'
38
+ assert_includes errors, 'Closing tag [/ul] doesn\'t match [li]'
39
+ assert_includes errors, '[ul] not closed'
40
+ assert_includes errors, '[li] not closed' # twice
41
+ end
42
+
43
+ def test_required_parameters
44
+ assert '[size=12]text[/size]'.bbcode_check_validity
45
+ assert '[size size=12]text[/size]'.bbcode_check_validity
46
+ assert_equal ['Tag [size] must have \'size\' parameter'], '[size]text[/size]'.bbcode_check_validity
47
+ end
48
+
49
+ def test_optional_parameters
50
+ assert '[img]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_check_validity
51
+ assert '[img width=100 height=100]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_check_validity
52
+
53
+ # Adding custom 'optional parameters' is not allowed though
54
+ assert_equal ['Tag [img] doesn\'t have a \'depth\' parameter'], '[img depth=100]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_check_validity
55
+ end
56
+
57
+ def test_quick_parameters
58
+ assert '[size=12]text[/size]'.bbcode_check_validity
59
+ assert_equal ['The size parameter \'\' is incorrect, a number is expected'], '[size=]text[/size]'.bbcode_check_validity
60
+ assert_equal ['The size parameter \'abc\' is incorrect, a number is expected'], '[size=abc]text[/size]'.bbcode_check_validity
61
+
62
+ assert '[img=100x200]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_check_validity
63
+ assert_equal ['The image parameters \'\' are incorrect, \'<width>x<height>\' excepted'], '[img=]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_check_validity
64
+ assert_equal ['The image parameters \'x\' are incorrect, \'<width>x<height>\' excepted'], '[img=x]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_check_validity
65
+ end
66
+
67
+ def test_illegal_link
68
+ assert_equal ['The URL should start with http:// https://, ftp:// or /, instead of \'index.html\''], '[url=index.html]Home[/url]'.bbcode_check_validity
69
+ assert_equal ['The URL should start with http:// https://, ftp:// or /, instead of \'www.google.com\''], '[url=www.google.com]Google[/url]'.bbcode_check_validity
70
+ assert_equal ['The URL should start with http:// https://, ftp:// or /, instead of \'htfp://www.google.com\''], '[url]htfp://www.google.com[/url]'.bbcode_check_validity
71
+ end
72
+
73
+ def test_no_ending_tag
74
+ assert_equal ["[b] not closed"], "this [b]should not be bold".bbcode_check_validity
75
+ end
76
+
77
+ def test_no_start_tag
78
+ assert_equal ["Closing tag [/b] doesn't match an opening tag"], "this should not be bold[/b]".bbcode_check_validity
79
+ end
80
+
81
+ def test_different_start_and_ending_tags
82
+ assert_equal ["Closing tag [/i] doesn't match [b]", "[b] not closed"], "this [b]should not do formatting[/i]".bbcode_check_validity
83
+ end
84
+
85
+ def test_failing_between_texts
86
+ assert_equal ['No text between [img] and [/img] tags.'], '[img][/img]'.bbcode_check_validity
87
+ assert_equal ['The URL should start with http:// https://, ftp:// or /, instead of \'illegal url\''], '[url]illegal url[/url]'.bbcode_check_validity
88
+ assert_equal ['Cannot determine multi-tag type: No text between [media] and [/media] tags.'], '[media][/media]'.bbcode_check_validity
89
+ end
90
+
91
+ def test_failing_media_tag
92
+ assert_equal ['Unknown multi-tag type for [media]'], '[media]http://www.youtoob.com/watch?v=cSohjlYQI2A[/media]'.bbcode_check_validity
93
+ end
94
+
95
+ def test_addition_of_tags
96
+ mydef = {
97
+ :test => {
98
+ :description => 'This is a test',
99
+ :example => '[test]Test here[/test]',
100
+ :param_tokens => [{:token => :param}]
101
+ }
102
+ }
103
+ # Currently, unknown tags are treated as text and no (missing) parameter values are checked for bbcode_check_validity
104
+ # So this test is quite boring
105
+ assert 'pre [test]Test here[/test] post'.bbcode_check_validity
106
+ assert 'pre [test]Test here[/test] post'.bbcode_check_validity(mydef)
107
+ end
108
+
109
+ # TODO: This stack level problem should be validated during the validations
110
+ #def test_stack_level_too_deep
111
+ # num = 2300 # increase this number if the test starts failing. It's very near the tipping point
112
+ # openers = "[s]hi i'm" * num
113
+ # closers = "[/s]" * num
114
+ # assert_raise( SystemStackError ) do
115
+ # (openers+closers).bbcode_to_html
116
+ # end
117
+ #end
118
+
119
+ end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
1
4
  require 'ruby-bbcode'
2
- require "test/unit"
5
+ require "minitest/autorun"
3
6
 
4
- # This hack allows us to make all the private methods of a class public.
7
+ # This hack allows us to make all the private methods of a class public.
5
8
  class Class
6
9
  def publicize_methods
7
10
  saved_private_instance_methods = self.private_instance_methods
@@ -1,51 +1,41 @@
1
1
  require 'test_helper'
2
2
 
3
- class TagSifterTest < Test::Unit::TestCase
3
+ class TagSifterTest < MiniTest::Test
4
+ include RubyBBCode::Tags
4
5
  def test_youtube_parser
5
6
  url1 = "http://www.youtube.com/watch?v=E4Fbk52Mk1w"
6
7
  just_an_id = 'E4Fbk52Mk1w'
7
8
  url_without_http = "www.youtube.com/watch?v=E4Fbk52Mk1w"
8
9
  url_without_www = "youtube.com/watch?v=E4Fbk52Mk1w"
9
10
  url_with_feature = "http://www.youtube.com/watch?feature=player_embedded&v=E4Fbk52Mk1w"
10
- mock_regex_matches = [/youtube.com.*[v]=([^&]*)/, /youtu.be\/([^&]*)/, /y2u.be\/([^&]*)/]
11
-
11
+ mock_regex_matches = @@tags[:youtube][:url_matches]
12
+
12
13
  expected_output = 'E4Fbk52Mk1w'
13
-
14
+
14
15
  RubyBBCode::TagSifter.publicize_methods do
15
16
  ts = RubyBBCode::TagSifter.new "", ""
16
- assert_equal expected_output,
17
- ts.conduct_special_formatting(url1, mock_regex_matches)
18
-
19
- assert_equal expected_output,
20
- ts.conduct_special_formatting(just_an_id, mock_regex_matches)
21
-
22
- assert_equal expected_output,
23
- ts.conduct_special_formatting(url_without_http, mock_regex_matches)
24
-
25
- assert_equal expected_output,
26
- ts.conduct_special_formatting(url_without_www, mock_regex_matches)
27
-
28
- assert_equal expected_output,
29
- ts.conduct_special_formatting(url_with_feature, mock_regex_matches)
17
+ assert_equal expected_output, ts.match_url_id(url1, mock_regex_matches)
18
+ assert_equal expected_output, ts.match_url_id(just_an_id, mock_regex_matches)
19
+ assert_equal expected_output, ts.match_url_id(url_without_http, mock_regex_matches)
20
+ assert_equal expected_output, ts.match_url_id(url_without_www, mock_regex_matches)
21
+ assert_equal expected_output, ts.match_url_id(url_with_feature, mock_regex_matches)
30
22
  end
31
-
23
+
32
24
  end
33
25
 
34
- # I think the answer to this is creating a new tag named [youtube]
35
- # but that captures specifically the .be or .com and treats them differently...
36
- def test_youtubes_via_there_url_shortener
37
- url_from_shortener = "http://youtu.be/E4Fbk52Mk1w"
38
- directory_format = "http://youtube.googleapis.com/v/E4Fbk52Mk1w"
26
+ # I think the answer to this is creating a new tag named [youtube]
27
+ # but that captures specifically the .be or .com and treats them differently...
28
+ def test_youtubes_via_there_url_shortener
29
+ url_from_shortener = "http://youtu.be/E4Fbk52Mk1w"
30
+ directory_format = "http://youtube.googleapis.com/v/E4Fbk52Mk1w"
39
31
  expected_output = 'E4Fbk52Mk1w'
40
- mock_regex_matches = [/youtube.com.*[v]=([^&]*)/, /youtu.be\/([^&]*)/, /y2u.be\/([^&]*)/]
32
+ mock_regex_matches = @@tags[:youtube][:url_matches]
41
33
 
42
-
43
34
  RubyBBCode::TagSifter.publicize_methods do
44
35
  ts = RubyBBCode::TagSifter.new "", ""
45
-
46
-
47
- assert_equal expected_output,
48
- ts.conduct_special_formatting(url_from_shortener, mock_regex_matches) # this test is now hopelessly broken because generating an ID from a link requires that @bbtree.current_node.definition be properly populated with regex matches...
49
- end
50
- end
36
+
37
+ # this test is now hopelessly broken because generating an ID from a link requires that @bbtree.current_node.definition be properly populated with regex matches...
38
+ assert_equal expected_output, ts.match_url_id(url_from_shortener, mock_regex_matches)
39
+ end
40
+ end
51
41
  end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class TagSifterTest < MiniTest::Test
4
+ def test_taglist_modification
5
+ tags = RubyBBCode::Tags.tag_list
6
+ assert_equal nil, RubyBBCode::Tags.tag_list[:test]
7
+ begin
8
+ tags[:test] = 'test'
9
+
10
+ assert_equal 'test', RubyBBCode::Tags.tag_list[:test]
11
+ ensure
12
+ # Always restore as this change is permanent (and messes with other tests)
13
+ tags.delete :test
14
+ end
15
+ assert_equal nil, RubyBBCode::Tags.tag_list[:test]
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-bbcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maarten Bezemer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-04 00:00:00.000000000 Z
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.0
41
69
  description: Convert BBCode to HTML and check whether the BBCode is valid.
42
70
  email: maarten.bezemer@gmail.com
43
71
  executables: []
@@ -57,14 +85,16 @@ files:
57
85
  - lib/ruby-bbcode/tag_info.rb
58
86
  - lib/ruby-bbcode/tag_node.rb
59
87
  - lib/ruby-bbcode/tag_sifter.rb
88
+ - lib/ruby-bbcode/templates/bbcode_errors_template.rb
89
+ - lib/ruby-bbcode/templates/html_template.rb
60
90
  - lib/ruby-bbcode/version.rb
61
91
  - lib/tags/tags.rb
62
- - test/current_test.rb
63
- - test/debugging.rb
64
- - test/ruby_bbcode_test.rb
92
+ - test/ruby_bbcode_bbcode_test.rb
93
+ - test/ruby_bbcode_html_test.rb
94
+ - test/ruby_bbcode_validity_test.rb
65
95
  - test/test_helper.rb
66
- - test/unit/debugging_test.rb
67
96
  - test/unit/tag_sifter_test.rb
97
+ - test/unit/tags_test.rb
68
98
  homepage: http://github.com/veger/ruby-bbcode
69
99
  licenses:
70
100
  - MIT
@@ -81,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
111
  requirements:
82
112
  - - ">="
83
113
  - !ruby/object:Gem::Version
84
- version: '0'
114
+ version: 1.9.3
85
115
  required_rubygems_version: !ruby/object:Gem::Requirement
86
116
  requirements:
87
117
  - - ">="
@@ -92,11 +122,11 @@ rubyforge_project:
92
122
  rubygems_version: 2.4.5
93
123
  signing_key:
94
124
  specification_version: 4
95
- summary: ruby-bbcode-1.0.1
125
+ summary: ruby-bbcode-2.0.0
96
126
  test_files:
97
127
  - test/test_helper.rb
98
- - test/current_test.rb
128
+ - test/ruby_bbcode_validity_test.rb
129
+ - test/ruby_bbcode_bbcode_test.rb
130
+ - test/ruby_bbcode_html_test.rb
99
131
  - test/unit/tag_sifter_test.rb
100
- - test/unit/debugging_test.rb
101
- - test/debugging.rb
102
- - test/ruby_bbcode_test.rb
132
+ - test/unit/tags_test.rb
data/test/current_test.rb DELETED
@@ -1,8 +0,0 @@
1
- require 'test_helper'
2
- require 'benchmark'
3
-
4
-
5
-
6
- class RubyBbcodeTest < Test::Unit::TestCase
7
-
8
- end
data/test/debugging.rb DELETED
@@ -1,93 +0,0 @@
1
- module RubyBBCode
2
- def self.log(string, clear_file = true)
3
- clear_log_file_at_beginning_of_execution clear_file
4
-
5
- File.open('/tmp/ruby-bbcode.log', 'a') do |f|
6
- f.puts string
7
- end
8
- end
9
-
10
- def self.clear_log_file_at_beginning_of_execution(clear_file)
11
- return if !clear_file
12
- if defined?(@@cleared_file).nil?
13
- @@cleared_file = true
14
- File.open('/tmp/ruby-bbcode.log', 'w+') do |f|
15
- puts ''
16
- end
17
- end
18
- end
19
-
20
-
21
- # This module can be included in the BBTree and TagNode to give them debugging features
22
- module DebugBBTree
23
- # For Debugging/ visualization purposes.
24
- # This can be used to render the #nodes array in a pretty manor, showing the hirarchy.
25
- def to_v
26
- tree = self
27
- visual_string = ''
28
-
29
- walk_tree(tree) do |node, depth|
30
- indentation = ' ' * depth
31
- case node[:is_tag]
32
- when true
33
- visual_string += "#{indentation}" + node[:tag].to_s + "\n"
34
- when false
35
- visual_string += "#{indentation}\"#{node[:text]}\"\n"
36
- end
37
- end
38
-
39
- visual_string
40
- end
41
-
42
-
43
- # this blocky method counts how many children are
44
- # in the TagNode.children, recursively walking the tree
45
- def count_child_nodes(hash = self)
46
- count = 0
47
- walk_tree(hash) do
48
- count += 1
49
- end
50
- count
51
- end
52
-
53
- # For BBTree, teh to_s method shows the count of the children plus a graphical
54
- # depiction of the tree, and the relation of the nodes.
55
- # For TagNodes, the root-most tag is displayed, and the children are counted.
56
- def to_s
57
- object_identifier = "#<#{self.class.to_s}:0x#{'%x' % (self.object_id << 1)}\n"
58
- close_object = ">\n"
59
-
60
- case self
61
- when RubyBBCode::BBTree
62
- object_identifier + "Children: #{count_child_nodes}\n" + self.to_v + close_object
63
- when RubyBBCode::TagNode # when inspecting TagNodes, it's better not to show the tree display
64
- if self[:is_tag]
65
- object_identifier + "Tag: #{self[:tag].to_s}, Children: #{count_child_nodes}\n" + close_object
66
- else
67
- object_identifier + '"' + self[:text].to_s + "\"\n" + close_object
68
- end
69
- end
70
- end
71
-
72
- private
73
-
74
- # This function is used by to_v and anything else that needs to iterate through the
75
- # @bbtree
76
- def walk_tree(tree, depth = -1, &blk)
77
- return enum_for(:walk_tree) unless blk # ignore me for now, I'm a convention for being versatile
78
-
79
- # Perform the block action specified at top level!!!
80
- yield tree, depth unless depth == -1
81
-
82
- # next if we're a text node
83
- return if tree.type == :text
84
-
85
- # Enter into recursion (including block action) for each child node in this node
86
- tree.children.each do |node|
87
- walk_tree(node, depth + 1, &blk)
88
- end
89
- end
90
-
91
- end
92
-
93
- end
@@ -1,48 +0,0 @@
1
- require 'test_helper'
2
- require 'debugging'
3
-
4
- class RubyBbcodeTest < Test::Unit::TestCase
5
- include ::RubyBBCode::Tags
6
-
7
- def test_bbtree_to_v
8
- text = "[i][b]a[/b][b]a[/b][b]a[/b][b]a[/b]item 1[/i][i]item 2[/i]"
9
- visual = <<eos
10
- i
11
- b
12
- "a"
13
- b
14
- "a"
15
- b
16
- "a"
17
- b
18
- "a"
19
- "item 1"
20
- i
21
- "item 2"
22
- eos
23
-
24
- tags = @@tags
25
- @tag_sifter = RubyBBCode::TagSifter.new(text, tags)
26
- @tag_sifter.process_text
27
-
28
- # manually include the debugging methods if they've been disabled for cleanliness purposes
29
- @tag_sifter.bbtree.extend(::RubyBBCode::DebugBBTree) unless @tag_sifter.bbtree.respond_to? :to_v
30
-
31
- assert_equal visual, @tag_sifter.bbtree.to_v
32
- end
33
-
34
- def test_bbtree_counting_nodes
35
- text = "[ol][li][b][/b][b][/b][b][/b][b][/b]item 1[/li][li]item 2[/li][/ol]"
36
- tags = @@tags
37
-
38
- @tag_sifter = RubyBBCode::TagSifter.new(text, tags)
39
-
40
- @tag_sifter.process_text
41
-
42
- # manually include debugging methods if needed.
43
- @tag_sifter.bbtree.extend(::RubyBBCode::DebugBBTree) unless @tag_sifter.bbtree.respond_to? :count_child_nodes
44
-
45
- assert_equal 9, @tag_sifter.bbtree.count_child_nodes
46
- end
47
-
48
- end