wlang 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/LICENCE.rdoc +25 -0
  2. data/README.rdoc +111 -0
  3. data/bin/wlang +24 -0
  4. data/doc/specification/about.rdoc +61 -0
  5. data/doc/specification/dialects.wtpl +0 -0
  6. data/doc/specification/examples.rb +3 -0
  7. data/doc/specification/glossary.wtpl +14 -0
  8. data/doc/specification/hosting.rdoc +0 -0
  9. data/doc/specification/overview.rdoc +116 -0
  10. data/doc/specification/rulesets.wtpl +87 -0
  11. data/doc/specification/specification.css +52 -0
  12. data/doc/specification/specification.html +1361 -0
  13. data/doc/specification/specification.js +8 -0
  14. data/doc/specification/specification.wtpl +41 -0
  15. data/doc/specification/specification.yml +430 -0
  16. data/doc/specification/symbols.wtpl +16 -0
  17. data/lib/wlang.rb +186 -0
  18. data/lib/wlang/basic_object.rb +19 -0
  19. data/lib/wlang/dialect.rb +230 -0
  20. data/lib/wlang/dialect_dsl.rb +136 -0
  21. data/lib/wlang/dialect_loader.rb +69 -0
  22. data/lib/wlang/dialects/coderay_dialect.rb +35 -0
  23. data/lib/wlang/dialects/plain_text_dialect.rb +75 -0
  24. data/lib/wlang/dialects/rdoc_dialect.rb +33 -0
  25. data/lib/wlang/dialects/ruby_dialect.rb +35 -0
  26. data/lib/wlang/dialects/sql_dialect.rb +38 -0
  27. data/lib/wlang/dialects/standard_dialects.rb +113 -0
  28. data/lib/wlang/dialects/xhtml_dialect.rb +40 -0
  29. data/lib/wlang/encoder.rb +66 -0
  30. data/lib/wlang/encoder_set.rb +117 -0
  31. data/lib/wlang/errors.rb +37 -0
  32. data/lib/wlang/intelligent_buffer.rb +94 -0
  33. data/lib/wlang/parser.rb +251 -0
  34. data/lib/wlang/parser_context.rb +146 -0
  35. data/lib/wlang/ruby_extensions.rb +21 -0
  36. data/lib/wlang/rule.rb +66 -0
  37. data/lib/wlang/rule_set.rb +93 -0
  38. data/lib/wlang/rulesets/basic_ruleset.rb +75 -0
  39. data/lib/wlang/rulesets/buffering_ruleset.rb +103 -0
  40. data/lib/wlang/rulesets/context_ruleset.rb +115 -0
  41. data/lib/wlang/rulesets/encoding_ruleset.rb +73 -0
  42. data/lib/wlang/rulesets/imperative_ruleset.rb +132 -0
  43. data/lib/wlang/rulesets/ruleset_utils.rb +296 -0
  44. data/lib/wlang/template.rb +79 -0
  45. data/lib/wlang/wlang_command.rb +54 -0
  46. data/lib/wlang/wlang_command_options.rb +158 -0
  47. data/test/sandbox.rb +1 -0
  48. data/test/test_all.rb +8 -0
  49. data/test/wlang/anagram_bugs_test.rb +111 -0
  50. data/test/wlang/basic_ruleset_test.rb +52 -0
  51. data/test/wlang/buffering_ruleset_test.rb +102 -0
  52. data/test/wlang/buffering_template1.wtpl +1 -0
  53. data/test/wlang/buffering_template2.wtpl +1 -0
  54. data/test/wlang/buffering_template3.wtpl +1 -0
  55. data/test/wlang/buffering_template4.wtpl +1 -0
  56. data/test/wlang/buffering_template5.wtpl +1 -0
  57. data/test/wlang/context_ruleset_test.rb +32 -0
  58. data/test/wlang/data.rb +3 -0
  59. data/test/wlang/encoder_set_test.rb +42 -0
  60. data/test/wlang/imperative_ruleset_test.rb +107 -0
  61. data/test/wlang/intelligent_buffer_test.rb +194 -0
  62. data/test/wlang/othersymbols_test.rb +16 -0
  63. data/test/wlang/parser_context_test.rb +29 -0
  64. data/test/wlang/parser_test.rb +89 -0
  65. data/test/wlang/plain_text_dialect_test.rb +21 -0
  66. data/test/wlang/ruby_dialect_test.rb +100 -0
  67. data/test/wlang/ruby_expected.rb +3 -0
  68. data/test/wlang/ruby_template.wrb +3 -0
  69. data/test/wlang/ruleset_utils_test.rb +245 -0
  70. data/test/wlang/specification_examples_test.rb +52 -0
  71. data/test/wlang/test_utils.rb +25 -0
  72. data/test/wlang/wlang_test.rb +80 -0
  73. metadata +136 -0
