Permalink
Browse files

few more working examples, more special cases

  • Loading branch information...
bglusman
bglusman committed Jan 18, 2012
1 parent a8c97b6 commit 343ec12f351347fa5a26edd5852a252cef0ddb2f
Showing with 58 additions and 4 deletions.
  1. +6 −1 lib/outlaw/law_dsl.rb
  2. +52 −3 test/outlaw_test.rb
View
@@ -8,8 +8,9 @@ def parse(restriction)
case
when special_case?(token)
next #TODO
when multipart?(token) #this jewel is for Const.new and similar stuff
parsed_restriction += Ripper.lex(token).reduce([]){|a, t| a << /#{t[2]}/}
when defined_collection?(token)
#binding.pry
parsed_restriction << Outlaw.const_get(string_to_sym(token.upcase))
when parameter?(token)
parsed_restriction << string_to_sym(token)
@@ -43,6 +44,10 @@ def build_regex(token)
[/\A#{token}/]
end
def multipart?(token)
!parameter?(token) && Ripper.lex(token).count > 1
end
def build_block(pattern)
lambda do |file|
program = Ripper.tokenize(file)
View
@@ -1,5 +1,4 @@
require_relative 'test_helper'
require 'pry'
module Outlaw
describe LawDSL do
@@ -89,7 +88,7 @@ module Thing
CODE
module_result = rule4.call(module_file)
result4a = rule4.call(@okay_file)
result4a = rule4.call(@okay_file)
module_result.must_equal true
result4a.must_equal false
@@ -107,11 +106,61 @@ def badthing(here)
end
CODE
core_result = rule5.call(core_file)
result5a = rule5.call(@okay_file)
result5a = rule5.call(@okay_file)
core_result .must_equal true
result5a.must_equal false
end
# it "correctly builds rule for unless else" do
# code6 = "unless :symbols
# :disjoint_code_seperator
# else :more_symbols"
# rule6 = LawDSL.parse(code6)
# core_file = <<CODE
# class Whatever < String
# def badthing(here)
# @here = here
# end
# end
# CODE
it "correctly builds rule for rescue nil" do
code7 = "rescue nil"
rule7 = LawDSL.parse(code7)
nil_file = <<CODE
begin
"hi"
rescue nil
"bye"
end
CODE
nil_result = rule7.call(nil_file)
result7a = rule7.call(@okay_file)
nil_result .must_equal true
result7a.must_equal false
end
it "correctly builds rule for inherit struct.new" do
code8 = "class :symbol < Struct.new"
rule8 = LawDSL.parse(code8)
struct_file = <<CODE
class MyClass < Struct.new("Customer", :name, :address)
CODE
struct_result = rule8.call(struct_file)
result8a = rule8.call(@okay_file)
struct_result.must_equal true
result8a.must_equal false
end
# end
it "returns a hash with key counts and nil placeholders" do
params = Rule.send(:params_count_hash, [/module/, :token1, :token2, :token1, /class/, :token3, /end/])
params.keys.size.must_equal 3

0 comments on commit 343ec12

Please sign in to comment.