Class: GrammarTest
- Inherits:
-
Test::Unit::TestCase
- Object
- Test::Unit::TestCase
- GrammarTest
- Defined in:
- lib/citrus_test.rb
Class Attribute Summary collapse
-
.grammar_dir ⇒ Object
Returns the value of attribute grammar_dir.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
Set the name of the grammar our subclass uses on the basis of its mangled name.
-
.load_grammar(grammar, force = false) ⇒ Object
Load the grammar specified by grammar.
Instance Method Summary collapse
-
#assert_parses(rule, str, attributes = {}) ⇒ Object
If attributes is a Hash, assert that the return value of parse(rule, str) has every attribute specified in attributes, and that they have the value specified in attributes.
-
#deny_parses(rule, str, msg = nil) ⇒ Object
Assert that rule raises Citrus::ParseError.
-
#mangle_class_name(name) ⇒ Object
Convert a camel-cased string to an underscored one.
-
#parse(rule, input) ⇒ Object
Parse input according to rule.
-
#rule(name) ⇒ Object
Get a specific rule from the grammar.
-
#setup ⇒ Object
Load the grammar file ../grammars/@@grammar.citrus.
Class Attribute Details
.grammar_dir ⇒ Object
Returns the value of attribute grammar_dir.
10 11 12 |
# File 'lib/citrus_test.rb', line 10 def grammar_dir @grammar_dir end |
Class Method Details
.inherited(subclass) ⇒ Object
Set the name of the grammar our subclass uses on the basis of its mangled name.
30 31 32 33 34 35 36 37 38 |
# File 'lib/citrus_test.rb', line 30 def self.inherited(subclass) # :nodoc: if subclass.name =~ /\ATest([A-Z_][A-Za-z_]*)\z/ class_variable_set(:@@grammar, $1) super else raise SyntaxError.new("give your class a proper name") end end |
.load_grammar(grammar, force = false) ⇒ Object
Load the grammar specified by grammar. If force is true, we will bypass Citrus’ internal cache.
19 20 21 22 23 24 25 |
# File 'lib/citrus_test.rb', line 19 def load_grammar(grammar, force=false) loc = File.join(@@grammar_dir, grammar+".citrus") Citrus.cache.delete(loc) if force Citrus.load(loc) end |
Instance Method Details
#assert_parses(rule, str, attributes = {}) ⇒ Object
If attributes is a Hash, assert that the return value of parse(rule, str) has every attribute specified in attributes, and that they have the value specified in attributes. Otherwise, assert that the return value is equal to attributes. (If you want to actually assert that the result is a hash, try using :to_hash as a key.)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/citrus_test.rb', line 72 def assert_parses(rule, str, attributes={}) err_prefix = "error in parsing #{str.inspect} with #{@@grammar}.#{rule}: " ret = nil assert_nothing_raised(err_prefix + "invalid syntax") do ret = parse(rule, str) end if attributes.is_a?(Hash) attributes.each do |attr, expected_val| a = "attribute #{attr}" assert(ret.respond_to?(attr), err_prefix + "#{a} doesn't exist") fetch_err = "An exception was raised when fetching #{a}" assert_nothing_raised(err_prefix + fetch_err) do wrong_val_err = "#{a} has an unexpected value" assert_equal(expected_val, ret.send(attr), err_prefix + wrong_val_err) end end else assert_equal(attributes, ret, err_prefix + "unexpected value") end ret end |
#deny_parses(rule, str, msg = nil) ⇒ Object
Assert that rule raises Citrus::ParseError. (By default, msg is “#@@grammar.##rule parsed #GrammarTest.strstr.inspect when it shouldn’t have.”)
99 100 101 102 |
# File 'lib/citrus_test.rb', line 99 def deny_parses(rule, str, msg=nil) msg ||= "#{@@grammar}.#{rule} parsed #{str.inspect} when it shouldn't have" assert_raises(Citrus::ParseError, msg){ rule(rule).parse(str) } end |
#mangle_class_name(name) ⇒ Object
Convert a camel-cased string to an underscored one. (e.g. “SatanSatanLendMeADollar” becomes “satan_satan_lend_me_a_dollar”)
42 43 44 45 46 47 48 |
# File 'lib/citrus_test.rb', line 42 def mangle_class_name(name) # :nodoc: unless name =~ /\A[A-Z_][A-Za-z_]*\z/ raise "#{name} doesn't look like a valid class name" end name.scan(/[A-Z_][a-z_]*/).map(&:downcase).join("_") end |
#parse(rule, input) ⇒ Object
Parse input according to rule.
63 64 65 |
# File 'lib/citrus_test.rb', line 63 def parse(rule, input) rule(rule).parse(input).value end |
#rule(name) ⇒ Object
Get a specific rule from the grammar.
57 58 59 60 |
# File 'lib/citrus_test.rb', line 57 def rule(name) @rules ||= {} @rules[@@grammar] ||= self.class.const_get(@@grammar).rule(name) end |
#setup ⇒ Object
Load the grammar file ../grammars/@@grammar.citrus
51 52 53 54 |
# File 'lib/citrus_test.rb', line 51 def setup grammar_file = "#{mangle_class_name(@@grammar)}.citrus" Citrus.load( File.join(@@grammar_dir, grammar_file) ) end |