1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/parser/lexer'
# This is a special matcher to match easily lexer output
Spec::Matchers.define :be_like do |*expected|
match do |actual|
expected.zip(actual).all? { |e,a| !e or a[0] == e or (e.is_a? Array and a[0] == e[0] and (a[1] == e[1] or (a[1].is_a?(Hash) and a[1][:value] == e[1]))) }
end
end
__ = nil
describe Puppet::Parser::Lexer do
describe "when reading strings" do
before { @lexer = Puppet::Parser::Lexer.new }
it "should increment the line count for every carriage return in the string" do
@lexer.line = 10
@lexer.string = "this\nis\natest'"
@lexer.slurpstring("'")
@lexer.line.should == 12
end
it "should not increment the line count for escapes in the string" do
@lexer.line = 10
@lexer.string = "this\\nis\\natest'"
@lexer.slurpstring("'")
@lexer.line.should == 10
end
it "should not think the terminator is escaped, when preceeded by an even number of backslashes" do
@lexer.line = 10
@lexer.string = "here\nis\nthe\nstring\\\\'with\nextra\njunk"
@lexer.slurpstring("'")
@lexer.line.should == 13
end
end
end
describe Puppet::Parser::Lexer::Token do
before do
@token = Puppet::Parser::Lexer::Token.new(%r{something}, :NAME)
end
[:regex, :name, :string, :skip, :incr_line, :skip_text, :accumulate].each do |param|
it "should have a #{param.to_s} reader" do
@token.should be_respond_to(param)
end
it "should have a #{param.to_s} writer" do
@token.should be_respond_to(param.to_s + "=")
end
end
end
describe Puppet::Parser::Lexer::Token, "when initializing" do
it "should create a regex if the first argument is a string" do
Puppet::Parser::Lexer::Token.new("something", :NAME).regex.should == %r{something}
end
it "should set the string if the first argument is one" do
Puppet::Parser::Lexer::Token.new("something", :NAME).string.should == "something"
end
it "should set the regex if the first argument is one" do
Puppet::Parser::Lexer::Token.new(%r{something}, :NAME).regex.should == %r{something}
end
end
describe Puppet::Parser::Lexer::TokenList do
before do
@list = Puppet::Parser::Lexer::TokenList.new
end
it "should have a method for retrieving tokens by the name" do
token = @list.add_token :name, "whatever"
@list[:name].should equal(token)
end
it "should have a method for retrieving string tokens by the string" do
token = @list.add_token :name, "whatever"
@list.lookup("whatever").should equal(token)
end
it "should add tokens to the list when directed" do
token = @list.add_token :name, "whatever"
@list[:name].should equal(token)
end
it "should have a method for adding multiple tokens at once" do
@list.add_tokens "whatever" => :name, "foo" => :bar
@list[:name].should_not be_nil
@list[:bar].should_not be_nil
end
it "should fail to add tokens sharing a name with an existing token" do
@list.add_token :name, "whatever"
lambda { @list.add_token :name, "whatever" }.should raise_error(ArgumentError)
end
it "should set provided options on tokens being added" do
token = @list.add_token :name, "whatever", :skip_text => true
token.skip_text.should == true
end
it "should define any provided blocks as a :convert method" do
token = @list.add_token(:name, "whatever") do "foo" end
token.convert.should == "foo"
end
it "should store all string tokens in the :string_tokens list" do
one = @list.add_token(:name, "1")
@list.string_tokens.should be_include(one)
end
it "should store all regex tokens in the :regex_tokens list" do
one = @list.add_token(:name, %r{one})
@list.regex_tokens.should be_include(one)
end
it "should not store string tokens in the :regex_tokens list" do
one = @list.add_token(:name, "1")
@list.regex_tokens.should_not be_include(one)
end
it "should not store regex tokens in the :string_tokens list" do
one = @list.add_token(:name, %r{one})
@list.string_tokens.should_not be_include(one)
end
it "should sort the string tokens inversely by length when asked" do
one = @list.add_token(:name, "1")
two = @list.add_token(:other, "12")
@list.sort_tokens
@list.string_tokens.should == [two, one]
end
end
describe Puppet::Parser::Lexer::TOKENS do
before do
@lexer = Puppet::Parser::Lexer.new
end
{
:LBRACK => '[',
:RBRACK => ']',
:LBRACE => '{',
:RBRACE => '}',
:LPAREN => '(',
:RPAREN => ')',
:EQUALS => '=',
:ISEQUAL => '==',
:GREATEREQUAL => '>=',
:GREATERTHAN => '>',
:LESSTHAN => '<',
:LESSEQUAL => '<=',
:NOTEQUAL => '!=',
:NOT => '!',
:COMMA => ',',
:DOT => '.',
:COLON => ':',
:AT => '@',
:LLCOLLECT => '<<|',
:RRCOLLECT => '|>>',
:LCOLLECT => '<|',
:RCOLLECT => '|>',
:SEMIC => ';',
:QMARK => '?',
:BACKSLASH => '\\',
:FARROW => '=>',
:PARROW => '+>',
:APPENDS => '+=',
:PLUS => '+',
:MINUS => '-',
:DIV => '/',
:TIMES => '*',
:LSHIFT => '<<',
:RSHIFT => '>>',
:MATCH => '=~',
:NOMATCH => '!~',
:IN_EDGE => '->',
:OUT_EDGE => '<-',
:IN_EDGE_SUB => '~>',
:OUT_EDGE_SUB => '<~',
}.each do |name, string|
it "should have a token named #{name.to_s}" do
Puppet::Parser::Lexer::TOKENS[name].should_not be_nil
end
it "should match '#{string}' for the token #{name.to_s}" do
Puppet::Parser::Lexer::TOKENS[name].string.should == string
end
end
{
"case" => :CASE,
"class" => :CLASS,
"default" => :DEFAULT,
"define" => :DEFINE,
"import" => :IMPORT,
"if" => :IF,
"elsif" => :ELSIF,
"else" => :ELSE,
"inherits" => :INHERITS,
"node" => :NODE,
"and" => :AND,
"or" => :OR,
"undef" => :UNDEF,
"false" => :FALSE,
"true" => :TRUE,
"in" => :IN,
}.each do |string, name|
it "should have a keyword named #{name.to_s}" do
Puppet::Parser::Lexer::KEYWORDS[name].should_not be_nil
end
it "should have the keyword for #{name.to_s} set to #{string}" do
Puppet::Parser::Lexer::KEYWORDS[name].string.should == string
end
end
# These tokens' strings don't matter, just that the tokens exist.
[:STRING, :DQPRE, :DQMID, :DQPOST, :BOOLEAN, :NAME, :NUMBER, :COMMENT, :MLCOMMENT, :RETURN, :SQUOTE, :DQUOTE, :VARIABLE].each do |name|
it "should have a token named #{name.to_s}" do
Puppet::Parser::Lexer::TOKENS[name].should_not be_nil
end
end
end
describe Puppet::Parser::Lexer::TOKENS[:CLASSNAME] do
before { @token = Puppet::Parser::Lexer::TOKENS[:CLASSNAME] }
it "should match against lower-case alpha-numeric terms separated by double colons" do
@token.regex.should =~ "one::two"
end
it "should match against many lower-case alpha-numeric terms separated by double colons" do
@token.regex.should =~ "one::two::three::four::five"
end
it "should match against lower-case alpha-numeric terms prefixed by double colons" do
@token.regex.should =~ "::one"
end
end
describe Puppet::Parser::Lexer::TOKENS[:CLASSREF] do
before { @token = Puppet::Parser::Lexer::TOKENS[:CLASSREF] }
it "should match against single upper-case alpha-numeric terms" do
@token.regex.should =~ "One"
end
it "should match against upper-case alpha-numeric terms separated by double colons" do
@token.regex.should =~ "One::Two"
end
it "should match against many upper-case alpha-numeric terms separated by double colons" do
@token.regex.should =~ "One::Two::Three::Four::Five"
end
it "should match against upper-case alpha-numeric terms prefixed by double colons" do
@token.regex.should =~ "::One"
end
end
describe Puppet::Parser::Lexer::TOKENS[:NAME] do
before { @token = Puppet::Parser::Lexer::TOKENS[:NAME] }
it "should match against lower-case alpha-numeric terms" do
@token.regex.should =~ "one-two"
end
it "should return itself and the value if the matched term is not a keyword" do
Puppet::Parser::Lexer::KEYWORDS.expects(:lookup).returns(nil)
@token.convert(stub("lexer"), "myval").should == [Puppet::Parser::Lexer::TOKENS[:NAME], "myval"]
end
it "should return the keyword token and the value if the matched term is a keyword" do
keyword = stub 'keyword', :name => :testing
Puppet::Parser::Lexer::KEYWORDS.expects(:lookup).returns(keyword)
@token.convert(stub("lexer"), "myval").should == [keyword, "myval"]
end
it "should return the BOOLEAN token and 'true' if the matched term is the string 'true'" do
keyword = stub 'keyword', :name => :TRUE
Puppet::Parser::Lexer::KEYWORDS.expects(:lookup).returns(keyword)
@token.convert(stub('lexer'), "true").should == [Puppet::Parser::Lexer::TOKENS[:BOOLEAN], true]
end
it "should return the BOOLEAN token and 'false' if the matched term is the string 'false'" do
keyword = stub 'keyword', :name => :FALSE
Puppet::Parser::Lexer::KEYWORDS.expects(:lookup).returns(keyword)
@token.convert(stub('lexer'), "false").should == [Puppet::Parser::Lexer::TOKENS[:BOOLEAN], false]
end
end
describe Puppet::Parser::Lexer::TOKENS[:NUMBER] do
before do
@token = Puppet::Parser::Lexer::TOKENS[:NUMBER]
@regex = @token.regex
end
it "should match against numeric terms" do
@regex.should =~ "2982383139"
end
it "should match against float terms" do
@regex.should =~ "29823.235"
end
it "should match against hexadecimal terms" do
@regex.should =~ "0xBEEF0023"
end
it "should match against float with exponent terms" do
@regex.should =~ "10e23"
end
it "should match against float terms with negative exponents" do
@regex.should =~ "10e-23"
end
it "should match against float terms with fractional parts and exponent" do
@regex.should =~ "1.234e23"
end
it "should return the NAME token and the value" do
@token.convert(stub("lexer"), "myval").should == [Puppet::Parser::Lexer::TOKENS[:NAME], "myval"]
end
end
describe Puppet::Parser::Lexer::TOKENS[:COMMENT] do
before { @token = Puppet::Parser::Lexer::TOKENS[:COMMENT] }
it "should match against lines starting with '#'" do
@token.regex.should =~ "# this is a comment"
end
it "should be marked to get skipped" do
@token.skip?.should be_true
end
it "should be marked to accumulate" do
@token.accumulate?.should be_true
end
it "'s block should return the comment without the #" do
@token.convert(@lexer,"# this is a comment")[1].should == "this is a comment"
end
end
describe Puppet::Parser::Lexer::TOKENS[:MLCOMMENT] do
before do
@token = Puppet::Parser::Lexer::TOKENS[:MLCOMMENT]
@lexer = stub 'lexer', :line => 0
end
it "should match against lines enclosed with '/*' and '*/'" do
@token.regex.should =~ "/* this is a comment */"
end
it "should match multiple lines enclosed with '/*' and '*/'" do
@token.regex.should =~ """/*
this is a comment
*/"""
end
it "should increase the lexer current line number by the amount of lines spanned by the comment" do
@lexer.expects(:line=).with(2)
@token.convert(@lexer, "1\n2\n3")
end
it "should not greedily match comments" do
match = @token.regex.match("/* first */ word /* second */")
match[1].should == " first "
end
it "should be marked to accumulate" do
@token.accumulate?.should be_true
end
it "'s block should return the comment without the comment marks" do
@lexer.stubs(:line=).with(0)
@token.convert(@lexer,"/* this is a comment */")[1].should == "this is a comment"
end
end
describe Puppet::Parser::Lexer::TOKENS[:RETURN] do
before { @token = Puppet::Parser::Lexer::TOKENS[:RETURN] }
it "should match against carriage returns" do
@token.regex.should =~ "\n"
end
it "should be marked to initiate text skipping" do
@token.skip_text.should be_true
end
it "should be marked to increment the line" do
@token.incr_line.should be_true
end
end
def tokens_scanned_from(s)
lexer = Puppet::Parser::Lexer.new
lexer.string = s
lexer.fullscan[0..-2]
end
describe Puppet::Parser::Lexer,"when lexing strings" do
{
%q{'single quoted string')} => [[:STRING,'single quoted string']],
%q{"double quoted string"} => [[:STRING,'double quoted string']],
%q{'single quoted string with an escaped "\\'"'} => [[:STRING,'single quoted string with an escaped "\'"']],
%q{'single quoted string with an escaped "\$"'} => [[:STRING,'single quoted string with an escaped "\$"']],
%q{'single quoted string with an escaped "\."'} => [[:STRING,'single quoted string with an escaped "\."']],
%q{'single quoted string with an escaped "\n"'} => [[:STRING,'single quoted string with an escaped "\n"']],
%q{'single quoted string with an escaped "\\\\"'} => [[:STRING,'single quoted string with an escaped "\\\\"']],
%q{"string with an escaped '\\"'"} => [[:STRING,"string with an escaped '\"'"]],
%q{"string with an escaped '\\$'"} => [[:STRING,"string with an escaped '$'"]],
%q{"string with $v (but no braces)"} => [[:DQPRE,"string with "],[:VARIABLE,'v'],[:DQPOST,' (but no braces)']],
%q["string with ${v} in braces"] => [[:DQPRE,"string with "],[:VARIABLE,'v'],[:DQPOST,' in braces']],
%q["string with ${qualified::var} in braces"] => [[:DQPRE,"string with "],[:VARIABLE,'qualified::var'],[:DQPOST,' in braces']],
%q{"string with $v and $v (but no braces)"} => [[:DQPRE,"string with "],[:VARIABLE,"v"],[:DQMID," and "],[:VARIABLE,"v"],[:DQPOST," (but no braces)"]],
%q["string with ${v} and ${v} in braces"] => [[:DQPRE,"string with "],[:VARIABLE,"v"],[:DQMID," and "],[:VARIABLE,"v"],[:DQPOST," in braces"]],
%q["string with ${'a nested single quoted string'} inside it."] => [[:DQPRE,"string with "],[:STRING,'a nested single quoted string'],[:DQPOST,' inside it.']],
%q["string with ${['an array ',$v2]} in it."] => [[:DQPRE,"string with "],:LBRACK,[:STRING,"an array "],:COMMA,[:VARIABLE,"v2"],:RBRACK,[:DQPOST," in it."]],
%q{a simple "scanner" test} => [[:NAME,"a"],[:NAME,"simple"], [:STRING,"scanner"],[:NAME,"test"]],
%q{a simple 'single quote scanner' test} => [[:NAME,"a"],[:NAME,"simple"], [:STRING,"single quote scanner"],[:NAME,"test"]],
%q{a harder 'a $b \c"'} => [[:NAME,"a"],[:NAME,"harder"], [:STRING,'a $b \c"']],
%q{a harder "scanner test"} => [[:NAME,"a"],[:NAME,"harder"], [:STRING,"scanner test"]],
%q{a hardest "scanner \"test\""} => [[:NAME,"a"],[:NAME,"hardest"],[:STRING,'scanner "test"']],
%Q{a hardestest "scanner \\"test\\"\n"} => [[:NAME,"a"],[:NAME,"hardestest"],[:STRING,%Q{scanner "test"\n}]],
%q{function("call")} => [[:NAME,"function"],[:LPAREN,"("],[:STRING,'call'],[:RPAREN,")"]],
%q["string with ${(3+5)/4} nested math."] => [[:DQPRE,"string with "],:LPAREN,[:NAME,"3"],:PLUS,[:NAME,"5"],:RPAREN,:DIV,[:NAME,"4"],[:DQPOST," nested math."]],
%q["$$$$"] => [[:STRING,"$$$$"]],
%q["$variable"] => [[:DQPRE,""],[:VARIABLE,"variable"],[:DQPOST,""]],
%q["$var$other"] => [[:DQPRE,""],[:VARIABLE,"var"],[:DQMID,""],[:VARIABLE,"other"],[:DQPOST,""]],
%q["foo$bar$"] => [[:DQPRE,"foo"],[:VARIABLE,"bar"],[:DQPOST,"$"]],
%q["foo$$bar"] => [[:DQPRE,"foo$"],[:VARIABLE,"bar"],[:DQPOST,""]],
%q[""] => [[:STRING,""]],
}.each { |src,expected_result|
it "should handle #{src} correctly" do
tokens_scanned_from(src).should be_like(*expected_result)
end
}
end
describe Puppet::Parser::Lexer::TOKENS[:DOLLAR_VAR] do
before { @token = Puppet::Parser::Lexer::TOKENS[:DOLLAR_VAR] }
it "should match against alpha words prefixed with '$'" do
@token.regex.should =~ '$this_var'
end
it "should return the VARIABLE token and the variable name stripped of the '$'" do
@token.convert(stub("lexer"), "$myval").should == [Puppet::Parser::Lexer::TOKENS[:VARIABLE], "myval"]
end
end
describe Puppet::Parser::Lexer::TOKENS[:REGEX] do
before { @token = Puppet::Parser::Lexer::TOKENS[:REGEX] }
it "should match against any expression enclosed in //" do
@token.regex.should =~ '/this is a regex/'
end
it 'should not match if there is \n in the regex' do
@token.regex.should_not =~ "/this is \n a regex/"
end
describe "when scanning" do
it "should not consider escaped slashes to be the end of a regex" do
tokens_scanned_from("$x =~ /this \\/ foo/").should be_like(__,__,[:REGEX,%r{this / foo}])
end
it "should not lex chained division as a regex" do
tokens_scanned_from("$x = $a/$b/$c").collect { |name, data| name }.should_not be_include( :REGEX )
end
it "should accept a regular expression after NODE" do
tokens_scanned_from("node /www.*\.mysite\.org/").should be_like(__,[:REGEX,Regexp.new("www.*\.mysite\.org")])
end
it "should accept regular expressions in a CASE" do
s = %q{case $variable {
"something": {$othervar = 4096 / 2}
/regex/: {notice("this notably sucks")}
}
}
tokens_scanned_from(s).should be_like(
:CASE,:VARIABLE,:LBRACE,:STRING,:COLON,:LBRACE,:VARIABLE,:EQUALS,:NAME,:DIV,:NAME,:RBRACE,[:REGEX,/regex/],:COLON,:LBRACE,:NAME,:LPAREN,:STRING,:RPAREN,:RBRACE,:RBRACE
)
end
end
it "should return the REGEX token and a Regexp" do
@token.convert(stub("lexer"), "/myregex/").should == [Puppet::Parser::Lexer::TOKENS[:REGEX], Regexp.new(/myregex/)]
end
end
describe Puppet::Parser::Lexer, "when lexing comments" do
before { @lexer = Puppet::Parser::Lexer.new }
it "should accumulate token in munge_token" do
token = stub 'token', :skip => true, :accumulate? => true, :incr_line => nil, :skip_text => false
token.stubs(:convert).with(@lexer, "# this is a comment").returns([token, " this is a comment"])
@lexer.munge_token(token, "# this is a comment")
@lexer.munge_token(token, "# this is a comment")
@lexer.getcomment.should == " this is a comment\n this is a comment\n"
end
it "should add a new comment stack level on LBRACE" do
@lexer.string = "{"
@lexer.expects(:commentpush)
@lexer.fullscan
end
it "should return the current comments on getcomment" do
@lexer.string = "# comment"
@lexer.fullscan
@lexer.getcomment.should == "comment\n"
end
it "should discard the previous comments on blank line" do
@lexer.string = "# 1\n\n# 2"
@lexer.fullscan
@lexer.getcomment.should == "2\n"
end
it "should skip whitespace before lexing the next token after a non-token" do
tokens_scanned_from("/* 1\n\n */ \ntest").should be_like([:NAME, "test"])
end
it "should not return comments seen after the current line" do
@lexer.string = "# 1\n\n# 2"
@lexer.fullscan
@lexer.getcomment(1).should == ""
end
it "should return a comment seen before the current line" do
@lexer.string = "# 1\n# 2"
@lexer.fullscan
@lexer.getcomment(2).should == "1\n2\n"
end
end
# FIXME: We need to rewrite all of these tests, but I just don't want to take the time right now.
describe "Puppet::Parser::Lexer in the old tests" do
before { @lexer = Puppet::Parser::Lexer.new }
it "should do simple lexing" do
{
%q{\\} => [[:BACKSLASH,"\\"]],
%q{simplest scanner test} => [[:NAME,"simplest"],[:NAME,"scanner"],[:NAME,"test"]],
%Q{returned scanner test\n} => [[:NAME,"returned"],[:NAME,"scanner"],[:NAME,"test"]]
}.each { |source,expected|
tokens_scanned_from(source).should be_like(*expected)
}
end
it "should fail usefully" do
lambda { tokens_scanned_from('^') }.should raise_error(RuntimeError)
end
it "should fail if the string is not set" do
lambda { @lexer.fullscan }.should raise_error(Puppet::LexError)
end
it "should correctly identify keywords" do
tokens_scanned_from("case").should be_like([:CASE, "case"])
end
it "should correctly parse class references" do
%w{Many Different Words A Word}.each { |t| tokens_scanned_from(t).should be_like([:CLASSREF,t])}
end
# #774
it "should correctly parse namespaced class refernces token" do
%w{Foo ::Foo Foo::Bar ::Foo::Bar}.each { |t| tokens_scanned_from(t).should be_like([:CLASSREF, t]) }
end
it "should correctly parse names" do
%w{this is a bunch of names}.each { |t| tokens_scanned_from(t).should be_like([:NAME,t]) }
end
it "should correctly parse names with numerals" do
%w{1name name1 11names names11}.each { |t| tokens_scanned_from(t).should be_like([:NAME,t]) }
end
it "should correctly parse empty strings" do
lambda { tokens_scanned_from('$var = ""') }.should_not raise_error
end
it "should correctly parse virtual resources" do
tokens_scanned_from("@type {").should be_like([:AT, "@"], [:NAME, "type"], [:LBRACE, "{"])
end
it "should correctly deal with namespaces" do
@lexer.string = %{class myclass}
@lexer.fullscan
@lexer.namespace.should == "myclass"
@lexer.namepop
@lexer.namespace.should == ""
@lexer.string = "class base { class sub { class more"
@lexer.fullscan
@lexer.namespace.should == "base::sub::more"
@lexer.namepop
@lexer.namespace.should == "base::sub"
end
it "should not put class instantiation on the namespace" do
@lexer.string = "class base { class sub { class { mode"
@lexer.fullscan
@lexer.namespace.should == "base::sub"
end
it "should correctly handle fully qualified names" do
@lexer.string = "class base { class sub::more {"
@lexer.fullscan
@lexer.namespace.should == "base::sub::more"
@lexer.namepop
@lexer.namespace.should == "base"
end
it "should correctly lex variables" do
["$variable", "$::variable", "$qualified::variable", "$further::qualified::variable"].each do |string|
tokens_scanned_from(string).should be_like([:VARIABLE,string.sub(/^\$/,'')])
end
end
end
require 'puppettest/support/utils'
describe "Puppet::Parser::Lexer in the old tests when lexing example files" do
extend PuppetTest::Support::Utils
textfiles do |file|
it "should correctly lex #{file}" do
lexer = Puppet::Parser::Lexer.new
lexer.file = file
lambda { lexer.fullscan }.should_not raise_error
end
end
end
|