@@ -0,0 +1,16 @@
1
+ require 'wlang'
2
+ require 'test/unit'
3
+ module WLang
4
+ class OtherSymbolsTest < Test::Unit::TestCase
5
+
6
+ def test_on_parentheses
7
+ template = "+{who}"
8
+ assert_equal 'blambeau', WLang.instantiate(template, {'who' => 'blambeau'}, 'wlang/active-string', :braces).to_s
9
+ template = "+(who)"
10
+ assert_equal 'blambeau', WLang.instantiate(template, {'who' => 'blambeau'}, 'wlang/active-string', :parentheses).to_s
11
+ template = "+[who]"
12
+ assert_equal 'blambeau', WLang.instantiate(template, {'who' => 'blambeau'}, 'wlang/active-string', :brackets).to_s
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require 'wlang'
3
+ require 'wlang/parser_context'
4
+ module WLang
5
+
6
+ #
7
+ # Tests WLang::Parser::Context class
8
+ #
9
+ class ParserContextTest < Test::Unit::TestCase
10
+
11
+ # Sets a context instance
12
+ def setup
13
+ customer = Struct.new(:name, :address)
14
+ hash = {"name" => "blambeau", "age" => 28,
15
+ "customer" => customer.new("Dave", "123 Main")}
16
+ @context = WLang::Parser::Context.new(hash)
17
+ end
18
+
19
+ # Tests Context#evaluate
20
+ def test_evaluate
21
+ assert_equal("blambeau", @context.evaluate("name"))
22
+ assert_equal(28, @context.evaluate("age"))
23
+ assert_equal("123 Main", @context.evaluate("customer.address"))
24
+ assert_not_nil @context.evaluate("self")
25
+ end
26
+
27
+ end # class ParserContextTest
28
+
29
+ end # module WLang
@@ -0,0 +1,89 @@
1
+ require 'test/unit/testcase'
2
+ require 'wlang'
3
+ module WLang
4
+
5
+ class ParserTest < Test::Unit::TestCase
6
+
7
+ # Installs some dialects on wlang
8
+ def setup
9
+ # wlang dialect, empty by default
10
+ WLang::dialect "example" do
11
+ rule '+' do |parser,offset|
12
+ parsed, reached = parser.parse(offset)
13
+ [parsed.upcase, reached]
14
+ end
15
+ rule '-' do |parser,offset|
16
+ parsed, reached = parser.parse(offset)
17
+ [parsed.downcase, reached]
18
+ end
19
+ end
20
+ end
21
+
22
+ # Instantiate the given string as template
23
+ def instantiate_str(str)
24
+ Parser.instantiator(Template.new(str, "example")).instantiate()[0]
25
+ end
26
+
27
+ # Asserts the result of an instanciation
28
+ def assert_instanciation_equal(expected, template, msg=nil)
29
+ template = Template.new(template, "example")
30
+ parser = Parser.instantiator(template)
31
+ assert_equal(expected, parser.instantiate()[0])
32
+ end
33
+
34
+ def test_parser_accepts_whole_tag
35
+ template, expected = "+{hello}", "HELLO"
36
+ assert_instanciation_equal(expected, template)
37
+ template, expected = " +{hello} ", " HELLO "
38
+ assert_instanciation_equal(expected, template)
39
+ end
40
+
41
+ def test_parser_accepts_whole_block
42
+ template, expected = "{hello}", "{hello}"
43
+ assert_instanciation_equal(expected, template)
44
+ end
45
+
46
+ def test_parser_ignores_backslashed_tags
47
+ template, expected = "\\+{hello\\}", "+{hello}"
48
+ assert_instanciation_equal(expected, template)
49
+ end
50
+
51
+ def test_parser_ignores_backslashed_blocks
52
+ template, expected = "\\{hello\\} world", "{hello} world"
53
+ assert_instanciation_equal(expected, template)
54
+ end
55
+
56
+ def test_parser_on_a_complex_case
57
+ template = "Hello \\\\} world! -{ITEMS AS I}{do +{something} \\{with \\} i} here is my name:+{name} and { that's not -{COMMON} text } !\\{"
58
+ expected = "Hello \\} world! items as i{do SOMETHING {with } i} here is my name:NAME and { that's not common text } !\{"
59
+ assert_instanciation_equal(expected, template)
60
+ end
61
+
62
+ def test_parser_raises_error
63
+ # Unclosed tag
64
+ assert_raise(ParseError){instantiate_str("-{unclosedtag")}
65
+ end
66
+
67
+ def assert_error_at_line_column(template, line, column)
68
+ begin
69
+ instantiate_str(template)
70
+ assert(false)
71
+ rescue ParseError => ex
72
+ assert_equal ex.line, line, "Line matches"
73
+ assert_equal ex.column, column, "Column matches"
74
+ end
75
+ end
76
+
77
+ def test_parser_error_find_line_and_column
78
+ assert_error_at_line_column("-{tag", 1, 5)
79
+ assert_error_at_line_column("-{tag with spaces ", 1, 20)
80
+ assert_error_at_line_column("-{tag as i}{\n", 1, 12)
81
+ assert_error_at_line_column("-{tag as i}{\n\n", 1, 12)
82
+ assert_error_at_line_column("-{tag as i}{\ntext\n", 2, 4)
83
+ assert_error_at_line_column("-{tag as i}{\n\ntext\n", 3, 4)
84
+ assert_error_at_line_column("-{tag as i}{\n\n\ntext\n", 4, 4)
85
+ end
86
+
87
+ end # ParserTest
88
+
89
+ end # WLang
@@ -0,0 +1,21 @@
1
+ require 'test/unit/testcase'
2
+ require 'wlang'
3
+ require 'wlang/dialects/plain_text_dialect'
4
+
5
+ class WLang::PlainTestDialectTest < Test::Unit::TestCase
6
+
7
+ # Tests the camel case function
8
+ def test_camel_case
9
+ tests = [
10
+ ["blambeau", "Blambeau"],
11
+ ["the world and the cat", "TheWorldAndTheCat"],
12
+ ["\nthe\t world and-the_cat", "TheWorldAndTheCat"],
13
+ #["l'éléphant", "LElephant"],
14
+ ]
15
+ tests.each do |t|
16
+ src, expected = t
17
+ assert_equal(expected, WLang::EncoderSet::PlainText.camel_case(src, nil))
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,100 @@
1
+ require 'test/unit/testcase'
2
+ require 'wlang'
3
+ require 'wlang/dialects/standard_dialects'
4
+ module WLang
5
+
6
+ # Tests the ruby dialect
7
+ class RubyDialectTest < Test::Unit::TestCase
8
+
9
+ def get_file_contents(file)
10
+ file = File.join(File.dirname(__FILE__), file)
11
+ File.read(file)
12
+ end
13
+
14
+ # Tests on template in HERE documents above
15
+ def test_on_template
16
+ $context = {"name" => "O'Neil"}
17
+ $template = get_file_contents('ruby_template.wrb')
18
+ $expected = get_file_contents('ruby_expected.rb')
19
+ result = $template.wlang($context, "wlang/ruby")
20
+ # puts "\n--template--"
21
+ # puts $template
22
+ # puts "\n--expected--"
23
+ # puts $expected
24
+ # puts "\n--result--"
25
+ # puts result
26
+ assert_equal($expected,result)
27
+ end
28
+
29
+ # Tests single quoting
30
+ def test_single_quoting
31
+ context = {}
32
+ tests = [
33
+ ["Hello world!", "Hello world!"],
34
+ ["Hello &'{world}!", "Hello world!"],
35
+ ["Hello &'{men's world}!", "Hello men\\'s world!"],
36
+ ["puts 'Hello &'{men's world}!'", "puts 'Hello men\\'s world!'"],
37
+ ["Hello O\'Neil", "Hello O\'Neil"],
38
+ ["Hello O\\'Neil", "Hello O\\'Neil"],
39
+ ["Hello O\\\\'Neil", "Hello O\\\\'Neil"],
40
+ ]
41
+ tests.each do |test|
42
+ expected = test[1]
43
+ result = test[0].wlang(context, "wlang/ruby")
44
+ assert_equal(expected, result)
45
+ end
46
+
47
+ context = {"name" => %q{O\'Neil}}
48
+ template = %q{puts '{name}'}
49
+ expected = %q{puts 'O\'Neil'}
50
+ result = template.wlang(context, "wlang/ruby")
51
+ assert_equal(expected, result)
52
+ end
53
+
54
+ # Tests double quoting
55
+ def test_double_quoting
56
+ context = {}
57
+ tests = [
58
+ ['Hello world!', 'Hello world!'],
59
+ ['Hello &"{world}!', 'Hello world!'],
60
+ ['Hello &"{men"s world}!', 'Hello men\\"s world!'],
61
+ ['puts "Hello &"{men"s world}!"', 'puts "Hello men\\"s world!"']
62
+ ]
63
+ tests.each do |test|
64
+ expected = test[1]
65
+ result = test[0].wlang(context, "wlang/ruby")
66
+ assert_equal(expected, result)
67
+ end
68
+ end
69
+
70
+ # Tests single quoted
71
+ def test_single_quoted
72
+ context = {"name" => "blambeau", "mensworld" => "men's world"}
73
+ tests = [
74
+ ["puts '{name}'", "puts 'blambeau'"],
75
+ ["puts '{mensworld}'", "puts 'men\\'s world'"],
76
+ ]
77
+ tests.each do |test|
78
+ expected = test[1]
79
+ result = test[0].wlang(context, "wlang/ruby")
80
+ assert_equal(expected, result)
81
+ end
82
+ end
83
+
84
+ # Tests double quoted
85
+ def test_double_quoted
86
+ context = {"name" => "blambeau", "mensworld" => 'men"s world'}
87
+ tests = [
88
+ ['puts "{name}"', 'puts "blambeau"'],
89
+ ['puts "{mensworld}"', 'puts "men\\"s world"'],
90
+ ]
91
+ tests.each do |test|
92
+ expected = test[1]
93
+ result = test[0].wlang(context, "wlang/ruby")
94
+ assert_equal(expected, result)
95
+ end
96
+ end
97
+
98
+ end # class RubyDialectTest
99
+
100
+ end # module WLang
@@ -0,0 +1,3 @@
1
+ puts 'Hello world'
2
+ puts 'Hello O\'Neil'
3
+ puts 'O\'Neil'
@@ -0,0 +1,3 @@
1
+ puts '&'{Hello world}'
2
+ puts '&'{Hello O'Neil}'
3
+ puts '{name}'
@@ -0,0 +1,245 @@
1
+ require 'test/unit'
2
+ require 'wlang'
3
+ require 'wlang/rulesets/ruleset_utils'
4
+
5
+ # Tests the ruleset utilities.
6
+ class WLang::RuleSetUtilsTest < Test::Unit::TestCase
7
+ include WLang::RuleSet::Utils
8
+
9
+ # Tests decoding qdialects
10
+ def test_decode_qdialect()
11
+ tests = ["wlang", "xhtml", "\nxhtml ", "plain-text", "wlang/plain-text", " wlang/sql"]
12
+ tests.each do |t|
13
+ expected = {:qdialect => t.strip}
14
+ result = WLang::RuleSet::Utils.expr(:qdialect).decode(t)
15
+ assert_equal(expected, result)
16
+ end
17
+ end
18
+
19
+ # Tests decoding qdialects
20
+ def test_decode_qdialect_as()
21
+ tests = ["wlang as var", "xhtml as var", "\nxhtml as var ",
22
+ "plain-text as var", "wlang/plain-text\tas\tvar", " wlang/sql as var"]
23
+ tests.each do |t|
24
+ /^\s*([^\s]+)\s+as\s+([^\s]+)\s*$/ =~ t
25
+ expected = {:qdialect => $1, :as => $2}
26
+ result = WLang::RuleSet::Utils.expr(:qdialect, ["as", :var]).decode(t)
27
+ assert_equal(expected, result)
28
+ end
29
+ end
30
+
31
+ # Tests decoding qdialects
32
+ def test_decode_qdialect_as_with_optional()
33
+ tests = ["wlang", "xhtml", "\nxhtml ", "plain-text", "wlang/plain-text", " wlang/sql"]
34
+ tests.each do |t|
35
+ expected = {:qdialect => t.strip, :as => nil}
36
+ result = WLang::RuleSet::Utils.expr(:qdialect, ["as", :var, false]).decode(t)
37
+ assert_equal(expected, result)
38
+ end
39
+ end
40
+
41
+ def test_decode_with_a_with
42
+ source = "/hello with who: 'blambeau'"
43
+ result = WLang::RuleSet::Utils.expr(:expr, ["using", :using, false], ["with", :with, false]).decode(source)
44
+ assert_equal({:expr => '/hello', :with => {'who' => "'blambeau'"}, :using => nil}, result)
45
+
46
+ source = "/hello using self with who: 'blambeau'"
47
+ result = WLang::RuleSet::Utils.expr(:expr, ["using", :using, false], ["with", :with, false]).decode(source)
48
+ assert_equal({:expr => '/hello', :with => {'who' => "'blambeau'"}, :using => ['self']}, result)
49
+
50
+ source = "buffering_template5.wtpl using self with who2: who"
51
+ result = WLang::RuleSet::Utils.expr(:uri, ["using", :using, false], ["with", :with, false]).decode(source)
52
+ assert_equal({:uri => 'buffering_template5.wtpl', :with => {'who2' => "who"}, :using => ['self']}, result)
53
+ end
54
+
55
+ def test_DIALECT_regexp
56
+ assert_not_nil(RG_DIALECT =~ "wlang")
57
+ assert_not_nil(RG_DIALECT =~ "xhtml")
58
+ assert_not_nil(RG_DIALECT =~ "\nxhtml ")
59
+ assert_not_nil(RG_DIALECT =~ "plain-text")
60
+ assert_nil(RG_DIALECT =~ "wlang/plain-text")
61
+ assert_nil(RG_DIALECT =~ " wlang/sql ")
62
+ assert_nil(RG_DIALECT =~ "WLang")
63
+ end
64
+
65
+ def test_ENCODER_regexp
66
+ assert_not_nil(RG_ENCODER =~ "entities-encoding")
67
+ assert_not_nil(RG_ENCODER =~ "quoting")
68
+ assert_not_nil(RG_ENCODER =~ "\nquoting ")
69
+ assert_nil(RG_ENCODER =~ "xhtml/entities-encoding")
70
+ assert_nil(RG_ENCODER =~ " wlang/quoting ")
71
+ assert_nil(RG_ENCODER =~ "ENCODING")
72
+ end
73
+
74
+ def test_QDIALECT_regexp
75
+ assert_not_nil(RG_QDIALECT =~ "wlang")
76
+ assert_not_nil(RG_QDIALECT =~ "wlang/plain-text")
77
+ assert_not_nil(RG_QDIALECT =~ "wlang/xhtml")
78
+ assert_not_nil(RG_QDIALECT =~ "wlang/sql")
79
+ assert_not_nil(RG_QDIALECT =~ " wlang/sql ")
80
+ assert_not_nil(RG_QDIALECT =~ "wlang/sql/sybase")
81
+ assert_nil(RG_QDIALECT =~ "WLang")
82
+ assert_nil(RG_QDIALECT =~ "wlang/ sql")
83
+ assert_nil(RG_QDIALECT =~ "WLang/sql")
84
+ assert_nil(RG_QDIALECT =~ "wlang/sql/ sybase")
85
+ end
86
+
87
+ def test_QENCODER_regexp
88
+ assert_not_nil(RG_QENCODER =~ "entities-encoding")
89
+ assert_not_nil(RG_QENCODER =~ "html/entities-encoding")
90
+ assert_not_nil(RG_QENCODER =~ "sql/back-quoting")
91
+ assert_not_nil(RG_QENCODER =~ "sql/back-quoting ")
92
+ assert_not_nil(RG_QENCODER =~ "sql/sybase/back-quoting")
93
+ assert_nil(RG_QENCODER =~ "sql/ sybase/back-quoting")
94
+ end
95
+
96
+ def test_VAR_regexp
97
+ assert_not_nil(RG_VAR =~ "name")
98
+ assert_not_nil(RG_VAR =~ "name_with_underscore")
99
+ assert_not_nil(RG_VAR =~ "\nname_with_underscore \t")
100
+ assert_nil(RG_VAR =~ "\nname _with_underscore \t")
101
+ tests = [
102
+ [{:variable =>"name"}, "name"],
103
+ [{:variable =>"name_with_underscore"}, "\nname_with_underscore \t"],
104
+ [nil, "\nname _with_underscore \t"],
105
+ ]
106
+ tests.each do |t|
107
+ expected, src = t
108
+ assert_equal(expected, WLang::RuleSet::Utils.decode_var(src))
109
+ end
110
+ end
111
+
112
+ def test_EXPR_regexp
113
+ assert_not_nil(RG_EXPR =~ "name")
114
+ assert_not_nil(RG_EXPR =~ "[12, 13]")
115
+ assert_not_nil(RG_EXPR =~ "Kernel.eval('some expression')")
116
+ end
117
+
118
+ def test_QDIALECT_AS_regexp
119
+ assert_not_nil(RG_QDIALECT_AS =~ "wlang as x")
120
+ assert_not_nil(RG_QDIALECT_AS =~ "wlang/xhtml as var")
121
+ assert_not_nil(RG_QDIALECT_AS =~ " wlang/xhtml as var ")
122
+ assert_nil(RG_QDIALECT_AS =~ "wlang/xhtml")
123
+ assert_nil(RG_QDIALECT_AS =~ "wlang/xhtml as ")
124
+ tests = [
125
+ [{:dialect =>"wlang", :variable => "x"}, "wlang as x"],
126
+ [{:dialect =>"wlang", :variable => "var"}, " wlang as var"],
127
+ [{:dialect =>"wlang/xhtml", :variable => "var"}, " wlang/xhtml as var"],
128
+ [nil, " wlang as "],
129
+ ]
130
+ tests.each do |t|
131
+ expected, src = t
132
+ assert_equal(expected, WLang::RuleSet::Utils.decode_qdialect_as(src))
133
+ end
134
+ end
135
+
136
+ def test_QENCODER_AS_regexp
137
+ assert_not_nil(RG_QENCODER_AS =~ "entities as x")
138
+ assert_not_nil(RG_QENCODER_AS =~ "html/entities-encoding as var")
139
+ assert_not_nil(RG_QENCODER_AS =~ " wlang/xhtml/entities-encoding as var ")
140
+ assert_nil(RG_QENCODER_AS =~ "wlang/entities")
141
+ assert_nil(RG_QENCODER_AS =~ "wlang/quoting as ")
142
+ tests = [
143
+ [{:encoder =>"entities", :variable => "x"}, "entities as x"],
144
+ [{:encoder =>"entities", :variable => "var"}, " entities as var"],
145
+ [{:encoder =>"xhtml/entities-encoding", :variable => "var"}, " xhtml/entities-encoding as var"],
146
+ [nil, " wlang as "],
147
+ ]
148
+ tests.each do |t|
149
+ expected, src = t
150
+ assert_equal(expected, WLang::RuleSet::Utils.decode_qencoder_as(src))
151
+ end
152
+ end
153
+
154
+ def test_EXPR_AS_regexp
155
+ assert_not_nil(RG_EXPR_AS =~ "name as n")
156
+ assert_not_nil(RG_EXPR_AS =~ "name.upcase as n")
157
+ assert_not_nil(RG_EXPR_AS =~ "[12,13,15] as n")
158
+ assert_not_nil(RG_EXPR_AS =~ "[12,13, 15] as n")
159
+ assert_nil(RG_EXPR_AS =~ "name.upcase")
160
+ tests = [
161
+ [{:expr =>"name", :variable => "n"}, "name as n"],
162
+ [{:expr =>"name.upcase", :variable => "n"}, "name.upcase as n"],
163
+ [{:expr =>"name.upcase", :variable => "n"}, " name.upcase as n "],
164
+ [{:expr =>"name/upcase", :variable => "n"}, " name/upcase as n "],
165
+ [{:expr =>"[12, 15, 276, 'blamebau']", :variable => "n"}, " [12, 15, 276, 'blamebau'] as n "],
166
+ [{:expr =>"'what if a as here'", :variable => "n"}, " 'what if a as here' as n "],
167
+ ]
168
+ tests.each do |t|
169
+ expected, src = t
170
+ assert_equal(expected, WLang::RuleSet::Utils.decode_expr_as(src))
171
+ end
172
+ end
173
+
174
+ def test_URI_AS_regexp
175
+ assert_not_nil(RG_URI_AS =~ "file as f")
176
+ assert_not_nil(RG_URI_AS =~ "file.yaml as f")
177
+ assert_not_nil(RG_URI_AS =~ "folder/folder/file.ext as data")
178
+ assert_not_nil(RG_URI_AS =~ "http://folder/folder/file.ext as data")
179
+ assert_not_nil(RG_URI_AS =~ "http://folder/folder/as/file.ext as data")
180
+ assert_nil(RG_URI_AS =~ "name.upcase")
181
+ tests = [
182
+ [{:uri =>"file", :variable => "f"}, "file as f"],
183
+ [{:uri =>"file.yaml", :variable => "f"}, "file.yaml as f"],
184
+ [{:uri =>"folder/folder/file.ext", :variable => "data"}, "folder/folder/file.ext as data"],
185
+ [{:uri =>"http://folder/folder/file.ext", :variable => "data"}, "http://folder/folder/file.ext as data"],
186
+ [{:uri =>"http://folder/folder/as/file.ext", :variable => "data"}, "http://folder/folder/as/file.ext as data"],
187
+ ]
188
+ tests.each do |t|
189
+ expected, src = t
190
+ assert_equal(expected, WLang::RuleSet::Utils.decode_uri_as(src))
191
+ end
192
+ end
193
+
194
+ def test_URI_WITH_regexp
195
+ tests = [
196
+ [{:uri =>"folder/file.yaml", :with => {"spec" => "spec"}}, "folder/file.yaml with spec: spec"],
197
+ [{:uri =>"folder/file.yaml", :with => {"a" => "a", "b" => "b"}},
198
+ "folder/file.yaml with a :a , b : b"],
199
+ [{:uri =>"folder/file.yaml", :with => {"spec" => "spec.reverse"}}, "folder/file.yaml with spec: spec.reverse"],
200
+ [{:uri =>"folder/file.yaml", :with => {"s" => "spec", "t" => "spec/rulesets", "xs" => "spec/uses"}},
201
+ "folder/file.yaml with s:spec, t:spec/rulesets, xs:spec/uses"],
202
+ ]
203
+ tests.each do |t|
204
+ expected, src = t
205
+ assert_equal(expected, WLang::RuleSet::Utils.decode_uri_with(src,nil))
206
+ end
207
+ end
208
+
209
+ def test_URI_WITH_regexp_with_optional
210
+ tests = [
211
+ [{:uri =>"file.yaml", :with => nil}, "file.yaml"],
212
+ [{:uri =>"folder/file.yaml", :with => nil}, "folder/file.yaml"],
213
+ [{:uri =>"folder/folder/file.ext", :with => nil}, "folder/folder/file.ext"],
214
+ [{:uri =>"http://folder/folder/with/file.ext", :with => nil}, "http://folder/folder/with/file.ext"],
215
+ ]
216
+ tests.each do |t|
217
+ expected, src = t
218
+ assert_equal(expected, WLang::RuleSet::Utils.decode_uri_with(src,nil,true))
219
+ end
220
+ end
221
+
222
+ def test_MULTI_AS
223
+ rx = Regexp.new(WLang::RuleSet::Utils::MULTI_AS)
224
+ assert_not_nil rx =~ "a"
225
+ assert_not_nil rx =~ "a, b"
226
+ assert_not_nil rx =~ "a , b"
227
+ assert_not_nil rx =~ "a,b"
228
+ assert_not_nil rx =~ "a,b,c"
229
+ assert_not_nil rx =~ "a, b, c"
230
+ assert_not_nil rx =~ "a , b , c"
231
+ end
232
+
233
+ def test_decode_multi_as
234
+ tests = [
235
+ [["a"], "a"],
236
+ [["a", "b"], "a, b"],
237
+ [["a", "b"], "a,b"],
238
+ [["a", "b"], "a , b"],
239
+ ]
240
+ tests.each do |test|
241
+ assert_equal(test[0], WLang::RuleSet::Utils.decode_multi_as(test[1], nil))
242
+ end
243
+ end
244
+
245
+ end